1 package org.apache.maven.scm.provider.accurev.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.util.ArrayList;
23 import java.util.Collection;
24 import java.util.Collections;
25 import java.util.Date;
26 import java.util.HashMap;
27 import java.util.List;
28 import java.util.Map;
29
30 import org.apache.maven.scm.ChangeFile;
31 import org.apache.maven.scm.ChangeSet;
32 import org.apache.maven.scm.CommandParameter;
33 import org.apache.maven.scm.CommandParameters;
34 import org.apache.maven.scm.ScmBranch;
35 import org.apache.maven.scm.ScmException;
36 import org.apache.maven.scm.ScmFileSet;
37 import org.apache.maven.scm.ScmResult;
38 import org.apache.maven.scm.ScmRevision;
39 import org.apache.maven.scm.ScmVersion;
40 import org.apache.maven.scm.command.changelog.ChangeLogScmResult;
41 import org.apache.maven.scm.command.changelog.ChangeLogSet;
42 import org.apache.maven.scm.log.ScmLogger;
43 import org.apache.maven.scm.provider.ScmProviderRepository;
44 import org.apache.maven.scm.provider.accurev.AccuRev;
45 import org.apache.maven.scm.provider.accurev.AccuRevCapability;
46 import org.apache.maven.scm.provider.accurev.AccuRevException;
47 import org.apache.maven.scm.provider.accurev.AccuRevScmProviderRepository;
48 import org.apache.maven.scm.provider.accurev.AccuRevVersion;
49 import org.apache.maven.scm.provider.accurev.FileDifference;
50 import org.apache.maven.scm.provider.accurev.Stream;
51 import org.apache.maven.scm.provider.accurev.Transaction;
52 import org.apache.maven.scm.provider.accurev.Transaction.Version;
53 import org.apache.maven.scm.provider.accurev.command.AbstractAccuRevCommand;
54 import org.codehaus.plexus.util.StringUtils;
55
56
57
58
59
60
61
62
63
64
65 public class AccuRevChangeLogCommand
66 extends AbstractAccuRevCommand
67 {
68
69 public AccuRevChangeLogCommand( ScmLogger logger )
70 {
71
72 super( logger );
73 }
74
75 @Override
76 protected ScmResult executeAccurevCommand( AccuRevScmProviderRepository repository, ScmFileSet fileSet,
77 CommandParameters parameters )
78 throws ScmException, AccuRevException
79 {
80
81
82 ScmBranch branch = (ScmBranch) parameters.getScmVersion( CommandParameter.BRANCH, null );
83 AccuRevVersion branchVersion = repository.getAccuRevVersion( branch );
84 String stream = branchVersion.getBasisStream();
85 String fromSpec = branchVersion.getTimeSpec();
86 String toSpec = "highest";
87
88
89 ScmVersion startVersion = parameters.getScmVersion( CommandParameter.START_SCM_VERSION, null );
90 ScmVersion endVersion = parameters.getScmVersion( CommandParameter.END_SCM_VERSION, null );
91
92 if ( startVersion != null && StringUtils.isNotEmpty( startVersion.getName() ) )
93 {
94 AccuRevVersion fromVersion = repository.getAccuRevVersion( startVersion );
95
96 AccuRevVersion toVersion =
97 endVersion == null ? new AccuRevVersion( fromVersion.getBasisStream(), "now" )
98 : repository.getAccuRevVersion( endVersion );
99
100 if ( !StringUtils.equals( fromVersion.getBasisStream(), toVersion.getBasisStream() ) )
101 {
102 throw new AccuRevException( "Not able to provide change log between different streams " + fromVersion
103 + "," + toVersion );
104 }
105
106 stream = fromVersion.getBasisStream();
107 fromSpec = fromVersion.getTimeSpec();
108 toSpec = toVersion.getTimeSpec();
109
110 }
111
112 Date startDate = parameters.getDate( CommandParameter.START_DATE, null );
113 Date endDate = parameters.getDate( CommandParameter.END_DATE, null );
114 int numDays = parameters.getInt( CommandParameter.NUM_DAYS, 0 );
115
116 if ( numDays > 0 )
117 {
118 if ( ( startDate != null || endDate != null ) )
119 {
120 throw new ScmException( "Start or end date cannot be set if num days is set." );
121 }
122
123 int day = 24 * 60 * 60 * 1000;
124 startDate = new Date( System.currentTimeMillis() - (long) numDays * day );
125 endDate = new Date( System.currentTimeMillis() + day );
126 }
127
128 if ( endDate != null && startDate == null )
129 {
130 throw new ScmException( "The end date is set but the start date isn't." );
131 }
132
133
134 if ( startDate != null )
135 {
136 fromSpec = AccuRevScmProviderRepository.formatTimeSpec( startDate );
137 }
138 else if ( fromSpec == null )
139 {
140 fromSpec = "1";
141 }
142
143
144
145 Transaction fromTransaction = getDepotTransaction( repository, stream, fromSpec );
146
147 long fromTranId = 1;
148 if ( fromTransaction != null )
149 {
150
151 fromTranId = fromTransaction.getTranId();
152 if ( startDate == null )
153 {
154 startDate = fromTransaction.getWhen();
155 }
156 }
157
158 if ( endDate != null )
159 {
160 toSpec = AccuRevScmProviderRepository.formatTimeSpec( endDate );
161 }
162 else if ( toSpec == null )
163 {
164 toSpec = "highest";
165 }
166
167 Transaction toTransaction = getDepotTransaction( repository, stream, toSpec );
168 long toTranId = 1;
169 if ( toTransaction != null )
170 {
171 toTranId = toTransaction.getTranId();
172 if ( endDate == null )
173 {
174 endDate = toTransaction.getWhen();
175 }
176 }
177 startVersion = new ScmRevision( repository.getRevision( stream, fromTranId ) );
178 endVersion = new ScmRevision( repository.getRevision( stream, toTranId ) );
179
180
181
182 List<Transaction> streamHistory = Collections.emptyList();
183 List<Transaction> workspaceHistory = Collections.emptyList();
184 List<FileDifference> streamDifferences = Collections.emptyList();
185
186 StringBuilder errorMessage = new StringBuilder();
187
188 AccuRev accurev = repository.getAccuRev();
189
190 Stream changelogStream = accurev.showStream( stream );
191 if ( changelogStream == null )
192 {
193 errorMessage.append( "Unknown accurev stream -" ).append( stream ).append( "." );
194 }
195 else
196 {
197
198 String message =
199 "Changelog on stream " + stream + "(" + changelogStream.getStreamType() + ") from " + fromTranId + " ("
200 + startDate + "), to " + toTranId + " (" + endDate + ")";
201
202 if ( startDate != null && startDate.after( endDate ) || fromTranId >= toTranId )
203 {
204 getLogger().warn( "Skipping out of range " + message );
205 }
206 else
207 {
208
209 getLogger().info( message );
210
211
212
213
214 Stream diffStream = changelogStream;
215 if ( changelogStream.isWorkspace() )
216 {
217
218 workspaceHistory =
219 accurev.history( stream, Long.toString( fromTranId + 1 ), Long.toString( toTranId ), 0, false,
220 false );
221
222 if ( workspaceHistory == null )
223 {
224 errorMessage.append( "history on workspace " + stream + " from " + fromTranId + 1 + " to "
225 + toTranId + " failed." );
226
227 }
228
229
230 stream = changelogStream.getBasis();
231 diffStream = accurev.showStream( stream );
232
233 }
234
235 if ( AccuRevCapability.DIFF_BETWEEN_STREAMS.isSupported( accurev.getClientVersion() ) )
236 {
237 if ( startDate.before( diffStream.getStartDate() ) )
238 {
239 getLogger().warn( "Skipping diff of " + stream + " due to start date out of range" );
240 }
241 else
242 {
243 streamDifferences =
244 accurev.diff( stream, Long.toString( fromTranId ), Long.toString( toTranId ) );
245 if ( streamDifferences == null )
246 {
247 errorMessage.append( "Diff " + stream + "- " + fromTranId + " to " + toTranId + "failed." );
248 }
249 }
250 }
251
252
253
254 streamHistory =
255 accurev.history( stream, Long.toString( fromTranId + 1 ), Long.toString( toTranId ), 0, false,
256 false );
257 if ( streamHistory == null )
258 {
259 errorMessage.append( "history on stream " + stream + " from " + fromTranId + 1 + " to " + toTranId
260 + " failed." );
261 }
262
263 }
264 }
265
266 String errorString = errorMessage.toString();
267 if ( StringUtils.isBlank( errorString ) )
268 {
269 ChangeLogSet changeLog =
270 getChangeLog( changelogStream, streamDifferences, streamHistory, workspaceHistory, startDate, endDate );
271
272 changeLog.setEndVersion( endVersion );
273 changeLog.setStartVersion( startVersion );
274
275 return new ChangeLogScmResult( accurev.getCommandLines(), changeLog );
276 }
277 else
278 {
279 return new ChangeLogScmResult( accurev.getCommandLines(), "AccuRev errors: " + errorMessage,
280 accurev.getErrorOutput(), false );
281 }
282
283 }
284
285 private Transaction getDepotTransaction( AccuRevScmProviderRepository repo, String stream, String tranSpec )
286 throws AccuRevException
287 {
288 return repo.getDepotTransaction( stream, tranSpec );
289
290 }
291
292 private ChangeLogSet getChangeLog( Stream stream, List<FileDifference> streamDifferences,
293 List<Transaction> streamHistory, List<Transaction> workspaceHistory,
294 Date startDate, Date endDate )
295 {
296
297
298
299
300
301 Map<Long, FileDifference> differencesMap = new HashMap<Long, FileDifference>();
302 for ( FileDifference fileDifference : streamDifferences )
303 {
304 differencesMap.put( fileDifference.getElementId(), fileDifference );
305 }
306
307 List<Transaction> mergedHistory = new ArrayList<Transaction>( streamHistory );
308
309 String streamPrefix = "/";
310
311 mergedHistory.addAll( workspaceHistory );
312 streamPrefix = stream.getId() + "/";
313
314 List<ChangeSet> entries = new ArrayList<ChangeSet>( streamHistory.size() );
315 for ( Transaction t : mergedHistory )
316 {
317 if ( ( startDate != null && t.getWhen().before( startDate ) )
318 || ( endDate != null && t.getWhen().after( endDate ) ) )
319 {
320
321 continue;
322 }
323
324
325
326
327
328 if ( "mkstream".equals( t.getTranType() ) )
329 {
330 continue;
331 }
332
333 Collection<Version> versions = t.getVersions();
334 List<ChangeFile> files = new ArrayList<ChangeFile>( versions.size() );
335
336 for ( Version v : versions )
337 {
338
339
340 FileDifference difference = differencesMap.get( v.getElementId() );
341
342 if ( difference != null )
343 {
344 String newVersionSpec = difference.getNewVersionSpec();
345 if ( newVersionSpec != null && newVersionSpec.equals( v.getRealSpec() ) )
346 {
347 if ( getLogger().isDebugEnabled() )
348 {
349 getLogger().debug( "Removing difference for " + v );
350 }
351 differencesMap.remove( v.getElementId() );
352 }
353 }
354
355
356
357
358 if ( v.getRealSpec().startsWith( streamPrefix ) && !v.getVirtualSpec().startsWith( streamPrefix ) )
359 {
360 if ( getLogger().isDebugEnabled() )
361 {
362 getLogger().debug( "Skipping workspace to basis stream promote " + v );
363 }
364 }
365 else
366 {
367 ChangeFile f =
368 new ChangeFile( v.getElementName(), v.getVirtualSpec() + " (" + v.getRealSpec() + ")" );
369 files.add( f );
370 }
371
372 }
373
374 if ( versions.isEmpty() || !files.isEmpty() )
375 {
376 ChangeSet changeSet = new ChangeSet( t.getWhen(), t.getComment(), t.getAuthor(), files );
377
378 entries.add( changeSet );
379 }
380 else
381 {
382 if ( getLogger().isDebugEnabled() )
383 {
384 getLogger().debug( "All versions removed for " + t );
385 }
386 }
387
388 }
389
390
391
392
393 if ( !differencesMap.isEmpty() )
394 {
395 List<ChangeFile> upstreamFiles = new ArrayList<ChangeFile>();
396 for ( FileDifference difference : differencesMap.values() )
397 {
398 if ( difference.getNewVersionSpec() != null )
399 {
400 upstreamFiles.add( new ChangeFile( difference.getNewFile().getPath(),
401 difference.getNewVersionSpec() ) );
402 }
403 else
404 {
405
406 upstreamFiles.add( new ChangeFile( difference.getOldFile().getPath(), null ) );
407 }
408 }
409 entries.add( new ChangeSet( endDate, "Upstream changes", "various", upstreamFiles ) );
410 }
411
412 return new ChangeLogSet( entries, startDate, endDate );
413 }
414
415 public ChangeLogScmResult changelog( ScmProviderRepository repo, ScmFileSet testFileSet, CommandParameters params )
416 throws ScmException
417 {
418
419 return (ChangeLogScmResult) execute( repo, testFileSet, params );
420 }
421
422 }