001    package org.apache.maven.scm.provider.jazz.command.add;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one
005     * or more contributor license agreements.  See the NOTICE file
006     * distributed with this work for additional information
007     * regarding copyright ownership.  The ASF licenses this file
008     * to you under the Apache License, Version 2.0 (the
009     * "License"); you may not use this file except in compliance
010     * with the License.  You may obtain a copy of the License at
011     *
012     * http://www.apache.org/licenses/LICENSE-2.0
013     *
014     * Unless required by applicable law or agreed to in writing,
015     * software distributed under the License is distributed on an
016     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017     * KIND, either express or implied.  See the License for the
018     * specific language governing permissions and limitations
019     * under the License.
020     */
021    
022    import org.apache.maven.scm.ScmException;
023    import org.apache.maven.scm.ScmFile;
024    import org.apache.maven.scm.ScmFileSet;
025    import org.apache.maven.scm.ScmFileStatus;
026    import org.apache.maven.scm.ScmResult;
027    import org.apache.maven.scm.command.add.AbstractAddCommand;
028    import org.apache.maven.scm.command.add.AddScmResult;
029    import org.apache.maven.scm.command.status.StatusScmResult;
030    import org.apache.maven.scm.provider.ScmProviderRepository;
031    import org.apache.maven.scm.provider.jazz.command.JazzConstants;
032    import org.apache.maven.scm.provider.jazz.command.JazzScmCommand;
033    import org.apache.maven.scm.provider.jazz.command.consumer.ErrorConsumer;
034    import org.apache.maven.scm.provider.jazz.command.status.JazzStatusCommand;
035    
036    import java.io.File;
037    import java.util.ArrayList;
038    import java.util.List;
039    
040    // RTC does not have the equivalent of the "add" goal. The closest we have is the "share" command, however
041    // that only shares directories (not individual or specific files), and it is recursive (which can not be
042    // switched off!).
043    //
044    // The Maven SCM plugin "add" goal's job is to add files to source control, but not commit them.
045    // The SVN equivalent of this is "svn add", which places a file under source control (Working Copy in svn terms)
046    // but does not commit them (svn commit).
047    // So, this provider will use the RTC "checkin" command for the implementation of the "add" goal.
048    // This will checkin the code into a remote repository workspace. It will not deliver.
049    //
050    // Additionally, "svn add" does not take a message, whereas commit does. Under RTC, the only way we can preserve
051    // the message, is to create a changeset. So we do that in the "checkin" goal, not the "add" goal.
052    // 
053    // The Maven SCM plugin "add" goal is roughly equivalent to the RTC "checkin" command.
054    //
055    // See the following links for additional information on the RTC "checkin" command.
056    // RTC 2.0.0.2:
057    // http://publib.boulder.ibm.com/infocenter/rtc/v2r0m0/topic/com.ibm.team.scm.doc/topics/r_scm_cli_checkin.html
058    // RTC 3.0:
059    // http://publib.boulder.ibm.com/infocenter/clmhelp/v3r0/topic/com.ibm.team.scm.doc/topics/r_scm_cli_checkin.html
060    // RTC 3.0.1:
061    // http://publib.boulder.ibm.com/infocenter/clmhelp/v3r0m1/topic/com.ibm.team.scm.doc/topics/r_scm_cli_checkin.html
062    //
063    // Currently this implementation does not use the comment message.
064    // Perhaps in the future this method can be used to deliver a change-set with the given comment message.
065    // However some users may want the checkin goal to only check in to the desired repository workspace.
066    // While some users may want the checkin goal to both check in to the desired repository workspace and deliver it to a
067    // stream.
068    // Currently this implementation only checks in the unresolved changes to the repository workspace.
069    //
070    // See the following links for additional information on the RTC "deliver" command.
071    // RTC 2.0.0.2:
072    // http://publib.boulder.ibm.com/infocenter/rtc/v2r0m0/topic/com.ibm.team.scm.doc/topics/r_scm_cli_deliver.html
073    // RTC 3.0:
074    // http://publib.boulder.ibm.com/infocenter/clmhelp/v3r0/topic/com.ibm.team.scm.doc/topics/r_scm_cli_deliver.html
075    // RTC 3.0.1:
076    // http://publib.boulder.ibm.com/infocenter/clmhelp/v3r0m1/topic/com.ibm.team.scm.doc/topics/r_scm_cli_deliver.html
077    //
078    
079    /**
080     * @author <a href="mailto:ChrisGWarp@gmail.com">Chris Graham</a>
081     */
082    public class JazzAddCommand
083        extends AbstractAddCommand
084    {
085        /**
086         * {@inheritDoc}
087         */
088        protected ScmResult executeAddCommand( ScmProviderRepository repo, ScmFileSet fileSet, String message,
089                                               boolean binary )
090            throws ScmException
091        {
092            if ( getLogger().isDebugEnabled() )
093            {
094                getLogger().debug( "Executing add command..." );
095            }
096    
097            // The message can only be used when we create a change set, which is only done on the checkin command.
098            // So that can be ignored.
099            // 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    }