1 package org.apache.maven.scm.provider.clearcase.command.blame;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.scm.ScmException;
23 import org.apache.maven.scm.ScmFileSet;
24 import org.apache.maven.scm.command.blame.AbstractBlameCommand;
25 import org.apache.maven.scm.command.blame.BlameScmResult;
26 import org.apache.maven.scm.provider.ScmProviderRepository;
27 import org.apache.maven.scm.provider.clearcase.command.ClearCaseCommand;
28 import org.codehaus.plexus.util.cli.CommandLineException;
29 import org.codehaus.plexus.util.cli.CommandLineUtils;
30 import org.codehaus.plexus.util.cli.Commandline;
31
32 import java.io.File;
33
34
35
36
37
38 public class ClearCaseBlameCommand
39 extends AbstractBlameCommand
40 implements ClearCaseCommand
41 {
42
43 public BlameScmResult executeBlameCommand( ScmProviderRepository repo, ScmFileSet workingDirectory,
44 String filename )
45 throws ScmException
46 {
47
48 if ( getLogger().isDebugEnabled() )
49 {
50 getLogger().debug( "executing blame command..." );
51 }
52 Commandline cl = createCommandLine( workingDirectory.getBasedir(), filename );
53
54 ClearCaseBlameConsumer consumer = new ClearCaseBlameConsumer( getLogger() );
55
56 CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
57
58 int exitCode;
59
60 try
61 {
62 if ( getLogger().isDebugEnabled() )
63 {
64 getLogger().debug( "Executing: " + cl.getWorkingDirectory().getAbsolutePath() + ">>" + cl.toString() );
65 }
66 exitCode = CommandLineUtils.executeCommandLine( cl, consumer, stderr );
67 }
68 catch ( CommandLineException ex )
69 {
70 throw new ScmException( "Error while executing cvs command.", ex );
71 }
72
73 if ( exitCode != 0 )
74 {
75 return new BlameScmResult( cl.toString(), "The cleartool command failed.", stderr.getOutput(), false );
76 }
77
78 return new BlameScmResult( cl.toString(), consumer.getLines() );
79 }
80
81 public static Commandline createCommandLine( File workingDirectory, String filename )
82 {
83 Commandline command = new Commandline();
84 command.setExecutable( "cleartool" );
85 command.createArg().setValue( "annotate" );
86
87 command.setWorkingDirectory( workingDirectory.getAbsolutePath() );
88
89 StringBuilder format = new StringBuilder();
90 format.append( "VERSION:%Ln@@@" );
91 format.append( "USER:%u@@@" );
92 format.append( "DATE:%Nd@@@" );
93
94 command.createArg().setValue( "-out" );
95 command.createArg().setValue( "-" );
96 command.createArg().setValue( "-fmt" );
97 command.createArg().setValue( format.toString() );
98 command.createArg().setValue( "-nheader" );
99 command.createArg().setValue( "-f" );
100 command.createArg().setValue( filename );
101
102 return command;
103 }
104 }