1 | |
package org.apache.maven.scm.provider.perforce; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
import java.io.BufferedReader; |
24 | |
import java.io.File; |
25 | |
import java.io.IOException; |
26 | |
import java.io.InputStreamReader; |
27 | |
import java.net.InetAddress; |
28 | |
import java.net.UnknownHostException; |
29 | |
|
30 | |
import org.apache.maven.scm.CommandParameters; |
31 | |
import org.apache.maven.scm.ScmException; |
32 | |
import org.apache.maven.scm.ScmFileSet; |
33 | |
import org.apache.maven.scm.command.add.AddScmResult; |
34 | |
import org.apache.maven.scm.command.blame.BlameScmResult; |
35 | |
import org.apache.maven.scm.command.changelog.ChangeLogScmResult; |
36 | |
import org.apache.maven.scm.command.checkin.CheckInScmResult; |
37 | |
import org.apache.maven.scm.command.checkout.CheckOutScmResult; |
38 | |
import org.apache.maven.scm.command.diff.DiffScmResult; |
39 | |
import org.apache.maven.scm.command.edit.EditScmResult; |
40 | |
import org.apache.maven.scm.command.login.LoginScmResult; |
41 | |
import org.apache.maven.scm.command.remove.RemoveScmResult; |
42 | |
import org.apache.maven.scm.command.status.StatusScmResult; |
43 | |
import org.apache.maven.scm.command.tag.TagScmResult; |
44 | |
import org.apache.maven.scm.command.unedit.UnEditScmResult; |
45 | |
import org.apache.maven.scm.command.update.UpdateScmResult; |
46 | |
import org.apache.maven.scm.log.ScmLogger; |
47 | |
import org.apache.maven.scm.provider.AbstractScmProvider; |
48 | |
import org.apache.maven.scm.provider.ScmProviderRepository; |
49 | |
import org.apache.maven.scm.provider.perforce.command.PerforceInfoCommand; |
50 | |
import org.apache.maven.scm.provider.perforce.command.PerforceWhereCommand; |
51 | |
import org.apache.maven.scm.provider.perforce.command.add.PerforceAddCommand; |
52 | |
import org.apache.maven.scm.provider.perforce.command.blame.PerforceBlameCommand; |
53 | |
import org.apache.maven.scm.provider.perforce.command.changelog.PerforceChangeLogCommand; |
54 | |
import org.apache.maven.scm.provider.perforce.command.checkin.PerforceCheckInCommand; |
55 | |
import org.apache.maven.scm.provider.perforce.command.checkout.PerforceCheckOutCommand; |
56 | |
import org.apache.maven.scm.provider.perforce.command.diff.PerforceDiffCommand; |
57 | |
import org.apache.maven.scm.provider.perforce.command.edit.PerforceEditCommand; |
58 | |
import org.apache.maven.scm.provider.perforce.command.login.PerforceLoginCommand; |
59 | |
import org.apache.maven.scm.provider.perforce.command.remove.PerforceRemoveCommand; |
60 | |
import org.apache.maven.scm.provider.perforce.command.status.PerforceStatusCommand; |
61 | |
import org.apache.maven.scm.provider.perforce.command.tag.PerforceTagCommand; |
62 | |
import org.apache.maven.scm.provider.perforce.command.unedit.PerforceUnEditCommand; |
63 | |
import org.apache.maven.scm.provider.perforce.command.update.PerforceUpdateCommand; |
64 | |
import org.apache.maven.scm.provider.perforce.repository.PerforceScmProviderRepository; |
65 | |
import org.apache.maven.scm.repository.ScmRepositoryException; |
66 | |
import org.codehaus.plexus.util.StringUtils; |
67 | |
import org.codehaus.plexus.util.cli.Commandline; |
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | 34 | public class PerforceScmProvider |
76 | |
extends AbstractScmProvider |
77 | |
{ |
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
public boolean requiresEditMode() |
83 | |
{ |
84 | 0 | return true; |
85 | |
} |
86 | |
|
87 | |
public ScmProviderRepository makeProviderScmRepository( String scmSpecificUrl, char delimiter ) |
88 | |
throws ScmRepositoryException |
89 | |
{ |
90 | |
String path; |
91 | 33 | int port = 0; |
92 | 33 | String host = null; |
93 | |
|
94 | 33 | int i1 = scmSpecificUrl.indexOf( delimiter ); |
95 | 33 | int i2 = scmSpecificUrl.indexOf( delimiter, i1 + 1 ); |
96 | |
|
97 | 33 | if ( i1 > 0 ) |
98 | |
{ |
99 | 15 | int lastDelimiter = scmSpecificUrl.lastIndexOf( delimiter ); |
100 | 15 | path = scmSpecificUrl.substring( lastDelimiter + 1 ); |
101 | 15 | host = scmSpecificUrl.substring( 0, i1 ); |
102 | |
|
103 | |
|
104 | 15 | if ( i2 >= 0 ) |
105 | |
{ |
106 | |
try |
107 | |
{ |
108 | 8 | String tmp = scmSpecificUrl.substring( i1 + 1, lastDelimiter ); |
109 | 8 | port = Integer.parseInt( tmp ); |
110 | |
} |
111 | 0 | catch ( NumberFormatException ex ) |
112 | |
{ |
113 | 0 | throw new ScmRepositoryException( "The port has to be a number." ); |
114 | 8 | } |
115 | |
} |
116 | 15 | } |
117 | |
else |
118 | |
{ |
119 | 18 | path = scmSpecificUrl; |
120 | |
} |
121 | |
|
122 | 33 | String user = null; |
123 | 33 | String password = null; |
124 | 33 | if ( host != null && host.indexOf( '@' ) > 1 ) |
125 | |
{ |
126 | 2 | user = host.substring( 0, host.indexOf( '@' ) ); |
127 | 2 | host = host.substring( host.indexOf( '@' ) + 1 ); |
128 | |
} |
129 | |
|
130 | 33 | if ( path.indexOf( '@' ) > 1 ) |
131 | |
{ |
132 | 12 | if ( host != null ) |
133 | |
{ |
134 | 11 | if ( getLogger().isWarnEnabled() ) |
135 | |
{ |
136 | 11 | getLogger().warn( |
137 | |
"Username as part of path is deprecated, the new format is " |
138 | |
+ "scm:perforce:[username@]host:port:path_to_repository" ); |
139 | |
} |
140 | |
} |
141 | |
|
142 | 12 | user = path.substring( 0, path.indexOf( '@' ) ); |
143 | 12 | path = path.substring( path.indexOf( '@' ) + 1 ); |
144 | |
} |
145 | |
|
146 | 33 | return new PerforceScmProviderRepository( host, port, path, user, password ); |
147 | |
} |
148 | |
|
149 | |
public String getScmType() |
150 | |
{ |
151 | 0 | return "perforce"; |
152 | |
} |
153 | |
|
154 | |
|
155 | |
protected ChangeLogScmResult changelog( ScmProviderRepository repository, ScmFileSet fileSet, |
156 | |
CommandParameters parameters ) |
157 | |
throws ScmException |
158 | |
{ |
159 | 0 | PerforceChangeLogCommand command = new PerforceChangeLogCommand(); |
160 | 0 | command.setLogger( getLogger() ); |
161 | 0 | return (ChangeLogScmResult) command.execute( repository, fileSet, parameters ); |
162 | |
} |
163 | |
|
164 | |
public AddScmResult add( ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters params ) |
165 | |
throws ScmException |
166 | |
{ |
167 | 0 | PerforceAddCommand command = new PerforceAddCommand(); |
168 | 0 | command.setLogger( getLogger() ); |
169 | 0 | return (AddScmResult) command.execute( repository, fileSet, params ); |
170 | |
} |
171 | |
|
172 | |
protected RemoveScmResult remove( ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters params ) |
173 | |
throws ScmException |
174 | |
{ |
175 | 0 | PerforceRemoveCommand command = new PerforceRemoveCommand(); |
176 | 0 | command.setLogger( getLogger() ); |
177 | 0 | return (RemoveScmResult) command.execute( repository, fileSet, params ); |
178 | |
} |
179 | |
|
180 | |
protected CheckInScmResult checkin( ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters params ) |
181 | |
throws ScmException |
182 | |
{ |
183 | 0 | PerforceCheckInCommand command = new PerforceCheckInCommand(); |
184 | 0 | command.setLogger( getLogger() ); |
185 | 0 | return (CheckInScmResult) command.execute( repository, fileSet, params ); |
186 | |
} |
187 | |
|
188 | |
protected CheckOutScmResult checkout( ScmProviderRepository repository, ScmFileSet fileSet, |
189 | |
CommandParameters params ) |
190 | |
throws ScmException |
191 | |
{ |
192 | 0 | PerforceCheckOutCommand command = new PerforceCheckOutCommand(); |
193 | 0 | command.setLogger( getLogger() ); |
194 | 0 | return (CheckOutScmResult) command.execute( repository, fileSet, params ); |
195 | |
} |
196 | |
|
197 | |
protected DiffScmResult diff( ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters params ) |
198 | |
throws ScmException |
199 | |
{ |
200 | 0 | PerforceDiffCommand command = new PerforceDiffCommand(); |
201 | 0 | command.setLogger( getLogger() ); |
202 | 0 | return (DiffScmResult) command.execute( repository, fileSet, params ); |
203 | |
} |
204 | |
|
205 | |
protected EditScmResult edit( ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters params ) |
206 | |
throws ScmException |
207 | |
{ |
208 | 0 | PerforceEditCommand command = new PerforceEditCommand(); |
209 | 0 | command.setLogger( getLogger() ); |
210 | 0 | return (EditScmResult) command.execute( repository, fileSet, params ); |
211 | |
} |
212 | |
|
213 | |
protected LoginScmResult login( ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters params ) |
214 | |
throws ScmException |
215 | |
{ |
216 | 0 | PerforceLoginCommand command = new PerforceLoginCommand(); |
217 | 0 | command.setLogger( getLogger() ); |
218 | 0 | return (LoginScmResult) command.execute( repository, fileSet, params ); |
219 | |
} |
220 | |
|
221 | |
protected StatusScmResult status( ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters params ) |
222 | |
throws ScmException |
223 | |
{ |
224 | 0 | PerforceStatusCommand command = new PerforceStatusCommand(); |
225 | 0 | command.setLogger( getLogger() ); |
226 | 0 | return (StatusScmResult) command.execute( repository, fileSet, params ); |
227 | |
} |
228 | |
|
229 | |
protected TagScmResult tag( ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters params ) |
230 | |
throws ScmException |
231 | |
{ |
232 | 0 | PerforceTagCommand command = new PerforceTagCommand(); |
233 | 0 | command.setLogger( getLogger() ); |
234 | 0 | return (TagScmResult) command.execute( repository, fileSet, params ); |
235 | |
} |
236 | |
|
237 | |
protected UnEditScmResult unedit( ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters params ) |
238 | |
throws ScmException |
239 | |
{ |
240 | 0 | PerforceUnEditCommand command = new PerforceUnEditCommand(); |
241 | 0 | command.setLogger( getLogger() ); |
242 | 0 | return (UnEditScmResult) command.execute( repository, fileSet, params ); |
243 | |
} |
244 | |
|
245 | |
protected UpdateScmResult update( ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters params ) |
246 | |
throws ScmException |
247 | |
{ |
248 | 0 | PerforceUpdateCommand command = new PerforceUpdateCommand(); |
249 | 0 | command.setLogger( getLogger() ); |
250 | 0 | return (UpdateScmResult) command.execute( repository, fileSet, params ); |
251 | |
} |
252 | |
|
253 | |
protected BlameScmResult blame( ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters params ) |
254 | |
throws ScmException |
255 | |
{ |
256 | 0 | PerforceBlameCommand command = new PerforceBlameCommand(); |
257 | 0 | command.setLogger( getLogger() ); |
258 | 0 | return (BlameScmResult) command.execute( repository, fileSet, params ); |
259 | |
} |
260 | |
|
261 | |
public static Commandline createP4Command( PerforceScmProviderRepository repo, File workingDir ) |
262 | |
{ |
263 | 26 | Commandline command = new Commandline(); |
264 | 26 | command.setExecutable( "p4" ); |
265 | 26 | if ( workingDir != null ) |
266 | |
{ |
267 | |
|
268 | 26 | command.createArg().setValue( "-d" ); |
269 | 26 | command.createArg().setValue( workingDir.getAbsolutePath() ); |
270 | |
} |
271 | |
|
272 | 26 | if ( repo.getHost() != null ) |
273 | |
{ |
274 | 10 | command.createArg().setValue( "-p" ); |
275 | 10 | String value = repo.getHost(); |
276 | 10 | if ( repo.getPort() != 0 ) |
277 | |
{ |
278 | 5 | value += ":" + Integer.toString( repo.getPort() ); |
279 | |
} |
280 | 10 | command.createArg().setValue( value ); |
281 | |
} |
282 | |
|
283 | 26 | if ( StringUtils.isNotEmpty( repo.getUser() ) ) |
284 | |
{ |
285 | 10 | command.createArg().setValue( "-u" ); |
286 | 10 | command.createArg().setValue( repo.getUser() ); |
287 | |
} |
288 | |
|
289 | 26 | if ( StringUtils.isNotEmpty( repo.getPassword() ) ) |
290 | |
{ |
291 | 0 | command.createArg().setValue( "-P" ); |
292 | 0 | command.createArg().setValue( repo.getPassword() ); |
293 | |
} |
294 | 26 | return command; |
295 | |
} |
296 | |
|
297 | |
public static String clean( String string ) |
298 | |
{ |
299 | 2 | if ( string.indexOf( " -P " ) == -1 ) |
300 | |
{ |
301 | 1 | return string; |
302 | |
} |
303 | 1 | int idx = string.indexOf( " -P " ) + 4; |
304 | 1 | int end = string.indexOf( ' ', idx ); |
305 | 1 | return string.substring( 0, idx ) + StringUtils.repeat( "*", end - idx ) + string.substring( end ); |
306 | |
} |
307 | |
|
308 | |
|
309 | |
|
310 | |
|
311 | |
|
312 | |
|
313 | |
|
314 | |
|
315 | |
|
316 | |
|
317 | |
|
318 | |
|
319 | |
public static String getCanonicalRepoPath( String repoPath ) |
320 | |
{ |
321 | 4 | if ( repoPath.endsWith( "/..." ) ) |
322 | |
{ |
323 | 1 | return repoPath; |
324 | |
} |
325 | 3 | else if ( repoPath.endsWith( "/" ) ) |
326 | |
{ |
327 | 1 | return repoPath + "..."; |
328 | |
} |
329 | |
else |
330 | |
{ |
331 | 2 | return repoPath + "/..."; |
332 | |
} |
333 | |
} |
334 | |
|
335 | |
private static final String NEWLINE = "\r\n"; |
336 | |
|
337 | |
|
338 | |
|
339 | |
|
340 | |
|
341 | |
|
342 | |
|
343 | |
|
344 | |
|
345 | |
|
346 | |
|
347 | |
|
348 | |
|
349 | |
|
350 | |
|
351 | |
|
352 | |
|
353 | |
public static String createClientspec( ScmLogger logger, PerforceScmProviderRepository repo, File workDir, |
354 | |
String repoPath ) |
355 | |
{ |
356 | 0 | String clientspecName = getClientspecName( logger, repo, workDir ); |
357 | 0 | String userName = getUsername( logger, repo ); |
358 | |
|
359 | |
String rootDir; |
360 | |
try |
361 | |
{ |
362 | |
|
363 | 0 | rootDir = workDir.getCanonicalPath(); |
364 | |
} |
365 | 0 | catch ( IOException ex ) |
366 | |
{ |
367 | |
|
368 | 0 | rootDir = workDir.getAbsolutePath(); |
369 | 0 | } |
370 | |
|
371 | 0 | StringBuilder buf = new StringBuilder(); |
372 | 0 | buf.append( "Client: " ).append( clientspecName ).append( NEWLINE ); |
373 | 0 | buf.append( "Root: " ).append( rootDir ).append( NEWLINE ); |
374 | 0 | buf.append( "Owner: " ).append( userName ).append( NEWLINE ); |
375 | 0 | buf.append( "View:" ).append( NEWLINE ); |
376 | 0 | buf.append( "\t" ).append( PerforceScmProvider.getCanonicalRepoPath( repoPath ) ); |
377 | 0 | buf.append( " //" ).append( clientspecName ).append( "/..." ).append( NEWLINE ); |
378 | 0 | buf.append( "Description:" ).append( NEWLINE ); |
379 | 0 | buf.append( "\t" ).append( "Created by maven-scm-provider-perforce" ).append( NEWLINE ); |
380 | 0 | return buf.toString(); |
381 | |
} |
382 | |
|
383 | |
public static final String DEFAULT_CLIENTSPEC_PROPERTY = "maven.scm.perforce.clientspec.name"; |
384 | |
|
385 | |
public static String getClientspecName( ScmLogger logger, PerforceScmProviderRepository repo, File workDir ) |
386 | |
{ |
387 | 0 | String def = generateDefaultClientspecName( logger, repo, workDir ); |
388 | |
|
389 | 0 | String l = System.getProperty( DEFAULT_CLIENTSPEC_PROPERTY, def ); |
390 | 0 | if ( l == null || "".equals( l.trim() ) ) |
391 | |
{ |
392 | 0 | return def; |
393 | |
} |
394 | 0 | return l; |
395 | |
} |
396 | |
|
397 | |
private static String generateDefaultClientspecName( ScmLogger logger, PerforceScmProviderRepository repo, |
398 | |
File workDir ) |
399 | |
{ |
400 | 0 | String username = getUsername( logger, repo ); |
401 | |
String hostname; |
402 | |
String path; |
403 | |
try |
404 | |
{ |
405 | 0 | hostname = InetAddress.getLocalHost().getHostName(); |
406 | |
|
407 | 0 | path = workDir.getCanonicalPath().replaceAll( "[/ ~]", "-" ); |
408 | |
} |
409 | 0 | catch ( UnknownHostException e ) |
410 | |
{ |
411 | |
|
412 | 0 | throw new RuntimeException( e ); |
413 | |
} |
414 | 0 | catch ( IOException e ) |
415 | |
{ |
416 | 0 | throw new RuntimeException( e ); |
417 | 0 | } |
418 | 0 | return username + "-" + hostname + "-MavenSCM-" + path; |
419 | |
} |
420 | |
|
421 | |
private static String getUsername( ScmLogger logger, PerforceScmProviderRepository repo ) |
422 | |
{ |
423 | 0 | String username = PerforceInfoCommand.getInfo( logger, repo ).getEntry( "User name" ); |
424 | 0 | if ( username == null ) |
425 | |
{ |
426 | |
|
427 | 0 | username = repo.getUser(); |
428 | 0 | if ( username == null ) |
429 | |
{ |
430 | 0 | username = System.getProperty( "user.name", "nouser" ); |
431 | |
} |
432 | |
} |
433 | 0 | return username; |
434 | |
} |
435 | |
|
436 | |
|
437 | |
|
438 | |
|
439 | |
|
440 | |
|
441 | |
|
442 | |
|
443 | |
|
444 | |
|
445 | |
|
446 | |
|
447 | |
public static String getRepoPath( ScmLogger log, PerforceScmProviderRepository repo, File basedir ) |
448 | |
{ |
449 | 0 | PerforceWhereCommand where = new PerforceWhereCommand( log, repo ); |
450 | |
|
451 | |
|
452 | |
|
453 | |
|
454 | |
|
455 | 0 | if ( basedir.toString().replace( '\\', '/' ).endsWith( "/target/checkout" ) ) |
456 | |
{ |
457 | 0 | String dir = basedir.toString(); |
458 | 0 | basedir = new File( dir.substring( 0, dir.length() - "/target/checkout".length() ) ); |
459 | 0 | log.debug( "Fixing checkout URL: " + basedir ); |
460 | |
} |
461 | 0 | File pom = new File( basedir, "pom.xml" ); |
462 | 0 | String loc = repo.getPath(); |
463 | 0 | log.debug( "SCM path in pom: " + loc ); |
464 | 0 | if ( pom.exists() ) |
465 | |
{ |
466 | 0 | loc = where.getDepotLocation( pom ); |
467 | 0 | if ( loc == null ) |
468 | |
{ |
469 | 0 | loc = repo.getPath(); |
470 | 0 | log.debug( "cannot find depot => using " + loc ); |
471 | |
} |
472 | 0 | else if ( loc.endsWith( "/pom.xml" ) ) |
473 | |
{ |
474 | 0 | loc = loc.substring( 0, loc.length() - "/pom.xml".length() ); |
475 | 0 | log.debug( "Actual POM location: " + loc ); |
476 | 0 | if ( !repo.getPath().equals( loc ) ) |
477 | |
{ |
478 | 0 | log.info( "The SCM location in your pom.xml (" + repo.getPath() |
479 | |
+ ") is not equal to the depot location (" + loc |
480 | |
+ "). This happens frequently with branches. " + "Ignoring the SCM location." ); |
481 | |
} |
482 | |
} |
483 | |
} |
484 | 0 | return loc; |
485 | |
} |
486 | |
|
487 | |
|
488 | 1 | private static Boolean live = null; |
489 | |
|
490 | |
public static boolean isLive() |
491 | |
{ |
492 | 1 | if ( live == null ) |
493 | |
{ |
494 | 1 | if ( !Boolean.getBoolean( "maven.scm.testing" ) ) |
495 | |
{ |
496 | |
|
497 | 0 | live = Boolean.TRUE; |
498 | |
} |
499 | |
else |
500 | |
{ |
501 | |
|
502 | |
|
503 | |
|
504 | |
|
505 | |
try |
506 | |
{ |
507 | 1 | Commandline command = new Commandline(); |
508 | 1 | command.setExecutable( "p4" ); |
509 | 1 | Process proc = command.execute(); |
510 | 1 | BufferedReader br = new BufferedReader( new InputStreamReader( proc.getInputStream() ) ); |
511 | |
@SuppressWarnings( "unused" ) |
512 | |
String line; |
513 | 1 | while ( ( line = br.readLine() ) != null ) |
514 | |
{ |
515 | |
|
516 | |
} |
517 | 1 | int rc = proc.exitValue(); |
518 | 0 | live = ( rc == 0 ? Boolean.TRUE : Boolean.FALSE ); |
519 | |
} |
520 | 1 | catch ( Exception e ) |
521 | |
{ |
522 | 1 | e.printStackTrace(); |
523 | 1 | live = Boolean.FALSE; |
524 | 0 | } |
525 | |
} |
526 | |
} |
527 | |
|
528 | 1 | return live.booleanValue(); |
529 | |
} |
530 | |
} |