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