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