001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.maven.scm.provider.svn.svnexe.command.diff;
020
021import java.io.File;
022
023import org.apache.commons.lang3.StringUtils;
024import org.apache.maven.scm.ScmException;
025import org.apache.maven.scm.ScmFileSet;
026import org.apache.maven.scm.ScmVersion;
027import org.apache.maven.scm.command.diff.AbstractDiffCommand;
028import org.apache.maven.scm.command.diff.DiffScmResult;
029import org.apache.maven.scm.provider.ScmProviderRepository;
030import org.apache.maven.scm.provider.svn.command.SvnCommand;
031import org.apache.maven.scm.provider.svn.command.diff.SvnDiffConsumer;
032import org.apache.maven.scm.provider.svn.repository.SvnScmProviderRepository;
033import org.apache.maven.scm.provider.svn.svnexe.command.SvnCommandLineUtils;
034import org.codehaus.plexus.util.Os;
035import org.codehaus.plexus.util.cli.CommandLineException;
036import org.codehaus.plexus.util.cli.CommandLineUtils;
037import org.codehaus.plexus.util.cli.Commandline;
038
039/**
040 * @author <a href="mailto:brett@apache.org">Brett Porter</a>
041 * @author Olivier Lamy
042 *
043 */
044public class SvnDiffCommand extends AbstractDiffCommand implements SvnCommand {
045    /** {@inheritDoc} */
046    protected DiffScmResult executeDiffCommand(
047            ScmProviderRepository repo, ScmFileSet fileSet, ScmVersion startVersion, ScmVersion endVersion)
048            throws ScmException {
049        Commandline cl =
050                createCommandLine((SvnScmProviderRepository) repo, fileSet.getBasedir(), startVersion, endVersion);
051
052        SvnDiffConsumer consumer = new SvnDiffConsumer(fileSet.getBasedir());
053
054        CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
055
056        if (logger.isInfoEnabled()) {
057            logger.info("Executing: " + SvnCommandLineUtils.cryptPassword(cl));
058
059            if (Os.isFamily(Os.FAMILY_WINDOWS)) {
060                logger.info("Working directory: " + cl.getWorkingDirectory().getAbsolutePath());
061            }
062        }
063
064        int exitCode;
065
066        try {
067            exitCode = SvnCommandLineUtils.execute(cl, consumer, stderr);
068        } catch (CommandLineException ex) {
069            throw new ScmException("Error while executing command.", ex);
070        }
071
072        if (exitCode != 0) {
073            return new DiffScmResult(cl.toString(), "The svn command failed.", stderr.getOutput(), false);
074        }
075
076        return new DiffScmResult(
077                cl.toString(), consumer.getChangedFiles(), consumer.getDifferences(), consumer.getPatch());
078    }
079
080    // ----------------------------------------------------------------------
081    //
082    // ----------------------------------------------------------------------
083
084    public static Commandline createCommandLine(
085            SvnScmProviderRepository repository,
086            File workingDirectory,
087            ScmVersion startVersion,
088            ScmVersion endVersion) {
089        Commandline cl = SvnCommandLineUtils.getBaseSvnCommandLine(workingDirectory, repository);
090
091        cl.createArg().setValue("diff");
092
093        cl.createArg().setValue("--internal-diff");
094
095        if (startVersion != null && StringUtils.isNotEmpty(startVersion.getName())) {
096            cl.createArg().setValue("-r");
097
098            if (endVersion != null && StringUtils.isNotEmpty(endVersion.getName())) {
099                cl.createArg().setValue(startVersion.getName() + ":" + endVersion.getName());
100            } else {
101                cl.createArg().setValue(startVersion.getName());
102            }
103        }
104
105        return cl;
106    }
107}