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.blame;
020
021import java.io.File;
022
023import org.apache.maven.scm.ScmException;
024import org.apache.maven.scm.ScmFileSet;
025import org.apache.maven.scm.command.blame.AbstractBlameCommand;
026import org.apache.maven.scm.command.blame.BlameScmResult;
027import org.apache.maven.scm.provider.ScmProviderRepository;
028import org.apache.maven.scm.provider.svn.command.SvnCommand;
029import org.apache.maven.scm.provider.svn.repository.SvnScmProviderRepository;
030import org.apache.maven.scm.provider.svn.svnexe.command.SvnCommandLineUtils;
031import org.codehaus.plexus.util.Os;
032import org.codehaus.plexus.util.cli.CommandLineException;
033import org.codehaus.plexus.util.cli.CommandLineUtils;
034import org.codehaus.plexus.util.cli.Commandline;
035
036/**
037 * @author Evgeny Mandrikov
038 * @author Olivier Lamy
039 * @since 1.4
040 */
041public class SvnBlameCommand extends AbstractBlameCommand implements SvnCommand {
042    private final boolean interactive;
043
044    public SvnBlameCommand(boolean interactive) {
045        this.interactive = interactive;
046    }
047
048    /**
049     * {@inheritDoc}
050     */
051    public BlameScmResult executeBlameCommand(ScmProviderRepository repo, ScmFileSet workingDirectory, String filename)
052            throws ScmException {
053        Commandline cl = createCommandLine((SvnScmProviderRepository) repo, workingDirectory.getBasedir(), filename);
054
055        SvnBlameConsumer consumer = new SvnBlameConsumer();
056
057        CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
058
059        if (logger.isInfoEnabled()) {
060            logger.info("Executing: " + SvnCommandLineUtils.cryptPassword(cl));
061
062            if (Os.isFamily(Os.FAMILY_WINDOWS)) {
063                logger.info("Working directory: " + cl.getWorkingDirectory().getAbsolutePath());
064            }
065        }
066
067        int exitCode;
068
069        try {
070            exitCode = SvnCommandLineUtils.execute(cl, consumer, stderr);
071        } catch (CommandLineException ex) {
072            throw new ScmException("Error while executing command.", ex);
073        }
074
075        if (exitCode != 0) {
076            return new BlameScmResult(cl.toString(), "The svn command failed.", stderr.getOutput(), false);
077        }
078
079        return new BlameScmResult(cl.toString(), consumer.getLines());
080    }
081
082    public Commandline createCommandLine(SvnScmProviderRepository repository, File workingDirectory, String filename) {
083        Commandline cl = SvnCommandLineUtils.getBaseSvnCommandLine(workingDirectory, repository, interactive);
084        cl.createArg().setValue("blame");
085        cl.createArg().setValue("--xml");
086        cl.createArg().setValue(filename);
087        return cl;
088    }
089}