View Javadoc
1   package org.apache.maven.scm.provider.jazz.command.add;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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  // RTC does not have the equivalent of the "add" goal. The closest we have is the "share" command, however
41  // that only shares directories (not individual or specific files), and it is recursive (which can not be
42  // switched off!).
43  //
44  // The Maven SCM plugin "add" goal's job is to add files to source control, but not commit them.
45  // The SVN equivalent of this is "svn add", which places a file under source control (Working Copy in svn terms)
46  // but does not commit them (svn commit).
47  // So, this provider will use the RTC "checkin" command for the implementation of the "add" goal.
48  // This will checkin the code into a remote repository workspace. It will not deliver.
49  //
50  // Additionally, "svn add" does not take a message, whereas commit does. Under RTC, the only way we can preserve
51  // the message, is to create a changeset. So we do that in the "checkin" goal, not the "add" goal.
52  // 
53  // The Maven SCM plugin "add" goal is roughly equivalent to the RTC "checkin" command.
54  //
55  // See the following links for additional information on the RTC "checkin" command.
56  // RTC 2.0.0.2:
57  // http://publib.boulder.ibm.com/infocenter/rtc/v2r0m0/topic/com.ibm.team.scm.doc/topics/r_scm_cli_checkin.html
58  // RTC 3.0:
59  // http://publib.boulder.ibm.com/infocenter/clmhelp/v3r0/topic/com.ibm.team.scm.doc/topics/r_scm_cli_checkin.html
60  // RTC 3.0.1:
61  // http://publib.boulder.ibm.com/infocenter/clmhelp/v3r0m1/topic/com.ibm.team.scm.doc/topics/r_scm_cli_checkin.html
62  //
63  // Currently this implementation does not use the comment message.
64  // Perhaps in the future this method can be used to deliver a change-set with the given comment message.
65  // However some users may want the checkin goal to only check in to the desired repository workspace.
66  // While some users may want the checkin goal to both check in to the desired repository workspace and deliver it to a
67  // stream.
68  // Currently this implementation only checks in the unresolved changes to the repository workspace.
69  //
70  // See the following links for additional information on the RTC "deliver" command.
71  // RTC 2.0.0.2:
72  // http://publib.boulder.ibm.com/infocenter/rtc/v2r0m0/topic/com.ibm.team.scm.doc/topics/r_scm_cli_deliver.html
73  // RTC 3.0:
74  // http://publib.boulder.ibm.com/infocenter/clmhelp/v3r0/topic/com.ibm.team.scm.doc/topics/r_scm_cli_deliver.html
75  // RTC 3.0.1:
76  // http://publib.boulder.ibm.com/infocenter/clmhelp/v3r0m1/topic/com.ibm.team.scm.doc/topics/r_scm_cli_deliver.html
77  //
78  
79  /**
80   * @author <a href="mailto:ChrisGWarp@gmail.com">Chris Graham</a>
81   */
82  public class JazzAddCommand
83      extends AbstractAddCommand
84  {
85      /**
86       * {@inheritDoc}
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          // The message can only be used when we create a change set, which is only done on the checkin command.
98          // So that can be ignored.
99          // The binary flag, is not needed for RTC.
100         return executeAddCommand( repo, fileSet );
101     }
102 
103     public AddScmResult executeAddCommand( ScmProviderRepository repo, ScmFileSet fileSet )
104         throws ScmException
105     {
106         // NOTE: THIS IS ALSO CALLED DIRECTLY FROM THE CHECKIN COMMAND.
107         //
108         // The "checkin" command does not produce consumable output as to which individual files were checked in. (in
109         // 2.0.0.2 at least). Since only "locally modified" changes get checked in, we call a "status" command to
110         // generate a list of these files.
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             // Either commit all local changes
139             commitedFiles = changedScmFiles;
140         }
141         else
142         {
143             // Or commit specific files
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         // Now that we have a list of files to process, we can "add" (scm checkin) them.
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() ); // Check in only the files specified
180             }
181         }
182         else
183         {
184             command.addArgument( "." ); // This will check in all local changes
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 }