1 package org.apache.maven.scm.provider.starteam.command.diff;
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.ScmVersion;
25 import org.apache.maven.scm.command.diff.AbstractDiffCommand;
26 import org.apache.maven.scm.command.diff.DiffScmResult;
27 import org.apache.maven.scm.provider.ScmProviderRepository;
28 import org.apache.maven.scm.provider.starteam.command.StarteamCommand;
29 import org.apache.maven.scm.provider.starteam.command.StarteamCommandLineUtils;
30 import org.apache.maven.scm.provider.starteam.repository.StarteamScmProviderRepository;
31 import org.codehaus.plexus.util.StringUtils;
32 import org.codehaus.plexus.util.cli.CommandLineUtils;
33 import org.codehaus.plexus.util.cli.Commandline;
34
35 import java.util.ArrayList;
36 import java.util.List;
37
38
39
40
41
42 public class StarteamDiffCommand
43 extends AbstractDiffCommand
44 implements StarteamCommand
45 {
46
47
48
49
50
51 protected DiffScmResult executeDiffCommand( ScmProviderRepository repo, ScmFileSet fileSet, ScmVersion startVersion,
52 ScmVersion endVersion )
53 throws ScmException
54 {
55 if ( getLogger().isInfoEnabled() )
56 {
57 getLogger().info( "Working directory: " + fileSet.getBasedir().getAbsolutePath() );
58 }
59
60 if ( fileSet.getFileList().isEmpty() )
61 {
62 throw new ScmException( "This provider doesn't support diff command on a subsets of a directory" );
63 }
64
65 StarteamScmProviderRepository repository = (StarteamScmProviderRepository) repo;
66
67 StarteamDiffConsumer consumer = new StarteamDiffConsumer( getLogger(), fileSet.getBasedir() );
68
69 CommandLineUtils.StringStreamConsumer stderr = new CommandLineUtils.StringStreamConsumer();
70
71 Commandline cl = createCommandLine( repository, fileSet, startVersion, endVersion );
72
73 int exitCode = StarteamCommandLineUtils.executeCommandline( cl, consumer, stderr, getLogger() );
74
75 if ( exitCode != 0 )
76 {
77 return new DiffScmResult( cl.toString(), "The starteam command failed.", stderr.getOutput(), false );
78 }
79
80 return new DiffScmResult( cl.toString(), consumer.getChangedFiles(), consumer.getDifferences(),
81 consumer.getPatch() );
82 }
83
84
85
86
87
88 public static Commandline createCommandLine( StarteamScmProviderRepository repo, ScmFileSet workingDirectory,
89 ScmVersion startLabel, ScmVersion endLabel )
90 throws ScmException
91 {
92
93 List<String> args = new ArrayList<String>();
94
95 args.add( "-filter" );
96 args.add( "M" );
97
98 if ( startLabel != null && StringUtils.isNotEmpty( startLabel.getName() ) )
99 {
100 args.add( "-vl" );
101
102 args.add( startLabel.getName() );
103 }
104
105 if ( endLabel != null && StringUtils.isNotEmpty( endLabel.getName() ) )
106 {
107 args.add( "-vl" );
108
109 args.add( endLabel.getName() );
110 }
111
112 if ( endLabel != null && ( startLabel == null || StringUtils.isEmpty( startLabel.getName() ) ) )
113 {
114 throw new ScmException( "Missing start label." );
115 }
116
117 StarteamCommandLineUtils.addEOLOption( args );
118
119 return StarteamCommandLineUtils.createStarteamCommandLine( "diff", args, workingDirectory, repo );
120 }
121 }