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.info;
20  
21  import java.io.File;
22  import java.util.Collections;
23  import java.util.LinkedList;
24  import java.util.List;
25  
26  import org.apache.maven.scm.CommandParameter;
27  import org.apache.maven.scm.CommandParameters;
28  import org.apache.maven.scm.ScmException;
29  import org.apache.maven.scm.ScmFileSet;
30  import org.apache.maven.scm.ScmResult;
31  import org.apache.maven.scm.command.AbstractCommand;
32  import org.apache.maven.scm.command.info.InfoItem;
33  import org.apache.maven.scm.command.info.InfoScmResult;
34  import org.apache.maven.scm.provider.ScmProviderRepository;
35  import org.apache.maven.scm.provider.git.command.GitCommand;
36  import org.apache.maven.scm.provider.git.gitexe.command.GitCommandLineUtils;
37  import org.codehaus.plexus.util.cli.CommandLineUtils;
38  import org.codehaus.plexus.util.cli.Commandline;
39  
40  /**
41   * Uses {@code git log} command to retrieve info about the most recent commits related to specific files.
42   * @author Olivier Lamy
43   * @since 1.5
44   */
45  public class GitInfoCommand extends AbstractCommand implements GitCommand {
46  
47      public static final int NO_REVISION_LENGTH = -1;
48  
49      @Override
50      protected ScmResult executeCommand(
51              ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters) throws ScmException {
52  
53          Commandline baseCli = GitCommandLineUtils.getBaseGitCommandLine(fileSet.getBasedir(), "log");
54          baseCli.createArg().setValue("-1"); // only most recent commit matters
55          baseCli.createArg().setValue("--no-merges"); // skip merge commits
56          baseCli.addArg(GitInfoConsumer.getFormatArgument());
57  
58          List<InfoItem> infoItems = new LinkedList<>();
59          if (fileSet.getFileList().isEmpty()) {
60              infoItems.add(executeInfoCommand(baseCli, parameters, fileSet.getBasedir()));
61          } else {
62              // iterate over files
63              for (File scmFile : fileSet.getFileList()) {
64                  baseCli = GitCommandLineUtils.getBaseGitCommandLine(fileSet.getBasedir(), "log");
65                  baseCli.createArg().setValue("-1"); // only most recent commit matters
66                  baseCli.createArg().setValue("--no-merges"); // skip merge commits
67                  baseCli.addArg(GitInfoConsumer.getFormatArgument());
68                  // Insert a separator to make sure that files aren't interpreted as part of the version spec
69                  baseCli.createArg().setValue("--");
70                  GitCommandLineUtils.addTarget(baseCli, Collections.singletonList(scmFile));
71                  infoItems.add(executeInfoCommand(baseCli, parameters, scmFile));
72              }
73          }
74          return new InfoScmResult(baseCli.toString(), infoItems);
75      }
76  
77      protected InfoItem executeInfoCommand(Commandline cli, CommandParameters parameters, File scmFile)
78              throws ScmException {
79          GitInfoConsumer consumer = new GitInfoConsumer(scmFile.toPath(), getRevisionLength(parameters));
80          CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
81          int exitCode = GitCommandLineUtils.execute(cli, consumer, stderr);
82          if (exitCode != 0) {
83              throw new ScmException("The git log command failed: " + cli.toString() + " returned " + stderr.getOutput());
84          }
85          return consumer.getInfoItem();
86      }
87  
88      /**
89       * Get the revision length from the parameters
90       *
91       * @param parameters
92       * @return -1 if parameter {@link CommandParameter.SCM_SHORT_REVISION_LENGTH} is absent, <br>
93       *         and otherwise - the length to be applied for the revision formatting
94       * @throws ScmException
95       * @since 1.7
96       */
97      private static int getRevisionLength(final CommandParameters parameters) throws ScmException {
98          if (parameters == null) {
99              return NO_REVISION_LENGTH;
100         } else {
101             return parameters.getInt(CommandParameter.SCM_SHORT_REVISION_LENGTH, NO_REVISION_LENGTH);
102         }
103     }
104 }