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 | 0 | 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 | 0 | PerforceScmProviderRepository prepo = (PerforceScmProviderRepository) repo; |
57 | 0 | actualLocation = PerforceScmProvider.getRepoPath( getLogger(), prepo, files.getBasedir() ); |
58 | 0 | PerforceStatusConsumer consumer = new PerforceStatusConsumer(); |
59 | 0 | Commandline command = readOpened( prepo, files, consumer ); |
60 | |
|
61 | 0 | if ( consumer.isSuccess() ) |
62 | |
{ |
63 | 0 | List<ScmFile> scmfiles = createResults( actualLocation, consumer ); |
64 | 0 | return new StatusScmResult( command.toString(), scmfiles ); |
65 | |
} |
66 | |
|
67 | 0 | 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 | 1 | List<ScmFile> results = new ArrayList<ScmFile>(); |
74 | 1 | List<String> files = consumer.getDepotfiles(); |
75 | 1 | RE re = new RE( "([^#]+)#\\d+ - ([^ ]+) .*" ); |
76 | 1 | for ( Iterator<String> it = files.iterator(); it.hasNext(); ) |
77 | |
{ |
78 | 4 | String filepath = it.next(); |
79 | 4 | if ( !re.match( filepath ) ) |
80 | |
{ |
81 | 0 | System.err.println( "Skipping " + filepath ); |
82 | 0 | continue; |
83 | |
} |
84 | 4 | String path = re.getParen( 1 ); |
85 | 4 | String verb = re.getParen( 2 ); |
86 | |
|
87 | 4 | ScmFile scmfile = new ScmFile( path.substring( repoPath.length() + 1 ).trim(), PerforceVerbMapper |
88 | |
.toStatus( verb ) ); |
89 | 4 | results.add( scmfile ); |
90 | 4 | } |
91 | 1 | return results; |
92 | |
} |
93 | |
|
94 | |
private Commandline readOpened( PerforceScmProviderRepository prepo, ScmFileSet files, |
95 | |
PerforceStatusConsumer consumer ) |
96 | |
{ |
97 | 0 | Commandline cl = createOpenedCommandLine( prepo, files.getBasedir(), actualLocation ); |
98 | |
try |
99 | |
{ |
100 | 0 | if ( getLogger().isDebugEnabled() ) |
101 | |
{ |
102 | 0 | getLogger().debug( PerforceScmProvider.clean( "Executing " + cl.toString() ) ); |
103 | |
} |
104 | |
|
105 | 0 | CommandLineUtils.StringStreamConsumer err = new CommandLineUtils.StringStreamConsumer(); |
106 | 0 | int exitCode = CommandLineUtils.executeCommandLine( cl, consumer, err ); |
107 | |
|
108 | 0 | if ( exitCode != 0 ) |
109 | |
{ |
110 | 0 | String cmdLine = CommandLineUtils.toString( cl.getCommandline() ); |
111 | |
|
112 | 0 | StringBuilder msg = new StringBuilder( "Exit code: " + exitCode + " - " + err.getOutput() ); |
113 | 0 | msg.append( '\n' ); |
114 | 0 | msg.append( "Command line was:" + cmdLine ); |
115 | |
|
116 | 0 | throw new CommandLineException( msg.toString() ); |
117 | |
} |
118 | |
} |
119 | 0 | catch ( CommandLineException e ) |
120 | |
{ |
121 | 0 | if ( getLogger().isErrorEnabled() ) |
122 | |
{ |
123 | 0 | getLogger().error( "CommandLineException " + e.getMessage(), e ); |
124 | |
} |
125 | 0 | } |
126 | |
|
127 | 0 | return cl; |
128 | |
} |
129 | |
|
130 | |
public static Commandline createOpenedCommandLine( PerforceScmProviderRepository repo, File workingDirectory, |
131 | |
String location ) |
132 | |
{ |
133 | 1 | Commandline command = PerforceScmProvider.createP4Command( repo, workingDirectory ); |
134 | 1 | command.createArg().setValue( "opened" ); |
135 | 1 | command.createArg().setValue( PerforceScmProvider.getCanonicalRepoPath( location ) ); |
136 | 1 | return command; |
137 | |
} |
138 | |
} |