Coverage Report - org.apache.maven.scm.provider.hg.HgUtils
 
Classes in this File Line Coverage Branch Coverage Complexity
HgUtils
21 %
18/83
3 %
1/26
2,4
HgUtils$HgBranchnameConsumer
0 %
0/5
N/A
2,4
HgUtils$HgRevNoConsumer
0 %
0/7
N/A
2,4
 
 1  
 package org.apache.maven.scm.provider.hg;
 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.ScmFileStatus;
 25  
 import org.apache.maven.scm.ScmResult;
 26  
 import org.apache.maven.scm.log.DefaultLog;
 27  
 import org.apache.maven.scm.log.ScmLogger;
 28  
 import org.apache.maven.scm.provider.hg.command.HgCommandConstants;
 29  
 import org.apache.maven.scm.provider.hg.command.HgConsumer;
 30  
 import org.apache.maven.scm.provider.hg.command.inventory.HgChangeSet;
 31  
 import org.apache.maven.scm.provider.hg.command.inventory.HgOutgoingConsumer;
 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  
 import java.util.ArrayList;
 38  
 import java.util.HashMap;
 39  
 import java.util.Iterator;
 40  
 import java.util.List;
 41  
 import java.util.Map;
 42  
 
 43  
 /**
 44  
  * Common code for executing hg commands.
 45  
  *
 46  
  * @author <a href="mailto:thurner.rupert@ymono.net">thurner rupert</a>
 47  
  * @version $Id: HgUtils.java 1306858 2012-03-29 13:41:29Z olamy $
 48  
  */
 49  
 public final class HgUtils
 50  
 {
 51  
 
 52  0
     private HgUtils() {
 53  0
     }
 54  
 
 55  
     /**
 56  
      * Map between command and its valid exit codes
 57  
      */
 58  1
     private static final Map<String,List<Integer>> EXIT_CODE_MAP = new HashMap<String,List<Integer>>();
 59  
 
 60  
     /**
 61  
      * Default exit codes for entries not in exitCodeMap
 62  
      */
 63  1
     private static final List<Integer> DEFAULT_EXIT_CODES = new ArrayList<Integer>();
 64  
 
 65  
     /** Setup exit codes*/
 66  
     static
 67  
     {
 68  1
         DEFAULT_EXIT_CODES.add( new Integer( 0 ) );
 69  
 
 70  
         //Diff is different
 71  1
         List<Integer> diffExitCodes = new ArrayList<Integer>( 3 );
 72  1
         diffExitCodes.add( Integer.valueOf( 0 ) ); //No difference
 73  1
         diffExitCodes.add( Integer.valueOf( 1 ) ); //Conflicts in merge-like or changes in diff-like
 74  1
         diffExitCodes.add( Integer.valueOf( 2 ) ); //Unrepresentable diff changes
 75  1
         EXIT_CODE_MAP.put( HgCommandConstants.DIFF_CMD, diffExitCodes );
 76  
         //Outgoing is different
 77  1
         List<Integer> outgoingExitCodes = new ArrayList<Integer>( 2 );
 78  1
         outgoingExitCodes.add( Integer.valueOf( 0 ) ); //There are changes
 79  1
         outgoingExitCodes.add( Integer.valueOf( 1 ) ); //No changes
 80  1
         EXIT_CODE_MAP.put( HgCommandConstants.OUTGOING_CMD, outgoingExitCodes );        
 81  1
     }
 82  
 
 83  
     public static ScmResult execute( HgConsumer consumer, ScmLogger logger, File workingDir, String[] cmdAndArgs )
 84  
         throws ScmException
 85  
     {
 86  
         try
 87  
         {
 88  
             //Build commandline
 89  0
             Commandline cmd = buildCmd( workingDir, cmdAndArgs );
 90  0
             if ( logger.isInfoEnabled() )
 91  
             {
 92  0
                 logger.info( "EXECUTING: " + cmd );
 93  
             }
 94  
 
 95  
             //Execute command
 96  0
             int exitCode = executeCmd( consumer, cmd );
 97  
 
 98  
             //Return result
 99  0
             List<Integer> exitCodes = DEFAULT_EXIT_CODES;
 100  0
             if ( EXIT_CODE_MAP.containsKey( cmdAndArgs[0] ) )
 101  
             {
 102  0
                 exitCodes = EXIT_CODE_MAP.get( cmdAndArgs[0] );
 103  
             }
 104  0
             boolean success = exitCodes.contains( Integer.valueOf( exitCode ) );
 105  
 
 106  
             //On failure (and not due to exceptions) - run diagnostics
 107  0
             String providerMsg = "Execution of hg command succeded";
 108  0
             if ( !success )
 109  
             {
 110  0
                 HgConfig config = new HgConfig( workingDir );
 111  0
                 providerMsg =
 112  
                     "\nEXECUTION FAILED" + "\n  Execution of cmd : " + cmdAndArgs[0] + " failed with exit code: " +
 113  
                         exitCode + "." + "\n  Working directory was: " + "\n    " + workingDir.getAbsolutePath() +
 114  
                         config.toString( workingDir ) + "\n";
 115  0
                 if ( logger.isErrorEnabled() )
 116  
                 {
 117  0
                     logger.error( providerMsg );
 118  
                 }
 119  
             }
 120  
 
 121  0
             return new ScmResult( cmd.toString(), providerMsg, consumer.getStdErr(), success );
 122  
         }
 123  0
         catch ( ScmException se )
 124  
         {
 125  0
             String msg =
 126  
                 "EXECUTION FAILED" + "\n  Execution failed before invoking the Hg command. Last exception:" + "\n    " +
 127  
                     se.getMessage();
 128  
 
 129  
             //Add nested cause if any
 130  0
             if ( se.getCause() != null )
 131  
             {
 132  0
                 msg += "\n  Nested exception:" + "\n    " + se.getCause().getMessage();
 133  
             }
 134  
 
 135  
             //log and return
 136  0
             if ( logger.isErrorEnabled() )
 137  
             {
 138  0
                 logger.error( msg );
 139  
             }
 140  0
             throw se;
 141  
         }
 142  
     }
 143  
 
 144  
     static Commandline buildCmd( File workingDir, String[] cmdAndArgs )
 145  
         throws ScmException
 146  
     {
 147  1
         Commandline cmd = new Commandline();
 148  1
         cmd.setExecutable( HgCommandConstants.EXEC );
 149  1
         cmd.addArguments( cmdAndArgs );
 150  1
         if ( workingDir != null )
 151  
         {
 152  0
             cmd.setWorkingDirectory( workingDir.getAbsolutePath() );
 153  
 
 154  0
             if ( !workingDir.exists() )
 155  
             {
 156  0
                 boolean success = workingDir.mkdirs();
 157  0
                 if ( !success )
 158  
                 {
 159  0
                     String msg = "Working directory did not exist" + " and it couldn't be created: " + workingDir;
 160  0
                     throw new ScmException( msg );
 161  
                 }
 162  
             }
 163  
         }
 164  1
         return cmd;
 165  
     }
 166  
 
 167  
     static int executeCmd( HgConsumer consumer, Commandline cmd )
 168  
         throws ScmException
 169  
     {
 170  
         final int exitCode;
 171  
         try
 172  
         {
 173  0
             exitCode = CommandLineUtils.executeCommandLine( cmd, consumer, consumer );
 174  
         }
 175  0
         catch ( CommandLineException ex )
 176  
         {
 177  0
             throw new ScmException( "Command could not be executed: " + cmd, ex );
 178  0
         }
 179  0
         return exitCode;
 180  
     }
 181  
 
 182  
     public static ScmResult execute( File workingDir, String[] cmdAndArgs )
 183  
         throws ScmException
 184  
     {
 185  0
         ScmLogger logger = new DefaultLog();
 186  0
         return execute( new HgConsumer( logger ), logger, workingDir, cmdAndArgs );
 187  
     }
 188  
 
 189  
     public static String[] expandCommandLine( String[] cmdAndArgs, ScmFileSet additionalFiles )
 190  
     {
 191  0
         List<File> filesList = additionalFiles.getFileList();
 192  0
         String[] cmd = new String[filesList.size() + cmdAndArgs.length];
 193  
 
 194  
         // Copy command into array
 195  0
         System.arraycopy( cmdAndArgs, 0, cmd, 0, cmdAndArgs.length );
 196  
 
 197  
         // Add files as additional parameter into the array
 198  0
         int i = 0;
 199  0
         for ( Iterator<File> iterator = filesList.iterator(); iterator.hasNext(); i++ )
 200  
         {
 201  0
             File scmFile = iterator.next();
 202  0
             String file = scmFile.getPath().replace( '\\', File.separatorChar );
 203  0
             cmd[i + cmdAndArgs.length] = file;
 204  
 
 205  
         }
 206  
 
 207  0
         return cmd;
 208  
     }
 209  
 
 210  
     public static int getCurrentRevisionNumber( ScmLogger logger, File workingDir )
 211  
         throws ScmException
 212  
     {
 213  
 
 214  0
         String[] revCmd = new String[]{ HgCommandConstants.REVNO_CMD };
 215  0
         HgRevNoConsumer consumer = new HgRevNoConsumer( logger );
 216  0
         HgUtils.execute( consumer, logger, workingDir, revCmd );
 217  
 
 218  0
         return consumer.getCurrentRevisionNumber();
 219  
     }
 220  
 
 221  
     public static String getCurrentBranchName( ScmLogger logger, File workingDir )
 222  
         throws ScmException
 223  
     {
 224  0
         String[] branchnameCmd = new String[]{ HgCommandConstants.BRANCH_NAME_CMD };
 225  0
         HgBranchnameConsumer consumer = new HgBranchnameConsumer( logger );
 226  0
         HgUtils.execute( consumer, logger, workingDir, branchnameCmd );
 227  0
         return consumer.getBranchName();
 228  
     }
 229  
 
 230  
     /**
 231  
      * Get current (working) revision.
 232  
      * <p/>
 233  
      * Resolve revision to the last integer found in the command output.
 234  
      */
 235  
     private static class HgRevNoConsumer
 236  
         extends HgConsumer
 237  
     {
 238  
 
 239  
         private int revNo;
 240  
 
 241  
         HgRevNoConsumer( ScmLogger logger )
 242  
         {
 243  0
             super( logger );
 244  0
         }
 245  
 
 246  
         public void doConsume( ScmFileStatus status, String line )
 247  
         {
 248  
             try
 249  
             {
 250  0
                 revNo = Integer.valueOf( line ).intValue();
 251  
             }
 252  0
             catch ( NumberFormatException e )
 253  
             {
 254  
                 // ignore
 255  0
             }
 256  0
         }
 257  
 
 258  
         int getCurrentRevisionNumber()
 259  
         {
 260  0
             return revNo;
 261  
         }
 262  
     }
 263  
 
 264  
     /**
 265  
      * Get current (working) branch name
 266  
      */
 267  
     private static class HgBranchnameConsumer
 268  
         extends HgConsumer
 269  
     {
 270  
 
 271  
         private String branchName;
 272  
 
 273  
         HgBranchnameConsumer( ScmLogger logger )
 274  
         {
 275  0
             super( logger );
 276  0
         }
 277  
 
 278  
         public void doConsume( ScmFileStatus status, String trimmedLine )
 279  
         {
 280  0
             branchName = String.valueOf( trimmedLine );
 281  0
         }
 282  
 
 283  
         String getBranchName()
 284  
         {
 285  0
             return branchName;
 286  
         }
 287  
     }
 288  
 
 289  
 
 290  
     /**
 291  
      * Check if there are outgoing changes on a different branch. If so, Mercurial default behaviour
 292  
      * is to block the push and warn using a 'push creates new remote branch !' message.
 293  
      * We also warn, and return true if a different outgoing branch was found
 294  
      * <p/>
 295  
      * Method users should not stop the push on a negative return, instead, they should hg push -r(branch being released)
 296  
      *
 297  
      * @param logger            the logger
 298  
      * @param workingDir        the working dir
 299  
      * @param workingbranchName the working branch name
 300  
      * @return true if a different outgoing branch was found
 301  
      * @throws ScmException on outgoing command error
 302  
      */
 303  
     public static boolean differentOutgoingBranchFound( ScmLogger logger, File workingDir, String workingbranchName )
 304  
         throws ScmException
 305  
     {
 306  0
         String[] outCmd = new String[]{ HgCommandConstants.OUTGOING_CMD };
 307  0
         HgOutgoingConsumer outConsumer = new HgOutgoingConsumer( logger );
 308  0
         ScmResult outResult = HgUtils.execute( outConsumer, logger, workingDir, outCmd );
 309  0
         List<HgChangeSet> changes = outConsumer.getChanges();
 310  0
         if ( outResult.isSuccess() )
 311  
         {
 312  0
             for ( int i = 0; i < changes.size(); i++ )
 313  
             {
 314  0
                 HgChangeSet set = changes.get( i );
 315  0
                 if ( set.getBranch() != null )
 316  
                 {
 317  0
                     logger.warn( "A different branch than " + workingbranchName +
 318  
                         " was found in outgoing changes, branch name was " + set.getBranch() +
 319  
                         ". Only local branch named " + workingbranchName + " will be pushed." );
 320  0
                     return true;
 321  
                 }
 322  
             }
 323  
         }
 324  0
         return false;
 325  
     }
 326  
 }