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, String filename )
042        throws ScmException
043    {
044        Commandline cl = createCommandLine( workingDirectory.getBasedir(), filename );
045
046        TfsBlameConsumer consumer = new TfsBlameConsumer( getLogger() );
047        CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
048
049        int exitCode;
050        try
051        {
052            exitCode = CommandLineUtils.executeCommandLine( cl, consumer, stderr );
053        }
054        catch ( CommandLineException ex )
055        {
056            throw new ScmException( "Error while executing command.", ex );
057        }
058        if ( exitCode != 0 )
059        {
060            return new BlameScmResult( cl.toString(), "The tfs command failed.", stderr.getOutput(), false );
061        }
062
063        return new BlameScmResult( cl.toString(), consumer.getLines() );
064    }
065
066    public static Commandline createCommandLine( File workingDirectory, String filename )
067    {
068        Commandline command = new Commandline();
069        command.setWorkingDirectory( workingDirectory );
070        command.setExecutable( "tfpt" );
071        command.createArg().setValue( "annotate" );
072        command.createArg().setValue( "/noprompt" );
073        command.createArg().setValue( filename );
074        return command;
075    }
076}