1 package org.apache.maven.scm.provider.git.gitexe.command.diff;
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.diff.AbstractDiffCommand;
26 import org.apache.maven.scm.command.diff.DiffScmResult;
27 import org.apache.maven.scm.provider.ScmProviderRepository;
28 import org.apache.maven.scm.provider.git.command.GitCommand;
29 import org.apache.maven.scm.provider.git.command.diff.GitDiffConsumer;
30 import org.apache.maven.scm.provider.git.gitexe.command.GitCommandLineUtils;
31 import org.codehaus.plexus.util.StringUtils;
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 public class GitDiffCommand
42 extends AbstractDiffCommand
43 implements GitCommand
44 {
45
46 protected DiffScmResult executeDiffCommand( ScmProviderRepository repo, ScmFileSet fileSet,
47 ScmVersion startVersion, ScmVersion endVersion )
48 throws ScmException
49 {
50 GitDiffConsumer consumer = new GitDiffConsumer( getLogger(), fileSet.getBasedir() );
51 CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
52 int exitCode;
53
54 Commandline clDiff2Index = createCommandLine( fileSet.getBasedir(), startVersion, endVersion, false );
55
56 exitCode = GitCommandLineUtils.execute( clDiff2Index, consumer, stderr, getLogger() );
57 if ( exitCode != 0 )
58 {
59 return new DiffScmResult( clDiff2Index.toString(), "The git-diff command failed.", stderr.getOutput(),
60 false );
61 }
62
63 Commandline clDiff2Head = createCommandLine( fileSet.getBasedir(), startVersion, endVersion, true );
64
65 exitCode = GitCommandLineUtils.execute( clDiff2Head, consumer, stderr, getLogger() );
66 if ( exitCode != 0 )
67 {
68 return new DiffScmResult( clDiff2Head.toString(), "The git-diff command failed.", stderr.getOutput(),
69 false );
70 }
71
72 return new DiffScmResult( clDiff2Index.toString(), consumer.getChangedFiles(), consumer.getDifferences(),
73 consumer.getPatch() );
74 }
75
76
77
78
79
80
81
82
83 public static Commandline createCommandLine( File workingDirectory, ScmVersion startVersion, ScmVersion endVersion,
84 boolean cached )
85 {
86 Commandline cl = GitCommandLineUtils.getBaseGitCommandLine( workingDirectory, "diff" );
87
88 if ( cached )
89 {
90 cl.createArg().setValue( "--cached" );
91 }
92
93 if ( startVersion != null && StringUtils.isNotEmpty( startVersion.getName() ) )
94 {
95 cl.createArg().setValue( startVersion.getName() );
96 }
97 if ( endVersion != null && StringUtils.isNotEmpty( endVersion.getName() ) )
98 {
99 cl.createArg().setValue( endVersion.getName() );
100 }
101
102 return cl;
103 }
104
105
106
107
108
109
110
111
112 public static Commandline createDiffRawCommandLine( File workingDirectory, String sha1 )
113 {
114 Commandline cl = GitCommandLineUtils.getBaseGitCommandLine( workingDirectory, "diff" );
115
116 cl.createArg().setValue( "--raw" );
117 cl.createArg().setValue( sha1 );
118
119 return cl;
120 }
121
122 }