Coverage Report - org.apache.maven.scm.provider.hg.command.branch.HgBranchCommand
 
Classes in this File Line Coverage Branch Coverage Complexity
HgBranchCommand
0 %
0/42
0 %
0/22
5,25
HgBranchCommand$1
0 %
0/2
N/A
5,25
 
 1  
 package org.apache.maven.scm.provider.hg.command.branch;
 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.util.ArrayList;
 24  
 import java.util.Iterator;
 25  
 import java.util.List;
 26  
 
 27  
 import org.apache.maven.scm.ScmBranchParameters;
 28  
 import org.apache.maven.scm.ScmException;
 29  
 import org.apache.maven.scm.ScmFile;
 30  
 import org.apache.maven.scm.ScmFileSet;
 31  
 import org.apache.maven.scm.ScmFileStatus;
 32  
 import org.apache.maven.scm.ScmResult;
 33  
 import org.apache.maven.scm.command.Command;
 34  
 import org.apache.maven.scm.command.branch.AbstractBranchCommand;
 35  
 import org.apache.maven.scm.command.branch.BranchScmResult;
 36  
 import org.apache.maven.scm.provider.ScmProviderRepository;
 37  
 import org.apache.maven.scm.provider.hg.HgUtils;
 38  
 import org.apache.maven.scm.provider.hg.command.HgCommandConstants;
 39  
 import org.apache.maven.scm.provider.hg.command.HgConsumer;
 40  
 import org.apache.maven.scm.provider.hg.command.inventory.HgListConsumer;
 41  
 import org.apache.maven.scm.provider.hg.repository.HgScmProviderRepository;
 42  
 import org.codehaus.plexus.util.StringUtils;
 43  
 
 44  
 /**
 45  
  * Branch. Mercurial has weird branches. After a branch is created, it must be committed to the server, otherwise
 46  
  * the branch does not exist (yet) in the repository.
 47  
  *
 48  
  * @author Henning Schmiedehausen
 49  
  * @version $Id: HgBranchCommand.java 1061024 2011-01-19 21:54:37Z olamy $
 50  
  */
 51  0
 public class HgBranchCommand
 52  
     extends AbstractBranchCommand
 53  
     implements Command
 54  
 {
 55  
 
 56  
     protected ScmResult executeBranchCommand( ScmProviderRepository scmProviderRepository, ScmFileSet fileSet, String branch,
 57  
                                            String message )
 58  
         throws ScmException
 59  
     {
 60  0
         return executeBranchCommand( scmProviderRepository, fileSet, branch, new ScmBranchParameters( message ) );
 61  
     }
 62  
 
 63  
     /**
 64  
      * {@inheritDoc}
 65  
      */
 66  
     protected ScmResult executeBranchCommand( ScmProviderRepository scmProviderRepository, ScmFileSet fileSet, String branch,
 67  
                                            ScmBranchParameters scmBranchParameters )
 68  
         throws ScmException
 69  
     {
 70  
 
 71  0
         if ( StringUtils.isBlank( branch ) )
 72  
         {
 73  0
             throw new ScmException( "branch must be specified" );
 74  
         }
 75  
 
 76  0
         if ( !fileSet.getFileList().isEmpty() )
 77  
         {
 78  0
             throw new ScmException( "This provider doesn't support branchging subsets of a directory" );
 79  
         }
 80  
 
 81  0
         File workingDir = fileSet.getBasedir();
 82  
 
 83  
         // build the command
 84  0
         String[] branchCmd =
 85  
             new String[]{ HgCommandConstants.BRANCH_CMD, branch };
 86  
 
 87  
         // keep the command about in string form for reporting
 88  
         ;
 89  0
         HgConsumer branchConsumer = new HgConsumer( getLogger() ) {
 90  
             public void doConsume( ScmFileStatus status, String trimmedLine )
 91  
             {
 92  0
             }
 93  
         };
 94  
 
 95  0
         ScmResult result = HgUtils.execute( branchConsumer, getLogger(), workingDir, branchCmd );
 96  0
         HgScmProviderRepository repository = (HgScmProviderRepository) scmProviderRepository;
 97  
 
 98  0
         if ( !result.isSuccess() )
 99  
         {
 100  0
             throw new ScmException( "Error while executing command " + joinCmd( branchCmd ) );
 101  
         }
 102  
 
 103  
         // First commit.
 104  0
         String[] commitCmd = new String[]{ HgCommandConstants.COMMIT_CMD, HgCommandConstants.MESSAGE_OPTION, scmBranchParameters.getMessage() };
 105  
 
 106  
 
 107  0
         result = HgUtils.execute( new HgConsumer( getLogger() ), getLogger(), workingDir, commitCmd );
 108  
 
 109  0
         if ( !result.isSuccess() )
 110  
         {
 111  0
             throw new ScmException( "Error while executing command " + joinCmd( commitCmd ) );
 112  
         }
 113  
 
 114  
         // now push, if we should.
 115  
 
 116  0
         if ( repository.isPushChanges() )
 117  
         {
 118  0
             if ( !repository.getURI().equals( fileSet.getBasedir().getAbsolutePath() ) )
 119  
             {
 120  
 
 121  0
                 String[] pushCmd = new String[] {
 122  
                     HgCommandConstants.PUSH_CMD,
 123  
                     HgCommandConstants.NEW_BRANCH_OPTION,
 124  
                     repository.getURI()
 125  
                 };
 126  
 
 127  0
                 result = HgUtils.execute( new HgConsumer( getLogger() ), getLogger(), fileSet.getBasedir(), pushCmd );
 128  
 
 129  0
                 if ( !result.isSuccess() )
 130  
                 {
 131  0
                     throw new ScmException( "Error while executing command " + joinCmd( pushCmd ) );
 132  
                 }
 133  
             }
 134  
         }
 135  
 
 136  
         // do an inventory to return the files branched (all of them)
 137  0
         String[] listCmd = new String[]{ HgCommandConstants.INVENTORY_CMD };
 138  0
         HgListConsumer listconsumer = new HgListConsumer( getLogger() );
 139  
 
 140  0
         result = HgUtils.execute( listconsumer, getLogger(), fileSet.getBasedir(), listCmd );
 141  
 
 142  0
         if ( !result.isSuccess() )
 143  
         {
 144  0
             throw new ScmException( "Error while executing command " + joinCmd(listCmd) );
 145  
         }
 146  
 
 147  0
         List<ScmFile> files = listconsumer.getFiles();
 148  0
         List<ScmFile> fileList = new ArrayList<ScmFile>();
 149  0
         for ( Iterator<ScmFile> i = files.iterator(); i.hasNext(); )
 150  
         {
 151  0
             ScmFile f = i.next();
 152  
 
 153  0
             fileList.add( new ScmFile( f.getPath(), ScmFileStatus.TAGGED ) );
 154  0
         }
 155  
 
 156  0
         return new BranchScmResult( fileList, result );
 157  
     }
 158  
 
 159  
     private String joinCmd( String[] cmd )
 160  
     {
 161  0
         StringBuilder result = new StringBuilder();
 162  0
         for ( int i = 0; i < cmd.length; i++ )
 163  
         {
 164  0
             String s = cmd[i];
 165  0
             result.append( s );
 166  0
             if ( i < cmd.length - 1 )
 167  
             {
 168  0
                 result.append( " " );
 169  
             }
 170  
         }
 171  0
         return result.toString();
 172  
     }
 173  
 }