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