Coverage Report - org.apache.maven.scm.provider.jazz.command.checkin.JazzCheckInCommand
 
Classes in this File Line Coverage Branch Coverage Complexity
JazzCheckInCommand
31 %
15/47
16 %
5/30
4,6
 
 1  
 package org.apache.maven.scm.provider.jazz.command.checkin;
 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.ScmFileSet;
 24  
 import org.apache.maven.scm.ScmVersion;
 25  
 import org.apache.maven.scm.command.add.AddScmResult;
 26  
 import org.apache.maven.scm.command.checkin.AbstractCheckInCommand;
 27  
 import org.apache.maven.scm.command.checkin.CheckInScmResult;
 28  
 import org.apache.maven.scm.provider.ScmProviderRepository;
 29  
 import org.apache.maven.scm.provider.jazz.command.JazzConstants;
 30  
 import org.apache.maven.scm.provider.jazz.command.JazzScmCommand;
 31  
 import org.apache.maven.scm.provider.jazz.command.add.JazzAddCommand;
 32  
 import org.apache.maven.scm.provider.jazz.command.consumer.DebugLoggerConsumer;
 33  
 import org.apache.maven.scm.provider.jazz.command.consumer.ErrorConsumer;
 34  
 import org.apache.maven.scm.provider.jazz.repository.JazzScmProviderRepository;
 35  
 import org.codehaus.plexus.util.StringUtils;
 36  
 import org.codehaus.plexus.util.cli.StreamConsumer;
 37  
 
 38  
 import java.io.File;
 39  
 import java.util.Iterator;
 40  
 import java.util.List;
 41  
 
 42  
 // The Maven SCM Plugin "checkin" goal is equivalent to the RTC "checkin" command.
 43  
 //
 44  
 // This implementation of the Maven SCM Plugin "checkin" goal creates a change set with the message provided.
 45  
 // It then uses the Jazz "scm "checkin" command to check the files into a remote workspace.
 46  
 // If there is a flow target defined and the pushChanges flag is true (the default), then the remote workspace
 47  
 // will be delivered ("scm deliver") to the flow target (a stream or other workspace).
 48  
 // 
 49  
 // Set the pushChanges flag to false, if you do not want the repository workspace delivered.
 50  
 //
 51  
 // NOTE: At this point, only a SINGLE flow target is supported. Jazz itself, allows for more than one.
 52  
 //
 53  
 // The differences between this and the "add" goal, are:
 54  
 //      - The add goal will only checkin into the remote repository workspace.
 55  
 //      - The add goal will never deliver.
 56  
 //      - The add goal does not create a change set.
 57  
 //
 58  
 // This is the best we can do to mimic the implementations of the other providers, that provide a working
 59  
 // "add" function (eg "svn add").
 60  
 //
 61  
 // Add may have had been able to use the "scm share" command, but that is recusive and only takes directory
 62  
 // names; we are not able to specify specific or single files.
 63  
 //
 64  
 // See the following links for additional information on the RTC "checkin" command.
 65  
 // RTC 2.0.0.2:
 66  
 // http://publib.boulder.ibm.com/infocenter/rtc/v2r0m0/topic/com.ibm.team.scm.doc/topics/r_scm_cli_checkin.html
 67  
 // RTC 3.0:
 68  
 // http://publib.boulder.ibm.com/infocenter/clmhelp/v3r0/topic/com.ibm.team.scm.doc/topics/r_scm_cli_checkin.html
 69  
 // RTC 3.0.1:
 70  
 // http://publib.boulder.ibm.com/infocenter/clmhelp/v3r0m1/topic/com.ibm.team.scm.doc/topics/r_scm_cli_checkin.html
 71  
 //
 72  
 // See the following links for additional information on the RTC "deliver" command.
 73  
 // RTC 2.0.0.2:
 74  
 // http://publib.boulder.ibm.com/infocenter/rtc/v2r0m0/topic/com.ibm.team.scm.doc/topics/r_scm_cli_deliver.html
 75  
 // RTC 3.0:
 76  
 // http://publib.boulder.ibm.com/infocenter/clmhelp/v3r0/topic/com.ibm.team.scm.doc/topics/r_scm_cli_deliver.html
 77  
 // RTC 3.0.1:
 78  
 // http://publib.boulder.ibm.com/infocenter/clmhelp/v3r0m1/topic/com.ibm.team.scm.doc/topics/r_scm_cli_deliver.html
 79  
 //
 80  
 
 81  
 /**
 82  
  * @author <a href="mailto:ChrisGWarp@gmail.com">Chris Graham</a>
 83  
  */
 84  3
 public class JazzCheckInCommand
 85  
     extends AbstractCheckInCommand
 86  
 {
 87  
 
 88  
     /**
 89  
      * {@inheritDoc}
 90  
      */
 91  
     protected CheckInScmResult executeCheckInCommand( ScmProviderRepository repository, ScmFileSet fileSet,
 92  
                                                       String message, ScmVersion scmVersion )
 93  
         throws ScmException
 94  
     {
 95  0
         if ( scmVersion != null && StringUtils.isNotEmpty( scmVersion.getName() ) )
 96  
         {
 97  0
             throw new ScmException( "This provider command can't handle tags." );
 98  
         }
 99  
 
 100  0
         if ( getLogger().isDebugEnabled() )
 101  
         {
 102  0
             getLogger().debug( "Executing checkin command..." );
 103  
         }
 104  
 
 105  
         // Create a changeset. We need to do this, as otherwise the information contained in the message
 106  
         // will be lost forever.
 107  0
         JazzScmCommand createChangesetCmd = createCreateChangesetCommand( repository, fileSet, message );
 108  0
         DebugLoggerConsumer outputConsumer = new DebugLoggerConsumer( getLogger() );
 109  0
         ErrorConsumer errConsumer = new ErrorConsumer( getLogger() );
 110  
 
 111  0
         int status = createChangesetCmd.execute( outputConsumer, errConsumer );
 112  0
         if ( status != 0 || errConsumer.hasBeenFed() )
 113  
         {
 114  0
             return new CheckInScmResult( createChangesetCmd.getCommandString(),
 115  
                                          "Error code for Jazz SCM create changeset command - " + status,
 116  
                                          errConsumer.getOutput(), false );
 117  
         }
 118  
 
 119  
         // Now check in the files themselves.
 120  0
         return executeCheckInCommand( repository, fileSet, scmVersion );
 121  
     }
 122  
 
 123  
     protected CheckInScmResult executeCheckInCommand( ScmProviderRepository repo, ScmFileSet fileSet,
 124  
                                                       ScmVersion scmVersion )
 125  
         throws ScmException
 126  
     {
 127  
         // Call the Add command to perform the checkin into the repository workspace.
 128  0
         JazzAddCommand addCommand = new JazzAddCommand();
 129  0
         addCommand.setLogger( getLogger() );
 130  0
         AddScmResult addResult = addCommand.executeAddCommand( repo, fileSet );
 131  
 
 132  
         // Now, if it has a flow target, deliver it.
 133  0
         JazzScmProviderRepository jazzRepo = (JazzScmProviderRepository) repo;
 134  0
         if ( jazzRepo.isPushChangesAndHaveFlowTargets() )
 135  
         {
 136  
             // Push if we need too
 137  0
             JazzScmCommand deliverCmd = createDeliverCommand( (JazzScmProviderRepository) repo, fileSet );
 138  0
             StreamConsumer deliverConsumer =
 139  
                 new DebugLoggerConsumer( getLogger() );      // No need for a dedicated consumer for this
 140  0
             ErrorConsumer errConsumer = new ErrorConsumer( getLogger() );
 141  
 
 142  0
             int status = deliverCmd.execute( deliverConsumer, errConsumer );
 143  0
             if ( status != 0 || errConsumer.hasBeenFed() )
 144  
             {
 145  0
                 return new CheckInScmResult( deliverCmd.getCommandString(),
 146  
                                              "Error code for Jazz SCM deliver command - " + status,
 147  
                                              errConsumer.getOutput(), false );
 148  
             }
 149  
         }
 150  
 
 151  
         // Return what was added.
 152  0
         return new CheckInScmResult( addResult.getCommandLine(), addResult.getAddedFiles() );
 153  
     }
 154  
 
 155  
     public JazzScmCommand createCreateChangesetCommand( ScmProviderRepository repo, ScmFileSet fileSet, String message )
 156  
     {
 157  1
         JazzScmCommand command =
 158  
             new JazzScmCommand( JazzConstants.CMD_CREATE, JazzConstants.CMD_SUB_CHANGESET, repo, false, fileSet,
 159  
                                 getLogger() );
 160  1
         command.addArgument( message );
 161  
 
 162  1
         return command;
 163  
     }
 164  
 
 165  
     public JazzScmCommand createCheckInCommand( ScmProviderRepository repo, ScmFileSet fileSet )
 166  
     {
 167  2
         JazzScmCommand command =
 168  
             new JazzScmCommand( JazzConstants.CMD_CHECKIN, null, repo, false, fileSet, getLogger() );
 169  
 
 170  
         // TODO, this was taken out to quickly test how the release plugin works.
 171  
         // The release plugin has the fileSet.getbaseDir() as the project it is checking in
 172  
         // This happens to be a folder under the sandbox root, and so the checkin would fail because it needs
 173  
         // to check in at the sandbox root level (not sub folders)
 174  
         // The SCM Plugin has a basedir parameter that you can pass it, so everythig works ok from the scm-plugin alone
 175  
         // but the release-plugin doesn't look like it lets you do that. (or I didn't have enough time
 176  
         // to figure out how to do it properly).
 177  
 
 178  
         // if (fileSet != null) {
 179  
         // command.addArgument(JazzConstants.LOAD_ROOT_DIRECTORY_ARG);
 180  
         // command.addArgument(fileSet.getBasedir().getAbsolutePath());
 181  
         // }
 182  
 
 183  2
         List<File> files = fileSet.getFileList();
 184  2
         if ( files != null && !files.isEmpty() )
 185  
         {
 186  1
             Iterator<File> it = files.iterator();
 187  3
             while ( it.hasNext() )
 188  
             {
 189  2
                 File file = (File) it.next();
 190  2
                 command.addArgument( file.getPath() ); // Check in only the files specified
 191  2
             }
 192  1
         }
 193  
         else
 194  
         {
 195  1
             command.addArgument( "." ); // This will check in all local changes
 196  
         }
 197  
 
 198  2
         return command;
 199  
     }
 200  
 
 201  
     // Create the JazzScmCommand to execute the "scm deliver ..." command
 202  
     // This will deliver the changes to the flow target (stream or other workspace).
 203  
     public JazzScmCommand createDeliverCommand( JazzScmProviderRepository repo, ScmFileSet fileSet )
 204  
     {
 205  0
         JazzScmCommand command = new JazzScmCommand( JazzConstants.CMD_DELIVER, repo, fileSet, getLogger() );
 206  
 
 207  0
         if ( repo.getWorkspace() != null && !repo.getWorkspace().equals( "" ) )
 208  
         {
 209  0
             command.addArgument( JazzConstants.ARG_DELIVER_SOURCE );
 210  0
             command.addArgument( repo.getWorkspace() );
 211  
         }
 212  
 
 213  0
         if ( repo.getFlowTarget() != null && !repo.getFlowTarget().equals( "" ) )
 214  
         {
 215  0
             command.addArgument( JazzConstants.ARG_DELIVER_TARGET );
 216  0
             command.addArgument( repo.getFlowTarget() );
 217  
         }
 218  
 
 219  
         // This command is needed so that the deliver operation will work.
 220  
         // Files that are not under source control (a--) [temp files etc]
 221  
         // will cause the deliver operation to fail with the error:
 222  
         // "Cannot deliver because there are one or more items that are not checked in.
 223  
         // Check in the changes or rerun with --overwrite-uncommitted."
 224  
         // However, from the maven perspective, we only need files that are
 225  
         // under source control to be delivered. Maven has already checked
 226  
         // for this (via the status command). 
 227  
         //
 228  
         // So we add this argument to allow the deliver to work.
 229  0
         command.addArgument( JazzConstants.ARG_OVERWRITE_UNCOMMITTED );
 230  
 
 231  0
         return command;
 232  
     }
 233  
 }