001package org.apache.maven.scm.provider.git.jgit.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.util.Collection;
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.log.ScmLogger;
031import org.apache.maven.scm.provider.ScmProviderRepository;
032import org.apache.maven.scm.provider.git.command.GitCommand;
033import org.apache.maven.scm.provider.git.jgit.command.JGitUtils;
034import org.apache.maven.scm.provider.git.repository.GitScmProviderRepository;
035import org.codehaus.plexus.util.StringUtils;
036import org.eclipse.jgit.api.Git;
037import org.eclipse.jgit.lib.Constants;
038import org.eclipse.jgit.transport.PushResult;
039import org.eclipse.jgit.transport.RefSpec;
040import org.eclipse.jgit.transport.RemoteRefUpdate;
041
042/** {@inheritDoc} */
043public class JGitUntagCommand extends AbstractUntagCommand implements GitCommand
044{
045
046    @Override
047    protected ScmResult executeUntagCommand( ScmProviderRepository repository, ScmFileSet fileSet,
048                                             ScmUntagParameters scmUntagParameters )
049        throws ScmException
050    {
051        String tagName = scmUntagParameters.getTag();
052        if ( tagName == null || StringUtils.isEmpty( tagName.trim() ) )
053        {
054            throw new ScmException( "tag name must be specified" );
055        }
056        String escapedTagName = tagName.trim().replace( ' ', '_' );
057
058        Git git = null;
059        try
060        {
061            git = JGitUtils.openRepo( fileSet.getBasedir() );
062
063            // delete the tag
064            if ( git.tagDelete().setTags( escapedTagName ).call().isEmpty() )
065            {
066                return new UntagScmResult( "JGit tagDelete", "Failed to delete tag", "", false );
067            }
068
069            if ( repository.isPushChanges() )
070            {
071                // From https://stackoverflow.com/q/11892766/696632
072                RefSpec refSpec = new RefSpec()
073                    .setSource( null )
074                    .setDestination( Constants.R_TAGS + escapedTagName );
075
076                getLogger().info( "push delete tag [" + escapedTagName + "] to remote..." );
077                ScmLogger logger = getLogger();
078
079                Iterable<PushResult> pushResultList
080                        = JGitUtils.push( logger, git, (GitScmProviderRepository) repository, refSpec );
081                if ( logger.isInfoEnabled() )
082                {
083                    for ( PushResult pushResult : pushResultList )
084                    {
085                        Collection<RemoteRefUpdate> ru = pushResult.getRemoteUpdates();
086                        for ( RemoteRefUpdate remoteRefUpdate : ru )
087                        {
088                            logger.info( remoteRefUpdate.getStatus() + " - " + remoteRefUpdate );
089                        }
090                    }
091                }
092            }
093
094            return new UntagScmResult( "JGit tagDelete" );
095        }
096        catch ( Exception e )
097        {
098            throw new ScmException( "JGit tagDelete failure!", e );
099        }
100        finally
101        {
102            JGitUtils.closeRepo( git );
103        }
104    }
105}