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