Coverage Report - org.apache.maven.scm.provider.perforce.command.status.PerforceStatusCommand
 
Classes in this File Line Coverage Branch Coverage Complexity
PerforceStatusCommand
37 %
16/43
25 %
3/12
3,25
 
 1  
 package org.apache.maven.scm.provider.perforce.command.status;
 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.ScmFile;
 24  
 import org.apache.maven.scm.ScmFileSet;
 25  
 import org.apache.maven.scm.command.status.AbstractStatusCommand;
 26  
 import org.apache.maven.scm.command.status.StatusScmResult;
 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.command.PerforceVerbMapper;
 31  
 import org.apache.maven.scm.provider.perforce.repository.PerforceScmProviderRepository;
 32  
 import org.apache.regexp.RE;
 33  
 import org.codehaus.plexus.util.cli.CommandLineException;
 34  
 import org.codehaus.plexus.util.cli.CommandLineUtils;
 35  
 import org.codehaus.plexus.util.cli.Commandline;
 36  
 
 37  
 import java.io.File;
 38  
 import java.util.ArrayList;
 39  
 import java.util.Iterator;
 40  
 import java.util.List;
 41  
 
 42  
 /**
 43  
  * @author Mike Perham
 44  
  * @version $Id: PerforceStatusCommand.java 1306867 2012-03-29 13:45:10Z olamy $
 45  
  */
 46  0
 public class PerforceStatusCommand
 47  
     extends AbstractStatusCommand
 48  
     implements PerforceCommand
 49  
 {
 50  
     private String actualLocation;
 51  
 
 52  
     /** {@inheritDoc} */
 53  
     protected StatusScmResult executeStatusCommand( ScmProviderRepository repo, ScmFileSet files )
 54  
         throws ScmException
 55  
     {
 56  0
         PerforceScmProviderRepository prepo = (PerforceScmProviderRepository) repo;
 57  0
         actualLocation = PerforceScmProvider.getRepoPath( getLogger(), prepo, files.getBasedir() );
 58  0
         PerforceStatusConsumer consumer = new PerforceStatusConsumer();
 59  0
         Commandline command = readOpened( prepo, files, consumer );
 60  
 
 61  0
         if ( consumer.isSuccess() )
 62  
         {
 63  0
             List<ScmFile> scmfiles = createResults( actualLocation, consumer );
 64  0
             return new StatusScmResult( command.toString(), scmfiles );
 65  
         }
 66  
 
 67  0
         return new StatusScmResult( command.toString(), "Unable to get status", consumer
 68  
                 .getOutput(), consumer.isSuccess() );
 69  
     }
 70  
 
 71  
     public static List<ScmFile> createResults( String repoPath, PerforceStatusConsumer consumer )
 72  
     {
 73  1
         List<ScmFile> results = new ArrayList<ScmFile>();
 74  1
         List<String> files = consumer.getDepotfiles();
 75  1
         RE re = new RE( "([^#]+)#\\d+ - ([^ ]+) .*" );
 76  1
         for ( Iterator<String> it = files.iterator(); it.hasNext(); )
 77  
         {
 78  4
             String filepath = it.next();
 79  4
             if ( !re.match( filepath ) )
 80  
             {
 81  0
                 System.err.println( "Skipping " + filepath );
 82  0
                 continue;
 83  
             }
 84  4
             String path = re.getParen( 1 );
 85  4
             String verb = re.getParen( 2 );
 86  
 
 87  4
             ScmFile scmfile = new ScmFile( path.substring( repoPath.length() + 1 ).trim(), PerforceVerbMapper
 88  
                 .toStatus( verb ) );
 89  4
             results.add( scmfile );
 90  4
         }
 91  1
         return results;
 92  
     }
 93  
 
 94  
     private Commandline readOpened( PerforceScmProviderRepository prepo, ScmFileSet files,
 95  
                                     PerforceStatusConsumer consumer )
 96  
     {
 97  0
         Commandline cl = createOpenedCommandLine( prepo, files.getBasedir(), actualLocation );
 98  
         try
 99  
         {
 100  0
             if ( getLogger().isDebugEnabled() )
 101  
             {
 102  0
                 getLogger().debug( PerforceScmProvider.clean( "Executing " + cl.toString() ) );
 103  
             }
 104  
 
 105  0
             CommandLineUtils.StringStreamConsumer err = new CommandLineUtils.StringStreamConsumer();
 106  0
             int exitCode = CommandLineUtils.executeCommandLine( cl, 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
         return cl;
 128  
     }
 129  
 
 130  
     public static Commandline createOpenedCommandLine( PerforceScmProviderRepository repo, File workingDirectory,
 131  
                                                        String location )
 132  
     {
 133  1
         Commandline command = PerforceScmProvider.createP4Command( repo, workingDirectory );
 134  1
         command.createArg().setValue( "opened" );
 135  1
         command.createArg().setValue( PerforceScmProvider.getCanonicalRepoPath( location ) );
 136  1
         return command;
 137  
     }
 138  
 }