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.untag;
020
021import java.io.File;
022import java.util.Map;
023
024import org.apache.maven.scm.ScmException;
025import org.apache.maven.scm.ScmFileSet;
026import org.apache.maven.scm.ScmResult;
027import org.apache.maven.scm.ScmUntagParameters;
028import org.apache.maven.scm.command.untag.AbstractUntagCommand;
029import org.apache.maven.scm.command.untag.UntagScmResult;
030import org.apache.maven.scm.provider.ScmProviderRepository;
031import org.apache.maven.scm.provider.git.command.GitCommand;
032import org.apache.maven.scm.provider.git.gitexe.command.GitCommandLineUtils;
033import org.apache.maven.scm.provider.git.repository.GitScmProviderRepository;
034import org.codehaus.plexus.util.cli.CommandLineUtils;
035import org.codehaus.plexus.util.cli.Commandline;
036
037/**
038 * {@inheritDoc}
039 */
040public class GitUntagCommand extends AbstractUntagCommand implements GitCommand {
041    private final Map<String, String> environmentVariables;
042
043    public GitUntagCommand(Map<String, String> environmentVariables) {
044        this.environmentVariables = environmentVariables;
045    }
046
047    /**
048     * {@inheritDoc}
049     */
050    public ScmResult executeUntagCommand(
051            ScmProviderRepository repo, ScmFileSet fileSet, ScmUntagParameters scmUntagParameters) throws ScmException {
052        String tag = scmUntagParameters.getTag();
053        if (tag == null || tag.trim().isEmpty()) {
054            throw new ScmException("tag name must be specified");
055        }
056
057        GitScmProviderRepository repository = (GitScmProviderRepository) repo;
058
059        CommandLineUtils.StringStreamConsumer stdout = new CommandLineUtils.StringStreamConsumer();
060        CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
061
062        int exitCode;
063
064        Commandline clTag = createCommandLine(repository, fileSet.getBasedir(), tag);
065
066        exitCode = GitCommandLineUtils.execute(clTag, stdout, stderr);
067        if (exitCode != 0) {
068            return new UntagScmResult(clTag.toString(), "The git-tag command failed.", stderr.getOutput(), false);
069        }
070
071        if (repo.isPushChanges()) {
072            // and now push the tag to the configured upstream repository
073            Commandline clPush = createPushCommandLine(repository, fileSet, tag);
074
075            exitCode = GitCommandLineUtils.execute(clPush, stdout, stderr);
076            if (exitCode != 0) {
077                return new UntagScmResult(clPush.toString(), "The git-push command failed.", stderr.getOutput(), false);
078            }
079        }
080
081        return new UntagScmResult(clTag.toString());
082    }
083
084    // ----------------------------------------------------------------------
085    //
086    // ----------------------------------------------------------------------
087
088    public static Commandline createCommandLine(
089            GitScmProviderRepository repository, File workingDirectory, String tag) {
090        Commandline cl = GitCommandLineUtils.getBaseGitCommandLine(workingDirectory, "tag");
091
092        cl.createArg().setValue("-d");
093        cl.createArg().setValue(tag);
094
095        return cl;
096    }
097
098    public Commandline createPushCommandLine(GitScmProviderRepository repository, ScmFileSet fileSet, String tag) {
099        Commandline cl = GitCommandLineUtils.getBaseGitCommandLine(
100                fileSet.getBasedir(), "push", repository, environmentVariables);
101
102        cl.createArg().setValue("--delete");
103        cl.createArg().setValue(repository.getPushUrl());
104        cl.createArg().setValue("refs/tags/" + tag);
105
106        return cl;
107    }
108}