Coverage Report - org.apache.maven.scm.provider.perforce.command.diff.PerforceDiffCommand
 
Classes in this File Line Coverage Branch Coverage Complexity
PerforceDiffCommand
29 %
8/27
31 %
5/16
6
 
 1  
 package org.apache.maven.scm.provider.perforce.command.diff;
 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.diff.AbstractDiffCommand;
 26  
 import org.apache.maven.scm.command.diff.DiffScmResult;
 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.codehaus.plexus.util.StringUtils;
 32  
 import org.codehaus.plexus.util.cli.CommandLineException;
 33  
 import org.codehaus.plexus.util.cli.CommandLineUtils;
 34  
 import org.codehaus.plexus.util.cli.Commandline;
 35  
 
 36  
 import java.io.File;
 37  
 
 38  
 /**
 39  
  * @author Mike Perham
 40  
  * @version $Id: PerforceDiffCommand.java 1306867 2012-03-29 13:45:10Z olamy $
 41  
  */
 42  0
 public class PerforceDiffCommand
 43  
     extends AbstractDiffCommand
 44  
     implements PerforceCommand
 45  
 {
 46  
     /** {@inheritDoc} */
 47  
     protected DiffScmResult executeDiffCommand( ScmProviderRepository repo, ScmFileSet files, ScmVersion startRev,
 48  
                                                 ScmVersion endRev )
 49  
         throws ScmException
 50  
     {
 51  0
         Commandline cl =
 52  
             createCommandLine( (PerforceScmProviderRepository) repo, files.getBasedir(), startRev, endRev );
 53  0
         PerforceDiffConsumer consumer = new PerforceDiffConsumer();
 54  0
         if ( getLogger().isInfoEnabled() )
 55  
         {
 56  0
             getLogger().info( "Executing: " + PerforceScmProvider.clean( cl.toString() ) );
 57  
         }
 58  0
         boolean success = false;
 59  
         try
 60  
         {
 61  0
             CommandLineUtils.StringStreamConsumer err = new CommandLineUtils.StringStreamConsumer();
 62  0
             int exitCode = CommandLineUtils.executeCommandLine( cl, consumer, err );
 63  
 
 64  0
             if ( exitCode != 0 )
 65  
             {
 66  0
                 String cmdLine = CommandLineUtils.toString( cl.getCommandline() );
 67  
 
 68  0
                 StringBuilder msg = new StringBuilder( "Exit code: " + exitCode + " - " + err.getOutput() );
 69  0
                 msg.append( '\n' );
 70  0
                 msg.append( "Command line was:" + cmdLine );
 71  
 
 72  0
                 throw new CommandLineException( msg.toString() );
 73  
             }
 74  
         }
 75  0
         catch ( CommandLineException e )
 76  
         {
 77  0
             if ( getLogger().isErrorEnabled() )
 78  
             {
 79  0
                 getLogger().error( "CommandLineException " + e.getMessage(), e );
 80  
             }
 81  0
         }
 82  
 
 83  0
         return new DiffScmResult( cl.toString(), success ? "Diff successful" : "Unable to diff", consumer
 84  
             .getOutput(), success );
 85  
     }
 86  
 
 87  
     public static Commandline createCommandLine( PerforceScmProviderRepository repo, File workingDirectory,
 88  
                                                  ScmVersion startRev, ScmVersion endRev )
 89  
     {
 90  2
         String start = startRev != null && StringUtils.isNotEmpty( startRev.getName() ) ? "@" + startRev.getName() : "";
 91  2
         String end = endRev != null && StringUtils.isNotEmpty( endRev.getName() ) ? endRev.getName() : "now";
 92  
 
 93  2
         Commandline command = PerforceScmProvider.createP4Command( repo, workingDirectory );
 94  
 
 95  2
         command.createArg().setValue( "diff2" );
 96  2
         command.createArg().setValue( "-u" );
 97  
         // I'm assuming the "revs" are actually labels
 98  2
         command.createArg().setValue( "..." + start );
 99  2
         command.createArg().setValue( "...@" + end );
 100  2
         return command;
 101  
     }
 102  
 
 103  
 }