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.git.gitexe.command.tag; 020 021import java.io.File; 022import java.io.IOException; 023 024import org.apache.commons.lang3.StringUtils; 025import org.apache.maven.scm.ScmException; 026import org.apache.maven.scm.ScmFileSet; 027import org.apache.maven.scm.ScmFileStatus; 028import org.apache.maven.scm.ScmResult; 029import org.apache.maven.scm.ScmTagParameters; 030import org.apache.maven.scm.command.checkout.CheckOutScmResult; 031import org.apache.maven.scm.command.tag.AbstractTagCommand; 032import org.apache.maven.scm.command.tag.TagScmResult; 033import org.apache.maven.scm.provider.ScmProviderRepository; 034import org.apache.maven.scm.provider.git.command.GitCommand; 035import org.apache.maven.scm.provider.git.gitexe.command.GitCommandLineUtils; 036import org.apache.maven.scm.provider.git.gitexe.command.list.GitListCommand; 037import org.apache.maven.scm.provider.git.gitexe.command.list.GitListConsumer; 038import org.apache.maven.scm.provider.git.repository.GitScmProviderRepository; 039import org.codehaus.plexus.util.FileUtils; 040import org.codehaus.plexus.util.cli.CommandLineUtils; 041import org.codehaus.plexus.util.cli.Commandline; 042 043/** 044 * @author <a href="mailto:struberg@yahoo.de">Mark Struberg</a> 045 * 046 */ 047public class GitTagCommand extends AbstractTagCommand implements GitCommand { 048 049 public ScmResult executeTagCommand(ScmProviderRepository repo, ScmFileSet fileSet, String tag, String message) 050 throws ScmException { 051 return executeTagCommand(repo, fileSet, tag, new ScmTagParameters(message)); 052 } 053 054 /** {@inheritDoc} */ 055 public ScmResult executeTagCommand( 056 ScmProviderRepository repo, ScmFileSet fileSet, String tag, ScmTagParameters scmTagParameters) 057 throws ScmException { 058 if (tag == null || StringUtils.isEmpty(tag.trim())) { 059 throw new ScmException("tag name must be specified"); 060 } 061 062 if (!fileSet.getFileList().isEmpty()) { 063 throw new ScmException("This provider doesn't support tagging subsets of a directory"); 064 } 065 066 GitScmProviderRepository repository = (GitScmProviderRepository) repo; 067 068 File messageFile = FileUtils.createTempFile("maven-scm-", ".commit", null); 069 070 try { 071 FileUtils.fileWrite(messageFile.getAbsolutePath(), "UTF-8", scmTagParameters.getMessage()); 072 } catch (IOException ex) { 073 return new TagScmResult( 074 null, 075 "Error while making a temporary file for the commit message: " + ex.getMessage(), 076 null, 077 false); 078 } 079 080 try { 081 CommandLineUtils.StringStreamConsumer stdout = new CommandLineUtils.StringStreamConsumer(); 082 CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer(); 083 084 int exitCode; 085 086 boolean sign = scmTagParameters.isSign(); 087 088 Commandline clTag = createCommandLine(repository, fileSet.getBasedir(), tag, messageFile, sign); 089 090 exitCode = GitCommandLineUtils.execute(clTag, stdout, stderr); 091 if (exitCode != 0) { 092 return new TagScmResult(clTag.toString(), "The git-tag command failed.", stderr.getOutput(), false); 093 } 094 095 if (repo.isPushChanges()) { 096 // and now push the tag to the configured upstream repository 097 Commandline clPush = createPushCommandLine(repository, fileSet, tag); 098 099 exitCode = GitCommandLineUtils.execute(clPush, stdout, stderr); 100 if (exitCode != 0) { 101 return new TagScmResult( 102 clPush.toString(), "The git-push command failed.", stderr.getOutput(), false); 103 } 104 } 105 106 // plus search for the tagged files 107 GitListConsumer listConsumer = new GitListConsumer(fileSet.getBasedir(), ScmFileStatus.TAGGED); 108 109 Commandline clList = GitListCommand.createCommandLine(repository, fileSet.getBasedir()); 110 111 exitCode = GitCommandLineUtils.execute(clList, listConsumer, stderr); 112 if (exitCode != 0) { 113 return new CheckOutScmResult( 114 clList.toString(), "The git-ls-files command failed.", stderr.getOutput(), false); 115 } 116 117 return new TagScmResult(clTag.toString(), listConsumer.getListedFiles()); 118 } finally { 119 try { 120 FileUtils.forceDelete(messageFile); 121 } catch (IOException ex) { 122 // ignore 123 } 124 } 125 } 126 127 // ---------------------------------------------------------------------- 128 // 129 // ---------------------------------------------------------------------- 130 131 public static Commandline createCommandLine( 132 GitScmProviderRepository repository, File workingDirectory, String tag, File messageFile, boolean sign) { 133 Commandline cl = GitCommandLineUtils.getBaseGitCommandLine(workingDirectory, "tag"); 134 135 if (sign) { 136 cl.createArg().setValue("-s"); 137 } 138 139 cl.createArg().setValue("-F"); 140 cl.createArg().setValue(messageFile.getAbsolutePath()); 141 142 // Note: this currently assumes you have the tag base checked out too 143 cl.createArg().setValue(tag); 144 145 return cl; 146 } 147 148 public static Commandline createPushCommandLine(GitScmProviderRepository repository, ScmFileSet fileSet, String tag) 149 throws ScmException { 150 Commandline cl = GitCommandLineUtils.getBaseGitCommandLine(fileSet.getBasedir(), "push"); 151 152 cl.createArg().setValue(repository.getPushUrl()); 153 cl.createArg().setValue("refs/tags/" + tag); 154 155 return cl; 156 } 157}