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