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.status;
20
21 import java.net.URI;
22
23 import org.apache.maven.scm.ScmException;
24 import org.apache.maven.scm.ScmFileSet;
25 import org.apache.maven.scm.command.status.AbstractStatusCommand;
26 import org.apache.maven.scm.command.status.StatusScmResult;
27 import org.apache.maven.scm.provider.ScmProviderRepository;
28 import org.apache.maven.scm.provider.git.command.GitCommand;
29 import org.apache.maven.scm.provider.git.gitexe.command.GitCommandLineUtils;
30 import org.apache.maven.scm.provider.git.repository.GitScmProviderRepository;
31 import org.codehaus.plexus.util.cli.CommandLineUtils;
32 import org.codehaus.plexus.util.cli.Commandline;
33 import org.slf4j.Logger;
34
35
36
37
38 public class GitStatusCommand extends AbstractStatusCommand implements GitCommand {
39
40
41
42 protected StatusScmResult executeStatusCommand(ScmProviderRepository repo, ScmFileSet fileSet) throws ScmException {
43 int exitCode;
44 CommandLineUtils.StringStreamConsumer stderr;
45
46 URI relativeRepositoryPath = getRelativeCWD(logger, fileSet);
47
48 Commandline cl = createCommandLine((GitScmProviderRepository) repo, fileSet);
49
50 GitStatusConsumer consumer = new GitStatusConsumer(fileSet.getBasedir(), relativeRepositoryPath, fileSet);
51
52 stderr = new CommandLineUtils.StringStreamConsumer();
53
54 exitCode = GitCommandLineUtils.execute(cl, consumer, stderr);
55 if (exitCode != 0) {
56
57 if (logger.isInfoEnabled()) {
58 logger.info("nothing added to commit but untracked files present (use \"git add\" to track)");
59 }
60 }
61
62 return new StatusScmResult(cl.toString(), consumer.getChangedFiles());
63 }
64
65
66
67
68
69
70
71
72
73
74
75
76
77 public static URI getRelativeCWD(Logger logger, ScmFileSet fileSet) throws ScmException {
78 Commandline clRevparse = createRevparseShowPrefix(fileSet);
79
80 CommandLineUtils.StringStreamConsumer stdout = new CommandLineUtils.StringStreamConsumer();
81 CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
82
83 URI relativeRepositoryPath = null;
84
85 int exitCode = GitCommandLineUtils.execute(clRevparse, stdout, stderr);
86 if (exitCode != 0) {
87
88 if (logger.isInfoEnabled()) {
89 logger.info("Could not resolve prefix");
90 }
91 } else {
92 relativeRepositoryPath =
93 GitStatusConsumer.uriFromPath(stdout.getOutput().trim());
94 }
95 return relativeRepositoryPath;
96 }
97
98 public static Commandline createCommandLine(GitScmProviderRepository repository, ScmFileSet fileSet) {
99 Commandline cl = GitCommandLineUtils.getBaseGitCommandLine(fileSet.getBasedir(), "status");
100 cl.addArguments(new String[] {"--porcelain", "."});
101 return cl;
102 }
103
104 public static Commandline createRevparseShowPrefix(ScmFileSet fileSet) {
105 Commandline cl = GitCommandLineUtils.getBaseGitCommandLine(fileSet.getBasedir(), "rev-parse");
106 cl.addArguments(new String[] {"--show-prefix"});
107 return cl;
108 }
109 }