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