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 public class GitInfoCommand extends AbstractCommand implements GitCommand {
46
47 public static final int NO_REVISION_LENGTH = -1;
48
49 @Override
50 protected ScmResult executeCommand(
51 ScmProviderRepository repository, ScmFileSet fileSet, CommandParameters parameters) throws ScmException {
52
53 Commandline baseCli = GitCommandLineUtils.getBaseGitCommandLine(fileSet.getBasedir(), "log");
54 baseCli.createArg().setValue("-1");
55 baseCli.createArg().setValue("--no-merges");
56 baseCli.addArg(GitInfoConsumer.getFormatArgument());
57
58 List<InfoItem> infoItems = new LinkedList<>();
59 if (fileSet.getFileList().isEmpty()) {
60 infoItems.add(executeInfoCommand(baseCli, parameters, fileSet.getBasedir()));
61 } else {
62
63 for (File scmFile : fileSet.getFileList()) {
64 baseCli = GitCommandLineUtils.getBaseGitCommandLine(fileSet.getBasedir(), "log");
65 baseCli.createArg().setValue("-1");
66 baseCli.createArg().setValue("--no-merges");
67 baseCli.addArg(GitInfoConsumer.getFormatArgument());
68
69 baseCli.createArg().setValue("--");
70 GitCommandLineUtils.addTarget(baseCli, Collections.singletonList(scmFile));
71 infoItems.add(executeInfoCommand(baseCli, parameters, scmFile));
72 }
73 }
74 return new InfoScmResult(baseCli.toString(), infoItems);
75 }
76
77 protected InfoItem executeInfoCommand(Commandline cli, CommandParameters parameters, File scmFile)
78 throws ScmException {
79 GitInfoConsumer consumer = new GitInfoConsumer(scmFile.toPath(), getRevisionLength(parameters));
80 CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
81 int exitCode = GitCommandLineUtils.execute(cli, consumer, stderr);
82 if (exitCode != 0) {
83 throw new ScmException("The git log command failed: " + cli.toString() + " returned " + stderr.getOutput());
84 }
85 return consumer.getInfoItem();
86 }
87
88
89
90
91
92
93
94
95
96
97 private static int getRevisionLength(final CommandParameters parameters) throws ScmException {
98 if (parameters == null) {
99 return NO_REVISION_LENGTH;
100 } else {
101 return parameters.getInt(CommandParameter.SCM_SHORT_REVISION_LENGTH, NO_REVISION_LENGTH);
102 }
103 }
104 }