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  public class GitDiffCommand extends AbstractDiffCommand implements GitCommand {
40      /**
41       * {@inheritDoc}
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       * @param cached if <code>true</code> diff the index to the head, else diff the tree to the index
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       * Create a CommandLine for executing a git diff --raw command.
96       * This will output all affected files affected since the given commit and
97       * the current version.
98       *
99       * @param workingDirectory
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 }