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