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