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 */
043public class SvnDiffCommand extends AbstractDiffCommand implements SvnCommand {
044    private final boolean interactive;
045
046    public SvnDiffCommand(boolean interactive) {
047        this.interactive = interactive;
048    }
049
050    /**
051     * {@inheritDoc}
052     */
053    protected DiffScmResult executeDiffCommand(
054            ScmProviderRepository repo, ScmFileSet fileSet, ScmVersion startVersion, ScmVersion endVersion)
055            throws ScmException {
056        Commandline cl =
057                createCommandLine((SvnScmProviderRepository) repo, fileSet.getBasedir(), startVersion, endVersion);
058
059        SvnDiffConsumer consumer = new SvnDiffConsumer(fileSet.getBasedir());
060
061        CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
062
063        if (logger.isInfoEnabled()) {
064            logger.info("Executing: " + SvnCommandLineUtils.cryptPassword(cl));
065
066            if (Os.isFamily(Os.FAMILY_WINDOWS)) {
067                logger.info("Working directory: " + cl.getWorkingDirectory().getAbsolutePath());
068            }
069        }
070
071        int exitCode;
072
073        try {
074            exitCode = SvnCommandLineUtils.execute(cl, consumer, stderr);
075        } catch (CommandLineException ex) {
076            throw new ScmException("Error while executing command.", ex);
077        }
078
079        if (exitCode != 0) {
080            return new DiffScmResult(cl.toString(), "The svn command failed.", stderr.getOutput(), false);
081        }
082
083        return new DiffScmResult(
084                cl.toString(), consumer.getChangedFiles(), consumer.getDifferences(), consumer.getPatch());
085    }
086
087    // ----------------------------------------------------------------------
088    //
089    // ----------------------------------------------------------------------
090
091    public Commandline createCommandLine(
092            SvnScmProviderRepository repository,
093            File workingDirectory,
094            ScmVersion startVersion,
095            ScmVersion endVersion) {
096        Commandline cl = SvnCommandLineUtils.getBaseSvnCommandLine(workingDirectory, repository, interactive);
097
098        cl.createArg().setValue("diff");
099
100        cl.createArg().setValue("--internal-diff");
101
102        if (startVersion != null && StringUtils.isNotEmpty(startVersion.getName())) {
103            cl.createArg().setValue("-r");
104
105            if (endVersion != null && StringUtils.isNotEmpty(endVersion.getName())) {
106                cl.createArg().setValue(startVersion.getName() + ":" + endVersion.getName());
107            } else {
108                cl.createArg().setValue(startVersion.getName());
109            }
110        }
111
112        return cl;
113    }
114}