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