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