001package org.apache.maven.scm.provider.git.gitexe.command.untag;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 * http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import java.io.File;
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.StringUtils;
035import org.codehaus.plexus.util.cli.CommandLineUtils;
036import org.codehaus.plexus.util.cli.Commandline;
037
038/** {@inheritDoc} */
039public class GitUntagCommand
040    extends AbstractUntagCommand
041    implements GitCommand
042{
043
044    /** {@inheritDoc} */
045    public ScmResult executeUntagCommand( ScmProviderRepository repo, ScmFileSet fileSet,
046                                          ScmUntagParameters scmUntagParameters )
047        throws ScmException
048    {
049        String tag = scmUntagParameters.getTag();
050        if ( tag == null || StringUtils.isEmpty( tag.trim() ) )
051        {
052            throw new ScmException( "tag name must be specified" );
053        }
054
055        GitScmProviderRepository repository = (GitScmProviderRepository) repo;
056
057        CommandLineUtils.StringStreamConsumer stdout = new CommandLineUtils.StringStreamConsumer();
058        CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
059
060        int exitCode;
061
062        Commandline clTag = createCommandLine( repository, fileSet.getBasedir(), tag );
063
064        exitCode = GitCommandLineUtils.execute( clTag, stdout, stderr, getLogger() );
065        if ( exitCode != 0 )
066        {
067            return new UntagScmResult( clTag.toString(), "The git-tag command failed.", stderr.getOutput(), false );
068        }
069
070        if ( repo.isPushChanges() )
071        {
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, getLogger() );
076            if ( exitCode != 0 )
077            {
078                return new UntagScmResult( clPush.toString(), "The git-push command failed.", stderr.getOutput(),
079                                         false );
080            }
081        }
082
083        return new UntagScmResult( clTag.toString() );
084    }
085
086    // ----------------------------------------------------------------------
087    //
088    // ----------------------------------------------------------------------
089
090    public static Commandline createCommandLine( GitScmProviderRepository repository, File workingDirectory,
091                                                 String tag )
092    {
093        Commandline cl = GitCommandLineUtils.getBaseGitCommandLine( workingDirectory, "tag" );
094
095        cl.createArg().setValue( "-d" );
096        cl.createArg().setValue( tag );
097
098        return cl;
099    }
100
101    public static Commandline createPushCommandLine( GitScmProviderRepository repository, ScmFileSet fileSet,
102                                                     String tag )
103    {
104        Commandline cl = GitCommandLineUtils.getBaseGitCommandLine( fileSet.getBasedir(), "push" );
105
106        cl.createArg().setValue( "--delete" );
107        cl.createArg().setValue( repository.getPushUrl() );
108        cl.createArg().setValue( "refs/tags/" + tag );
109
110        return cl;
111    }
112
113}