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