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.svn.svnexe.command.untag;
020
021import java.io.File;
022import java.io.IOException;
023
024import org.apache.maven.scm.ScmException;
025import org.apache.maven.scm.ScmFileSet;
026import org.apache.maven.scm.ScmResult;
027import org.apache.maven.scm.ScmTag;
028import org.apache.maven.scm.ScmUntagParameters;
029import org.apache.maven.scm.command.untag.AbstractUntagCommand;
030import org.apache.maven.scm.command.untag.UntagScmResult;
031import org.apache.maven.scm.provider.ScmProviderRepository;
032import org.apache.maven.scm.provider.svn.SvnCommandUtils;
033import org.apache.maven.scm.provider.svn.SvnTagBranchUtils;
034import org.apache.maven.scm.provider.svn.command.SvnCommand;
035import org.apache.maven.scm.provider.svn.repository.SvnScmProviderRepository;
036import org.apache.maven.scm.provider.svn.svnexe.command.SvnCommandLineUtils;
037import org.codehaus.plexus.util.FileUtils;
038import org.codehaus.plexus.util.Os;
039import org.codehaus.plexus.util.cli.CommandLineException;
040import org.codehaus.plexus.util.cli.CommandLineUtils;
041import org.codehaus.plexus.util.cli.Commandline;
042
043/**
044 * Scm:untag for provider svn is done by removing the tag dir.
045 *
046 * @since 1.11.2
047 */
048public class SvnUntagCommand extends AbstractUntagCommand implements SvnCommand {
049
050    private final boolean interactive;
051
052    public SvnUntagCommand(boolean interactive) {
053        this.interactive = interactive;
054    }
055
056    /**
057     * {@inheritDoc}
058     */
059    @Override
060    public ScmResult executeUntagCommand(
061            ScmProviderRepository repo, ScmFileSet fileSet, ScmUntagParameters scmUntagParameters) throws ScmException {
062        String tag = scmUntagParameters.getTag();
063        if (tag == null || tag.trim().isEmpty()) {
064            throw new ScmException("tag must be specified");
065        }
066
067        SvnScmProviderRepository repository = (SvnScmProviderRepository) repo;
068
069        File messageFile = FileUtils.createTempFile("maven-scm-", ".commit", null);
070
071        String message = scmUntagParameters.getMessage();
072        try {
073            FileUtils.fileWrite(messageFile.getAbsolutePath(), "UTF-8", message);
074        } catch (IOException ex) {
075            return new UntagScmResult(
076                    null,
077                    "Error while making a temporary file for the commit message: " + ex.getMessage(),
078                    null,
079                    false);
080        }
081
082        Commandline cl = createCommandline(repository, fileSet, tag, messageFile);
083
084        CommandLineUtils.StringStreamConsumer stdout = new CommandLineUtils.StringStreamConsumer();
085
086        CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
087
088        if (logger.isDebugEnabled()) {
089            logger.debug("Executing: " + SvnCommandLineUtils.cryptPassword(cl));
090
091            if (Os.isFamily(Os.FAMILY_WINDOWS)) {
092                logger.debug("Working directory: " + cl.getWorkingDirectory().getAbsolutePath());
093            }
094        }
095
096        int exitCode;
097
098        try {
099            exitCode = SvnCommandLineUtils.execute(cl, stdout, stderr);
100        } catch (CommandLineException ex) {
101            throw new ScmException("Error while executing svn remove command.", ex);
102        } finally {
103            try {
104                FileUtils.forceDelete(messageFile);
105            } catch (IOException ex) {
106                // ignore
107            }
108        }
109
110        if (exitCode == 0) {
111            return new UntagScmResult(
112                    cl.toString(), "The svn remove command was successful.", stderr.getOutput(), true);
113        } else {
114            return new UntagScmResult(cl.toString(), "The svn remove command failed.", stderr.getOutput(), false);
115        }
116    }
117
118    /**
119     * Create command line from parameters.
120     *
121     * @param repo        svn repo tu delete tag from
122     * @param fileSet     file set containing base dir
123     * @param tag         tag to delete
124     * @param messageFile file containing commit message
125     * @return  command line instance
126     */
127    Commandline createCommandline(SvnScmProviderRepository repo, ScmFileSet fileSet, String tag, File messageFile) {
128        Commandline cl = SvnCommandLineUtils.getBaseSvnCommandLine(fileSet.getBasedir(), repo, interactive);
129
130        cl.createArg().setValue("--file");
131
132        cl.createArg().setValue(messageFile.getAbsolutePath());
133
134        cl.createArg().setValue("remove");
135
136        String tagUrl = SvnTagBranchUtils.resolveTagUrl(repo, new ScmTag(tag));
137        tagUrl = SvnCommandUtils.fixUrl(tagUrl, repo.getUser());
138        cl.createArg().setValue(tagUrl + "@");
139
140        return cl;
141    }
142}