1 package org.apache.maven.scm.provider.git.gitexe.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.CommandParameter;
23 import org.apache.maven.scm.CommandParameters;
24 import org.apache.maven.scm.ScmException;
25 import org.apache.maven.scm.ScmFileSet;
26 import org.apache.maven.scm.ScmResult;
27 import org.apache.maven.scm.command.blame.AbstractBlameCommand;
28 import org.apache.maven.scm.command.blame.BlameScmResult;
29 import org.apache.maven.scm.provider.ScmProviderRepository;
30 import org.apache.maven.scm.provider.git.command.GitCommand;
31 import org.apache.maven.scm.provider.git.gitexe.command.GitCommandLineUtils;
32 import org.codehaus.plexus.util.cli.CommandLineUtils;
33 import org.codehaus.plexus.util.cli.Commandline;
34
35 import java.io.File;
36
37
38
39
40
41
42 public class GitBlameCommand
43 extends AbstractBlameCommand
44 implements GitCommand
45 {
46
47 @Override
48 protected ScmResult executeCommand( ScmProviderRepository repository, ScmFileSet workingDirectory,
49 CommandParameters parameters )
50 throws ScmException
51 {
52 String filename = parameters.getString( CommandParameter.FILE );
53 Commandline cl = createCommandLine( workingDirectory.getBasedir(), filename,
54 parameters.getBoolean( CommandParameter.IGNORE_WHITESPACE, false ) );
55 GitBlameConsumer consumer = new GitBlameConsumer( getLogger() );
56 CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
57
58 int exitCode = GitCommandLineUtils.execute( cl, consumer, stderr, getLogger() );
59 if ( exitCode != 0 )
60 {
61 return new BlameScmResult( cl.toString(), "The git blame command failed.", stderr.getOutput(), false );
62 }
63 return new BlameScmResult( cl.toString(), consumer.getLines() );
64 }
65
66
67
68
69 public BlameScmResult executeBlameCommand( ScmProviderRepository repo, ScmFileSet workingDirectory,
70 String filename )
71 throws ScmException
72 {
73 CommandParameters commandParameters = new CommandParameters();
74 commandParameters.setString( CommandParameter.FILE, filename );
75 commandParameters.setString( CommandParameter.IGNORE_WHITESPACE, Boolean.FALSE.toString() );
76 return (BlameScmResult) execute( repo, workingDirectory, commandParameters );
77 }
78
79 protected static Commandline createCommandLine( File workingDirectory, String filename, boolean ignoreWhitespace )
80 {
81 Commandline cl = GitCommandLineUtils.getBaseGitCommandLine( workingDirectory, "blame" );
82 cl.createArg().setValue( "--porcelain" );
83 cl.createArg().setValue( filename );
84 if ( ignoreWhitespace )
85 {
86 cl.createArg().setValue( "-w" );
87 }
88 return cl;
89 }
90 }