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