001 package org.apache.maven.scm.provider.git.gitexe.command.blame; 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.CommandParameter; 023 import org.apache.maven.scm.CommandParameters; 024 import org.apache.maven.scm.ScmException; 025 import org.apache.maven.scm.ScmFileSet; 026 import org.apache.maven.scm.ScmResult; 027 import org.apache.maven.scm.command.blame.AbstractBlameCommand; 028 import org.apache.maven.scm.command.blame.BlameScmResult; 029 import org.apache.maven.scm.provider.ScmProviderRepository; 030 import org.apache.maven.scm.provider.git.command.GitCommand; 031 import org.apache.maven.scm.provider.git.gitexe.command.GitCommandLineUtils; 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 Evgeny Mandrikov 039 * @author Olivier Lamy 040 * @since 1.4 041 */ 042 public class GitBlameCommand 043 extends AbstractBlameCommand 044 implements GitCommand 045 { 046 047 @Override 048 protected ScmResult executeCommand( ScmProviderRepository repository, ScmFileSet workingDirectory, 049 CommandParameters parameters ) 050 throws ScmException 051 { 052 String filename = parameters.getString( CommandParameter.FILE ); 053 Commandline cl = createCommandLine( workingDirectory.getBasedir(), filename, 054 parameters.getBoolean( CommandParameter.IGNORE_WHITESPACE, false ) ); 055 GitBlameConsumer consumer = new GitBlameConsumer( getLogger() ); 056 CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer(); 057 058 int exitCode = GitCommandLineUtils.execute( cl, consumer, stderr, getLogger() ); 059 if ( exitCode != 0 ) 060 { 061 return new BlameScmResult( cl.toString(), "The git blame command failed.", stderr.getOutput(), false ); 062 } 063 return new BlameScmResult( cl.toString(), consumer.getLines() ); 064 } 065 066 /** 067 * {@inheritDoc} 068 */ 069 public BlameScmResult executeBlameCommand( ScmProviderRepository repo, ScmFileSet workingDirectory, 070 String filename ) 071 throws ScmException 072 { 073 CommandParameters commandParameters = new CommandParameters(); 074 commandParameters.setString( CommandParameter.FILE, filename ); 075 commandParameters.setString( CommandParameter.IGNORE_WHITESPACE, Boolean.FALSE.toString() ); 076 return (BlameScmResult) execute( repo, workingDirectory, commandParameters ); 077 } 078 079 protected static Commandline createCommandLine( File workingDirectory, String filename, boolean ignoreWhitespace ) 080 { 081 Commandline cl = GitCommandLineUtils.getBaseGitCommandLine( workingDirectory, "blame" ); 082 cl.createArg().setValue( "--porcelain" ); 083 cl.createArg().setValue( filename ); 084 if ( ignoreWhitespace ) 085 { 086 cl.createArg().setValue( "-w" ); 087 } 088 return cl; 089 } 090 }