Coverage Report - org.apache.maven.scm.provider.perforce.command.checkin.PerforceCheckInCommand
 
Classes in this File Line Coverage Branch Coverage Complexity
PerforceCheckInCommand
47 %
24/51
30 %
6/20
5,333
 
 1  
 package org.apache.maven.scm.provider.perforce.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.checkin.AbstractCheckInCommand;
 26  
 import org.apache.maven.scm.command.checkin.CheckInScmResult;
 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.cli.CommandLineException;
 32  
 import org.codehaus.plexus.util.cli.CommandLineUtils;
 33  
 import org.codehaus.plexus.util.cli.Commandline;
 34  
 
 35  
 import java.io.ByteArrayInputStream;
 36  
 import java.io.File;
 37  
 import java.io.IOException;
 38  
 import java.util.HashSet;
 39  
 import java.util.List;
 40  
 import java.util.Set;
 41  
 
 42  
 /**
 43  
  * @author Mike Perham
 44  
  * @version $Id: PerforceCheckInCommand.java 1329117 2012-04-23 08:21:11Z olamy $
 45  
  */
 46  0
 public class PerforceCheckInCommand
 47  
     extends AbstractCheckInCommand
 48  
     implements PerforceCommand
 49  
 {
 50  
     /**
 51  
      * {@inheritDoc}
 52  
      */
 53  
     protected CheckInScmResult executeCheckInCommand( ScmProviderRepository repo, ScmFileSet files, String message,
 54  
                                                       ScmVersion version )
 55  
         throws ScmException
 56  
     {
 57  0
         Commandline cl = createCommandLine( (PerforceScmProviderRepository) repo, files.getBasedir() );
 58  0
         PerforceCheckInConsumer consumer = new PerforceCheckInConsumer();
 59  
         try
 60  
         {
 61  0
             String jobs = System.getProperty( "maven.scm.jobs" );
 62  
 
 63  0
             if ( getLogger().isDebugEnabled() )
 64  
             {
 65  0
                 getLogger().debug( PerforceScmProvider.clean( "Executing " + cl.toString() ) );
 66  
             }
 67  
 
 68  0
             PerforceScmProviderRepository prepo = (PerforceScmProviderRepository) repo;
 69  0
             String changes = createChangeListSpecification( prepo, files, message,
 70  
                                                             PerforceScmProvider.getRepoPath( getLogger(), prepo,
 71  
                                                                                              files.getBasedir() ),
 72  
                                                             jobs );
 73  
 
 74  0
             if ( getLogger().isDebugEnabled() )
 75  
             {
 76  0
                 getLogger().debug( "Sending changelist:\n" + changes );
 77  
             }
 78  
 
 79  0
             CommandLineUtils.StringStreamConsumer err = new CommandLineUtils.StringStreamConsumer();
 80  0
             int exitCode =
 81  
                 CommandLineUtils.executeCommandLine( cl, new ByteArrayInputStream( changes.getBytes() ), consumer,
 82  
                                                      err );
 83  
 
 84  0
             if ( exitCode != 0 )
 85  
             {
 86  0
                 String cmdLine = CommandLineUtils.toString( cl.getCommandline() );
 87  
 
 88  0
                 StringBuilder msg = new StringBuilder( "Exit code: " + exitCode + " - " + err.getOutput() );
 89  0
                 msg.append( '\n' );
 90  0
                 msg.append( "Command line was:" + cmdLine );
 91  
 
 92  0
                 throw new CommandLineException( msg.toString() );
 93  
             }
 94  
         }
 95  0
         catch ( CommandLineException e )
 96  
         {
 97  0
             if ( getLogger().isErrorEnabled() )
 98  
             {
 99  0
                 getLogger().error( "CommandLineException " + e.getMessage(), e );
 100  
             }
 101  0
         }
 102  
 
 103  0
         return new CheckInScmResult( cl.toString(), consumer.isSuccess() ? "Checkin successful" : "Unable to submit",
 104  
                                      consumer.getOutput(), consumer.isSuccess() );
 105  
     }
 106  
 
 107  
     public static Commandline createCommandLine( PerforceScmProviderRepository repo, File workingDirectory )
 108  
     {
 109  3
         Commandline command = PerforceScmProvider.createP4Command( repo, workingDirectory );
 110  
 
 111  3
         command.createArg().setValue( "submit" );
 112  3
         command.createArg().setValue( "-i" );
 113  3
         return command;
 114  
     }
 115  
 
 116  
     private static final String NEWLINE = "\r\n";
 117  
 
 118  
     public static String createChangeListSpecification( PerforceScmProviderRepository repo, ScmFileSet files,
 119  
                                                         String msg, String canonicalPath, String jobs )
 120  
     {
 121  3
         StringBuilder buf = new StringBuilder();
 122  3
         buf.append( "Change: new" ).append( NEWLINE ).append( NEWLINE );
 123  3
         buf.append( "Description:" ).append( NEWLINE ).append( "\t" ).append( msg ).append( NEWLINE ).append( NEWLINE );
 124  3
         if ( jobs != null && jobs.length() != 0 )
 125  
         {
 126  
             // Multiple jobs are not handled with this implementation
 127  3
             buf.append( "Jobs:" ).append( NEWLINE ).append( "\t" ).append( jobs ).append( NEWLINE ).append( NEWLINE );
 128  
         }
 129  
 
 130  3
         buf.append( "Files:" ).append( NEWLINE );
 131  
         try
 132  
         {
 133  3
             Set<String> dupes = new HashSet<String>();
 134  3
             File workingDir = files.getBasedir();
 135  3
             String candir = workingDir.getCanonicalPath();
 136  3
             List<File> fs = files.getFileList();
 137  9
             for ( int i = 0; i < fs.size(); i++ )
 138  
             {
 139  6
                 File file = new File( workingDir, fs.get( i ).getPath() );
 140  
                 // XXX Submit requires the canonical repository path for each
 141  
                 // file.
 142  
                 // It is unclear how to get that from a File object.
 143  
                 // We assume the repo object has the relative prefix
 144  
                 // "//depot/some/project"
 145  
                 // and canfile has the relative path "src/foo.xml" to be added
 146  
                 // to that prefix.
 147  
                 // "//depot/some/project/src/foo.xml"
 148  6
                 String canfile = file.getCanonicalPath();
 149  6
                 if ( dupes.contains( canfile ) )
 150  
                 {
 151  
                     // XXX I am seeing duplicate files in the ScmFileSet.
 152  
                     // I don't know why this is but we have to weed them out
 153  
                     // or Perforce will barf
 154  0
                     System.err.println( "Skipping duplicate file: " + file );
 155  0
                     continue;
 156  
                 }
 157  6
                 dupes.add( canfile );
 158  6
                 if ( canfile.startsWith( candir ) )
 159  
                 {
 160  6
                     canfile = canfile.substring( candir.length() + 1 );
 161  
                 }
 162  6
                 buf.append( "\t" ).append( canonicalPath ).append( "/" ).append( canfile.replace( '\\', '/' ) ).append(
 163  
                     NEWLINE );
 164  
             }
 165  
         }
 166  0
         catch ( IOException e )
 167  
         {
 168  0
             e.printStackTrace();
 169  3
         }
 170  3
         return buf.toString();
 171  
     }
 172  
 }