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 org.apache.maven.scm.CommandParameter;
22  import org.apache.maven.scm.CommandParameters;
23  import org.apache.maven.scm.ScmException;
24  import org.apache.maven.scm.ScmFileSet;
25  import org.apache.maven.scm.ScmResult;
26  import org.apache.maven.scm.command.AbstractCommand;
27  import org.apache.maven.scm.command.info.InfoScmResult;
28  import org.apache.maven.scm.provider.ScmProviderRepository;
29  import org.apache.maven.scm.provider.git.command.GitCommand;
30  import org.apache.maven.scm.provider.git.gitexe.command.GitCommandLineUtils;
31  import org.codehaus.plexus.util.cli.CommandLineUtils;
32  import org.codehaus.plexus.util.cli.Commandline;
33  
34  /**
35   * @author Olivier Lamy
36   * @since 1.5
37   */
38  public class GitInfoCommand extends AbstractCommand implements GitCommand {
39  
40      public static final int NO_REVISION_LENGTH = -1;
41  
42      @Override
43      protected ScmResult executeCommand(
44              ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters) throws ScmException {
45  
46          GitInfoConsumer consumer = new GitInfoConsumer(fileSet);
47          CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
48  
49          Commandline cli = createCommandLine(repository, fileSet, parameters);
50  
51          int exitCode = GitCommandLineUtils.execute(cli, consumer, stderr);
52          if (exitCode != 0) {
53              return new InfoScmResult(cli.toString(), "The git rev-parse command failed.", stderr.getOutput(), false);
54          }
55          return new InfoScmResult(cli.toString(), consumer.getInfoItems());
56      }
57  
58      public static Commandline createCommandLine(
59              ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters) throws ScmException {
60          Commandline cli = GitCommandLineUtils.getBaseGitCommandLine(fileSet.getBasedir(), "rev-parse");
61          cli.createArg().setValue("--verify");
62          final int revLength = getRevisionLength(parameters);
63          if (revLength > NO_REVISION_LENGTH) // set the --short key only if revision length parameter is passed and
64          // different from -1
65          {
66              cli.createArg().setValue("--short=" + revLength);
67          }
68          cli.createArg().setValue("HEAD");
69  
70          return cli;
71      }
72  
73      /**
74       * Get the revision length from the parameters
75       *
76       * @param parameters
77       * @return -1 if parameter {@link CommandParameter.SCM_SHORT_REVISION_LENGTH} is absent, <br>
78       *         and otherwise - the length to be applied for the revision formatting
79       * @throws ScmException
80       * @since 1.7
81       */
82      private static int getRevisionLength(final CommandParameters parameters) throws ScmException {
83          if (parameters == null) {
84              return NO_REVISION_LENGTH;
85          } else {
86              return parameters.getInt(CommandParameter.SCM_SHORT_REVISION_LENGTH, NO_REVISION_LENGTH);
87          }
88      }
89  }