comparison src/org/tmatesoft/hg/internal/BlameHelper.java @ 569:c4fd1037bc6f

Support for copy/rename follow/no-follow for annotate
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Wed, 10 Apr 2013 20:04:54 +0200
parents
children 707b5c7c6fa4
comparison
equal deleted inserted replaced
568:8ed4f4f4f0a6 569:c4fd1037bc6f
1 /*
2 * Copyright (c) 2013 TMate Software Ltd
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * For information on how to redistribute this software under
14 * the terms of a license other than GNU General Public License
15 * contact TMate Software at support@hg4j.com
16 */
17 package org.tmatesoft.hg.internal;
18
19 import static org.tmatesoft.hg.repo.HgRepository.NO_REVISION;
20
21 import java.util.LinkedList;
22 import java.util.ListIterator;
23
24 import org.tmatesoft.hg.core.HgCallbackTargetException;
25 import org.tmatesoft.hg.internal.DiffHelper.LineSequence;
26 import org.tmatesoft.hg.internal.DiffHelper.LineSequence.ByteChain;
27 import org.tmatesoft.hg.repo.HgBlameFacility.Block;
28 import org.tmatesoft.hg.repo.HgBlameFacility.BlockData;
29 import org.tmatesoft.hg.repo.HgBlameFacility.ChangeBlock;
30 import org.tmatesoft.hg.repo.HgBlameFacility.EqualBlock;
31 import org.tmatesoft.hg.repo.HgBlameFacility.Inspector;
32 import org.tmatesoft.hg.repo.HgBlameFacility.RevisionDescriptor;
33 import org.tmatesoft.hg.repo.HgBlameFacility.RevisionDescriptor.Recipient;
34 import org.tmatesoft.hg.repo.HgBlameFacility;
35 import org.tmatesoft.hg.repo.HgDataFile;
36 import org.tmatesoft.hg.repo.HgInvalidStateException;
37 import org.tmatesoft.hg.util.Adaptable;
38 import org.tmatesoft.hg.util.CancelledException;
39 import org.tmatesoft.hg.util.Pair;
40
41 /**
42 * Blame implementation
43 * @see HgBlameFacility
44 * @author Artem Tikhomirov
45 * @author TMate Software Ltd.
46 */
47 public class BlameHelper {
48
49 private final Inspector insp;
50 private FileLinesCache linesCache;
51
52 // FIXME exposing internals (use of FileLinesCache through cons arg and #useFileUpTo) smells bad, refactor!
53
54 public BlameHelper(Inspector inspector, int cacheHint) {
55 insp = inspector;
56 linesCache = new FileLinesCache(cacheHint);
57 }
58
59 public void useFileUpTo(HgDataFile df, int clogRevIndex) {
60 linesCache.useFileUpTo(df, clogRevIndex);
61 }
62
63 // NO_REVISION is not allowed as any argument
64 public void diff(int fileRevIndex1, int clogRevIndex1, int fileRevIndex2, int clogRevIndex2) throws HgCallbackTargetException {
65 HgDataFile targetFile = linesCache.getFile(clogRevIndex2);
66 LineSequence c1 = linesCache.lines(clogRevIndex1, fileRevIndex1);
67 LineSequence c2 = linesCache.lines(clogRevIndex2, fileRevIndex2);
68 DiffHelper<LineSequence> pg = new DiffHelper<LineSequence>();
69 pg.init(c1, c2);
70 BlameBlockInspector bbi = new BlameBlockInspector(targetFile, fileRevIndex2, insp, clogRevIndex1, clogRevIndex2);
71 pg.findMatchingBlocks(bbi);
72 bbi.checkErrors();
73 }
74
75 public void annotateChange(int fileRevIndex, int csetRevIndex, int[] fileParentRevs, int[] fileParentClogRevs) throws HgCallbackTargetException {
76 HgDataFile targetFile = linesCache.getFile(csetRevIndex);
77 final LineSequence fileRevLines = linesCache.lines(csetRevIndex, fileRevIndex);
78 if (fileParentClogRevs[0] != NO_REVISION && fileParentClogRevs[1] != NO_REVISION) {
79 int p1ClogIndex = fileParentClogRevs[0];
80 int p2ClogIndex = fileParentClogRevs[1];
81 LineSequence p1Lines = linesCache.lines(p1ClogIndex, fileParentRevs[0]);
82 LineSequence p2Lines = linesCache.lines(p2ClogIndex, fileParentRevs[1]);
83 DiffHelper<LineSequence> pg = new DiffHelper<LineSequence>();
84 pg.init(p2Lines, fileRevLines);
85 EqualBlocksCollector p2MergeCommon = new EqualBlocksCollector();
86 pg.findMatchingBlocks(p2MergeCommon);
87 //
88 pg.init(p1Lines);
89 BlameBlockInspector bbi = new BlameBlockInspector(targetFile, fileRevIndex, insp, p1ClogIndex, csetRevIndex);
90 bbi.setMergeParent2(p2MergeCommon, p2ClogIndex);
91 pg.findMatchingBlocks(bbi);
92 bbi.checkErrors();
93 } else if (fileParentClogRevs[0] == fileParentClogRevs[1]) {
94 // may be equal iff both are unset
95 assert fileParentClogRevs[0] == NO_REVISION;
96 // everything added
97 BlameBlockInspector bbi = new BlameBlockInspector(targetFile, fileRevIndex, insp, NO_REVISION, csetRevIndex);
98 bbi.begin(LineSequence.newlines(new byte[0]), fileRevLines);
99 bbi.match(0, fileRevLines.chunkCount()-1, 0);
100 bbi.end();
101 bbi.checkErrors();
102 } else {
103 int soleParentIndex = fileParentClogRevs[0] == NO_REVISION ? 1 : 0;
104 assert fileParentClogRevs[soleParentIndex] != NO_REVISION;
105 LineSequence parentLines = linesCache.lines(fileParentClogRevs[soleParentIndex], fileParentRevs[soleParentIndex]);
106
107 DiffHelper<LineSequence> pg = new DiffHelper<LineSequence>();
108 pg.init(parentLines, fileRevLines);
109 BlameBlockInspector bbi = new BlameBlockInspector(targetFile, fileRevIndex, insp, fileParentClogRevs[soleParentIndex], csetRevIndex);
110 pg.findMatchingBlocks(bbi);
111 bbi.checkErrors();
112 }
113 }
114
115 private static class FileLinesCache {
116 private final LinkedList<Pair<Integer, LineSequence>> lruCache;
117 private final int limit;
118 private final LinkedList<Pair<Integer, HgDataFile>> files; // TODO in fact, need sparse array
119
120 public FileLinesCache(int lruLimit) {
121 limit = lruLimit;
122 lruCache = new LinkedList<Pair<Integer, LineSequence>>();
123 files = new LinkedList<Pair<Integer,HgDataFile>>();
124 }
125
126 public void useFileUpTo(HgDataFile df, int clogRevIndex) {
127 Pair<Integer, HgDataFile> newEntry = new Pair<Integer, HgDataFile>(clogRevIndex, df);
128 for (ListIterator<Pair<Integer, HgDataFile>> it = files.listIterator(); it.hasNext();) {
129 Pair<Integer, HgDataFile> e = it.next();
130 if (e.first() == clogRevIndex) {
131 assert e.second().getPath().equals(df.getPath());
132 return;
133 }
134 if (e.first() > clogRevIndex) {
135 // insert new entry before current
136 it.previous();
137 it.add(newEntry);
138 return;
139 }
140 }
141 files.add(newEntry);
142 }
143
144 public HgDataFile getFile(int clogRevIndex) {
145 for (Pair<Integer, HgDataFile> e : files) {
146 if (e.first() >= clogRevIndex) {
147 return e.second();
148 }
149 }
150 throw new HgInvalidStateException(String.format("Got %d file-changelog mappings, but no luck for revision %d.", files.size(), clogRevIndex));
151 }
152
153 public LineSequence lines(int clogRevIndex, int fileRevIndex) {
154 Pair<Integer, LineSequence> cached = checkCache(clogRevIndex);
155 if (cached != null) {
156 return cached.second();
157 }
158 HgDataFile df = getFile(clogRevIndex);
159 try {
160 ByteArrayChannel c;
161 df.content(fileRevIndex, c = new ByteArrayChannel());
162 LineSequence rv = LineSequence.newlines(c.toArray());
163 lruCache.addFirst(new Pair<Integer, LineSequence>(clogRevIndex, rv));
164 if (lruCache.size() > limit) {
165 lruCache.removeLast();
166 }
167 return rv;
168 } catch (CancelledException ex) {
169 // TODO likely it was bad idea to throw cancelled exception from content()
170 // deprecate and provide alternative?
171 HgInvalidStateException ise = new HgInvalidStateException("ByteArrayChannel never throws CancelledException");
172 ise.initCause(ex);
173 throw ise;
174 }
175 }
176
177 private Pair<Integer,LineSequence> checkCache(int fileRevIndex) {
178 Pair<Integer, LineSequence> rv = null;
179 for (ListIterator<Pair<Integer, LineSequence>> it = lruCache.listIterator(); it.hasNext(); ) {
180 Pair<Integer, LineSequence> p = it.next();
181 if (p.first() == fileRevIndex) {
182 rv = p;
183 it.remove();
184 break;
185 }
186 }
187 if (rv != null) {
188 lruCache.addFirst(rv);
189 }
190 return rv;
191 }
192 }
193
194 private static class BlameBlockInspector extends DiffHelper.DeltaInspector<LineSequence> {
195 private final Inspector insp;
196 private final int csetOrigin;
197 private final int csetTarget;
198 private EqualBlocksCollector p2MergeCommon;
199 private int csetMergeParent;
200 private IntVector mergeRanges;
201 private final AnnotateRev annotatedRevision;
202 private HgCallbackTargetException error;
203
204 public BlameBlockInspector(HgDataFile df, int fileRevIndex, Inspector inspector, int originCset, int targetCset) {
205 assert inspector != null;
206 insp = inspector;
207 annotatedRevision = new AnnotateRev();
208 annotatedRevision.set(df, fileRevIndex);
209 csetOrigin = originCset;
210 csetTarget = targetCset;
211 }
212
213 public void setMergeParent2(EqualBlocksCollector p2Merge, int parentCset2) {
214 p2MergeCommon = p2Merge;
215 csetMergeParent = parentCset2;
216 mergeRanges = new IntVector(3*10, 3*10);
217 }
218
219 @Override
220 public void begin(LineSequence s1, LineSequence s2) {
221 super.begin(s1, s2);
222 if (shallStop()) {
223 return;
224 }
225 ContentBlock originContent = new ContentBlock(s1);
226 ContentBlock targetContent = new ContentBlock(s2);
227 annotatedRevision.set(originContent, targetContent);
228 annotatedRevision.set(csetOrigin, csetTarget, p2MergeCommon != null ? csetMergeParent : NO_REVISION);
229 Recipient curious = Adaptable.Factory.getAdapter(insp, Recipient.class, null);
230 if (curious != null) {
231 try {
232 curious.start(annotatedRevision);
233 } catch (HgCallbackTargetException ex) {
234 error = ex;
235 }
236 }
237 }
238
239 @Override
240 public void end() {
241 super.end();
242 if (shallStop()) {
243 return;
244 }
245 Recipient curious = Adaptable.Factory.getAdapter(insp, Recipient.class, null);
246 if (curious != null) {
247 try {
248 curious.done(annotatedRevision);
249 } catch (HgCallbackTargetException ex) {
250 error = ex;
251 }
252 }
253 p2MergeCommon = null;
254 }
255
256 @Override
257 protected void changed(int s1From, int s1To, int s2From, int s2To) {
258 if (shallStop()) {
259 return;
260 }
261 try {
262 if (p2MergeCommon != null) {
263 mergeRanges.clear();
264 p2MergeCommon.combineAndMarkRangesWithTarget(s2From, s2To - s2From, csetOrigin, csetMergeParent, mergeRanges);
265
266 /*
267 * Usecases, how it USED TO BE initially:
268 * 3 lines changed to 10 lines. range of 10 lines breaks down to 2 from p2, 3 from p1, and 5 from p2.
269 * We report: 2 lines changed to 2(p2), then 1 line changed with 3(p1) and 5 lines added from p2.
270 *
271 * 10 lines changed to 3 lines, range of 3 lines breaks down to 2 line from p1 and 1 line from p2.
272 * We report: 2 lines changed to 2(p1) and 8 lines changed to 1(p2)
273 *
274 * NOW, lines from p2 are always reported as pure add (since we need their insertion point to be in p2, not in p1)
275 * and we try to consume p1 changes as soon as we see first p1's range
276 */
277 int s1TotalLines = s1To - s1From, s1ConsumedLines = 0, s1Start = s1From;
278
279 for (int i = 0; i < mergeRanges.size(); i += 3) {
280 final int rangeOrigin = mergeRanges.get(i);
281 final int rangeStart = mergeRanges.get(i+1);
282 final int rangeLen = mergeRanges.get(i+2);
283 final boolean lastRange = i+3 >= mergeRanges.size();
284 final int s1LinesLeft = s1TotalLines - s1ConsumedLines;
285 // how many lines we may report as changed (don't use more than in range unless it's the very last range)
286 final int s1LinesToBorrow = lastRange ? s1LinesLeft : Math.min(s1LinesLeft, rangeLen);
287 if (rangeOrigin != csetMergeParent && s1LinesToBorrow > 0) {
288 ChangeBlockImpl block = getChangeBlock(s1Start, s1LinesToBorrow, rangeStart, rangeLen);
289 block.setOriginAndTarget(rangeOrigin, csetTarget);
290 insp.changed(block);
291 s1ConsumedLines += s1LinesToBorrow;
292 s1Start += s1LinesToBorrow;
293 } else {
294 int blockInsPoint = rangeOrigin != csetMergeParent ? s1Start : p2MergeCommon.reverseMapLine(rangeStart);
295 ChangeBlockImpl block = getAddBlock(rangeStart, rangeLen, blockInsPoint);
296 block.setOriginAndTarget(rangeOrigin, csetTarget);
297 insp.added(block);
298 }
299 }
300 if (s1ConsumedLines != s1TotalLines) {
301 assert s1ConsumedLines < s1TotalLines : String.format("Expected to process %d lines, but actually was %d", s1TotalLines, s1ConsumedLines);
302 // either there were no ranges from p1, whole s2From..s2To range came from p2, shall report as deleted
303 // or the ranges found were not enough to consume whole s2From..s2To
304 // The "deletion point" is shifted to the end of last csetOrigin->csetTarget change
305 int s2DeletePoint = s2From + s1ConsumedLines;
306 ChangeBlockImpl block = new ChangeBlockImpl(annotatedRevision.origin, null, s1Start, s1To - s1Start, -1, -1, -1, s2DeletePoint);
307 block.setOriginAndTarget(csetOrigin, csetTarget);
308 insp.deleted(block);
309 }
310 } else {
311 ChangeBlockImpl block = getChangeBlock(s1From, s1To - s1From, s2From, s2To - s2From);
312 block.setOriginAndTarget(csetOrigin, csetTarget);
313 insp.changed(block);
314 }
315 } catch (HgCallbackTargetException ex) {
316 error = ex;
317 }
318 }
319
320 @Override
321 protected void added(int s1InsertPoint, int s2From, int s2To) {
322 if (shallStop()) {
323 return;
324 }
325 try {
326 if (p2MergeCommon != null) {
327 mergeRanges.clear();
328 p2MergeCommon.combineAndMarkRangesWithTarget(s2From, s2To - s2From, csetOrigin, csetMergeParent, mergeRanges);
329 int insPoint = s1InsertPoint; // track changes to insertion point
330 for (int i = 0; i < mergeRanges.size(); i += 3) {
331 int rangeOrigin = mergeRanges.get(i);
332 int rangeStart = mergeRanges.get(i+1);
333 int rangeLen = mergeRanges.get(i+2);
334 ChangeBlockImpl block = getAddBlock(rangeStart, rangeLen, insPoint);
335 block.setOriginAndTarget(rangeOrigin, csetTarget);
336 insp.added(block);
337 // indicate insPoint moved down number of lines we just reported
338 insPoint += rangeLen;
339 }
340 } else {
341 ChangeBlockImpl block = getAddBlock(s2From, s2To - s2From, s1InsertPoint);
342 block.setOriginAndTarget(csetOrigin, csetTarget);
343 insp.added(block);
344 }
345 } catch (HgCallbackTargetException ex) {
346 error = ex;
347 }
348 }
349
350 @Override
351 protected void deleted(int s2DeletePoint, int s1From, int s1To) {
352 if (shallStop()) {
353 return;
354 }
355 try {
356 ChangeBlockImpl block = new ChangeBlockImpl(annotatedRevision.origin, null, s1From, s1To - s1From, -1, -1, -1, s2DeletePoint);
357 block.setOriginAndTarget(csetOrigin, csetTarget);
358 insp.deleted(block);
359 } catch (HgCallbackTargetException ex) {
360 error = ex;
361 }
362 }
363
364 @Override
365 protected void unchanged(int s1From, int s2From, int length) {
366 if (shallStop()) {
367 return;
368 }
369 try {
370 EqualBlockImpl block = new EqualBlockImpl(s1From, s2From, length, annotatedRevision.target);
371 block.setOriginAndTarget(csetOrigin, csetTarget);
372 insp.same(block);
373 } catch (HgCallbackTargetException ex) {
374 error = ex;
375 }
376 }
377
378 void checkErrors() throws HgCallbackTargetException {
379 if (error != null) {
380 throw error;
381 }
382 }
383
384 private boolean shallStop() {
385 return error != null;
386 }
387
388 private ChangeBlockImpl getAddBlock(int start, int len, int insPoint) {
389 return new ChangeBlockImpl(null, annotatedRevision.target, -1, -1, start, len, insPoint, -1);
390 }
391
392 private ChangeBlockImpl getChangeBlock(int start1, int len1, int start2, int len2) {
393 return new ChangeBlockImpl(annotatedRevision.origin, annotatedRevision.target, start1, len1, start2, len2, start1, start2);
394 }
395 }
396
397 private static class BlockImpl implements Block {
398 private int originCset;
399 private int targetCset;
400
401 void setOriginAndTarget(int originChangesetIndex, int targetChangesetIndex) {
402 // XXX perhaps, shall be part of Inspector API, rather than Block's
403 // as they don't change between blocks (although the moment about merged revisions)
404 // is not yet clear to me
405 originCset = originChangesetIndex;
406 targetCset = targetChangesetIndex;
407 }
408
409 public int originChangesetIndex() {
410 return originCset;
411 }
412
413 public int targetChangesetIndex() {
414 return targetCset;
415 }
416 }
417
418 private static class EqualBlockImpl extends BlockImpl implements EqualBlock {
419 private final int start1, start2;
420 private final int length;
421 private final ContentBlock fullContent;
422 private FilterBlock myContent;
423
424 EqualBlockImpl(int blockStartSeq1, int blockStartSeq2, int blockLength, ContentBlock targetContent) {
425 start1 = blockStartSeq1;
426 start2 = blockStartSeq2;
427 length = blockLength;
428 fullContent = targetContent;
429 }
430
431 public int originStart() {
432 return start1;
433 }
434
435 public int targetStart() {
436 return start2;
437 }
438
439 public int length() {
440 return length;
441 }
442
443 public BlockData content() {
444 if (myContent == null) {
445 myContent = new FilterBlock(fullContent, start2, length);
446 }
447 return myContent;
448 }
449
450 @Override
451 public String toString() {
452 return String.format("@@ [%d..%d) == [%d..%d) @@", start1, start1+length, start2, start2+length);
453 }
454 }
455
456 private static class ChangeBlockImpl extends BlockImpl implements ChangeBlock {
457 private final ContentBlock oldContent;
458 private final ContentBlock newContent;
459 private final int s1Start;
460 private final int s1Len;
461 private final int s2Start;
462 private final int s2Len;
463 private final int s1InsertPoint;
464 private final int s2DeletePoint;
465 private FilterBlock addedBlock, removedBlock;
466
467 public ChangeBlockImpl(ContentBlock c1, ContentBlock c2, int s1Start, int s1Len, int s2Start, int s2Len, int s1InsertPoint, int s2DeletePoint) {
468 oldContent = c1;
469 newContent = c2;
470 this.s1Start = s1Start;
471 this.s1Len = s1Len;
472 this.s2Start = s2Start;
473 this.s2Len = s2Len;
474 this.s1InsertPoint = s1InsertPoint;
475 this.s2DeletePoint = s2DeletePoint;
476 }
477
478 public int insertedAt() {
479 return s1InsertPoint;
480 }
481
482 public int firstAddedLine() {
483 return s2Start;
484 }
485
486 public int totalAddedLines() {
487 return s2Len;
488 }
489
490 public BlockData addedLines() {
491 if (addedBlock == null) {
492 addedBlock = new FilterBlock(newContent, firstAddedLine(), totalAddedLines());
493 }
494 return addedBlock;
495 }
496
497 public int removedAt() {
498 return s2DeletePoint;
499 }
500
501 public int firstRemovedLine() {
502 return s1Start;
503 }
504
505 public int totalRemovedLines() {
506 return s1Len;
507 }
508
509 public BlockData removedLines() {
510 if (removedBlock == null) {
511 removedBlock = new FilterBlock(oldContent, firstRemovedLine(), totalRemovedLines());
512 }
513 return removedBlock;
514 }
515
516 @Override
517 public String toString() {
518 if (s2DeletePoint == -1) {
519 return String.format("@@ -%d,0 +%d,%d @@", insertedAt(), firstAddedLine(), totalAddedLines());
520 } else if (s1InsertPoint == -1) {
521 // delete only
522 return String.format("@@ -%d,%d +%d,0 @@", firstRemovedLine(), totalRemovedLines(), removedAt());
523 }
524 return String.format("@@ -%d,%d +%d,%d @@", firstRemovedLine(), totalRemovedLines(), firstAddedLine(), totalAddedLines());
525 }
526 }
527
528 private static class SingleLine implements BlockData {
529 private final ByteChain line;
530
531 public SingleLine(ByteChain lineContent) {
532 line = lineContent;
533 }
534
535 public BlockData elementAt(int index) {
536 assert false;
537 return null;
538 }
539
540 public int elementCount() {
541 return 0;
542 }
543
544 public byte[] asArray() {
545 return line.data();
546 }
547 }
548
549 private static class ContentBlock implements BlockData {
550 private final LineSequence seq;
551
552 public ContentBlock(LineSequence sequence) {
553 seq = sequence;
554 }
555
556 public BlockData elementAt(int index) {
557 return new SingleLine(seq.chunk(index));
558 }
559
560 public int elementCount() {
561 return seq.chunkCount() - 1;
562 }
563
564 public byte[] asArray() {
565 return seq.data(0, seq.chunkCount() - 1);
566 }
567 }
568
569 private static class FilterBlock implements BlockData {
570 private final ContentBlock contentBlock;
571 private final int from;
572 private final int length;
573
574 public FilterBlock(ContentBlock bd, int startFrom, int len) {
575 assert bd != null;
576 assert startFrom + len < bd.seq.chunkCount(); // there's one extra chunk in the end, so strict less is ok
577 contentBlock = bd;
578 from = startFrom;
579 length = len;
580 }
581
582 public BlockData elementAt(int index) {
583 if (index < 0 || index >= length) {
584 throw new IllegalArgumentException(String.format("Expected value from [0..%d), got %d", length, index));
585 }
586 return contentBlock.elementAt(from + index);
587 }
588
589 public int elementCount() {
590 return length;
591 }
592
593 public byte[] asArray() {
594 return contentBlock.seq.data(from, from + length);
595 }
596 }
597
598
599 private static class EqualBlocksCollector implements DiffHelper.MatchInspector<LineSequence> {
600 private final RangeSeq matches = new RangeSeq();
601
602 public void begin(LineSequence s1, LineSequence s2) {
603 }
604
605 public void match(int startSeq1, int startSeq2, int matchLength) {
606 matches.add(startSeq1, startSeq2, matchLength);
607 }
608
609 public void end() {
610 }
611
612 public int reverseMapLine(int ln) {
613 return matches.reverseMapLine(ln);
614 }
615
616 public void intersectWithTarget(int start, int length, IntVector result) {
617 int s = start;
618 for (int l = start, x = start + length; l < x; l++) {
619 if (!matches.includesTargetLine(l)) {
620 if (l - s > 0) {
621 result.add(s);
622 result.add(l - s);
623 }
624 s = l+1;
625 }
626 }
627 if (s < start+length) {
628 result.add(s);
629 result.add((start + length) - s);
630 }
631 }
632
633 /*
634 * intersects [start..start+length) with ranges of target lines, and based on the intersection
635 * breaks initial range into smaller ranges and records them into result, with marker to indicate
636 * whether the range is from initial range (markerSource) or is a result of the intersection with target
637 * (markerTarget)
638 */
639 public void combineAndMarkRangesWithTarget(int start, int length, int markerSource, int markerTarget, IntVector result) {
640 int sourceStart = start, targetStart = start, sourceEnd = start + length;
641 for (int l = sourceStart; l < sourceEnd; l++) {
642 if (matches.includesTargetLine(l)) {
643 // l is from target
644 if (sourceStart < l) {
645 // few lines from source range were not in the target, report them
646 result.add(markerSource);
647 result.add(sourceStart);
648 result.add(l - sourceStart);
649 }
650 // indicate the earliest line from source range to use
651 sourceStart = l + 1;
652 } else {
653 // l is not in target
654 if (targetStart < l) {
655 // report lines from target range
656 result.add(markerTarget);
657 result.add(targetStart);
658 result.add(l - targetStart);
659 }
660 // next line *may* be from target
661 targetStart = l + 1;
662 }
663 }
664 // if source range end with line from target, sourceStart would be == sourceEnd, and we need to add range with markerTarget
665 // if source range doesn't end with target line, targetStart == sourceEnd, while sourceStart < sourceEnd
666 if (sourceStart < sourceEnd) {
667 assert targetStart == sourceEnd;
668 // something left from the source range
669 result.add(markerSource);
670 result.add(sourceStart);
671 result.add(sourceEnd - sourceStart);
672 } else if (targetStart < sourceEnd) {
673 assert sourceStart == sourceEnd;
674 result.add(markerTarget);
675 result.add(targetStart);
676 result.add(sourceEnd - targetStart);
677 }
678 }
679 }
680
681 private static class AnnotateRev implements RevisionDescriptor {
682 public ContentBlock origin, target;
683 public int originCset, targetCset, mergeCset, fileRevIndex;
684 public HgDataFile df;
685
686 public void set(HgDataFile file, int fileRev) {
687 df = file;
688 fileRevIndex = fileRev;
689 }
690 public void set(ContentBlock o, ContentBlock t) {
691 origin = o;
692 target = t;
693 }
694 public void set(int o, int t, int m) {
695 originCset = o;
696 targetCset = t;
697 mergeCset = m;
698 }
699
700 public BlockData origin() {
701 return origin;
702 }
703
704 public BlockData target() {
705 return target;
706 }
707
708 public int originChangesetIndex() {
709 return originCset;
710 }
711
712 public int targetChangesetIndex() {
713 return targetCset;
714 }
715
716 public boolean isMerge() {
717 return mergeCset != NO_REVISION;
718 }
719
720 public int mergeChangesetIndex() {
721 return mergeCset;
722 }
723
724 public int fileRevisionIndex() {
725 return fileRevIndex;
726 }
727 public HgDataFile file() {
728 return df;
729 }
730 @Override
731 public String toString() {
732 if (isMerge()) {
733 return String.format("[%d,%d->%d]", originCset, mergeCset, targetCset);
734 }
735 return String.format("[%d->%d]", originCset, targetCset);
736 }
737 }
738
739 public static void main(String[] args) {
740 EqualBlocksCollector bc = new EqualBlocksCollector();
741 bc.match(-1, 5, 3);
742 bc.match(-1, 10, 2);
743 bc.match(-1, 15, 3);
744 bc.match(-1, 20, 3);
745 IntVector r = new IntVector();
746 bc.intersectWithTarget(7, 10, r);
747 for (int i = 0; i < r.size(); i+=2) {
748 System.out.printf("[%d..%d) ", r.get(i), r.get(i) + r.get(i+1));
749 }
750 System.out.println();
751 r.clear();
752 bc.combineAndMarkRangesWithTarget(0, 16, 508, 514, r);
753 for (int i = 0; i < r.size(); i+=3) {
754 System.out.printf("%d:[%d..%d) ", r.get(i), r.get(i+1), r.get(i+1) + r.get(i+2));
755 }
756 }
757 }