Coverage Report - org.apache.maven.scm.provider.perforce.command.changelog.PerforceChangeLogCommand
 
Classes in this File Line Coverage Branch Coverage Complexity
PerforceChangeLogCommand
26 %
18/69
28 %
8/28
5,75
 
 1  
 package org.apache.maven.scm.provider.perforce.command.changelog;
 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 java.io.File;
 23  
 import java.text.DateFormat;
 24  
 import java.text.SimpleDateFormat;
 25  
 import java.util.Date;
 26  
 import java.util.List;
 27  
 
 28  
 import org.apache.maven.scm.ScmBranch;
 29  
 import org.apache.maven.scm.ScmException;
 30  
 import org.apache.maven.scm.ScmFileSet;
 31  
 import org.apache.maven.scm.ScmVersion;
 32  
 import org.apache.maven.scm.command.changelog.AbstractChangeLogCommand;
 33  
 import org.apache.maven.scm.command.changelog.ChangeLogScmResult;
 34  
 import org.apache.maven.scm.command.changelog.ChangeLogSet;
 35  
 import org.apache.maven.scm.provider.ScmProviderRepository;
 36  
 import org.apache.maven.scm.provider.perforce.PerforceScmProvider;
 37  
 import org.apache.maven.scm.provider.perforce.command.PerforceCommand;
 38  
 import org.apache.maven.scm.provider.perforce.repository.PerforceScmProviderRepository;
 39  
 import org.codehaus.plexus.util.StringUtils;
 40  
 import org.codehaus.plexus.util.cli.CommandLineException;
 41  
 import org.codehaus.plexus.util.cli.CommandLineUtils;
 42  
 import org.codehaus.plexus.util.cli.Commandline;
 43  
 
 44  
 /**
 45  
  * @author <a href="mailto:evenisse@apache.org">Emmanuel Venisse</a>
 46  
  * @author Olivier Lamy
 47  
  * @version $Id: PerforceChangeLogCommand.java 1306867 2012-03-29 13:45:10Z olamy $
 48  
  */
 49  0
 public class PerforceChangeLogCommand
 50  
     extends AbstractChangeLogCommand
 51  
     implements PerforceCommand
 52  
 {
 53  
     /** {@inheritDoc} */
 54  
     protected ChangeLogScmResult executeChangeLogCommand( ScmProviderRepository repo, ScmFileSet fileSet,
 55  
                                                           ScmVersion startVersion, ScmVersion endVersion,
 56  
                                                           String datePattern )
 57  
         throws ScmException
 58  
     {
 59  0
         return executeChangeLogCommand( repo, fileSet, null, null, null, datePattern, startVersion, endVersion );
 60  
     }
 61  
 
 62  
     /** {@inheritDoc} */
 63  
     protected ChangeLogScmResult executeChangeLogCommand( ScmProviderRepository repo, ScmFileSet fileSet,
 64  
                                                           Date startDate, Date endDate, ScmBranch branch,
 65  
                                                           String datePattern )
 66  
         throws ScmException
 67  
     {
 68  0
         if ( branch != null && StringUtils.isNotEmpty( branch.getName() ) )
 69  
         {
 70  0
             throw new ScmException( "This SCM doesn't support branches." );
 71  
         }
 72  
 
 73  0
         return executeChangeLogCommand( repo, fileSet, startDate, endDate, branch, datePattern, null, null );
 74  
     }
 75  
 
 76  
     protected ChangeLogScmResult executeChangeLogCommand( ScmProviderRepository repo, ScmFileSet fileSet,
 77  
                                                           Date startDate, Date endDate, ScmBranch branch,
 78  
                                                           String datePattern, ScmVersion startVersion,
 79  
                                                           ScmVersion endVersion )
 80  
         throws ScmException
 81  
     {
 82  0
         PerforceScmProviderRepository p4repo = (PerforceScmProviderRepository) repo;
 83  0
         String clientspec = PerforceScmProvider.getClientspecName( getLogger(), p4repo, fileSet.getBasedir() );
 84  0
         Commandline cl = createCommandLine( p4repo, fileSet.getBasedir(), clientspec, null, startDate, endDate, startVersion, endVersion );
 85  
 
 86  0
         String location = PerforceScmProvider.getRepoPath( getLogger(), p4repo, fileSet.getBasedir() );
 87  0
         PerforceChangesConsumer consumer =
 88  
             new PerforceChangesConsumer( getLogger() );
 89  
 
 90  
         try
 91  
         {
 92  0
             if ( getLogger().isDebugEnabled() )
 93  
             {
 94  0
                 getLogger().debug( PerforceScmProvider.clean( "Executing " + cl.toString() ) );
 95  
             }
 96  
 
 97  0
             CommandLineUtils.StringStreamConsumer err = new CommandLineUtils.StringStreamConsumer();
 98  0
             int exitCode = CommandLineUtils.executeCommandLine( cl, consumer, err );
 99  
 
 100  0
             if ( exitCode != 0 )
 101  
             {
 102  0
                 String cmdLine = CommandLineUtils.toString( cl.getCommandline() );
 103  
 
 104  0
                 StringBuilder msg = new StringBuilder( "Exit code: " + exitCode + " - " + err.getOutput() );
 105  0
                 msg.append( '\n' );
 106  0
                 msg.append( "Command line was:" + cmdLine );
 107  
 
 108  0
                 throw new CommandLineException( msg.toString() );
 109  
             }
 110  
         }
 111  0
         catch ( CommandLineException e )
 112  
         {
 113  0
             if ( getLogger().isErrorEnabled() )
 114  
             {
 115  0
                 getLogger().error( "CommandLineException " + e.getMessage(), e );
 116  
             }
 117  0
         }
 118  
 
 119  0
         List<String> changes = consumer.getChanges();
 120  
 
 121  0
         cl = PerforceScmProvider.createP4Command( p4repo, fileSet.getBasedir() );
 122  0
         cl.createArg().setValue( "describe" );
 123  0
         cl.createArg().setValue( "-s" );
 124  
 
 125  0
         for( String change : changes )
 126  
         {
 127  0
             cl.createArg().setValue( change );
 128  
         }
 129  
 
 130  0
         PerforceDescribeConsumer describeConsumer =
 131  
             new PerforceDescribeConsumer( location, datePattern, getLogger() );
 132  
 
 133  
         try
 134  
         {
 135  0
             if ( getLogger().isDebugEnabled() )
 136  
             {
 137  0
                 getLogger().debug( PerforceScmProvider.clean( "Executing " + cl.toString() ) );
 138  
             }
 139  
 
 140  0
             CommandLineUtils.StringStreamConsumer err = new CommandLineUtils.StringStreamConsumer();
 141  0
             int exitCode = CommandLineUtils.executeCommandLine( cl, describeConsumer, err );
 142  
 
 143  0
             if ( exitCode != 0 )
 144  
             {
 145  0
                 String cmdLine = CommandLineUtils.toString( cl.getCommandline() );
 146  
 
 147  0
                 StringBuilder msg = new StringBuilder( "Exit code: " + exitCode + " - " + err.getOutput() );
 148  0
                 msg.append( '\n' );
 149  0
                 msg.append( "Command line was:" + cmdLine );
 150  
 
 151  0
                 throw new CommandLineException( msg.toString() );
 152  
             }
 153  
         }
 154  0
         catch ( CommandLineException e )
 155  
         {
 156  0
             if ( getLogger().isErrorEnabled() )
 157  
             {
 158  0
                 getLogger().error( "CommandLineException " + e.getMessage(), e );
 159  
             }
 160  0
         }
 161  
 
 162  0
         ChangeLogSet cls = new ChangeLogSet( describeConsumer.getModifications(), null, null );
 163  0
         cls.setStartVersion(startVersion);
 164  0
         cls.setEndVersion(endVersion);
 165  0
         return new ChangeLogScmResult( cl.toString(), cls );
 166  
     }
 167  
 
 168  
     public static Commandline createCommandLine( PerforceScmProviderRepository repo, File workingDirectory,
 169  
                                                  String clientspec,
 170  
                                                  ScmBranch branch, Date startDate, Date endDate,
 171  
                                                  ScmVersion startVersion, ScmVersion endVersion )
 172  
     {
 173  5
         DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd:HH:mm:ss");
 174  5
         Commandline command = PerforceScmProvider.createP4Command( repo, workingDirectory );
 175  
 
 176  5
         if ( clientspec != null )
 177  
         {
 178  3
             command.createArg().setValue( "-c" );
 179  3
             command.createArg().setValue( clientspec );
 180  
         }
 181  5
         command.createArg().setValue( "changes" );
 182  5
         command.createArg().setValue( "-t" );
 183  
 
 184  5
         StringBuilder fileSpec = new StringBuilder("...");
 185  5
         if ( startDate != null )
 186  
         {
 187  1
             fileSpec.append( "@" )
 188  
                  .append( dateFormat.format(startDate) )
 189  
                  .append( "," );
 190  
 
 191  1
             if ( endDate != null )
 192  
             {
 193  1
                 fileSpec.append( dateFormat.format(endDate) );
 194  
             }
 195  
             else
 196  
             {
 197  0
                 fileSpec.append( "now" );
 198  
             }
 199  
         }
 200  
 
 201  5
         if ( startVersion != null )
 202  
         {
 203  1
             fileSpec.append("@").append(startVersion.getName()).append(",");
 204  
 
 205  1
             if ( endVersion != null )
 206  
             {
 207  1
                 fileSpec.append( endVersion.getName() );
 208  
             }
 209  
             else
 210  
             {
 211  0
                 fileSpec.append("now");
 212  
             }
 213  
         }
 214  
 
 215  5
         command.createArg().setValue( fileSpec.toString() );
 216  
 
 217  5
         return command;
 218  
     }
 219  
 }