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