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    /**
043     * {@inheritDoc}
044     */
045    public BlameScmResult executeBlameCommand(ScmProviderRepository repo, ScmFileSet workingDirectory, String filename)
046            throws ScmException {
047        Commandline cl = createCommandLine((SvnScmProviderRepository) repo, workingDirectory.getBasedir(), filename);
048
049        SvnBlameConsumer consumer = new SvnBlameConsumer();
050
051        CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
052
053        if (logger.isInfoEnabled()) {
054            logger.info("Executing: " + SvnCommandLineUtils.cryptPassword(cl));
055
056            if (Os.isFamily(Os.FAMILY_WINDOWS)) {
057                logger.info("Working directory: " + cl.getWorkingDirectory().getAbsolutePath());
058            }
059        }
060
061        int exitCode;
062
063        try {
064            exitCode = SvnCommandLineUtils.execute(cl, consumer, stderr);
065        } catch (CommandLineException ex) {
066            throw new ScmException("Error while executing command.", ex);
067        }
068
069        if (exitCode != 0) {
070            return new BlameScmResult(cl.toString(), "The svn command failed.", stderr.getOutput(), false);
071        }
072
073        return new BlameScmResult(cl.toString(), consumer.getLines());
074    }
075
076    public static Commandline createCommandLine(
077            SvnScmProviderRepository repository, File workingDirectory, String filename) {
078        Commandline cl = SvnCommandLineUtils.getBaseSvnCommandLine(workingDirectory, repository);
079        cl.createArg().setValue("blame");
080        cl.createArg().setValue("--xml");
081        cl.createArg().setValue(filename);
082        return cl;
083    }
084}