Coverage Report - org.apache.maven.scm.provider.perforce.command.update.PerforceUpdateCommand
 
Classes in this File Line Coverage Branch Coverage Complexity
PerforceUpdateCommand
0 %
0/40
0 %
0/10
3,667
 
 1  
 package org.apache.maven.scm.provider.perforce.command.update;
 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.CommandParameter;
 23  
 import org.apache.maven.scm.CommandParameters;
 24  
 import org.apache.maven.scm.ScmException;
 25  
 import org.apache.maven.scm.ScmFileSet;
 26  
 import org.apache.maven.scm.ScmVersion;
 27  
 import org.apache.maven.scm.command.changelog.ChangeLogCommand;
 28  
 import org.apache.maven.scm.command.checkout.CheckOutScmResult;
 29  
 import org.apache.maven.scm.command.update.AbstractUpdateCommand;
 30  
 import org.apache.maven.scm.command.update.UpdateScmResult;
 31  
 import org.apache.maven.scm.command.update.UpdateScmResultWithRevision;
 32  
 import org.apache.maven.scm.provider.ScmProviderRepository;
 33  
 import org.apache.maven.scm.provider.perforce.PerforceScmProvider;
 34  
 import org.apache.maven.scm.provider.perforce.repository.PerforceScmProviderRepository;
 35  
 import org.apache.maven.scm.provider.perforce.command.PerforceCommand;
 36  
 import org.apache.maven.scm.provider.perforce.command.changelog.PerforceChangeLogCommand;
 37  
 import org.apache.maven.scm.provider.perforce.command.checkout.PerforceCheckOutCommand;
 38  
 
 39  
 import org.codehaus.plexus.util.cli.CommandLineException;
 40  
 import org.codehaus.plexus.util.cli.CommandLineUtils;
 41  
 import org.codehaus.plexus.util.cli.Commandline;
 42  
 
 43  
 import java.io.File;
 44  
 
 45  
 /**
 46  
  * @author Mike Perham
 47  
  * @version $Id: PerforceUpdateCommand.java 1306867 2012-03-29 13:45:10Z olamy $
 48  
  */
 49  0
 public class PerforceUpdateCommand
 50  
     extends AbstractUpdateCommand
 51  
     implements PerforceCommand
 52  
 {
 53  
     /** {@inheritDoc} */
 54  
     protected UpdateScmResult executeUpdateCommand( ScmProviderRepository repo, ScmFileSet files,
 55  
                                                     ScmVersion scmVersion )
 56  
         throws ScmException
 57  
     {
 58  
         // In Perforce, there is no difference between update and checkout.
 59  
         // Here we just run the checkout command and map the result onto an
 60  
         // UpdateScmResult.
 61  0
         PerforceCheckOutCommand command = new PerforceCheckOutCommand();
 62  0
         command.setLogger( getLogger() );
 63  0
         CommandParameters params = new CommandParameters();
 64  0
         params.setScmVersion( CommandParameter.SCM_VERSION, scmVersion );
 65  
 
 66  0
         CheckOutScmResult cosr = (CheckOutScmResult) command.execute( repo, files, params );
 67  0
         if ( !cosr.isSuccess() )
 68  
         {
 69  0
             return new UpdateScmResult( cosr.getCommandLine(), cosr.getProviderMessage(), cosr.getCommandOutput(),
 70  
                                         false );
 71  
         }
 72  
 
 73  0
         PerforceScmProviderRepository p4repo = (PerforceScmProviderRepository) repo;
 74  0
         String clientspec = PerforceScmProvider.getClientspecName( getLogger(), p4repo, files.getBasedir() );
 75  0
         Commandline cl = createCommandLine( p4repo, files.getBasedir(), clientspec );
 76  
 
 77  
         @SuppressWarnings( "unused" )
 78  0
         String location = PerforceScmProvider.getRepoPath( getLogger(), p4repo, files.getBasedir() );
 79  0
         PerforceHaveConsumer consumer =
 80  
             new PerforceHaveConsumer( getLogger() );
 81  
 
 82  
         try
 83  
         {
 84  0
             if ( getLogger().isDebugEnabled() )
 85  
             {
 86  0
                 getLogger().debug( PerforceScmProvider.clean( "Executing " + cl.toString() ) );
 87  
             }
 88  
 
 89  0
             CommandLineUtils.StringStreamConsumer err = new CommandLineUtils.StringStreamConsumer();
 90  0
             int exitCode = CommandLineUtils.executeCommandLine( cl, consumer, err );
 91  
 
 92  0
             if ( exitCode != 0 )
 93  
             {
 94  0
                 String cmdLine = CommandLineUtils.toString( cl.getCommandline() );
 95  
 
 96  0
                 StringBuilder msg = new StringBuilder( "Exit code: " + exitCode + " - " + err.getOutput() );
 97  0
                 msg.append( '\n' );
 98  0
                 msg.append( "Command line was:" + cmdLine );
 99  
 
 100  0
                 throw new CommandLineException( msg.toString() );
 101  
             }
 102  
         }
 103  0
         catch ( CommandLineException e )
 104  
         {
 105  0
             if ( getLogger().isErrorEnabled() )
 106  
             {
 107  0
                 getLogger().error( "CommandLineException " + e.getMessage(), e );
 108  
             }
 109  0
         }
 110  
 
 111  0
         return new UpdateScmResultWithRevision( cosr.getCommandLine(), cosr.getCheckedOutFiles(),
 112  
                                                 String.valueOf( consumer.getHave() ) );
 113  
     }
 114  
 
 115  
     /** {@inheritDoc} */
 116  
     protected ChangeLogCommand getChangeLogCommand()
 117  
     {
 118  0
         PerforceChangeLogCommand command = new PerforceChangeLogCommand();
 119  0
         command.setLogger( getLogger() );
 120  0
         return command;
 121  
     }
 122  
 
 123  
     public static Commandline createCommandLine( PerforceScmProviderRepository repo, File workingDirectory,
 124  
                                                  String clientspec )
 125  
     {
 126  0
         Commandline command = PerforceScmProvider.createP4Command( repo, workingDirectory );
 127  
 
 128  0
         if ( clientspec != null )
 129  
         {
 130  0
             command.createArg().setValue( "-c" );
 131  0
             command.createArg().setValue( clientspec );
 132  
         }
 133  0
         command.createArg().setValue( "changes" );
 134  0
         command.createArg().setValue( "-m1" );
 135  0
         command.createArg().setValue( "-ssubmitted" );
 136  0
         command.createArg().setValue( "//" + clientspec + "/...#have" );
 137  
 
 138  0
         return command;
 139  
     }
 140  
 }