001package 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 022import org.apache.maven.scm.ScmException; 023import org.apache.maven.scm.ScmFileSet; 024import org.apache.maven.scm.ScmVersion; 025import org.apache.maven.scm.command.diff.AbstractDiffCommand; 026import org.apache.maven.scm.command.diff.DiffScmResult; 027import org.apache.maven.scm.provider.ScmProviderRepository; 028import org.apache.maven.scm.provider.git.command.GitCommand; 029import org.apache.maven.scm.provider.git.command.diff.GitDiffConsumer; 030import org.apache.maven.scm.provider.git.gitexe.command.GitCommandLineUtils; 031import org.codehaus.plexus.util.StringUtils; 032import org.codehaus.plexus.util.cli.CommandLineUtils; 033import org.codehaus.plexus.util.cli.Commandline; 034 035import java.io.File; 036 037/** 038 * @author <a href="mailto:struberg@yahoo.de">Mark Struberg</a> 039 * 040 */ 041public 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(), 069 false ); 070 } 071 072 return new DiffScmResult( clDiff2Index.toString(), consumer.getChangedFiles(), consumer.getDifferences(), 073 consumer.getPatch() ); 074 } 075 076 // ---------------------------------------------------------------------- 077 // 078 // ---------------------------------------------------------------------- 079 080 /** 081 * @param cached if <code>true</code> diff the index to the head, else diff the tree to the index 082 */ 083 public static Commandline createCommandLine( File workingDirectory, ScmVersion startVersion, ScmVersion endVersion, 084 boolean cached ) 085 { 086 Commandline cl = GitCommandLineUtils.getBaseGitCommandLine( workingDirectory, "diff" ); 087 088 if ( cached ) 089 { 090 cl.createArg().setValue( "--cached" ); 091 } 092 093 if ( startVersion != null && StringUtils.isNotEmpty( startVersion.getName() ) ) 094 { 095 cl.createArg().setValue( startVersion.getName() ); 096 } 097 if ( endVersion != null && StringUtils.isNotEmpty( endVersion.getName() ) ) 098 { 099 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}