1 package org.apache.maven.scm.provider.git.gitexe.command.add;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.commons.io.FilenameUtils;
23 import org.apache.maven.scm.ScmException;
24 import org.apache.maven.scm.ScmFile;
25 import org.apache.maven.scm.ScmFileSet;
26 import org.apache.maven.scm.ScmResult;
27 import org.apache.maven.scm.command.add.AbstractAddCommand;
28 import org.apache.maven.scm.command.add.AddScmResult;
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.gitexe.command.status.GitStatusCommand;
33 import org.apache.maven.scm.provider.git.gitexe.command.status.GitStatusConsumer;
34 import org.apache.maven.scm.provider.git.repository.GitScmProviderRepository;
35 import org.codehaus.plexus.util.Os;
36 import org.codehaus.plexus.util.cli.CommandLineUtils;
37 import org.codehaus.plexus.util.cli.Commandline;
38
39 import java.io.File;
40 import java.net.URI;
41 import java.util.ArrayList;
42 import java.util.Collections;
43 import java.util.List;
44
45
46
47
48 public class GitAddCommand
49 extends AbstractAddCommand
50 implements GitCommand
51 {
52
53
54
55 protected ScmResult executeAddCommand( ScmProviderRepository repo, ScmFileSet fileSet, String message,
56 boolean binary )
57 throws ScmException
58 {
59 GitScmProviderRepository repository = (GitScmProviderRepository) repo;
60
61 if ( fileSet.getFileList().isEmpty() )
62 {
63 throw new ScmException( "You must provide at least one file/directory to add" );
64 }
65
66 AddScmResult result = executeAddFileSet( fileSet );
67
68 if ( result != null )
69 {
70 return result;
71 }
72
73
74 URI relativeRepositoryPath = GitStatusCommand.getRelativeCWD( this, fileSet );
75
76 int exitCode;
77 CommandLineUtils.StringStreamConsumer stderr;
78
79
80
81
82 Commandline clStatus = GitStatusCommand.createCommandLine( repository, fileSet );
83
84 GitStatusConsumer statusConsumer =
85 new GitStatusConsumer( getLogger(), fileSet.getBasedir(), relativeRepositoryPath );
86 stderr = new CommandLineUtils.StringStreamConsumer();
87 exitCode = GitCommandLineUtils.execute( clStatus, statusConsumer, stderr, getLogger() );
88 if ( exitCode != 0 )
89 {
90
91 if ( getLogger().isInfoEnabled() )
92 {
93 getLogger().info( "nothing added to commit but untracked files present (use \"git add\" to track)" );
94 }
95 }
96
97 List<ScmFile> changedFiles = new ArrayList<ScmFile>();
98
99
100 for ( ScmFile scmfile : statusConsumer.getChangedFiles() )
101 {
102
103 for ( File f : fileSet.getFileList() )
104 {
105 if ( FilenameUtils.separatorsToUnix( f.getPath() ).equals( scmfile.getPath() ) )
106 {
107 changedFiles.add( scmfile );
108 }
109 }
110 }
111
112 Commandline cl = createCommandLine( fileSet.getBasedir(), fileSet.getFileList() );
113 return new AddScmResult( cl.toString(), changedFiles );
114 }
115
116 public static Commandline createCommandLine( File workingDirectory, List<File> files )
117 throws ScmException
118 {
119 Commandline cl = GitCommandLineUtils.getBaseGitCommandLine( workingDirectory, "add" );
120
121
122 cl.createArg().setValue( "--" );
123
124 GitCommandLineUtils.addTarget( cl, files );
125
126 return cl;
127 }
128
129 private AddScmResult executeAddFileSet( ScmFileSet fileSet )
130 throws ScmException
131 {
132 File workingDirectory = fileSet.getBasedir();
133 List<File> files = fileSet.getFileList();
134
135
136 if ( Os.isFamily( Os.FAMILY_WINDOWS ) )
137 {
138 for ( File file : files )
139 {
140 AddScmResult result = executeAddFiles( workingDirectory, Collections.singletonList( file ) );
141
142 if ( result != null )
143 {
144 return result;
145 }
146 }
147 }
148 else
149 {
150 AddScmResult result = executeAddFiles( workingDirectory, files );
151
152 if ( result != null )
153 {
154 return result;
155 }
156 }
157
158 return null;
159 }
160
161 private AddScmResult executeAddFiles( File workingDirectory, List<File> files )
162 throws ScmException
163 {
164 Commandline cl = createCommandLine( workingDirectory, files );
165
166 CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
167 CommandLineUtils.StringStreamConsumer stdout = new CommandLineUtils.StringStreamConsumer();
168
169 int exitCode = GitCommandLineUtils.execute( cl, stdout, stderr, getLogger() );
170
171 if ( exitCode != 0 )
172 {
173 return new AddScmResult( cl.toString(), "The git-add command failed.", stderr.getOutput(), false );
174 }
175
176 return null;
177 }
178 }