001package org.apache.maven.scm.provider.svn.svnexe.command.checkin;
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.ScmVersion;
025import org.apache.maven.scm.command.checkin.AbstractCheckInCommand;
026import org.apache.maven.scm.command.checkin.CheckInScmResult;
027import org.apache.maven.scm.provider.ScmProviderRepository;
028import org.apache.maven.scm.provider.svn.command.SvnCommand;
029import org.apache.maven.scm.provider.svn.repository.SvnScmProviderRepository;
030import org.apache.maven.scm.provider.svn.svnexe.command.SvnCommandLineUtils;
031import org.codehaus.plexus.util.FileUtils;
032import org.codehaus.plexus.util.Os;
033import org.codehaus.plexus.util.StringUtils;
034import org.codehaus.plexus.util.cli.CommandLineException;
035import org.codehaus.plexus.util.cli.CommandLineUtils;
036import org.codehaus.plexus.util.cli.Commandline;
037
038import java.io.File;
039import java.io.IOException;
040
041/**
042 * @author <a href="mailto:trygvis@inamo.no">Trygve Laugst&oslash;l</a>
043 * @author Olivier Lamy
044 *
045 */
046public class SvnCheckInCommand
047    extends AbstractCheckInCommand
048    implements SvnCommand
049{
050    /** {@inheritDoc} */
051    protected CheckInScmResult executeCheckInCommand( ScmProviderRepository repo, ScmFileSet fileSet, String message,
052                                                      ScmVersion version )
053        throws ScmException
054    {
055        if ( version != null && StringUtils.isNotEmpty( version.getName() ) )
056        {
057            throw new ScmException( "This provider command can't handle tags." );
058        }
059
060        File messageFile = FileUtils.createTempFile( "maven-scm-", ".commit", null );
061
062        try
063        {
064            FileUtils.fileWrite( messageFile.getAbsolutePath(), "UTF-8", message );
065        }
066        catch ( IOException ex )
067        {
068            return new CheckInScmResult( null, "Error while making a temporary file for the commit message: "
069                + ex.getMessage(), null, false );
070        }
071
072        Commandline cl = createCommandLine( (SvnScmProviderRepository) repo, fileSet, messageFile );
073
074        SvnCheckInConsumer consumer = new SvnCheckInConsumer( getLogger(), fileSet.getBasedir() );
075
076        CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
077
078        if ( getLogger().isInfoEnabled() )
079        {
080            getLogger().info( "Executing: " + SvnCommandLineUtils.cryptPassword( cl ) );
081
082            if ( Os.isFamily( Os.FAMILY_WINDOWS ) )
083            {
084                getLogger().info( "Working directory: " + cl.getWorkingDirectory().getAbsolutePath() );
085            }
086        }
087
088        int exitCode;
089
090        try
091        {
092            exitCode = SvnCommandLineUtils.execute( cl, consumer, stderr, getLogger() );
093        }
094        catch ( CommandLineException ex )
095        {
096            throw new ScmException( "Error while executing command.", ex );
097        }
098        finally
099        {
100            try
101            {
102                FileUtils.forceDelete( messageFile );
103            }
104            catch ( IOException ex )
105            {
106                // ignore
107            }
108        }
109
110        if ( exitCode != 0 )
111        {
112            return new CheckInScmResult( cl.toString(), "The svn command failed.", stderr.getOutput(), false );
113        }
114
115        return new CheckInScmResult( cl.toString(), consumer.getCheckedInFiles(),
116                                     Integer.toString( consumer.getRevision() ) );
117    }
118
119    // ----------------------------------------------------------------------
120    //
121    // ----------------------------------------------------------------------
122
123    public static Commandline createCommandLine( SvnScmProviderRepository repository, ScmFileSet fileSet,
124                                                 File messageFile )
125        throws ScmException
126    {
127        Commandline cl = SvnCommandLineUtils.getBaseSvnCommandLine( fileSet.getBasedir(), repository );
128
129        cl.createArg().setValue( "commit" );
130
131        cl.createArg().setValue( "--file" );
132
133        cl.createArg().setValue( messageFile.getAbsolutePath() );
134
135        cl.createArg().setValue( "--encoding" );
136
137        cl.createArg().setValue( "UTF-8" );
138
139        try
140        {
141            SvnCommandLineUtils.addTarget( cl, fileSet.getFileList() );
142        }
143        catch ( IOException e )
144        {
145            throw new ScmException( "Can't create the targets file", e );
146        }
147
148        return cl;
149    }
150}