View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * @author <a href="mailto:struberg@yahoo.de">Mark Struberg</a>
38   *
39   */
40  public class GitDiffCommand extends AbstractDiffCommand implements GitCommand {
41      /** {@inheritDoc} */
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       * @param cached if <code>true</code> diff the index to the head, else diff the tree to the index
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       * Create a CommandLine for executing a git diff --raw command.
95       * This will output all affected files affected since the given commit and
96       * the current version.
97       *
98       * @param workingDirectory
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 }