001    package org.apache.maven.scm.provider.git.gitexe.command.diff;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one
005     * or more contributor license agreements.  See the NOTICE file
006     * distributed with this work for additional information
007     * regarding copyright ownership.  The ASF licenses this file
008     * to you under the Apache License, Version 2.0 (the
009     * "License"); you may not use this file except in compliance
010     * with the License.  You may obtain a copy of the License at
011     *
012     * http://www.apache.org/licenses/LICENSE-2.0
013     *
014     * Unless required by applicable law or agreed to in writing,
015     * software distributed under the License is distributed on an
016     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017     * KIND, either express or implied.  See the License for the
018     * specific language governing permissions and limitations
019     * under the License.
020     */
021    
022    import org.apache.maven.scm.ScmException;
023    import org.apache.maven.scm.ScmFileSet;
024    import org.apache.maven.scm.ScmVersion;
025    import org.apache.maven.scm.command.diff.AbstractDiffCommand;
026    import org.apache.maven.scm.command.diff.DiffScmResult;
027    import org.apache.maven.scm.provider.ScmProviderRepository;
028    import org.apache.maven.scm.provider.git.command.GitCommand;
029    import org.apache.maven.scm.provider.git.command.diff.GitDiffConsumer;
030    import org.apache.maven.scm.provider.git.gitexe.command.GitCommandLineUtils;
031    import org.codehaus.plexus.util.StringUtils;
032    import org.codehaus.plexus.util.cli.CommandLineUtils;
033    import org.codehaus.plexus.util.cli.Commandline;
034    
035    import java.io.File;
036    
037    /**
038     * @author <a href="mailto:struberg@yahoo.de">Mark Struberg</a>
039     *
040     */
041    public class GitDiffCommand
042        extends AbstractDiffCommand
043        implements GitCommand
044    {
045        /** {@inheritDoc} */
046        protected DiffScmResult executeDiffCommand( ScmProviderRepository repo, ScmFileSet fileSet,
047                                                    ScmVersion startVersion, ScmVersion endVersion )
048            throws ScmException
049        {
050            GitDiffConsumer consumer = new GitDiffConsumer( getLogger(), fileSet.getBasedir() );
051            CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
052            int exitCode;
053    
054            Commandline clDiff2Index = createCommandLine( fileSet.getBasedir(), startVersion, endVersion, false );
055    
056            exitCode = GitCommandLineUtils.execute( clDiff2Index, consumer, stderr, getLogger() );
057            if ( exitCode != 0 )
058            {
059                return new DiffScmResult( clDiff2Index.toString(), "The git-diff command failed.", stderr.getOutput(),
060                                          false );
061            }
062    
063            Commandline clDiff2Head = createCommandLine( fileSet.getBasedir(), startVersion, endVersion, true );
064    
065            exitCode = GitCommandLineUtils.execute( clDiff2Head, consumer, stderr, getLogger() );
066            if ( exitCode != 0 )
067            {
068                return new DiffScmResult( clDiff2Head.toString(), "The git-diff command failed.", stderr.getOutput(), false );
069            }
070    
071            return new DiffScmResult( clDiff2Index.toString(), consumer.getChangedFiles(), consumer.getDifferences(),
072                                      consumer.getPatch() );
073        }
074    
075        // ----------------------------------------------------------------------
076        //
077        // ----------------------------------------------------------------------
078    
079        /**
080         * @param cached if <code>true</code> diff the index to the head, else diff the tree to the index
081         */
082        public static Commandline createCommandLine( File workingDirectory, ScmVersion startVersion, ScmVersion endVersion,
083                                                     boolean cached )
084        {
085            Commandline cl = GitCommandLineUtils.getBaseGitCommandLine( workingDirectory, "diff" );
086    
087            if ( cached )
088            {
089                cl.createArg().setValue( "--cached" );
090            }
091    
092            if ( startVersion != null && StringUtils.isNotEmpty( startVersion.getName() ) )
093            {
094                cl.createArg().setValue( startVersion.getName() );
095            }
096            if ( endVersion != null && StringUtils.isNotEmpty( endVersion.getName() ) )
097            {
098                cl.createArg().setValue( endVersion.getName() );
099            }
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    }