1 | |
package org.apache.maven.scm.provider.perforce.command.blame; |
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.command.blame.AbstractBlameCommand; |
25 | |
import org.apache.maven.scm.command.blame.BlameLine; |
26 | |
import org.apache.maven.scm.command.blame.BlameScmResult; |
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.File; |
36 | |
import java.util.List; |
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | 0 | public class PerforceBlameCommand |
44 | |
extends AbstractBlameCommand |
45 | |
implements PerforceCommand |
46 | |
{ |
47 | |
public BlameScmResult executeBlameCommand( ScmProviderRepository repo, ScmFileSet workingDirectory, String filename ) |
48 | |
throws ScmException |
49 | |
{ |
50 | |
|
51 | 0 | PerforceScmProviderRepository p4repo = (PerforceScmProviderRepository) repo; |
52 | 0 | String clientspec = PerforceScmProvider.getClientspecName( getLogger(), p4repo, workingDirectory.getBasedir() ); |
53 | 0 | Commandline cl = createCommandLine( (PerforceScmProviderRepository) repo, workingDirectory.getBasedir(), |
54 | |
filename, clientspec); |
55 | |
|
56 | 0 | PerforceBlameConsumer blameConsumer = new PerforceBlameConsumer( getLogger() ); |
57 | |
|
58 | 0 | CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer(); |
59 | |
|
60 | |
int exitCode; |
61 | |
|
62 | |
try |
63 | |
{ |
64 | 0 | exitCode = CommandLineUtils.executeCommandLine( cl, blameConsumer, stderr ); |
65 | |
} |
66 | 0 | catch ( CommandLineException ex ) |
67 | |
{ |
68 | 0 | throw new ScmException( "Error while executing command.", ex ); |
69 | 0 | } |
70 | 0 | if ( exitCode != 0 ) |
71 | |
{ |
72 | 0 | return new BlameScmResult( cl.toString(), "The perforce command failed.", stderr.getOutput(), false ); |
73 | |
} |
74 | |
|
75 | |
|
76 | |
|
77 | 0 | cl = createFilelogCommandLine( (PerforceScmProviderRepository) repo, workingDirectory.getBasedir(), filename, clientspec ); |
78 | |
|
79 | 0 | PerforceFilelogConsumer filelogConsumer = new PerforceFilelogConsumer( getLogger() ); |
80 | |
|
81 | |
try |
82 | |
{ |
83 | 0 | exitCode = CommandLineUtils.executeCommandLine( cl, filelogConsumer, stderr ); |
84 | |
} |
85 | 0 | catch ( CommandLineException ex ) |
86 | |
{ |
87 | 0 | throw new ScmException( "Error while executing command.", ex ); |
88 | 0 | } |
89 | 0 | if ( exitCode != 0 ) |
90 | |
{ |
91 | 0 | return new BlameScmResult( cl.toString(), "The perforce command failed.", stderr.getOutput(), false ); |
92 | |
} |
93 | |
|
94 | |
|
95 | |
|
96 | 0 | List<BlameLine> lines = blameConsumer.getLines(); |
97 | 0 | for ( int i = 0; i < lines.size(); i++ ) |
98 | |
{ |
99 | 0 | BlameLine line = lines.get( i ); |
100 | 0 | String revision = line.getRevision(); |
101 | 0 | line.setAuthor( filelogConsumer.getAuthor( revision ) ); |
102 | 0 | line.setDate( filelogConsumer.getDate( revision ) ); |
103 | |
} |
104 | |
|
105 | 0 | return new BlameScmResult( cl.toString(), lines ); |
106 | |
} |
107 | |
|
108 | |
public static Commandline createCommandLine( PerforceScmProviderRepository repo, File workingDirectory, |
109 | |
String filename, final String clientspec ) |
110 | |
{ |
111 | 0 | Commandline cl = PerforceScmProvider.createP4Command( repo, workingDirectory ); |
112 | 0 | if(clientspec != null) |
113 | |
{ |
114 | 0 | cl.createArg().setValue( "-c" ); |
115 | 0 | cl.createArg().setValue( clientspec ); |
116 | |
} |
117 | 0 | cl.createArg().setValue( "annotate" ); |
118 | 0 | cl.createArg().setValue( "-q" ); |
119 | 0 | cl.createArg().setValue( filename ); |
120 | 0 | return cl; |
121 | |
} |
122 | |
|
123 | |
public static Commandline createFilelogCommandLine( PerforceScmProviderRepository repo, File workingDirectory, |
124 | |
String filename, final String clientspec ) |
125 | |
{ |
126 | 0 | Commandline cl = PerforceScmProvider.createP4Command( repo, workingDirectory ); |
127 | 0 | if(clientspec != null) |
128 | |
{ |
129 | 0 | cl.createArg().setValue( "-c" ); |
130 | 0 | cl.createArg().setValue( clientspec ); |
131 | |
} |
132 | 0 | cl.createArg().setValue( "filelog" ); |
133 | 0 | cl.createArg().setValue( filename ); |
134 | 0 | return cl; |
135 | |
} |
136 | |
} |