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 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
42
43
44
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");
56 baseCli.createArg().setValue("--no-merges");
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
64 for (File scmFile : fileSet.getFileList()) {
65 baseCli = GitCommandLineUtils.getBaseGitCommandLine(fileSet.getBasedir(), "log");
66 baseCli.createArg().setValue("-1");
67 baseCli.createArg().setValue("--no-merges");
68 baseCli.addArg(GitInfoConsumer.getFormatArgument());
69
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
91
92
93
94
95
96
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 }