001package org.apache.maven.scm.provider.clearcase.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.clearcase.command.ClearCaseCommand;
028import org.codehaus.plexus.util.cli.CommandLineException;
029import org.codehaus.plexus.util.cli.CommandLineUtils;
030import org.codehaus.plexus.util.cli.Commandline;
031
032import java.io.File;
033
034/**
035 * @author Jérémie Lagarde
036 * @since 1.4
037 */
038public class ClearCaseBlameCommand
039    extends AbstractBlameCommand
040    implements ClearCaseCommand
041{
042
043    public BlameScmResult executeBlameCommand( ScmProviderRepository repo, ScmFileSet workingDirectory,
044                                               String filename )
045        throws ScmException
046    {
047
048        if ( getLogger().isDebugEnabled() )
049        {
050            getLogger().debug( "executing blame command..." );
051        }
052        Commandline cl = createCommandLine( workingDirectory.getBasedir(), filename );
053
054        ClearCaseBlameConsumer consumer = new ClearCaseBlameConsumer( getLogger() );
055
056        CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
057
058        int exitCode;
059
060        try
061        {
062            if ( getLogger().isDebugEnabled() )
063            {
064                getLogger().debug( "Executing: " + cl.getWorkingDirectory().getAbsolutePath() + ">>" + cl.toString() );
065            }
066            exitCode = CommandLineUtils.executeCommandLine( cl, consumer, stderr );
067        }
068        catch ( CommandLineException ex )
069        {
070            throw new ScmException( "Error while executing cvs command.", ex );
071        }
072
073        if ( exitCode != 0 )
074        {
075            return new BlameScmResult( cl.toString(), "The cleartool command failed.", stderr.getOutput(), false );
076        }
077
078        return new BlameScmResult( cl.toString(), consumer.getLines() );
079    }
080
081    public static Commandline createCommandLine( File workingDirectory, String filename )
082    {
083        Commandline command = new Commandline();
084        command.setExecutable( "cleartool" );
085        command.createArg().setValue( "annotate" );
086
087        command.setWorkingDirectory( workingDirectory.getAbsolutePath() );
088
089        StringBuilder format = new StringBuilder();
090        format.append( "VERSION:%Ln@@@" );
091        format.append( "USER:%u@@@" );
092        format.append( "DATE:%Nd@@@" );
093
094        command.createArg().setValue( "-out" );
095        command.createArg().setValue( "-" );
096        command.createArg().setValue( "-fmt" );
097        command.createArg().setValue( format.toString() );
098        command.createArg().setValue( "-nheader" );
099        command.createArg().setValue( "-f" );
100        command.createArg().setValue( filename );
101
102        return command;
103    }
104}