View Javadoc

1   package org.apache.maven.scm.provider.git.gitexe.command.blame;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.scm.CommandParameter;
23  import org.apache.maven.scm.CommandParameters;
24  import org.apache.maven.scm.ScmException;
25  import org.apache.maven.scm.ScmFileSet;
26  import org.apache.maven.scm.ScmResult;
27  import org.apache.maven.scm.command.blame.AbstractBlameCommand;
28  import org.apache.maven.scm.command.blame.BlameScmResult;
29  import org.apache.maven.scm.provider.ScmProviderRepository;
30  import org.apache.maven.scm.provider.git.command.GitCommand;
31  import org.apache.maven.scm.provider.git.gitexe.command.GitCommandLineUtils;
32  import org.codehaus.plexus.util.cli.CommandLineUtils;
33  import org.codehaus.plexus.util.cli.Commandline;
34  
35  import java.io.File;
36  
37  /**
38   * @author Evgeny Mandrikov
39   * @author Olivier Lamy
40   * @since 1.4
41   */
42  public class GitBlameCommand
43      extends AbstractBlameCommand
44      implements GitCommand
45  {
46  
47      @Override
48      protected ScmResult executeCommand( ScmProviderRepository repository, ScmFileSet workingDirectory,
49                                          CommandParameters parameters )
50          throws ScmException
51      {
52          String filename = parameters.getString( CommandParameter.FILE );
53          Commandline cl = createCommandLine( workingDirectory.getBasedir(), filename,
54                                              parameters.getBoolean( CommandParameter.IGNORE_WHITESPACE, false ) );
55          GitBlameConsumer consumer = new GitBlameConsumer( getLogger() );
56          CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
57  
58          int exitCode = GitCommandLineUtils.execute( cl, consumer, stderr, getLogger() );
59          if ( exitCode != 0 )
60          {
61              return new BlameScmResult( cl.toString(), "The git blame command failed.", stderr.getOutput(), false );
62          }
63          return new BlameScmResult( cl.toString(), consumer.getLines() );
64      }
65  
66      /**
67       * {@inheritDoc}
68       */
69      public BlameScmResult executeBlameCommand( ScmProviderRepository repo, ScmFileSet workingDirectory,
70                                                 String filename )
71          throws ScmException
72      {
73          CommandParameters commandParameters = new CommandParameters();
74          commandParameters.setString( CommandParameter.FILE, filename );
75          commandParameters.setString( CommandParameter.IGNORE_WHITESPACE, Boolean.FALSE.toString() );
76          return (BlameScmResult) execute( repo, workingDirectory, commandParameters );
77      }
78  
79      protected static Commandline createCommandLine( File workingDirectory, String filename, boolean ignoreWhitespace )
80      {
81          Commandline cl = GitCommandLineUtils.getBaseGitCommandLine( workingDirectory, "blame" );
82          cl.createArg().setValue( "--porcelain" );
83          cl.createArg().setValue( filename );
84          if ( ignoreWhitespace )
85          {
86              cl.createArg().setValue( "-w" );
87          }
88          return cl;
89      }
90  }