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 || file.getStatus() == ScmFileStatus.DELETED
127 || file.getStatus() == ScmFileStatus.MODIFIED )
128 {
129 changedScmFiles.add( new ScmFile( file.getPath(), ScmFileStatus.CHECKED_IN ) );
130 changedFiles.add( new File( parentFolder, file.getPath() ) );
131 }
132 }
133
134 List<File> files = fileSet.getFileList();
135 if ( files.size() == 0 )
136 {
137
138 commitedFiles = changedScmFiles;
139 }
140 else
141 {
142
143 for ( File file : files )
144 {
145 if ( fileExistsInFileList( file, changedFiles ) )
146 {
147 commitedFiles.add( new ScmFile( file.getPath(), ScmFileStatus.CHECKED_IN ) );
148 }
149 }
150 }
151
152
153 JazzAddConsumer addConsumer = new JazzAddConsumer( repo, getLogger() );
154 ErrorConsumer errConsumer = new ErrorConsumer( getLogger() );
155 JazzScmCommand command = createAddCommand( repo, fileSet );
156
157 int status = command.execute( addConsumer, errConsumer );
158 if ( status != 0 || errConsumer.hasBeenFed() )
159 {
160 return new AddScmResult( command.getCommandString(),
161 "Error code for Jazz SCM add (checkin) command - " + status,
162 errConsumer.getOutput(), false );
163 }
164
165 return new AddScmResult( command.getCommandString(), addConsumer.getFiles() );
166 }
167
168 public JazzScmCommand createAddCommand( ScmProviderRepository repo, ScmFileSet fileSet )
169 {
170 JazzScmCommand command =
171 new JazzScmCommand( JazzConstants.CMD_CHECKIN, null, repo, false, fileSet, getLogger() );
172
173 List<File> files = fileSet.getFileList();
174 if ( files != null && !files.isEmpty() )
175 {
176 for ( File file : files )
177 {
178 command.addArgument( file.getPath() );
179 }
180 }
181 else
182 {
183 command.addArgument( "." );
184 }
185
186 return command;
187 }
188
189 private boolean fileExistsInFileList( File file, List<File> fileList )
190 {
191 boolean exists = false;
192 for ( File changedFile : fileList )
193 {
194 if ( changedFile.compareTo( file ) == 0 )
195 {
196 exists = true;
197 break;
198 }
199 }
200 return exists;
201 }
202
203 }