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