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