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