1 | |
package org.apache.maven.scm.provider.perforce.command.checkin; |
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.ScmFileSet; |
24 | |
import org.apache.maven.scm.ScmVersion; |
25 | |
import org.apache.maven.scm.command.checkin.AbstractCheckInCommand; |
26 | |
import org.apache.maven.scm.command.checkin.CheckInScmResult; |
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.repository.PerforceScmProviderRepository; |
31 | |
import org.codehaus.plexus.util.cli.CommandLineException; |
32 | |
import org.codehaus.plexus.util.cli.CommandLineUtils; |
33 | |
import org.codehaus.plexus.util.cli.Commandline; |
34 | |
|
35 | |
import java.io.ByteArrayInputStream; |
36 | |
import java.io.File; |
37 | |
import java.io.IOException; |
38 | |
import java.util.HashSet; |
39 | |
import java.util.List; |
40 | |
import java.util.Set; |
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | 0 | public class PerforceCheckInCommand |
47 | |
extends AbstractCheckInCommand |
48 | |
implements PerforceCommand |
49 | |
{ |
50 | |
|
51 | |
|
52 | |
|
53 | |
protected CheckInScmResult executeCheckInCommand( ScmProviderRepository repo, ScmFileSet files, String message, |
54 | |
ScmVersion version ) |
55 | |
throws ScmException |
56 | |
{ |
57 | 0 | Commandline cl = createCommandLine( (PerforceScmProviderRepository) repo, files.getBasedir() ); |
58 | 0 | PerforceCheckInConsumer consumer = new PerforceCheckInConsumer(); |
59 | |
try |
60 | |
{ |
61 | 0 | String jobs = System.getProperty( "maven.scm.jobs" ); |
62 | |
|
63 | 0 | if ( getLogger().isDebugEnabled() ) |
64 | |
{ |
65 | 0 | getLogger().debug( PerforceScmProvider.clean( "Executing " + cl.toString() ) ); |
66 | |
} |
67 | |
|
68 | 0 | PerforceScmProviderRepository prepo = (PerforceScmProviderRepository) repo; |
69 | 0 | String changes = createChangeListSpecification( prepo, files, message, |
70 | |
PerforceScmProvider.getRepoPath( getLogger(), prepo, |
71 | |
files.getBasedir() ), |
72 | |
jobs ); |
73 | |
|
74 | 0 | if ( getLogger().isDebugEnabled() ) |
75 | |
{ |
76 | 0 | getLogger().debug( "Sending changelist:\n" + changes ); |
77 | |
} |
78 | |
|
79 | 0 | CommandLineUtils.StringStreamConsumer err = new CommandLineUtils.StringStreamConsumer(); |
80 | 0 | int exitCode = |
81 | |
CommandLineUtils.executeCommandLine( cl, new ByteArrayInputStream( changes.getBytes() ), consumer, |
82 | |
err ); |
83 | |
|
84 | 0 | if ( exitCode != 0 ) |
85 | |
{ |
86 | 0 | String cmdLine = CommandLineUtils.toString( cl.getCommandline() ); |
87 | |
|
88 | 0 | StringBuilder msg = new StringBuilder( "Exit code: " + exitCode + " - " + err.getOutput() ); |
89 | 0 | msg.append( '\n' ); |
90 | 0 | msg.append( "Command line was:" + cmdLine ); |
91 | |
|
92 | 0 | throw new CommandLineException( msg.toString() ); |
93 | |
} |
94 | |
} |
95 | 0 | catch ( CommandLineException e ) |
96 | |
{ |
97 | 0 | if ( getLogger().isErrorEnabled() ) |
98 | |
{ |
99 | 0 | getLogger().error( "CommandLineException " + e.getMessage(), e ); |
100 | |
} |
101 | 0 | } |
102 | |
|
103 | 0 | return new CheckInScmResult( cl.toString(), consumer.isSuccess() ? "Checkin successful" : "Unable to submit", |
104 | |
consumer.getOutput(), consumer.isSuccess() ); |
105 | |
} |
106 | |
|
107 | |
public static Commandline createCommandLine( PerforceScmProviderRepository repo, File workingDirectory ) |
108 | |
{ |
109 | 3 | Commandline command = PerforceScmProvider.createP4Command( repo, workingDirectory ); |
110 | |
|
111 | 3 | command.createArg().setValue( "submit" ); |
112 | 3 | command.createArg().setValue( "-i" ); |
113 | 3 | return command; |
114 | |
} |
115 | |
|
116 | |
private static final String NEWLINE = "\r\n"; |
117 | |
|
118 | |
public static String createChangeListSpecification( PerforceScmProviderRepository repo, ScmFileSet files, |
119 | |
String msg, String canonicalPath, String jobs ) |
120 | |
{ |
121 | 3 | StringBuilder buf = new StringBuilder(); |
122 | 3 | buf.append( "Change: new" ).append( NEWLINE ).append( NEWLINE ); |
123 | 3 | buf.append( "Description:" ).append( NEWLINE ).append( "\t" ).append( msg ).append( NEWLINE ).append( NEWLINE ); |
124 | 3 | if ( jobs != null && jobs.length() != 0 ) |
125 | |
{ |
126 | |
|
127 | 3 | buf.append( "Jobs:" ).append( NEWLINE ).append( "\t" ).append( jobs ).append( NEWLINE ).append( NEWLINE ); |
128 | |
} |
129 | |
|
130 | 3 | buf.append( "Files:" ).append( NEWLINE ); |
131 | |
try |
132 | |
{ |
133 | 3 | Set<String> dupes = new HashSet<String>(); |
134 | 3 | File workingDir = files.getBasedir(); |
135 | 3 | String candir = workingDir.getCanonicalPath(); |
136 | 3 | List<File> fs = files.getFileList(); |
137 | 9 | for ( int i = 0; i < fs.size(); i++ ) |
138 | |
{ |
139 | 6 | File file = new File( workingDir, fs.get( i ).getPath() ); |
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | 6 | String canfile = file.getCanonicalPath(); |
149 | 6 | if ( dupes.contains( canfile ) ) |
150 | |
{ |
151 | |
|
152 | |
|
153 | |
|
154 | 0 | System.err.println( "Skipping duplicate file: " + file ); |
155 | 0 | continue; |
156 | |
} |
157 | 6 | dupes.add( canfile ); |
158 | 6 | if ( canfile.startsWith( candir ) ) |
159 | |
{ |
160 | 6 | canfile = canfile.substring( candir.length() + 1 ); |
161 | |
} |
162 | 6 | buf.append( "\t" ).append( canonicalPath ).append( "/" ).append( canfile.replace( '\\', '/' ) ).append( |
163 | |
NEWLINE ); |
164 | |
} |
165 | |
} |
166 | 0 | catch ( IOException e ) |
167 | |
{ |
168 | 0 | e.printStackTrace(); |
169 | 3 | } |
170 | 3 | return buf.toString(); |
171 | |
} |
172 | |
} |