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(), false );
69          }
70  
71          return new DiffScmResult( clDiff2Index.toString(), consumer.getChangedFiles(), consumer.getDifferences(),
72                                    consumer.getPatch() );
73      }
74  
75      // ----------------------------------------------------------------------
76      //
77      // ----------------------------------------------------------------------
78  
79      /**
80       * @param cached if <code>true</code> diff the index to the head, else diff the tree to the index
81       */
82      public static Commandline createCommandLine( File workingDirectory, ScmVersion startVersion, ScmVersion endVersion,
83                                                   boolean cached )
84      {
85          Commandline cl = GitCommandLineUtils.getBaseGitCommandLine( workingDirectory, "diff" );
86  
87          if ( cached )
88          {
89              cl.createArg().setValue( "--cached" );
90          }
91  
92          if ( startVersion != null && StringUtils.isNotEmpty( startVersion.getName() ) )
93          {
94              cl.createArg().setValue( startVersion.getName() );
95          }
96          if ( endVersion != null && StringUtils.isNotEmpty( endVersion.getName() ) )
97          {
98              cl.createArg().setValue( endVersion.getName() );
99          }
100 
101         return cl;
102     }
103 
104     /**
105      * Create a CommandLine for executing a git diff --raw command.
106      * This will output all affected files affected since the given commit and
107      * the current version.
108      *
109      * @param workingDirectory
110      */
111     public static Commandline createDiffRawCommandLine( File workingDirectory, String sha1 )
112     {
113         Commandline cl = GitCommandLineUtils.getBaseGitCommandLine( workingDirectory, "diff" );
114 
115         cl.createArg().setValue( "--raw" );
116         cl.createArg().setValue( sha1 );
117 
118         return cl;
119     }
120 
121 }