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.add;
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.add.AbstractAddCommand;
029import org.apache.maven.scm.command.add.AddScmResult;
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 */
041public class SvnAddCommand extends AbstractAddCommand implements SvnCommand {
042    /**
043     * {@inheritDoc}
044     */
045    protected ScmResult executeAddCommand(
046            ScmProviderRepository repository, ScmFileSet fileSet, String message, boolean binary) throws ScmException {
047        if (fileSet.getFileList().isEmpty()) {
048            throw new ScmException("You must provide at least one file/directory to add");
049        }
050
051        Commandline cl = createCommandLine(fileSet.getBasedir(), fileSet.getFileList());
052
053        if (binary) {
054            cl.createArg().setValue("--config-option");
055            cl.createArg().setValue("config:miscellany:enable-auto-props=no");
056        }
057
058        SvnAddConsumer consumer = new SvnAddConsumer();
059
060        CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
061
062        if (logger.isInfoEnabled()) {
063            logger.info("Executing: " + SvnCommandLineUtils.cryptPassword(cl));
064
065            if (Os.isFamily(Os.FAMILY_WINDOWS)) {
066                logger.info("Working directory: " + cl.getWorkingDirectory().getAbsolutePath());
067            }
068        }
069
070        int exitCode;
071
072        try {
073            exitCode = SvnCommandLineUtils.execute(cl, consumer, stderr);
074        } catch (CommandLineException ex) {
075            throw new ScmException("Error while executing command.", ex);
076        }
077
078        if (exitCode != 0) {
079            return new AddScmResult(cl.toString(), "The svn command failed.", stderr.getOutput(), false);
080        }
081
082        return new AddScmResult(cl.toString(), consumer.getAddedFiles());
083    }
084
085    private static Commandline createCommandLine(File workingDirectory, List<File> files) throws ScmException {
086        // Base command line doesn't make sense here - username/password not needed, and non-interactive is not valid
087
088        Commandline cl = new Commandline();
089
090        cl.setExecutable("svn");
091
092        cl.setWorkingDirectory(workingDirectory.getAbsolutePath());
093
094        cl.createArg().setValue("add");
095
096        cl.createArg().setValue("--parents");
097
098        cl.createArg().setValue("--non-recursive");
099
100        try {
101            SvnCommandLineUtils.addTarget(cl, files);
102        } catch (IOException e) {
103            throw new ScmException("Can't create the targets file", e);
104        }
105
106        return cl;
107    }
108}