1 package org.apache.maven.scm.provider.perforce.command.changelog;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.File;
23 import java.text.DateFormat;
24 import java.text.SimpleDateFormat;
25 import java.util.Date;
26 import java.util.List;
27
28 import org.apache.maven.scm.ScmBranch;
29 import org.apache.maven.scm.ScmException;
30 import org.apache.maven.scm.ScmFileSet;
31 import org.apache.maven.scm.ScmVersion;
32 import org.apache.maven.scm.command.changelog.AbstractChangeLogCommand;
33 import org.apache.maven.scm.command.changelog.ChangeLogScmResult;
34 import org.apache.maven.scm.command.changelog.ChangeLogSet;
35 import org.apache.maven.scm.provider.ScmProviderRepository;
36 import org.apache.maven.scm.provider.perforce.PerforceScmProvider;
37 import org.apache.maven.scm.provider.perforce.command.PerforceCommand;
38 import org.apache.maven.scm.provider.perforce.repository.PerforceScmProviderRepository;
39 import org.codehaus.plexus.util.StringUtils;
40 import org.codehaus.plexus.util.cli.CommandLineException;
41 import org.codehaus.plexus.util.cli.CommandLineUtils;
42 import org.codehaus.plexus.util.cli.Commandline;
43
44
45
46
47
48
49 public class PerforceChangeLogCommand
50 extends AbstractChangeLogCommand
51 implements PerforceCommand
52 {
53
54 protected ChangeLogScmResult executeChangeLogCommand( ScmProviderRepository repo, ScmFileSet fileSet,
55 ScmVersion startVersion, ScmVersion endVersion,
56 String datePattern )
57 throws ScmException
58 {
59 return executeChangeLogCommand( repo, fileSet, null, null, null, datePattern, startVersion, endVersion );
60 }
61
62
63 protected ChangeLogScmResult executeChangeLogCommand( ScmProviderRepository repo, ScmFileSet fileSet,
64 Date startDate, Date endDate, ScmBranch branch,
65 String datePattern )
66 throws ScmException
67 {
68 if ( branch != null && StringUtils.isNotEmpty( branch.getName() ) )
69 {
70 throw new ScmException( "This SCM doesn't support branches." );
71 }
72
73 return executeChangeLogCommand( repo, fileSet, startDate, endDate, branch, datePattern, null, null );
74 }
75
76 protected ChangeLogScmResult executeChangeLogCommand( ScmProviderRepository repo, ScmFileSet fileSet,
77 Date startDate, Date endDate, ScmBranch branch,
78 String datePattern, ScmVersion startVersion,
79 ScmVersion endVersion )
80 throws ScmException
81 {
82 PerforceScmProviderRepository p4repo = (PerforceScmProviderRepository) repo;
83 String clientspec = PerforceScmProvider.getClientspecName( getLogger(), p4repo, fileSet.getBasedir() );
84 Commandline cl =
85 createCommandLine( p4repo, fileSet.getBasedir(), clientspec, null, startDate, endDate, startVersion,
86 endVersion );
87
88 String location = PerforceScmProvider.getRepoPath( getLogger(), p4repo, fileSet.getBasedir() );
89 PerforceChangesConsumer consumer =
90 new PerforceChangesConsumer( getLogger() );
91
92 try
93 {
94 if ( getLogger().isDebugEnabled() )
95 {
96 getLogger().debug( PerforceScmProvider.clean( "Executing " + cl.toString() ) );
97 }
98
99 CommandLineUtils.StringStreamConsumer err = new CommandLineUtils.StringStreamConsumer();
100 int exitCode = CommandLineUtils.executeCommandLine( cl, consumer, err );
101
102 if ( exitCode != 0 )
103 {
104 String cmdLine = CommandLineUtils.toString( cl.getCommandline() );
105
106 StringBuilder msg = new StringBuilder( "Exit code: " + exitCode + " - " + err.getOutput() );
107 msg.append( '\n' );
108 msg.append( "Command line was:" + cmdLine );
109
110 throw new CommandLineException( msg.toString() );
111 }
112 }
113 catch ( CommandLineException e )
114 {
115 if ( getLogger().isErrorEnabled() )
116 {
117 getLogger().error( "CommandLineException " + e.getMessage(), e );
118 }
119 }
120
121 List<String> changes = consumer.getChanges();
122
123 cl = PerforceScmProvider.createP4Command( p4repo, fileSet.getBasedir() );
124 cl.createArg().setValue( "describe" );
125 cl.createArg().setValue( "-s" );
126
127 for ( String change : changes )
128 {
129 cl.createArg().setValue( change );
130 }
131
132 PerforceDescribeConsumer describeConsumer =
133 new PerforceDescribeConsumer( location, datePattern, getLogger() );
134
135 try
136 {
137 if ( getLogger().isDebugEnabled() )
138 {
139 getLogger().debug( PerforceScmProvider.clean( "Executing " + cl.toString() ) );
140 }
141
142 CommandLineUtils.StringStreamConsumer err = new CommandLineUtils.StringStreamConsumer();
143 int exitCode = CommandLineUtils.executeCommandLine( cl, describeConsumer, err );
144
145 if ( exitCode != 0 )
146 {
147 String cmdLine = CommandLineUtils.toString( cl.getCommandline() );
148
149 StringBuilder msg = new StringBuilder( "Exit code: " + exitCode + " - " + err.getOutput() );
150 msg.append( '\n' );
151 msg.append( "Command line was:" + cmdLine );
152
153 throw new CommandLineException( msg.toString() );
154 }
155 }
156 catch ( CommandLineException e )
157 {
158 if ( getLogger().isErrorEnabled() )
159 {
160 getLogger().error( "CommandLineException " + e.getMessage(), e );
161 }
162 }
163
164 ChangeLogSet cls = new ChangeLogSet( describeConsumer.getModifications(), null, null );
165 cls.setStartVersion( startVersion );
166 cls.setEndVersion( endVersion );
167 return new ChangeLogScmResult( cl.toString(), cls );
168 }
169
170 public static Commandline createCommandLine( PerforceScmProviderRepository repo, File workingDirectory,
171 String clientspec, ScmBranch branch, Date startDate, Date endDate,
172 ScmVersion startVersion, ScmVersion endVersion )
173 {
174 DateFormat dateFormat = new SimpleDateFormat( "yyyy/MM/dd:HH:mm:ss" );
175 Commandline command = PerforceScmProvider.createP4Command( repo, workingDirectory );
176
177 if ( clientspec != null )
178 {
179 command.createArg().setValue( "-c" );
180 command.createArg().setValue( clientspec );
181 }
182 command.createArg().setValue( "changes" );
183 command.createArg().setValue( "-t" );
184
185 StringBuilder fileSpec = new StringBuilder( "..." );
186 if ( startDate != null )
187 {
188 fileSpec.append( "@" )
189 .append( dateFormat.format( startDate ) )
190 .append( "," );
191
192 if ( endDate != null )
193 {
194 fileSpec.append( dateFormat.format( endDate ) );
195 }
196 else
197 {
198 fileSpec.append( "now" );
199 }
200 }
201
202 if ( startVersion != null )
203 {
204 fileSpec.append( "@" ).append( startVersion.getName() ).append( "," );
205
206 if ( endVersion != null )
207 {
208 fileSpec.append( endVersion.getName() );
209 }
210 else
211 {
212 fileSpec.append( "now" );
213 }
214 }
215
216 command.createArg().setValue( fileSpec.toString() );
217
218 return command;
219 }
220 }