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