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