1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
package net.sf.statsvn.input; |
24 | |
|
25 | |
import java.io.IOException; |
26 | |
import java.util.ArrayList; |
27 | |
import java.util.Date; |
28 | |
import java.util.Iterator; |
29 | |
import java.util.List; |
30 | |
import java.util.ListIterator; |
31 | |
import java.util.Map; |
32 | |
import java.util.SortedSet; |
33 | |
import java.util.TreeSet; |
34 | |
|
35 | |
import net.sf.statcvs.input.NoLineCountException; |
36 | |
import net.sf.statcvs.model.Revision; |
37 | |
import net.sf.statcvs.model.SymbolicName; |
38 | |
import net.sf.statcvs.model.VersionedFile; |
39 | |
import net.sf.statsvn.output.SvnConfigurationOptions; |
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
public class FileBuilder { |
72 | |
private static final int ONE_SECOND = 1000; |
73 | |
|
74 | |
private static final int ONE_MIN_IN_MS = 60000; |
75 | |
|
76 | |
private final Builder builder; |
77 | |
|
78 | |
private final String name; |
79 | |
|
80 | |
private boolean binary; |
81 | |
|
82 | 35420 | private final List revisions = new ArrayList(); |
83 | |
|
84 | |
private final Map revBySymnames; |
85 | |
|
86 | |
private final Map dateBySymnames; |
87 | |
|
88 | |
private int locDelta; |
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | 35420 | public FileBuilder(final Builder builder, final String name, final boolean isBinary, final Map revBySymnames, final Map dateBySymnames) { |
102 | 35420 | this.builder = builder; |
103 | 35420 | this.name = name; |
104 | 35420 | this.binary = isBinary; |
105 | 35420 | this.revBySymnames = revBySymnames; |
106 | 35420 | this.dateBySymnames = dateBySymnames; |
107 | |
|
108 | 35420 | SvnConfigurationOptions.getTaskLogger().log("logging " + name); |
109 | 35420 | } |
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
public void addRevisionData(final RevisionData data) { |
119 | 203506 | if (binary && !data.isCreationOrRestore()) { |
120 | 2199 | data.setLines(0, 0); |
121 | |
} |
122 | 203506 | this.revisions.add(data); |
123 | |
|
124 | 203506 | locDelta += getLOCChange(data); |
125 | 203506 | } |
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
public VersionedFile createFile(final Date beginOfLogDate) { |
138 | 32373 | if (isFilteredFile() || !fileExistsInLogPeriod()) { |
139 | 129 | return null; |
140 | |
} |
141 | |
|
142 | 32244 | final VersionedFile file = new VersionedFile(name, builder.getDirectory(name)); |
143 | |
|
144 | 32244 | if (revisions.isEmpty()) { |
145 | 98 | buildBeginOfLogRevision(file, beginOfLogDate, getFinalLOC(), null); |
146 | 98 | return file; |
147 | |
} |
148 | |
|
149 | 32146 | final Iterator it = revisions.iterator(); |
150 | 32146 | RevisionData currentData = (RevisionData) it.next(); |
151 | 32146 | int currentLOC = getFinalLOC(); |
152 | |
RevisionData previousData; |
153 | |
int previousLOC; |
154 | |
SortedSet symbolicNames; |
155 | |
|
156 | 165768 | while (it.hasNext()) { |
157 | 133622 | previousData = currentData; |
158 | 133622 | previousLOC = currentLOC; |
159 | 133622 | currentData = (RevisionData) it.next(); |
160 | 133622 | currentLOC = previousLOC - getLOCChange(previousData); |
161 | |
|
162 | |
|
163 | 133622 | symbolicNames = createSymbolicNamesCollection(previousData); |
164 | |
|
165 | 133622 | if (previousData.isCreationOrRestore() || previousData.isChange() || isBinary()) { |
166 | 120205 | if (currentData.isDeletion()) { |
167 | 5462 | buildCreationRevision(file, previousData, previousLOC, symbolicNames); |
168 | |
} else { |
169 | 114743 | buildChangeRevision(file, previousData, previousLOC, symbolicNames); |
170 | |
} |
171 | 13417 | } else if (previousData.isDeletion()) { |
172 | 13417 | buildDeletionRevision(file, previousData, previousLOC, symbolicNames); |
173 | |
} else { |
174 | 0 | SvnConfigurationOptions.getTaskLogger().info("illegal state in " + file.getFilenameWithPath() + ":" + previousData.getRevisionNumber()); |
175 | |
} |
176 | |
} |
177 | |
|
178 | |
|
179 | 32146 | symbolicNames = createSymbolicNamesCollection(currentData); |
180 | |
|
181 | 32146 | final int nextLinesOfCode = currentLOC - getLOCChange(currentData); |
182 | 32146 | if (currentData.isCreationOrRestore()) { |
183 | 31328 | buildCreationRevision(file, currentData, currentLOC, symbolicNames); |
184 | 818 | } else if (currentData.isDeletion()) { |
185 | 60 | buildDeletionRevision(file, currentData, currentLOC, symbolicNames); |
186 | 60 | buildBeginOfLogRevision(file, beginOfLogDate, nextLinesOfCode, symbolicNames); |
187 | 758 | } else if (currentData.isChange()) { |
188 | 758 | buildChangeRevision(file, currentData, currentLOC, symbolicNames); |
189 | 758 | currentData.setDate(new Date(currentData.getDate().getTime() - ONE_SECOND)); |
190 | 758 | buildCreationRevision(file, currentData, 0, symbolicNames); |
191 | 758 | buildBeginOfLogRevision(file, beginOfLogDate, nextLinesOfCode, symbolicNames); |
192 | |
} else { |
193 | 0 | SvnConfigurationOptions.getTaskLogger().info("illegal state in " + file.getFilenameWithPath() + ":" + currentData.getRevisionNumber()); |
194 | |
} |
195 | 32146 | return file; |
196 | |
} |
197 | |
|
198 | |
|
199 | |
|
200 | |
|
201 | |
|
202 | |
|
203 | |
|
204 | |
|
205 | |
|
206 | |
private int getFinalLOC() { |
207 | 32244 | if (binary) { |
208 | 4510 | return 0; |
209 | |
} |
210 | |
|
211 | 27734 | String revision = null; |
212 | |
try { |
213 | 27734 | revision = builder.getRevision(name); |
214 | 8214 | } catch (final IOException e) { |
215 | 8214 | if (!finalRevisionIsDead()) { |
216 | 231 | SvnConfigurationOptions.getTaskLogger().info(e.getMessage()); |
217 | |
} |
218 | 19520 | } |
219 | |
|
220 | |
try { |
221 | |
|
222 | |
|
223 | |
|
224 | 27734 | if (!revisions.isEmpty()) { |
225 | 27636 | final RevisionData firstAdded = (RevisionData) revisions.get(0); |
226 | 27636 | if (!finalRevisionIsDead() && !firstAdded.getRevisionNumber().equals(revision)) { |
227 | 601 | SvnConfigurationOptions.getTaskLogger().info("Revision of " + name + " does not match expected revision"); |
228 | |
} |
229 | |
} |
230 | 27734 | return builder.getLOC(name); |
231 | |
|
232 | 8274 | } catch (final NoLineCountException e) { |
233 | 8274 | if (!finalRevisionIsDead()) { |
234 | 259 | SvnConfigurationOptions.getTaskLogger().info(e.getMessage()); |
235 | |
} |
236 | 8274 | return approximateFinalLOC(); |
237 | |
} |
238 | |
} |
239 | |
|
240 | |
|
241 | |
|
242 | |
|
243 | |
|
244 | |
|
245 | |
protected boolean finalRevisionIsDead() { |
246 | 52344 | if (revisions.isEmpty()) { |
247 | 66 | return false; |
248 | |
} |
249 | 52278 | return ((RevisionData) revisions.get(0)).isDeletion(); |
250 | |
} |
251 | |
|
252 | |
|
253 | |
|
254 | |
|
255 | |
|
256 | |
|
257 | |
public boolean existRevision() { |
258 | 0 | return !revisions.isEmpty(); |
259 | |
} |
260 | |
|
261 | |
|
262 | |
|
263 | |
|
264 | |
|
265 | |
|
266 | |
|
267 | |
|
268 | |
|
269 | |
|
270 | |
private int approximateFinalLOC() { |
271 | 8274 | int max = 0; |
272 | 8274 | int current = 0; |
273 | 8274 | final Iterator it = revisions.iterator(); |
274 | 46000 | while (it.hasNext()) { |
275 | 37726 | final RevisionData data = (RevisionData) it.next(); |
276 | 37726 | current += data.getLinesAdded(); |
277 | 37726 | max = Math.max(current, max); |
278 | 37726 | current -= data.getLinesRemoved(); |
279 | 33958 | } |
280 | 8274 | return max; |
281 | |
} |
282 | |
|
283 | |
|
284 | |
|
285 | |
|
286 | |
|
287 | |
|
288 | |
|
289 | |
|
290 | |
|
291 | |
|
292 | |
private int getLOCChange(final RevisionData data) { |
293 | 369274 | return data.getLinesAdded() - data.getLinesRemoved(); |
294 | |
} |
295 | |
|
296 | |
private void buildCreationRevision(final VersionedFile file, final RevisionData data, final int loc, final SortedSet symbolicNames) { |
297 | 37548 | file.addInitialRevision(data.getRevisionNumber(), builder.getAuthor(data.getLoginName()), data.getDate(), data.getComment(), loc, symbolicNames); |
298 | 37548 | } |
299 | |
|
300 | |
private void buildChangeRevision(final VersionedFile file, final RevisionData data, final int loc, final SortedSet symbolicNames) { |
301 | 115501 | file.addChangeRevision(data.getRevisionNumber(), builder.getAuthor(data.getLoginName()), data.getDate(), data.getComment(), loc, data.getLinesAdded() |
302 | |
- data.getLinesRemoved(), Math.min(data.getLinesAdded(), data.getLinesRemoved()), symbolicNames); |
303 | 115501 | } |
304 | |
|
305 | |
private void buildDeletionRevision(final VersionedFile file, final RevisionData data, final int loc, final SortedSet symbolicNames) { |
306 | 13477 | file.addDeletionRevision(data.getRevisionNumber(), builder.getAuthor(data.getLoginName()), data.getDate(), data.getComment(), loc, symbolicNames); |
307 | 13477 | } |
308 | |
|
309 | |
private void buildBeginOfLogRevision(final VersionedFile file, final Date beginOfLogDate, final int loc, final SortedSet symbolicNames) { |
310 | 916 | final Date date = new Date(beginOfLogDate.getTime() - ONE_MIN_IN_MS); |
311 | 916 | final Revision dummyForMove = file.addBeginOfLogRevision(date, loc, symbolicNames); |
312 | |
|
313 | |
|
314 | |
|
315 | |
|
316 | 916 | if (symbolicNames != null) { |
317 | 0 | final Iterator it = symbolicNames.iterator(); |
318 | 0 | while (it.hasNext()) { |
319 | 0 | ((SymbolicName) it.next()).getRevisions().remove(dummyForMove); |
320 | |
} |
321 | |
} |
322 | 916 | } |
323 | |
|
324 | |
|
325 | |
|
326 | |
|
327 | |
|
328 | |
|
329 | |
|
330 | |
private boolean isFilteredFile() { |
331 | 32373 | return !this.builder.matchesPatterns(this.name); |
332 | |
} |
333 | |
|
334 | |
|
335 | |
|
336 | |
|
337 | |
|
338 | |
|
339 | |
|
340 | |
|
341 | |
|
342 | |
|
343 | |
|
344 | |
|
345 | |
private boolean fileExistsInLogPeriod() { |
346 | 32373 | if (revisions.size() > 0 || binary) { |
347 | 32146 | return true; |
348 | |
} |
349 | |
try { |
350 | 227 | builder.getLOC(name); |
351 | 98 | return true; |
352 | 129 | } catch (final NoLineCountException fileDoesNotExistInTimespan) { |
353 | 129 | return false; |
354 | |
} |
355 | |
} |
356 | |
|
357 | |
|
358 | |
|
359 | |
|
360 | |
|
361 | |
|
362 | |
|
363 | |
|
364 | |
|
365 | |
|
366 | |
private SortedSet createSymbolicNamesCollection(final RevisionData revisionData) { |
367 | 165768 | SortedSet symbolicNames = null; |
368 | |
|
369 | 165768 | final int currentRevision = getRevisionAsInt(revisionData.getRevisionNumber()); |
370 | 165768 | SvnConfigurationOptions.getTaskLogger().log("\n" + name + " CURRENT REVISION = " + currentRevision + " Deleted " + revisionData.isDeletion()); |
371 | |
|
372 | 165768 | if (revisions.isEmpty()) { |
373 | 0 | SvnConfigurationOptions.getTaskLogger().log("NO Revisions...."); |
374 | 0 | return symbolicNames; |
375 | |
} |
376 | |
|
377 | |
|
378 | 165768 | for (final Iterator tags = revBySymnames.entrySet().iterator(); tags.hasNext();) { |
379 | 0 | final Map.Entry tag = (Map.Entry) tags.next(); |
380 | |
|
381 | 0 | final int tagRevision = getRevisionAsInt((String) tag.getValue()); |
382 | |
|
383 | 0 | SvnConfigurationOptions.getTaskLogger().log("Considering tag REV " + tagRevision + " name=" + tag.getKey()); |
384 | |
|
385 | |
|
386 | |
|
387 | 0 | int previousRevisionForThisFile = getRevisionAsInt(((RevisionData) revisions.get(revisions.size() - 1)).getRevisionNumber()); |
388 | 0 | int revisionToTag = -1; |
389 | 0 | for (final ListIterator it = revisions.listIterator(revisions.size()); it.hasPrevious();) { |
390 | 0 | final RevisionData data = (RevisionData) it.previous(); |
391 | |
|
392 | 0 | SvnConfigurationOptions.getTaskLogger().log( |
393 | |
"File REV " + data.getRevisionNumber() + " =>" + data.getDate() + " vs " + tagRevision + " Deletion:" + data.isDeletion()); |
394 | |
|
395 | 0 | final int dataRev = getRevisionAsInt(data.getRevisionNumber()); |
396 | |
|
397 | 0 | if (revisionData.isDeletion() && currentRevision < dataRev) { |
398 | |
|
399 | |
|
400 | |
|
401 | 0 | previousRevisionForThisFile = getRevisionAsInt(data.getRevisionNumber()); |
402 | 0 | continue; |
403 | 0 | } else if (dataRev == tagRevision) { |
404 | 0 | revisionToTag = tagRevision; |
405 | 0 | break; |
406 | 0 | } else if (dataRev > tagRevision && tagRevision >= previousRevisionForThisFile) { |
407 | 0 | revisionToTag = previousRevisionForThisFile; |
408 | 0 | SvnConfigurationOptions.getTaskLogger().log("1/ Revision to TAG " + revisionToTag); |
409 | 0 | break; |
410 | |
} |
411 | |
|
412 | 0 | previousRevisionForThisFile = getRevisionAsInt(data.getRevisionNumber()); |
413 | 0 | } |
414 | |
|
415 | |
|
416 | |
|
417 | 0 | if (previousRevisionForThisFile < tagRevision && !revisionData.isDeletion()) { |
418 | 0 | revisionToTag = previousRevisionForThisFile; |
419 | 0 | SvnConfigurationOptions.getTaskLogger().log("2/ Revision to TAG " + revisionToTag); |
420 | |
} |
421 | |
|
422 | 0 | SvnConfigurationOptions.getTaskLogger().log("Revision to TAG " + revisionToTag); |
423 | |
|
424 | 0 | if (revisionToTag > 0 && revisionToTag == currentRevision) { |
425 | |
|
426 | 0 | if (symbolicNames == null) { |
427 | 0 | symbolicNames = new TreeSet(); |
428 | |
} |
429 | 0 | SvnConfigurationOptions.getTaskLogger().log( |
430 | |
"adding revision " + name + "," + currentRevision + " to symname " + tag.getKey() + " Date:" + dateBySymnames.get(tag.getKey()) + " A:" |
431 | |
+ revisionData.getLinesAdded() + " R:" + revisionData.getLinesRemoved()); |
432 | 0 | symbolicNames.add(builder.getSymbolicName((String) tag.getKey(), (Date) dateBySymnames.get(tag.getKey()))); |
433 | |
} |
434 | 0 | } |
435 | |
|
436 | 165768 | return symbolicNames; |
437 | |
} |
438 | |
|
439 | |
private int getRevisionAsInt(final String revisionNumber) { |
440 | 165768 | int rev = 0; |
441 | 165768 | if (revisionNumber != null && !revisionNumber.equals("0.0")) { |
442 | 165768 | rev = Integer.valueOf(revisionNumber).intValue(); |
443 | |
} |
444 | 165768 | return rev; |
445 | |
} |
446 | |
|
447 | |
|
448 | |
|
449 | |
|
450 | |
|
451 | |
|
452 | |
|
453 | |
|
454 | |
public String getName() { |
455 | 211800 | return name; |
456 | |
} |
457 | |
|
458 | |
|
459 | |
|
460 | |
|
461 | |
|
462 | |
|
463 | |
|
464 | |
|
465 | |
public List getRevisions() { |
466 | 1965120 | return revisions; |
467 | |
} |
468 | |
|
469 | |
|
470 | |
|
471 | |
|
472 | |
|
473 | |
|
474 | |
|
475 | |
|
476 | |
private RevisionData findRevision(final String revisionNumber) { |
477 | 780480 | for (int i = 0; i < revisions.size(); i++) { |
478 | 780480 | final RevisionData data = (RevisionData) revisions.get(i); |
479 | 780480 | if (data.getRevisionNumber().equals(revisionNumber)) { |
480 | 112320 | return data; |
481 | |
} |
482 | |
} |
483 | 0 | return null; |
484 | |
} |
485 | |
|
486 | |
|
487 | |
|
488 | |
|
489 | |
|
490 | |
|
491 | |
|
492 | |
|
493 | |
|
494 | |
|
495 | |
public synchronized boolean isBinary() { |
496 | 76510 | return binary; |
497 | |
} |
498 | |
|
499 | |
|
500 | |
|
501 | |
|
502 | |
|
503 | |
|
504 | |
|
505 | |
|
506 | |
|
507 | |
public synchronized void setBinary(final boolean isBinary) { |
508 | 0 | this.binary = isBinary; |
509 | 0 | } |
510 | |
|
511 | |
|
512 | |
|
513 | |
|
514 | |
|
515 | |
|
516 | |
|
517 | |
|
518 | |
|
519 | |
|
520 | |
|
521 | |
|
522 | |
|
523 | |
|
524 | |
|
525 | |
|
526 | |
public void updateRevision(final String revisionNumber, final int linesAdded, final int linesRemoved) { |
527 | 112320 | final RevisionData data = findRevision(revisionNumber); |
528 | 112320 | if (data != null) { |
529 | 112320 | data.setLines(linesAdded, linesRemoved); |
530 | |
} |
531 | 112320 | } |
532 | |
|
533 | |
} |