001package org.apache.maven.scm.provider.tfs.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.codehaus.plexus.util.cli.CommandLineException;
028import org.codehaus.plexus.util.cli.CommandLineUtils;
029import org.codehaus.plexus.util.cli.Commandline;
030
031import java.io.File;
032
033/**
034 * @author Evgeny Mandrikov
035 * @author Olivier Lamy
036 * @since 1.4
037 */
038public class TfsBlameCommand
039    extends AbstractBlameCommand
040{
041    public BlameScmResult executeBlameCommand( ScmProviderRepository repo, ScmFileSet workingDirectory,
042                                               String filename )
043        throws ScmException
044    {
045        Commandline cl = createCommandLine( workingDirectory.getBasedir(), filename );
046
047        TfsBlameConsumer consumer = new TfsBlameConsumer( getLogger() );
048        CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
049
050        int exitCode;
051        try
052        {
053            exitCode = CommandLineUtils.executeCommandLine( cl, consumer, stderr );
054        }
055        catch ( CommandLineException ex )
056        {
057            throw new ScmException( "Error while executing command.", ex );
058        }
059        if ( exitCode != 0 )
060        {
061            return new BlameScmResult( cl.toString(), "The tfs command failed.", stderr.getOutput(), false );
062        }
063
064        return new BlameScmResult( cl.toString(), consumer.getLines() );
065    }
066
067    public static Commandline createCommandLine( File workingDirectory, String filename )
068    {
069        Commandline command = new Commandline();
070        command.setWorkingDirectory( workingDirectory );
071        command.setExecutable( "tfpt" );
072        command.createArg().setValue( "annotate" );
073        command.createArg().setValue( "/noprompt" );
074        command.createArg().setValue( filename );
075        return command;
076    }
077}