001package org.apache.maven.scm.provider.hg.command.tag; 002 003/* 004 * Licensed to the Apache Software Foundation (ASF) under one 005 * or more contributor license agreements. See the NOTICE file 006 * distributed with this work for additional information 007 * regarding copyright ownership. The ASF licenses this file 008 * to you under the Apache License, Version 2.0 (the 009 * "License"); you may not use this file except in compliance 010 * with the License. You may obtain a copy of the License at 011 * 012 * http://www.apache.org/licenses/LICENSE-2.0 013 * 014 * Unless required by applicable law or agreed to in writing, 015 * software distributed under the License is distributed on an 016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 017 * KIND, either express or implied. See the License for the 018 * specific language governing permissions and limitations 019 * under the License. 020 */ 021 022import java.io.File; 023import java.util.ArrayList; 024import java.util.List; 025 026import org.apache.maven.scm.ScmException; 027import org.apache.maven.scm.ScmFile; 028import org.apache.maven.scm.ScmFileSet; 029import org.apache.maven.scm.ScmFileStatus; 030import org.apache.maven.scm.ScmResult; 031import org.apache.maven.scm.ScmTagParameters; 032import org.apache.maven.scm.command.Command; 033import org.apache.maven.scm.command.tag.AbstractTagCommand; 034import org.apache.maven.scm.command.tag.TagScmResult; 035import org.apache.maven.scm.provider.ScmProviderRepository; 036import org.apache.maven.scm.provider.hg.HgUtils; 037import org.apache.maven.scm.provider.hg.command.HgCommandConstants; 038import org.apache.maven.scm.provider.hg.command.HgConsumer; 039import org.apache.maven.scm.provider.hg.command.inventory.HgListConsumer; 040import org.apache.maven.scm.provider.hg.repository.HgScmProviderRepository; 041import org.codehaus.plexus.util.StringUtils; 042 043/** 044 * Tag 045 * 046 * @author <a href="mailto:ryan@darksleep.com">ryan daum</a> 047 * @author Olivier Lamy 048 * 049 */ 050public class HgTagCommand 051 extends AbstractTagCommand 052 implements Command 053{ 054 055 protected ScmResult executeTagCommand( ScmProviderRepository scmProviderRepository, ScmFileSet fileSet, String tag, 056 String message ) 057 throws ScmException 058 { 059 return executeTagCommand( scmProviderRepository, fileSet, tag, new ScmTagParameters( message ) ); 060 } 061 062 /** 063 * {@inheritDoc} 064 */ 065 protected ScmResult executeTagCommand( ScmProviderRepository scmProviderRepository, ScmFileSet fileSet, String tag, 066 ScmTagParameters scmTagParameters ) 067 throws ScmException 068 { 069 070 if ( tag == null || StringUtils.isEmpty( tag.trim() ) ) 071 { 072 throw new ScmException( "tag must be specified" ); 073 } 074 075 if ( !fileSet.getFileList().isEmpty() ) 076 { 077 throw new ScmException( "This provider doesn't support tagging subsets of a directory : " 078 + fileSet.getFileList() ); 079 } 080 081 File workingDir = fileSet.getBasedir(); 082 083 // build the command 084 String[] tagCmd = 085 new String[]{ HgCommandConstants.TAG_CMD, HgCommandConstants.MESSAGE_OPTION, scmTagParameters.getMessage(), 086 tag }; 087 088 // keep the command about in string form for reporting 089 StringBuilder cmd = joinCmd( tagCmd ); 090 HgTagConsumer consumer = new HgTagConsumer( getLogger() ); 091 ScmResult result = HgUtils.execute( consumer, getLogger(), workingDir, tagCmd ); 092 HgScmProviderRepository repository = (HgScmProviderRepository) scmProviderRepository; 093 if ( result.isSuccess() ) 094 { 095 // now push 096 // Push to parent branch if any 097 098 if ( repository.isPushChanges() ) 099 { 100 if ( !repository.getURI().equals( fileSet.getBasedir().getAbsolutePath() ) ) 101 { 102 String branchName = HgUtils.getCurrentBranchName( getLogger(), workingDir ); 103 boolean differentOutgoingBranch = 104 HgUtils.differentOutgoingBranchFound( getLogger(), workingDir, branchName ); 105 106 String[] pushCmd = new String[]{ HgCommandConstants.PUSH_CMD, 107 differentOutgoingBranch ? HgCommandConstants.REVISION_OPTION + branchName : null, 108 repository.getURI() }; 109 110 result = 111 HgUtils.execute( new HgConsumer( getLogger() ), getLogger(), fileSet.getBasedir(), pushCmd ); 112 } 113 } 114 } 115 else 116 { 117 throw new ScmException( "Error while executing command " + cmd.toString() ); 118 } 119 120 // do an inventory to return the files tagged (all of them) 121 String[] listCmd = new String[]{ HgCommandConstants.INVENTORY_CMD }; 122 HgListConsumer listconsumer = new HgListConsumer( getLogger() ); 123 result = HgUtils.execute( listconsumer, getLogger(), fileSet.getBasedir(), listCmd ); 124 if ( result.isSuccess() ) 125 { 126 List<ScmFile> files = listconsumer.getFiles(); 127 List<ScmFile> fileList = new ArrayList<ScmFile>(); 128 for ( ScmFile f : files ) 129 { 130 if ( !f.getPath().endsWith( ".hgtags" ) ) 131 { 132 fileList.add( new ScmFile( f.getPath(), ScmFileStatus.TAGGED ) ); 133 } 134 } 135 136 return new TagScmResult( fileList, result ); 137 } 138 else 139 { 140 throw new ScmException( "Error while executing command " + cmd.toString() ); 141 } 142 } 143 144 private StringBuilder joinCmd( String[] cmd ) 145 { 146 StringBuilder result = new StringBuilder(); 147 for ( int i = 0; i < cmd.length; i++ ) 148 { 149 String s = cmd[i]; 150 result.append( s ); 151 if ( i < cmd.length - 1 ) 152 { 153 result.append( " " ); 154 } 155 } 156 return result; 157 } 158}