1 package org.apache.maven.scm.provider.jazz.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.maven.scm.ScmException;
23 import org.apache.maven.scm.ScmFile;
24 import org.apache.maven.scm.ScmFileSet;
25 import org.apache.maven.scm.ScmFileStatus;
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.command.status.StatusScmResult;
30 import org.apache.maven.scm.provider.ScmProviderRepository;
31 import org.apache.maven.scm.provider.jazz.command.JazzConstants;
32 import org.apache.maven.scm.provider.jazz.command.JazzScmCommand;
33 import org.apache.maven.scm.provider.jazz.command.consumer.ErrorConsumer;
34 import org.apache.maven.scm.provider.jazz.command.status.JazzStatusCommand;
35
36 import java.io.File;
37 import java.util.ArrayList;
38 import java.util.List;
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82 public class JazzAddCommand
83 extends AbstractAddCommand
84 {
85
86
87
88 protected ScmResult executeAddCommand( ScmProviderRepository repo, ScmFileSet fileSet, String message,
89 boolean binary )
90 throws ScmException
91 {
92 if ( getLogger().isDebugEnabled() )
93 {
94 getLogger().debug( "Executing add command..." );
95 }
96
97
98
99
100 return executeAddCommand( repo, fileSet );
101 }
102
103 public AddScmResult executeAddCommand( ScmProviderRepository repo, ScmFileSet fileSet )
104 throws ScmException
105 {
106
107
108
109
110
111 File baseDir = fileSet.getBasedir();
112 File parentFolder = ( baseDir.getParentFile() != null ) ? baseDir.getParentFile() : baseDir;
113
114 List<ScmFile> changedScmFiles = new ArrayList<ScmFile>();
115 List<File> changedFiles = new ArrayList<File>();
116 List<ScmFile> commitedFiles = new ArrayList<ScmFile>();
117
118 JazzStatusCommand statusCmd = new JazzStatusCommand();
119 statusCmd.setLogger( getLogger() );
120 StatusScmResult statusCmdResult = statusCmd.executeStatusCommand( repo, fileSet );
121 List<ScmFile> statusScmFiles = statusCmdResult.getChangedFiles();
122
123 for ( ScmFile file : statusScmFiles )
124 {
125 getLogger().debug( "Iterating over statusScmFiles: " + file );
126 if ( file.getStatus() == ScmFileStatus.ADDED ||
127 file.getStatus() == ScmFileStatus.DELETED ||
128 file.getStatus() == ScmFileStatus.MODIFIED )
129 {
130 changedScmFiles.add( new ScmFile( file.getPath(), ScmFileStatus.CHECKED_IN ) );
131 changedFiles.add( new File( parentFolder, file.getPath() ) );
132 }
133 }
134
135 List<File> files = fileSet.getFileList();
136 if ( files.size() == 0 )
137 {
138
139 commitedFiles = changedScmFiles;
140 }
141 else
142 {
143
144 for ( File file : files )
145 {
146 if ( fileExistsInFileList( file, changedFiles ) )
147 {
148 commitedFiles.add( new ScmFile( file.getPath(), ScmFileStatus.CHECKED_IN ) );
149 }
150 }
151 }
152
153
154 JazzAddConsumer addConsumer = new JazzAddConsumer( repo, getLogger() );
155 ErrorConsumer errConsumer = new ErrorConsumer( getLogger() );
156 JazzScmCommand command = createAddCommand( repo, fileSet );
157
158 int status = command.execute( addConsumer, errConsumer );
159 if ( status != 0 || errConsumer.hasBeenFed() )
160 {
161 return new AddScmResult( command.getCommandString(),
162 "Error code for Jazz SCM add (checkin) command - " + status,
163 errConsumer.getOutput(), false );
164 }
165
166 return new AddScmResult( command.getCommandString(), addConsumer.getFiles() );
167 }
168
169 public JazzScmCommand createAddCommand( ScmProviderRepository repo, ScmFileSet fileSet )
170 {
171 JazzScmCommand command =
172 new JazzScmCommand( JazzConstants.CMD_CHECKIN, null, repo, false, fileSet, getLogger() );
173
174 List<File> files = fileSet.getFileList();
175 if ( files != null && !files.isEmpty() )
176 {
177 for( File file : files )
178 {
179 command.addArgument( file.getPath() );
180 }
181 }
182 else
183 {
184 command.addArgument( "." );
185 }
186
187 return command;
188 }
189
190 private boolean fileExistsInFileList( File file, List<File> fileList )
191 {
192 boolean exists = false;
193 for ( File changedFile : fileList )
194 {
195 if ( changedFile.compareTo( file ) == 0 )
196 {
197 exists = true;
198 break;
199 }
200 }
201 return exists;
202 }
203
204 }