Coverage Report - org.apache.maven.scm.provider.perforce.command.checkout.PerforceCheckOutCommand
 
Classes in this File Line Coverage Branch Coverage Complexity
PerforceCheckOutCommand
6 %
9/137
6 %
4/60
15
 
 1  
 package org.apache.maven.scm.provider.perforce.command.checkout;
 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.checkout.AbstractCheckOutCommand;
 26  
 import org.apache.maven.scm.command.checkout.CheckOutScmResult;
 27  
 import org.apache.maven.scm.provider.ScmProviderRepository;
 28  
 import org.apache.maven.scm.provider.perforce.PerforceScmProvider;
 29  
 import org.apache.maven.scm.provider.perforce.command.PerforceCommand;
 30  
 import org.apache.maven.scm.provider.perforce.repository.PerforceScmProviderRepository;
 31  
 import org.apache.regexp.RE;
 32  
 import org.codehaus.plexus.util.IOUtil;
 33  
 import org.codehaus.plexus.util.StringUtils;
 34  
 import org.codehaus.plexus.util.cli.CommandLineException;
 35  
 import org.codehaus.plexus.util.cli.CommandLineUtils;
 36  
 import org.codehaus.plexus.util.cli.Commandline;
 37  
 
 38  
 import java.io.BufferedReader;
 39  
 import java.io.ByteArrayInputStream;
 40  
 import java.io.File;
 41  
 import java.io.IOException;
 42  
 import java.io.InputStreamReader;
 43  
 
 44  
 /**
 45  
  * @author Mike Perham
 46  
  * @version $Id: PerforceCheckOutCommand.java 1306867 2012-03-29 13:45:10Z olamy $
 47  
  */
 48  0
 public class PerforceCheckOutCommand
 49  
     extends AbstractCheckOutCommand
 50  
     implements PerforceCommand
 51  
 {
 52  
     private String actualLocation;
 53  
 
 54  
     /**
 55  
      * Check out the depot code at <code>repo.getPath()</code> into the target
 56  
      * directory at <code>files.getBasedir</code>. Perforce does not support
 57  
      * arbitrary checkout of versioned source so we need to set up a well-known
 58  
      * clientspec which will hold the required info.
 59  
      * <p/>
 60  
      * 1) A clientspec will be created or updated which holds a temporary
 61  
      * mapping from the repo path to the target directory.
 62  
      * 2) This clientspec is sync'd to pull all the files onto the client
 63  
      * <p/>
 64  
      * {@inheritDoc}
 65  
      */
 66  
     protected CheckOutScmResult executeCheckOutCommand( ScmProviderRepository repo, ScmFileSet files,
 67  
                                                         ScmVersion version, boolean recursive )
 68  
         throws ScmException
 69  
     {
 70  0
         PerforceScmProviderRepository prepo = (PerforceScmProviderRepository) repo;
 71  0
         File workingDirectory = new File( files.getBasedir().getAbsolutePath() );
 72  
 
 73  0
         actualLocation = PerforceScmProvider.getRepoPath( getLogger(), prepo, files.getBasedir() );
 74  
 
 75  0
         String specname = PerforceScmProvider.getClientspecName( getLogger(), prepo, workingDirectory );
 76  0
         PerforceCheckOutConsumer consumer = new PerforceCheckOutConsumer( specname, actualLocation );
 77  0
         if ( getLogger().isInfoEnabled() )
 78  
         {
 79  0
             getLogger().info( "Checkout working directory: " + workingDirectory );
 80  
         }
 81  0
         Commandline cl = null;
 82  
 
 83  
         // Create or update a clientspec so we can checkout the code to a particular location
 84  
         try
 85  
         {
 86  
             // Ahhh, glorious Perforce.  Create and update of clientspecs is the exact
 87  
             // same operation so we don't need to distinguish between the two modes.
 88  0
             cl = PerforceScmProvider.createP4Command( prepo, workingDirectory );
 89  0
             cl.createArg().setValue( "client" );
 90  0
             cl.createArg().setValue( "-i" );
 91  0
             if ( getLogger().isInfoEnabled() )
 92  
             {
 93  0
                 getLogger().info( "Executing: " + PerforceScmProvider.clean( cl.toString() ) );
 94  
             }
 95  
 
 96  0
             String client =
 97  
                 PerforceScmProvider.createClientspec( getLogger(), prepo, workingDirectory, actualLocation );
 98  
 
 99  0
             if ( getLogger().isDebugEnabled() )
 100  
             {
 101  0
                 getLogger().debug( "Updating clientspec:\n" + client );
 102  
             }
 103  
 
 104  0
             CommandLineUtils.StringStreamConsumer err = new CommandLineUtils.StringStreamConsumer();
 105  0
             int exitCode =
 106  
                 CommandLineUtils.executeCommandLine( cl, new ByteArrayInputStream( client.getBytes() ), consumer, err );
 107  
 
 108  0
             if ( exitCode != 0 )
 109  
             {
 110  0
                 String cmdLine = CommandLineUtils.toString( cl.getCommandline() );
 111  
 
 112  0
                 StringBuilder msg = new StringBuilder( "Exit code: " + exitCode + " - " + err.getOutput() );
 113  0
                 msg.append( '\n' );
 114  0
                 msg.append( "Command line was:" + cmdLine );
 115  
 
 116  0
                 throw new CommandLineException( msg.toString() );
 117  
             }
 118  
         }
 119  0
         catch ( CommandLineException e )
 120  
         {
 121  0
             if ( getLogger().isErrorEnabled() )
 122  
             {
 123  0
                 getLogger().error( "CommandLineException " + e.getMessage(), e );
 124  
             }
 125  0
         }
 126  
 
 127  0
         boolean clientspecExists = consumer.isSuccess();
 128  
 
 129  
         // Perform the actual checkout using that clientspec
 130  
         try
 131  
         {
 132  0
             if ( clientspecExists )
 133  
             {
 134  
                 try
 135  
                 {
 136  0
                     getLastChangelist( prepo, workingDirectory, specname );
 137  0
                     cl = createCommandLine( prepo, workingDirectory, version, specname );
 138  0
                     if ( getLogger().isDebugEnabled() )
 139  
                     {
 140  0
                         getLogger().debug( "Executing: " + PerforceScmProvider.clean( cl.toString() ) );
 141  
                     }
 142  0
                     Process proc = cl.execute();
 143  0
                     BufferedReader br = new BufferedReader( new InputStreamReader( proc.getInputStream() ) );
 144  
                     String line;
 145  0
                     while ( ( line = br.readLine() ) != null )
 146  
                     {
 147  0
                         if ( getLogger().isDebugEnabled() )
 148  
                         {
 149  0
                             getLogger().debug( "Consuming: " + line );
 150  
                         }
 151  0
                         consumer.consumeLine( line );
 152  
                     }
 153  0
                     CommandLineUtils.StringStreamConsumer err = new CommandLineUtils.StringStreamConsumer();
 154  0
                     int exitCode = CommandLineUtils.executeCommandLine( cl, consumer, err );
 155  
 
 156  0
                     if ( exitCode != 0 )
 157  
                     {
 158  0
                         String cmdLine = CommandLineUtils.toString( cl.getCommandline() );
 159  
 
 160  0
                         StringBuilder msg = new StringBuilder( "Exit code: " + exitCode + " - " + err.getOutput() );
 161  0
                         msg.append( '\n' );
 162  0
                         msg.append( "Command line was:" + cmdLine );
 163  
 
 164  0
                         throw new CommandLineException( msg.toString() );
 165  
                     }
 166  0
                     if ( getLogger().isDebugEnabled() )
 167  
                     {
 168  0
                         getLogger().debug( "Perforce sync complete." );
 169  
                     }
 170  
                 }
 171  0
                 catch ( CommandLineException e )
 172  
                 {
 173  0
                     if ( getLogger().isErrorEnabled() )
 174  
                     {
 175  0
                         getLogger().error( "CommandLineException " + e.getMessage(), e );
 176  
                     }
 177  
                 }
 178  0
                 catch ( IOException e )
 179  
                 {
 180  0
                     if ( getLogger().isErrorEnabled() )
 181  
                     {
 182  0
                         getLogger().error( "IOException " + e.getMessage(), e );
 183  
                     }
 184  0
                 }
 185  
             }
 186  
 
 187  0
             if ( consumer.isSuccess() )
 188  
             {
 189  0
                 return new CheckOutScmResult( cl.toString(), consumer.getCheckedout() );
 190  
             }
 191  
             else
 192  
             {
 193  0
                 return new CheckOutScmResult( cl.toString(), "Unable to sync.  Are you logged in?",
 194  
                                               consumer.getOutput(), consumer.isSuccess() );
 195  
             }
 196  
         }
 197  
         finally
 198  
         {
 199  
             // See SCM-113
 200  
             // Support transient clientspecs as we don't want to create 1000s of permanent clientspecs
 201  0
             if ( clientspecExists && !prepo.isPersistCheckout() )
 202  
             {
 203  
                 // Delete the clientspec
 204  0
                 InputStreamReader isReader = null;
 205  0
                 InputStreamReader isReaderErr = null;
 206  
                 try
 207  
                 {
 208  0
                     cl = PerforceScmProvider.createP4Command( prepo, workingDirectory );
 209  0
                     cl.createArg().setValue( "client" );
 210  0
                     cl.createArg().setValue( "-d" );
 211  0
                     cl.createArg().setValue( specname );
 212  0
                     if ( getLogger().isInfoEnabled() )
 213  
                     {
 214  0
                         getLogger().info( "Executing: " + PerforceScmProvider.clean( cl.toString() ) );
 215  
                     }
 216  0
                     Process proc = cl.execute();
 217  0
                     isReader = new InputStreamReader( proc.getInputStream() );
 218  0
                     BufferedReader br = new BufferedReader( isReader );
 219  
                     String line;
 220  0
                     while ( ( line = br.readLine() ) != null )
 221  
                     {
 222  0
                         if ( getLogger().isDebugEnabled() )
 223  
                         {
 224  0
                             getLogger().debug( "Consuming: " + line );
 225  
                         }
 226  0
                         consumer.consumeLine( line );
 227  
                     }
 228  0
                     br.close();
 229  
                     // Read errors from STDERR
 230  0
                     isReaderErr = new InputStreamReader( proc.getErrorStream() );
 231  0
                     BufferedReader brErr = new BufferedReader( isReaderErr );
 232  0
                     while ( ( line = brErr.readLine() ) != null )
 233  
                     {
 234  0
                         if ( getLogger().isDebugEnabled() )
 235  
                         {
 236  0
                             getLogger().debug( "Consuming stderr: " + line );
 237  
                         }
 238  0
                         consumer.consumeLine( line );
 239  
                     }
 240  0
                     brErr.close();
 241  0
                 }
 242  0
                 catch ( CommandLineException e )
 243  
                 {
 244  0
                     if ( getLogger().isErrorEnabled() )
 245  
                     {
 246  0
                         getLogger().error( "CommandLineException " + e.getMessage(), e );
 247  
                     }
 248  0
                 }
 249  0
                 catch ( IOException e )
 250  
                 {
 251  0
                     if ( getLogger().isErrorEnabled() )
 252  
                     {
 253  0
                         getLogger().error( "IOException " + e.getMessage(), e );
 254  
                     }
 255  0
                 }
 256  
                 finally
 257  
                 {
 258  0
                     IOUtil.close( isReader );
 259  0
                     IOUtil.close( isReaderErr );
 260  0
                 }
 261  0
             }
 262  0
             else if ( clientspecExists )
 263  
             {
 264  
                 // SCM-165 Save clientspec in memory so we can reuse it with further commands in this VM.
 265  0
                 System.setProperty( PerforceScmProvider.DEFAULT_CLIENTSPEC_PROPERTY, specname );
 266  
             }
 267  
         }
 268  
     }
 269  
 
 270  
     public static Commandline createCommandLine( PerforceScmProviderRepository repo, File workingDirectory,
 271  
                                                  ScmVersion version, String specname )
 272  
     {
 273  3
         Commandline command = PerforceScmProvider.createP4Command( repo, workingDirectory );
 274  
 
 275  3
         command.createArg().setValue( "-c" + specname );
 276  3
         command.createArg().setValue( "sync" );
 277  
 
 278  
         // Use a simple heuristic to determine if we should use the Force flag
 279  
         // on sync.  Forcing sync is a HUGE performance hit but is required in
 280  
         // rare instances where source is somehow deleted.  If the target
 281  
         // directory is completely empty, assume a force is required.  If
 282  
         // not empty, we assume a previous checkout was already done and a normal
 283  
         // sync will suffice.
 284  
         // SCM-110
 285  3
         String[] files = workingDirectory.list();
 286  3
         if ( files == null || files.length == 0 )
 287  
         {
 288  
             // We need to force so checkout to an empty directory will work.
 289  3
             command.createArg().setValue( "-f" );
 290  
         }
 291  
 
 292  
         // Not sure what to do here. I'm unclear whether we should be
 293  
         // sync'ing each file individually to the label or just sync the
 294  
         // entire contents of the workingDir. I'm going to assume the
 295  
         // latter until the exact semantics are clearer.
 296  3
         if ( version != null && StringUtils.isNotEmpty( version.getName() ) )
 297  
         {
 298  3
             command.createArg().setValue( "@" + version.getName() );
 299  
         }
 300  3
         return command;
 301  
     }
 302  
 
 303  
     private int getLastChangelist( PerforceScmProviderRepository repo, File workingDirectory, String specname )
 304  
     {
 305  0
         int lastChangelist = 0;
 306  
         try
 307  
         {
 308  0
             Commandline command = PerforceScmProvider.createP4Command( repo, workingDirectory );
 309  
 
 310  0
             command.createArg().setValue( "-c" + specname );
 311  0
             command.createArg().setValue( "changes" );
 312  0
             command.createArg().setValue( "-m1" );
 313  0
             command.createArg().setValue( "-ssubmitted" );
 314  0
             command.createArg().setValue( "//" + specname + "/..." );
 315  0
             getLogger().debug( "Executing: " + PerforceScmProvider.clean( command.toString() ) );
 316  0
             Process proc = command.execute();
 317  0
             BufferedReader br = new BufferedReader( new InputStreamReader( proc.getInputStream() ) );
 318  
             String line;
 319  
 
 320  0
             String lastChangelistStr = "";
 321  0
             while ( ( line = br.readLine() ) != null )
 322  
             {
 323  0
                 getLogger().debug( "Consuming: " + line );
 324  0
                 RE changeRegexp = new RE( "Change (\\d+)" );
 325  0
                 if ( changeRegexp.match( line ) )
 326  
                 {
 327  0
                     lastChangelistStr = changeRegexp.getParen( 1 );
 328  
                 }
 329  0
             }
 330  0
             br.close();
 331  
             // TODO: Read errors from STDERR?
 332  
 
 333  
             try
 334  
             {
 335  0
                 lastChangelist = Integer.parseInt( lastChangelistStr );
 336  
             }
 337  0
             catch ( NumberFormatException nfe )
 338  
             {
 339  0
                 getLogger().debug( "Could not parse changelist from line " + line );
 340  0
             }
 341  
         }
 342  0
         catch ( IOException e )
 343  
         {
 344  0
             getLogger().error( e );
 345  
         }
 346  0
         catch ( CommandLineException e )
 347  
         {
 348  0
             getLogger().error( e );
 349  0
         }
 350  
 
 351  0
         return lastChangelist;
 352  
     }
 353  
 }