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