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