001    package org.apache.maven.scm.provider.integrity.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.ScmResult;
025    import org.apache.maven.scm.command.blame.AbstractBlameCommand;
026    import org.apache.maven.scm.command.blame.BlameScmResult;
027    import org.apache.maven.scm.provider.ScmProviderRepository;
028    import org.apache.maven.scm.provider.integrity.APISession;
029    import org.apache.maven.scm.provider.integrity.repository.IntegrityScmProviderRepository;
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    /**
035     * MKS Integrity implementation for Maven's AbstractBlameCommand
036     * <br>This class will execute a 'si annotate' command for the specified filename
037     *
038     * @author <a href="mailto:cletus@mks.com">Cletus D'Souza</a>
039     * @version $Id: IntegrityBlameCommand.java 1.3 2011/08/22 13:06:15EDT Cletus D'Souza (dsouza) Exp  $
040     * @since 1.6
041     */
042    public class IntegrityBlameCommand
043        extends AbstractBlameCommand
044    {
045        /**
046         * {@inheritDoc}
047         */
048        @Override
049        public BlameScmResult executeBlameCommand( ScmProviderRepository repository, ScmFileSet workingDirectory,
050                                                   String filename )
051            throws ScmException
052        {
053            getLogger().info( "Attempting to display blame results for file: " + filename );
054            if ( null == filename || filename.length() == 0 )
055            {
056                throw new ScmException( "A single filename is required to execute the blame command!" );
057            }
058            BlameScmResult result;
059            IntegrityScmProviderRepository iRepo = (IntegrityScmProviderRepository) repository;
060            APISession api = iRepo.getAPISession();
061            // Since the si annotate command is not completely API ready, we will use the CLI for this command
062            Commandline shell = new Commandline();
063            shell.setWorkingDirectory( workingDirectory.getBasedir() );
064            shell.setExecutable( "si" );
065            shell.createArg().setValue( "annotate" );
066            shell.createArg().setValue( "--hostname=" + api.getHostName() );
067            shell.createArg().setValue( "--port=" + api.getPort() );
068            shell.createArg().setValue( "--user=" + api.getUserName() );
069            shell.createArg().setValue( "--fields=date,revision,author" );
070            shell.createArg().setValue( '"' + filename + '"' );
071            IntegrityBlameConsumer shellConsumer = new IntegrityBlameConsumer( getLogger() );
072    
073            try
074            {
075                getLogger().debug( "Executing: " + shell.getCommandline() );
076                int exitCode = CommandLineUtils.executeCommandLine( shell, shellConsumer,
077                                                                    new CommandLineUtils.StringStreamConsumer() );
078                boolean success = ( exitCode == 128 ? false : true );
079                ScmResult scmResult =
080                    new ScmResult( shell.getCommandline().toString(), "", "Exit Code: " + exitCode, success );
081                return new BlameScmResult( shellConsumer.getBlameList(), scmResult );
082            }
083            catch ( CommandLineException cle )
084            {
085                getLogger().error( "Command Line Exception: " + cle.getMessage() );
086                result = new BlameScmResult( shell.getCommandline().toString(), cle.getMessage(), "", false );
087            }
088    
089            return result;
090        }
091    
092    }