001package org.apache.maven.scm.provider.svn.svnexe.command.remove;
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 org.apache.maven.scm.ScmException;
023import org.apache.maven.scm.ScmFileSet;
024import org.apache.maven.scm.ScmResult;
025import org.apache.maven.scm.command.remove.AbstractRemoveCommand;
026import org.apache.maven.scm.command.remove.RemoveScmResult;
027import org.apache.maven.scm.provider.ScmProviderRepository;
028import org.apache.maven.scm.provider.svn.command.SvnCommand;
029import org.apache.maven.scm.provider.svn.svnexe.command.SvnCommandLineUtils;
030import org.codehaus.plexus.util.Os;
031import org.codehaus.plexus.util.cli.CommandLineException;
032import org.codehaus.plexus.util.cli.CommandLineUtils;
033import org.codehaus.plexus.util.cli.Commandline;
034
035import java.io.File;
036import java.io.IOException;
037import java.util.List;
038
039/**
040 * @author <a href="mailto:brett@apache.org">Brett Porter</a>
041 * @author Olivier Lamy
042 *
043 */
044public class SvnRemoveCommand
045    extends AbstractRemoveCommand
046    implements SvnCommand
047{
048    /** {@inheritDoc} */
049    protected ScmResult executeRemoveCommand( ScmProviderRepository repository, ScmFileSet fileSet, String message )
050        throws ScmException
051    {
052        if ( fileSet.getFileList().isEmpty() )
053        {
054            throw new ScmException( "You must provide at least one file/directory to remove" );
055        }
056
057        Commandline cl = createCommandLine( fileSet.getBasedir(), fileSet.getFileList() );
058
059        SvnRemoveConsumer consumer = new SvnRemoveConsumer( getLogger() );
060
061        CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
062
063        if ( getLogger().isInfoEnabled() )
064        {
065            getLogger().info( "Executing: " + SvnCommandLineUtils.cryptPassword( cl ) );
066
067            if ( Os.isFamily( Os.FAMILY_WINDOWS ) )
068            {
069                getLogger().info( "Working directory: " + cl.getWorkingDirectory().getAbsolutePath() );
070            }
071        }
072
073        int exitCode;
074
075        try
076        {
077            exitCode = SvnCommandLineUtils.execute( cl, consumer, stderr, getLogger() );
078        }
079        catch ( CommandLineException ex )
080        {
081            throw new ScmException( "Error while executing command.", ex );
082        }
083
084        if ( exitCode != 0 )
085        {
086            return new RemoveScmResult( cl.toString(), "The svn command failed.", stderr.getOutput(), false );
087        }
088
089        return new RemoveScmResult( cl.toString(), consumer.getRemovedFiles() );
090    }
091
092    private static Commandline createCommandLine( File workingDirectory, List<File> files )
093        throws ScmException
094    {
095        // Base command line doesn't make sense here - username/password not needed, and non-interactive/non-recusive is
096        // not valid
097
098        Commandline cl = new Commandline();
099
100        cl.setExecutable( "svn" );
101
102        cl.setWorkingDirectory( workingDirectory.getAbsolutePath() );
103
104        cl.createArg().setValue( "remove" );
105
106        try
107        {
108            SvnCommandLineUtils.addTarget( cl, files );
109        }
110        catch ( IOException e )
111        {
112            throw new ScmException( "Can't create the targets file", e );
113        }
114
115        return cl;
116    }
117
118}