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