Mercurial > hg4j
changeset 709:497e697636fc
Report merged lines as changed block if possible, not as a sequence of added/deleted blocks. To facilitate access to merge parent lines AddBlock got mergeLineAt() method that reports index of the line in the second parent (if any), while insertedAt() has been changed to report index in the first parent always
author | Artem Tikhomirov <tikhomirov.artem@gmail.com> |
---|---|
date | Wed, 21 Aug 2013 16:23:27 +0200 |
parents | 4ffc17c0b534 |
children | cf200271439a |
files | src/org/tmatesoft/hg/core/HgBlameInspector.java src/org/tmatesoft/hg/core/HgDiffCommand.java src/org/tmatesoft/hg/internal/diff/BlameHelper.java src/org/tmatesoft/hg/internal/diff/ForwardAnnotateInspector.java src/org/tmatesoft/hg/internal/diff/ReverseAnnotateInspector.java test/org/tmatesoft/hg/test/TestBlame.java |
diffstat | 6 files changed, 46 insertions(+), 45 deletions(-) [+] |
line wrap: on
line diff
--- a/src/org/tmatesoft/hg/core/HgBlameInspector.java Tue Aug 20 18:41:34 2013 +0200 +++ b/src/org/tmatesoft/hg/core/HgBlameInspector.java Wed Aug 21 16:23:27 2013 +0200 @@ -150,9 +150,13 @@ public interface AddBlock extends Block { /** - * @return line index in the origin where this block is inserted + * @return line index in the origin (first parent in case of merge) where this block is inserted */ - int insertedAt(); + int insertedAt(); + /** + * @return line index in the second (merge) parent, if this block comes into target as a result of a merge, or -1 otherwise. + */ + int mergeLineAt(); /** * @return line index of the first added line in the target revision */
--- a/src/org/tmatesoft/hg/core/HgDiffCommand.java Tue Aug 20 18:41:34 2013 +0200 +++ b/src/org/tmatesoft/hg/core/HgDiffCommand.java Wed Aug 21 16:23:27 2013 +0200 @@ -254,7 +254,7 @@ progress.start(2); try { final CancelSupport cancel = getCancelSupport(insp, true); - int changelogRevisionIndex = clogRevIndexEnd.get(); + int changelogRevisionIndex = clogRevIndexEnd.get(TIP); // TODO detect if file is text/binary (e.g. looking for chars < ' ' and not \t\r\n\f int fileRevIndex = fileRevIndex(df, changelogRevisionIndex); int[] fileRevParents = new int[2];
--- a/src/org/tmatesoft/hg/internal/diff/BlameHelper.java Tue Aug 20 18:41:34 2013 +0200 +++ b/src/org/tmatesoft/hg/internal/diff/BlameHelper.java Wed Aug 21 16:23:27 2013 +0200 @@ -379,16 +379,21 @@ final int s1LinesLeft = s1TotalLines - s1ConsumedLines; // how many lines we may report as changed (don't use more than in range unless it's the very last range) final int s1LinesToBorrow = lastRange ? s1LinesLeft : Math.min(s1LinesLeft, rangeLen); - if (rangeOrigin != csetMergeParent && s1LinesToBorrow > 0) { + if (s1LinesToBorrow > 0) { ChangeBlockImpl block = getChangeBlock(s1Start, s1LinesToBorrow, rangeStart, rangeLen); block.setOriginAndTarget(rangeOrigin, csetTarget); + if (rangeOrigin == csetMergeParent) { + block.setMergeFromLine(p2MergeCommon.getLineInP2(rangeStart)); + } insp.changed(block); s1ConsumedLines += s1LinesToBorrow; s1Start += s1LinesToBorrow; } else { - int blockInsPoint = rangeOrigin != csetMergeParent ? s1Start : p2MergeCommon.getLineInP2(rangeStart); - ChangeBlockImpl block = getAddBlock(rangeStart, rangeLen, blockInsPoint); + ChangeBlockImpl block = getAddBlock(rangeStart, rangeLen, s1Start); block.setOriginAndTarget(rangeOrigin, csetTarget); + if (rangeOrigin == csetMergeParent) { + block.setMergeFromLine(p2MergeCommon.getLineInP2(rangeStart)); + } insp.added(block); } } @@ -398,7 +403,7 @@ // or the ranges found were not enough to consume whole s2From..s2To // The "deletion point" is shifted to the end of last csetOrigin->csetTarget change int s2DeletePoint = s2From + s1ConsumedLines; - ChangeBlockImpl block = new ChangeBlockImpl(annotatedRevision.origin, null, s1Start, s1To - s1Start, -1, -1, -1, s2DeletePoint); + ChangeBlockImpl block = getDeleteBlock(s1Start, s1To - s1Start, s2DeletePoint); block.setOriginAndTarget(csetOrigin, csetTarget); insp.deleted(block); } @@ -425,11 +430,11 @@ int rangeOrigin = mergeRange.at(0); int rangeStart = mergeRange.at(1); int rangeLen = mergeRange.at(2); - // XXX likely need somewhat similar to the code above: - // int blockInsPoint = rangeOrigin != csetMergeParent ? s1Start : p2MergeCommon.reverseMapLine(rangeStart); - // ChangeBlockImpl block = getAddBlock(rangeStart, rangeLen, insPoint); block.setOriginAndTarget(rangeOrigin, csetTarget); + if (rangeOrigin == csetMergeParent) { + block.setMergeFromLine(p2MergeCommon.getLineInP2(rangeStart)); + } insp.added(block); // indicate insPoint moved down number of lines we just reported insPoint += rangeLen; @@ -450,7 +455,7 @@ return; } try { - ChangeBlockImpl block = new ChangeBlockImpl(annotatedRevision.origin, null, s1From, s1To - s1From, -1, -1, -1, s2DeletePoint); + ChangeBlockImpl block = getDeleteBlock(s1From, s1To - s1From, s2DeletePoint); block.setOriginAndTarget(csetOrigin, csetTarget); insp.deleted(block); } catch (HgCallbackTargetException ex) { @@ -489,6 +494,10 @@ private ChangeBlockImpl getChangeBlock(int start1, int len1, int start2, int len2) { return new ChangeBlockImpl(annotatedRevision.origin, annotatedRevision.target, start1, len1, start2, len2, start1, start2); } + + private ChangeBlockImpl getDeleteBlock(int start, int len, int delPoint) { + return new ChangeBlockImpl(annotatedRevision.origin, null, start, len, -1, -1, -1, delPoint); + } } private static class BlockImpl implements Block { @@ -560,6 +569,7 @@ private final int s1InsertPoint; private final int s2DeletePoint; private FilterBlock addedBlock, removedBlock; + private int mergedFromLine = -1; public ChangeBlockImpl(ContentBlock c1, ContentBlock c2, int s1Start, int s1Len, int s2Start, int s2Len, int s1InsertPoint, int s2DeletePoint) { oldContent = c1; @@ -575,6 +585,10 @@ public int insertedAt() { return s1InsertPoint; } + + public int mergeLineAt() { + return mergedFromLine; + } public int firstAddedLine() { return s2Start; @@ -620,6 +634,10 @@ } return String.format("@@ -%d,%d +%d,%d @@", firstRemovedLine(), totalRemovedLines(), firstAddedLine(), totalAddedLines()); } + + void setMergeFromLine(int lineInOrigin) { + mergedFromLine = lineInOrigin; + } } private static class SingleLine implements BlockData { @@ -732,6 +750,9 @@ */ public IntSliceSeq combineAndMarkRangesWithSource(int start1, int end1, int start2, int end2, int source1, int source2); public IntSliceSeq combineAndMarkRangesWithSource(int insPoint, int start, int end, int source1, int source2); + /** + * index in p2 of the mergeLine's origin in p2 + */ public int getLineInP2(int mergeLine); }
--- a/src/org/tmatesoft/hg/internal/diff/ForwardAnnotateInspector.java Tue Aug 20 18:41:34 2013 +0200 +++ b/src/org/tmatesoft/hg/internal/diff/ForwardAnnotateInspector.java Wed Aug 21 16:23:27 2013 +0200 @@ -92,7 +92,8 @@ public void added(AddBlock block) throws HgCallbackTargetException { if (revDescriptor.isMerge() && block.originChangesetIndex() == revDescriptor.mergeChangesetIndex()) { - copyBlock(block.originChangesetIndex(), block.insertedAt(), block.totalAddedLines()); + assert block.mergeLineAt() != -1; + copyBlock(block.originChangesetIndex(), block.mergeLineAt(), block.totalAddedLines()); return; } BlockData addedLines = block.addedLines();
--- a/src/org/tmatesoft/hg/internal/diff/ReverseAnnotateInspector.java Tue Aug 20 18:41:34 2013 +0200 +++ b/src/org/tmatesoft/hg/internal/diff/ReverseAnnotateInspector.java Wed Aug 21 16:23:27 2013 +0200 @@ -145,7 +145,7 @@ int lnInFinal = activeEquals.mapLineIndex(ln); if (lnInFinal != -1/* && !knownLines[lnInFinal]*/) { if (rs != null) { - rs.add(block.insertedAt() + i, lnInFinal, 1); + rs.add(block.mergeLineAt() + i, lnInFinal, 1); } else { line(lnInFinal, ln, block.targetChangesetIndex()); }
--- a/test/org/tmatesoft/hg/test/TestBlame.java Tue Aug 20 18:41:34 2013 +0200 +++ b/test/org/tmatesoft/hg/test/TestBlame.java Wed Aug 21 16:23:27 2013 +0200 @@ -377,42 +377,17 @@ } } - private void ccc() throws Throwable { - HgRepository repo = new HgLookup().detect("/home/artem/hg/hgtest-annotate-merge/"); - HgDataFile df = repo.getFileNode("file.txt"); - DiffOutInspector dump = new DiffOutInspector(System.out); - dump.needRevisions(true); - HgDiffCommand diffCmd = new HgDiffCommand(repo); - diffCmd.file(df); - diffCmd.range(0, 8).order(NewToOld); - diffCmd.executeAnnotate(dump); -// af.annotateSingleRevision(df, 113, dump); -// System.out.println(); -// af.annotate(df, TIP, new LineDumpInspector(true), HgIterateDirection.NewToOld); -// System.out.println(); -// af.annotate(df, TIP, new LineDumpInspector(false), HgIterateDirection.NewToOld); -// System.out.println(); - /* - OutputParser.Stub op = new OutputParser.Stub(); - eh = new ExecHelper(op, repo.getWorkingDir()); - for (int cs : new int[] { 24, 46, 49, 52, 59, 62, 64, TIP}) { - doLineAnnotateTest(df, cs, op); - } - errorCollector.verify(); - */ - ForwardAnnotateInspector insp = new ForwardAnnotateInspector(); - diffCmd.range(0, 8).order(insp.iterateDirection()); - diffCmd.executeAnnotate(insp); - AnnotateInspector fa = new AnnotateInspector().fill(8, insp); - for (int i = 0; i < fa.changesets.size(); i++) { - final String line = fa.lines.get(i); - System.out.printf("%d: %s", fa.changesets.get(i), line == null ? "null\n" : line); - } + private void ddd() throws Throwable { +// HgRepository repo = new HgLookup().detect("/home/artem/hg/blame-merge/"); + HgRepository repo = new HgLookup().detect("/home/artem/hg/junit-test-repos/test-annotate3/"); + final DiffOutInspector insp = new DiffOutInspector(System.out); + insp.needRevisions(true); + new HgDiffCommand(repo).file(Path.create("file1")).executeParentsAnnotate(insp); } public static void main(String[] args) throws Throwable { TestBlame tt = new TestBlame(); - tt.ccc(); + tt.ddd(); } private static class DiffOutInspector implements HgBlameInspector {