View Javadoc
1   package org.apache.maven.scm.provider.integrity.command.blame;
2   
3   /**
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   * http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.scm.ScmException;
23  import org.apache.maven.scm.ScmFileSet;
24  import org.apache.maven.scm.ScmResult;
25  import org.apache.maven.scm.command.blame.AbstractBlameCommand;
26  import org.apache.maven.scm.command.blame.BlameScmResult;
27  import org.apache.maven.scm.provider.ScmProviderRepository;
28  import org.apache.maven.scm.provider.integrity.APISession;
29  import org.apache.maven.scm.provider.integrity.repository.IntegrityScmProviderRepository;
30  import org.codehaus.plexus.util.cli.CommandLineException;
31  import org.codehaus.plexus.util.cli.CommandLineUtils;
32  import org.codehaus.plexus.util.cli.Commandline;
33  
34  /**
35   * MKS Integrity implementation for Maven's AbstractBlameCommand
36   * <br>This class will execute a 'si annotate' command for the specified filename
37   *
38   * @author <a href="mailto:cletus@mks.com">Cletus D'Souza</a>
39   * @version $Id: IntegrityBlameCommand.java 1.3 2011/08/22 13:06:15EDT Cletus D'Souza (dsouza) Exp  $
40   * @since 1.6
41   */
42  public class IntegrityBlameCommand
43      extends AbstractBlameCommand
44  {
45      /**
46       * {@inheritDoc}
47       */
48      @Override
49      public BlameScmResult executeBlameCommand( ScmProviderRepository repository, ScmFileSet workingDirectory,
50                                                 String filename )
51          throws ScmException
52      {
53          getLogger().info( "Attempting to display blame results for file: " + filename );
54          if ( null == filename || filename.length() == 0 )
55          {
56              throw new ScmException( "A single filename is required to execute the blame command!" );
57          }
58          BlameScmResult result;
59          IntegrityScmProviderRepository iRepo = (IntegrityScmProviderRepository) repository;
60          APISession api = iRepo.getAPISession();
61          // Since the si annotate command is not completely API ready, we will use the CLI for this command
62          Commandline shell = new Commandline();
63          shell.setWorkingDirectory( workingDirectory.getBasedir() );
64          shell.setExecutable( "si" );
65          shell.createArg().setValue( "annotate" );
66          shell.createArg().setValue( "--hostname=" + api.getHostName() );
67          shell.createArg().setValue( "--port=" + api.getPort() );
68          shell.createArg().setValue( "--user=" + api.getUserName() );
69          shell.createArg().setValue( "--fields=date,revision,author" );
70          shell.createArg().setValue( '"' + filename + '"' );
71          IntegrityBlameConsumer shellConsumer = new IntegrityBlameConsumer( getLogger() );
72  
73          try
74          {
75              getLogger().debug( "Executing: " + shell.getCommandline() );
76              int exitCode = CommandLineUtils.executeCommandLine( shell, shellConsumer,
77                                                                  new CommandLineUtils.StringStreamConsumer() );
78              boolean success = ( exitCode == 128 ? false : true );
79              ScmResult scmResult =
80                  new ScmResult( shell.getCommandline().toString(), "", "Exit Code: " + exitCode, success );
81              return new BlameScmResult( shellConsumer.getBlameList(), scmResult );
82          }
83          catch ( CommandLineException cle )
84          {
85              getLogger().error( "Command Line Exception: " + cle.getMessage() );
86              result = new BlameScmResult( shell.getCommandline().toString(), cle.getMessage(), "", false );
87          }
88  
89          return result;
90      }
91  
92  }