001package org.apache.maven.scm.provider.svn.svnexe.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;
023import java.io.IOException;
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
051    /** {@inheritDoc} */
052    @Override
053    public ScmResult executeUntagCommand( ScmProviderRepository repo, ScmFileSet fileSet,
054            ScmUntagParameters scmUntagParameters ) throws ScmException
055    {
056        String tag = scmUntagParameters.getTag();
057        if ( tag == null || tag.trim().isEmpty() )
058        {
059            throw new ScmException( "tag must be specified" );
060        }
061
062        SvnScmProviderRepository repository = (SvnScmProviderRepository) repo;
063
064        File messageFile = FileUtils.createTempFile( "maven-scm-", ".commit", null );
065
066        String message = scmUntagParameters.getMessage();
067        try
068        {
069            FileUtils.fileWrite( messageFile.getAbsolutePath(), "UTF-8", message );
070        }
071        catch ( IOException ex )
072        {
073            return new UntagScmResult( null, "Error while making a temporary file for the commit message: "
074                + ex.getMessage(), null, false );
075        }
076
077        Commandline cl = createCommandline( repository, fileSet, tag, messageFile );
078
079        CommandLineUtils.StringStreamConsumer stdout = new CommandLineUtils.StringStreamConsumer();
080
081        CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
082
083        if ( getLogger().isInfoEnabled() )
084        {
085            getLogger().info( "Executing: " + SvnCommandLineUtils.cryptPassword( cl ) );
086
087            if ( Os.isFamily( Os.FAMILY_WINDOWS ) )
088            {
089                getLogger().info( "Working directory: " + cl.getWorkingDirectory().getAbsolutePath() );
090            }
091        }
092
093        int exitCode;
094
095        try
096        {
097            exitCode = SvnCommandLineUtils.execute( cl, stdout, stderr, getLogger() );
098        }
099        catch ( CommandLineException ex )
100        {
101            throw new ScmException( "Error while executing svn remove command.", ex );
102        }
103        finally
104        {
105            try
106            {
107                FileUtils.forceDelete( messageFile );
108            }
109            catch ( IOException ex )
110            {
111                // ignore
112            }
113        }
114
115        if ( exitCode == 0 )
116        {
117            return new UntagScmResult( cl.toString(), "The svn remove command was successful.",
118                    stderr.getOutput(), true );
119        }
120        else
121        {
122            return new UntagScmResult( cl.toString(), "The svn remove command failed.", stderr.getOutput(), false );
123        }
124
125    }
126
127    /**
128     * create command line from parameters
129     *
130     * @param repo        svn repo tu delete tag from
131     * @param fileSet     file set containing base dir
132     * @param tag         tag to delete
133     * @param messageFile file containing commit message
134     * @return            command line instance
135     */
136    Commandline createCommandline( SvnScmProviderRepository repo, ScmFileSet fileSet, String tag, File messageFile )
137    {
138        Commandline cl = SvnCommandLineUtils.getBaseSvnCommandLine( fileSet.getBasedir(), repo );
139
140        cl.createArg().setValue( "--file" );
141
142        cl.createArg().setValue( messageFile.getAbsolutePath() );
143
144        cl.createArg().setValue( "remove" );
145
146        String tagUrl = SvnTagBranchUtils.resolveTagUrl( repo, new ScmTag( tag ) );
147        tagUrl = SvnCommandUtils.fixUrl( tagUrl, repo.getUser() );
148        cl.createArg().setValue( tagUrl + "@" );
149
150        return cl;
151    }
152
153}