1 package org.apache.maven.scm.provider.integrity.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.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.repository.IntegrityScmProviderRepository;
29 import org.codehaus.plexus.util.cli.CommandLineException;
30 import org.codehaus.plexus.util.cli.CommandLineUtils;
31 import org.codehaus.plexus.util.cli.Commandline;
32
33
34
35
36
37
38
39
40 public class IntegrityBlameCommand
41 extends AbstractBlameCommand
42 {
43
44
45
46 @Override
47 public BlameScmResult executeBlameCommand( ScmProviderRepository repository, ScmFileSet workingDirectory,
48 String filename )
49 throws ScmException
50 {
51 getLogger().info( "Attempting to display blame results for file: " + filename );
52 if ( null == filename || filename.length() == 0 )
53 {
54 throw new ScmException( "A single filename is required to execute the blame command!" );
55 }
56 BlameScmResult result;
57 IntegrityScmProviderRepository iRepo = (IntegrityScmProviderRepository) repository;
58
59
60 doShellConnect( iRepo, workingDirectory );
61 result = doShellAnnotate( iRepo, workingDirectory, filename );
62
63 return result;
64 }
65
66
67
68
69
70
71
72
73 private void doShellConnect( IntegrityScmProviderRepository iRepo, ScmFileSet workingDirectory )
74 throws ScmException
75 {
76 Commandline shell = new Commandline();
77 shell.setWorkingDirectory( workingDirectory.getBasedir() );
78 shell.setExecutable( "si" );
79 shell.createArg().setValue( "connect" );
80 shell.createArg().setValue( "--hostname=" + iRepo.getHost() );
81 shell.createArg().setValue( "--port=" + iRepo.getPort() );
82 shell.createArg().setValue( "--user=" + iRepo.getUser() );
83 shell.createArg().setValue( "--batch" );
84 shell.createArg().setValue( "--password=" + iRepo.getPassword() );
85 CommandLineUtils.StringStreamConsumer shellConsumer = new CommandLineUtils.StringStreamConsumer();
86
87 try
88 {
89 getLogger().debug( "Executing: " + CommandLineUtils.toString( shell.getCommandline() ) );
90 int exitCode = CommandLineUtils.executeCommandLine( shell, shellConsumer, shellConsumer );
91 if ( exitCode != 0 )
92 {
93 throw new ScmException( "Can't login to integrity. Message : " + shellConsumer.toString() );
94 }
95 }
96 catch ( CommandLineException cle )
97 {
98 getLogger().error( "Command Line Connect Exception: " + cle.getMessage() );
99 throw new ScmException( "Can't login to integrity. Message : " + cle.getMessage() );
100 }
101
102 }
103
104
105
106
107
108
109
110
111
112 private BlameScmResult doShellAnnotate( IntegrityScmProviderRepository iRepo, ScmFileSet workingDirectory,
113 String filename )
114 {
115 BlameScmResult result;
116 Commandline shell = new Commandline();
117 shell.setWorkingDirectory( workingDirectory.getBasedir() );
118 shell.setExecutable( "si" );
119 shell.createArg().setValue( "annotate" );
120 shell.createArg().setValue( "--hostname=" + iRepo.getHost() );
121 shell.createArg().setValue( "--port=" + iRepo.getPort() );
122 shell.createArg().setValue( "--user=" + iRepo.getUser() );
123 shell.createArg().setValue( "--fields=date,revision,author" );
124 shell.createArg().setValue( '"' + filename + '"' );
125 IntegrityBlameConsumer shellConsumer = new IntegrityBlameConsumer( getLogger() );
126 String commandLine = CommandLineUtils.toString( shell.getCommandline() );
127
128 try
129 {
130 getLogger().debug( "Executing: " + commandLine );
131 int exitCode = CommandLineUtils.executeCommandLine( shell, shellConsumer,
132 new CommandLineUtils.StringStreamConsumer() );
133 boolean success = ( exitCode == 0 ? true : false );
134 ScmResult scmResult =
135 new ScmResult( commandLine, "", "Exit Code: " + exitCode, success );
136 return new BlameScmResult( shellConsumer.getBlameList(), scmResult );
137 }
138 catch ( CommandLineException cle )
139 {
140 getLogger().error( "Command Line Exception: " + cle.getMessage() );
141 result = new BlameScmResult( commandLine, cle.getMessage(), "", false );
142 }
143
144 return result;
145 }
146
147 }