1 package org.apache.maven.scm.provider.perforce.command.status;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.scm.ScmException;
23 import org.apache.maven.scm.ScmFile;
24 import org.apache.maven.scm.ScmFileSet;
25 import org.apache.maven.scm.command.status.AbstractStatusCommand;
26 import org.apache.maven.scm.command.status.StatusScmResult;
27 import org.apache.maven.scm.provider.ScmProviderRepository;
28 import org.apache.maven.scm.provider.perforce.PerforceScmProvider;
29 import org.apache.maven.scm.provider.perforce.command.PerforceCommand;
30 import org.apache.maven.scm.provider.perforce.command.PerforceVerbMapper;
31 import org.apache.maven.scm.provider.perforce.repository.PerforceScmProviderRepository;
32 import org.codehaus.plexus.util.cli.CommandLineException;
33 import org.codehaus.plexus.util.cli.CommandLineUtils;
34 import org.codehaus.plexus.util.cli.Commandline;
35
36 import java.io.File;
37 import java.util.ArrayList;
38 import java.util.Iterator;
39 import java.util.List;
40 import java.util.regex.Matcher;
41 import java.util.regex.Pattern;
42
43
44
45
46
47 public class PerforceStatusCommand
48 extends AbstractStatusCommand
49 implements PerforceCommand
50 {
51 private String actualLocation;
52
53
54 protected StatusScmResult executeStatusCommand( ScmProviderRepository repo, ScmFileSet files )
55 throws ScmException
56 {
57 PerforceScmProviderRepository prepo = (PerforceScmProviderRepository) repo;
58 actualLocation = PerforceScmProvider.getRepoPath( getLogger(), prepo, files.getBasedir() );
59 PerforceStatusConsumer consumer = new PerforceStatusConsumer();
60 Commandline command = readOpened( prepo, files, consumer );
61
62 if ( consumer.isSuccess() )
63 {
64 List<ScmFile> scmfiles = createResults( actualLocation, consumer );
65 return new StatusScmResult( command.toString(), scmfiles );
66 }
67
68 return new StatusScmResult( command.toString(), "Unable to get status", consumer
69 .getOutput(), consumer.isSuccess() );
70 }
71
72 public static List<ScmFile> createResults( String repoPath, PerforceStatusConsumer consumer )
73 {
74 List<ScmFile> results = new ArrayList<ScmFile>();
75 List<String> files = consumer.getDepotfiles();
76 Pattern re = Pattern.compile( "([^#]+)#\\d+ - ([^ ]+) .*" );
77 for ( Iterator<String> it = files.iterator(); it.hasNext(); )
78 {
79 String filepath = it.next();
80 Matcher matcher = re.matcher( filepath );
81 if ( !matcher.matches() )
82 {
83 System.err.println( "Skipping " + filepath );
84 continue;
85 }
86 String path = matcher.group( 1 );
87 String verb = matcher.group( 2 );
88
89 ScmFile scmfile = new ScmFile( path.substring( repoPath.length() + 1 ).trim(), PerforceVerbMapper
90 .toStatus( verb ) );
91 results.add( scmfile );
92 }
93 return results;
94 }
95
96 private Commandline readOpened( PerforceScmProviderRepository prepo, ScmFileSet files,
97 PerforceStatusConsumer consumer )
98 {
99 Commandline cl = createOpenedCommandLine( prepo, files.getBasedir(), actualLocation );
100 try
101 {
102 if ( getLogger().isDebugEnabled() )
103 {
104 getLogger().debug( PerforceScmProvider.clean( "Executing " + cl.toString() ) );
105 }
106
107 CommandLineUtils.StringStreamConsumer err = new CommandLineUtils.StringStreamConsumer();
108 int exitCode = CommandLineUtils.executeCommandLine( cl, consumer, err );
109
110 if ( exitCode != 0 )
111 {
112 String cmdLine = CommandLineUtils.toString( cl.getCommandline() );
113
114 StringBuilder msg = new StringBuilder( "Exit code: " + exitCode + " - " + err.getOutput() );
115 msg.append( '\n' );
116 msg.append( "Command line was:" + cmdLine );
117
118 throw new CommandLineException( msg.toString() );
119 }
120 }
121 catch ( CommandLineException e )
122 {
123 if ( getLogger().isErrorEnabled() )
124 {
125 getLogger().error( "CommandLineException " + e.getMessage(), e );
126 }
127 }
128
129 return cl;
130 }
131
132 public static Commandline createOpenedCommandLine( PerforceScmProviderRepository repo, File workingDirectory,
133 String location )
134 {
135 Commandline command = PerforceScmProvider.createP4Command( repo, workingDirectory );
136 command.createArg().setValue( "opened" );
137 command.createArg().setValue( PerforceScmProvider.getCanonicalRepoPath( location ) );
138 return command;
139 }
140 }