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