1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.scm.provider.git.gitexe.command.diff;
20
21 import java.io.File;
22
23 import org.apache.commons.lang3.StringUtils;
24 import org.apache.maven.scm.ScmException;
25 import org.apache.maven.scm.ScmFileSet;
26 import org.apache.maven.scm.ScmVersion;
27 import org.apache.maven.scm.command.diff.AbstractDiffCommand;
28 import org.apache.maven.scm.command.diff.DiffScmResult;
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.command.diff.GitDiffConsumer;
32 import org.apache.maven.scm.provider.git.gitexe.command.GitCommandLineUtils;
33 import org.codehaus.plexus.util.cli.CommandLineUtils;
34 import org.codehaus.plexus.util.cli.Commandline;
35
36
37
38
39 public class GitDiffCommand extends AbstractDiffCommand implements GitCommand {
40
41
42
43 protected DiffScmResult executeDiffCommand(
44 ScmProviderRepository repo, ScmFileSet fileSet, ScmVersion startVersion, ScmVersion endVersion)
45 throws ScmException {
46 GitDiffConsumer consumer = new GitDiffConsumer(fileSet.getBasedir());
47 CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
48 int exitCode;
49
50 Commandline clDiff2Index = createCommandLine(fileSet.getBasedir(), startVersion, endVersion, false);
51
52 exitCode = GitCommandLineUtils.execute(clDiff2Index, consumer, stderr);
53 if (exitCode != 0) {
54 return new DiffScmResult(
55 clDiff2Index.toString(), "The git-diff command failed.", stderr.getOutput(), false);
56 }
57
58 Commandline clDiff2Head = createCommandLine(fileSet.getBasedir(), startVersion, endVersion, true);
59
60 exitCode = GitCommandLineUtils.execute(clDiff2Head, consumer, stderr);
61 if (exitCode != 0) {
62 return new DiffScmResult(clDiff2Head.toString(), "The git-diff command failed.", stderr.getOutput(), false);
63 }
64
65 return new DiffScmResult(
66 clDiff2Index.toString(), consumer.getChangedFiles(), consumer.getDifferences(), consumer.getPatch());
67 }
68
69
70
71
72
73
74
75
76 public static Commandline createCommandLine(
77 File workingDirectory, ScmVersion startVersion, ScmVersion endVersion, boolean cached) {
78 Commandline cl = GitCommandLineUtils.getBaseGitCommandLine(workingDirectory, "diff");
79
80 if (cached) {
81 cl.createArg().setValue("--cached");
82 }
83
84 if (startVersion != null && StringUtils.isNotEmpty(startVersion.getName())) {
85 cl.createArg().setValue(startVersion.getName());
86 }
87 if (endVersion != null && StringUtils.isNotEmpty(endVersion.getName())) {
88 cl.createArg().setValue(endVersion.getName());
89 }
90
91 return cl;
92 }
93
94
95
96
97
98
99
100
101 public static Commandline createDiffRawCommandLine(File workingDirectory, String sha1) {
102 Commandline cl = GitCommandLineUtils.getBaseGitCommandLine(workingDirectory, "diff");
103
104 cl.createArg().setValue("--raw");
105 cl.createArg().setValue(sha1);
106
107 return cl;
108 }
109 }