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