Coverage Report - org.apache.maven.scm.provider.hg.command.tag.HgTagCommand
 
Classes in this File Line Coverage Branch Coverage Complexity
HgTagCommand
0 %
0/39
0 %
0/24
6,667
 
 1  
 package org.apache.maven.scm.provider.hg.command.tag;
 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.List;
 25  
 
 26  
 import org.apache.maven.scm.ScmException;
 27  
 import org.apache.maven.scm.ScmFile;
 28  
 import org.apache.maven.scm.ScmFileSet;
 29  
 import org.apache.maven.scm.ScmFileStatus;
 30  
 import org.apache.maven.scm.ScmResult;
 31  
 import org.apache.maven.scm.ScmTagParameters;
 32  
 import org.apache.maven.scm.command.Command;
 33  
 import org.apache.maven.scm.command.tag.AbstractTagCommand;
 34  
 import org.apache.maven.scm.command.tag.TagScmResult;
 35  
 import org.apache.maven.scm.provider.ScmProviderRepository;
 36  
 import org.apache.maven.scm.provider.hg.HgUtils;
 37  
 import org.apache.maven.scm.provider.hg.command.HgCommandConstants;
 38  
 import org.apache.maven.scm.provider.hg.command.HgConsumer;
 39  
 import org.apache.maven.scm.provider.hg.command.inventory.HgListConsumer;
 40  
 import org.apache.maven.scm.provider.hg.repository.HgScmProviderRepository;
 41  
 import org.codehaus.plexus.util.StringUtils;
 42  
 
 43  
 /**
 44  
  * Tag
 45  
  *
 46  
  * @author <a href="mailto:ryan@darksleep.com">ryan daum</a>
 47  
  * @author Olivier Lamy
 48  
  * @version $Id: HgTagCommand.java 1306867 2012-03-29 13:45:10Z olamy $
 49  
  */
 50  0
 public class HgTagCommand
 51  
     extends AbstractTagCommand
 52  
     implements Command
 53  
 {
 54  
 
 55  
     protected ScmResult executeTagCommand( ScmProviderRepository scmProviderRepository, ScmFileSet fileSet, String tag,
 56  
                                            String message )
 57  
         throws ScmException
 58  
     {
 59  0
         return executeTagCommand( scmProviderRepository, fileSet, tag, new ScmTagParameters( message ) );
 60  
     }
 61  
 
 62  
     /**
 63  
      * {@inheritDoc}
 64  
      */
 65  
     protected ScmResult executeTagCommand( ScmProviderRepository scmProviderRepository, ScmFileSet fileSet, String tag,
 66  
                                            ScmTagParameters scmTagParameters )
 67  
         throws ScmException
 68  
     {
 69  
 
 70  0
         if ( tag == null || StringUtils.isEmpty( tag.trim() ) )
 71  
         {
 72  0
             throw new ScmException( "tag must be specified" );
 73  
         }
 74  
 
 75  0
         if ( !fileSet.getFileList().isEmpty() )
 76  
         {
 77  0
             throw new ScmException( "This provider doesn't support tagging subsets of a directory : " + fileSet.getFileList() );
 78  
         }
 79  
 
 80  0
         File workingDir = fileSet.getBasedir();
 81  
 
 82  
         // build the command
 83  0
         String[] tagCmd =
 84  
             new String[]{ HgCommandConstants.TAG_CMD, HgCommandConstants.MESSAGE_OPTION, scmTagParameters.getMessage(),
 85  
                 tag };
 86  
 
 87  
         // keep the command about in string form for reporting
 88  0
         StringBuilder cmd = joinCmd( tagCmd );
 89  0
         HgTagConsumer consumer = new HgTagConsumer( getLogger() );
 90  0
         ScmResult result = HgUtils.execute( consumer, getLogger(), workingDir, tagCmd );
 91  0
         HgScmProviderRepository repository = (HgScmProviderRepository) scmProviderRepository;
 92  0
         if ( result.isSuccess() )
 93  
         {
 94  
             // now push
 95  
             // Push to parent branch if any
 96  
 
 97  0
             if ( repository.isPushChanges() )
 98  
             {
 99  0
                 if ( !repository.getURI().equals( fileSet.getBasedir().getAbsolutePath() ) )
 100  
                 {
 101  0
                     String branchName = HgUtils.getCurrentBranchName( getLogger(), workingDir );
 102  0
                     boolean differentOutgoingBranch =
 103  
                         HgUtils.differentOutgoingBranchFound( getLogger(), workingDir, branchName );
 104  
 
 105  0
                     String[] pushCmd = new String[]{ HgCommandConstants.PUSH_CMD,
 106  
                         differentOutgoingBranch ? HgCommandConstants.REVISION_OPTION + branchName : null,
 107  
                         repository.getURI() };
 108  
 
 109  0
                     result =
 110  
                         HgUtils.execute( new HgConsumer( getLogger() ), getLogger(), fileSet.getBasedir(), pushCmd );
 111  0
                 }
 112  
             }
 113  
         }
 114  
         else
 115  
         {
 116  0
             throw new ScmException( "Error while executing command " + cmd.toString() );
 117  
         }
 118  
 
 119  
         // do an inventory to return the files tagged (all of them)
 120  0
         String[] listCmd = new String[]{ HgCommandConstants.INVENTORY_CMD };
 121  0
         HgListConsumer listconsumer = new HgListConsumer( getLogger() );
 122  0
         result = HgUtils.execute( listconsumer, getLogger(), fileSet.getBasedir(), listCmd );
 123  0
         if ( result.isSuccess() )
 124  
         {
 125  0
             List<ScmFile> files = listconsumer.getFiles();
 126  0
             List<ScmFile> fileList = new ArrayList<ScmFile>();
 127  0
             for ( ScmFile f : files )
 128  
             {
 129  0
                 if ( !f.getPath().endsWith( ".hgtags" ) )
 130  
                 {
 131  0
                     fileList.add( new ScmFile( f.getPath(), ScmFileStatus.TAGGED ) );
 132  
                 }
 133  
             }
 134  
 
 135  0
             return new TagScmResult( fileList, result );
 136  
         }
 137  
         else
 138  
         {
 139  0
             throw new ScmException( "Error while executing command " + cmd.toString() );
 140  
         }
 141  
     }
 142  
 
 143  
     private StringBuilder joinCmd( String[] cmd )
 144  
     {
 145  0
         StringBuilder result = new StringBuilder();
 146  0
         for ( int i = 0; i < cmd.length; i++ )
 147  
         {
 148  0
             String s = cmd[i];
 149  0
             result.append( s );
 150  0
             if ( i < cmd.length - 1 )
 151  
             {
 152  0
                 result.append( " " );
 153  
             }
 154  
         }
 155  0
         return result;
 156  
     }
 157  
 }