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