1 package org.apache.maven.scm.provider.git.gitexe.command.info;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
37
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 > NO_REVISION_LENGTH )
73
74 {
75 cli.createArg().setValue( "--short=" + revLength );
76 }
77 cli.createArg().setValue( "HEAD" );
78
79 return cli;
80 }
81
82
83
84
85
86
87
88
89
90
91 private static int getRevisionLength( final CommandParameters parameters )
92 throws ScmException
93 {
94 if ( parameters == null )
95 {
96 return NO_REVISION_LENGTH;
97 }
98 else
99 {
100 return parameters.getInt( CommandParameter.SCM_SHORT_REVISION_LENGTH, NO_REVISION_LENGTH );
101 }
102 }
103
104
105 }