1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
36
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)
64
65 {
66 cli.createArg().setValue("--short=" + revLength);
67 }
68 cli.createArg().setValue("HEAD");
69
70 return cli;
71 }
72
73
74
75
76
77
78
79
80
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 }