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