001    package org.apache.maven.scm.provider.svn.svnexe.command.diff;
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.diff.AbstractDiffCommand;
026    import org.apache.maven.scm.command.diff.DiffScmResult;
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.command.diff.SvnDiffConsumer;
030    import org.apache.maven.scm.provider.svn.repository.SvnScmProviderRepository;
031    import org.apache.maven.scm.provider.svn.svnexe.command.SvnCommandLineUtils;
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    
039    /**
040     * @author <a href="mailto:brett@apache.org">Brett Porter</a>
041     * @author Olivier Lamy
042     * @version $Id: SvnDiffCommand.java 1054754 2011-01-03 21:15:57Z olamy $
043     */
044    public class SvnDiffCommand
045        extends AbstractDiffCommand
046        implements SvnCommand
047    {
048        /** {@inheritDoc} */
049        protected DiffScmResult executeDiffCommand( ScmProviderRepository repo, ScmFileSet fileSet, ScmVersion startVersion,
050                                                    ScmVersion endVersion )
051            throws ScmException
052        {
053            Commandline cl =
054                createCommandLine( (SvnScmProviderRepository) repo, fileSet.getBasedir(), startVersion, endVersion );
055    
056            SvnDiffConsumer consumer = new SvnDiffConsumer( getLogger(), fileSet.getBasedir() );
057    
058            CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
059    
060            if ( getLogger().isInfoEnabled() )
061            {
062                getLogger().info( "Executing: " + SvnCommandLineUtils.cryptPassword( cl ) );
063                getLogger().info( "Working directory: " + cl.getWorkingDirectory().getAbsolutePath() );
064            }
065    
066            int exitCode;
067    
068            try
069            {
070                exitCode = SvnCommandLineUtils.execute( cl, consumer, stderr, getLogger() );
071            }
072            catch ( CommandLineException ex )
073            {
074                throw new ScmException( "Error while executing command.", ex );
075            }
076    
077            if ( exitCode != 0 )
078            {
079                return new DiffScmResult( cl.toString(), "The svn command failed.", stderr.getOutput(), false );
080            }
081    
082            return new DiffScmResult( cl.toString(), consumer.getChangedFiles(), consumer.getDifferences(),
083                                      consumer.getPatch() );
084        }
085    
086        // ----------------------------------------------------------------------
087        //
088        // ----------------------------------------------------------------------
089    
090        public static Commandline createCommandLine( SvnScmProviderRepository repository, File workingDirectory,
091                                                     ScmVersion startVersion, ScmVersion endVersion )
092        {
093            Commandline cl = SvnCommandLineUtils.getBaseSvnCommandLine( workingDirectory, repository );
094    
095            cl.createArg().setValue( "diff" );
096    
097            if ( startVersion != null && StringUtils.isNotEmpty( startVersion.getName() ) )
098            {
099                cl.createArg().setValue( "-r" );
100    
101                if ( endVersion != null && StringUtils.isNotEmpty( endVersion.getName() ) )
102                {
103                    cl.createArg().setValue( startVersion.getName() + ":" + endVersion.getName() );
104                }
105                else
106                {
107                    cl.createArg().setValue( startVersion.getName() );
108                }
109            }
110    
111            return cl;
112        }
113    }