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