comparison src/org/tmatesoft/hg/internal/ForwardAnnotateInspector.java @ 677:1c49c0cee540

Report line number at the first appearance, like 'hg annotate -l' does
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Thu, 18 Jul 2013 18:47:45 +0200
parents 3219cfadda49
children 58a6900f845d
comparison
equal deleted inserted replaced
676:3219cfadda49 677:1c49c0cee540
65 for (IntTuple t : all.get(revision)) { 65 for (IntTuple t : all.get(revision)) {
66 IntMap<byte[]> revLines = lineContent.get(t.at(1)); 66 IntMap<byte[]> revLines = lineContent.get(t.at(1));
67 for (int i = 0, x = t.at(0); i < x; i++) { 67 for (int i = 0, x = t.at(0); i < x; i++) {
68 final int lineInRev = t.at(2) + i; 68 final int lineInRev = t.at(2) + i;
69 final byte[] lc = revLines.get(lineInRev); 69 final byte[] lc = revLines.get(lineInRev);
70 li.init(line++, t.at(1), lc); 70 li.init(line++, lineInRev+1, t.at(1), lc);
71 insp.next(li); 71 insp.next(li);
72 progress.worked(1); 72 progress.worked(1);
73 cancel.checkCancelled(); 73 cancel.checkCancelled();
74 } 74 }
75 } 75 }
110 } 110 }
111 111
112 public void deleted(DeleteBlock block) throws HgCallbackTargetException { 112 public void deleted(DeleteBlock block) throws HgCallbackTargetException {
113 } 113 }
114 114
115 private void copyBlock(int originChangesetIndex, int originStart, int length) { 115 private void copyBlock(int originChangesetIndex, int blockStart, int length) {
116 IntSliceSeq origin = all.get(originChangesetIndex); 116 IntSliceSeq origin = all.get(originChangesetIndex);
117 assert origin != null; // shall visit parents before came to this child 117 assert origin != null; // shall visit parents before came to this child
118 int originPos = 0; 118 int originPos = 0;
119 int targetBlockLen = length; 119 int targetBlockLen = length;
120 for (IntTuple t : origin) { 120 for (IntTuple t : origin) {
121 int originBlockLen = t.at(0); 121 int originBlockLen = t.at(0);
122 int originBlockEnd = originPos + originBlockLen; 122 int originBlockEnd = originPos + originBlockLen;
123 if (originBlockEnd > originStart) { 123 if (originBlockEnd > blockStart) {
124 int originBlockOverlap = Math.min(originBlockLen, originBlockEnd - originStart); 124 // part of origin block from blockStart up to originBlockEnd, but not more
125 // than size of the block (when blockStart is out of block start, i.e. < originPos)
126 int originBlockOverlap = Math.min(originBlockLen, originBlockEnd - blockStart);
125 assert originBlockOverlap > 0; 127 assert originBlockOverlap > 0;
126 originBlockOverlap = Math.min(originBlockOverlap, targetBlockLen); 128 // eat as much as there's left in the block being copied
129 int originBlockConsumed = Math.min(originBlockOverlap, targetBlockLen);
127 int originBlockLine = t.at(2); 130 int originBlockLine = t.at(2);
128 if (originPos < originStart) { 131 if (originPos < blockStart) {
129 originBlockLine += originBlockLen-originBlockOverlap; 132 originBlockLine += originBlockLen-originBlockOverlap;
130 } 133 }
131 // copy fragment of original block; 134 // copy fragment of original block;
132 current.add(originBlockOverlap, t.at(1), originBlockLine); 135 current.add(originBlockConsumed, t.at(1), originBlockLine);
133 targetBlockLen -= originBlockOverlap; 136 targetBlockLen -= originBlockConsumed;
134 if (targetBlockLen == 0) { 137 if (targetBlockLen == 0) {
135 break; 138 break;
136 } 139 }
137 } 140 }
138 originPos += originBlockLen; 141 originPos += originBlockLen;
142 145
143 public static void main(String[] args) throws HgCallbackTargetException, CancelledException, HgException { 146 public static void main(String[] args) throws HgCallbackTargetException, CancelledException, HgException {
144 HgRepository repo = new HgLookup().detect("/home/artem/hg/junit-test-repos/test-annotate/"); 147 HgRepository repo = new HgLookup().detect("/home/artem/hg/junit-test-repos/test-annotate/");
145 HgDiffCommand cmd = new HgDiffCommand(repo); 148 HgDiffCommand cmd = new HgDiffCommand(repo);
146 cmd.file(repo.getFileNode("file1")).order(HgIterateDirection.OldToNew); 149 cmd.file(repo.getFileNode("file1")).order(HgIterateDirection.OldToNew);
147 cmd.range(0, 8); 150 final int cset = 8;
151 cmd.range(0, cset);
148 final ForwardAnnotateInspector c2 = new ForwardAnnotateInspector(); 152 final ForwardAnnotateInspector c2 = new ForwardAnnotateInspector();
149 cmd.executeAnnotate(c2); 153 cmd.executeAnnotate(c2);
150 for (IntTuple t : c2.all.get(8)) { 154 for (IntTuple t : c2.all.get(cset)) {
151 System.out.printf("Block %d lines from revision %d (starts with line %d in the origin)\n", t.at(0), t.at(1), t.at(2));
152 }
153 for (IntTuple t : c2.all.get(8)) {
154 System.out.printf("Block %d lines from revision %d (starts with line %d in the origin)\n", t.at(0), t.at(1), 1+t.at(2)); 155 System.out.printf("Block %d lines from revision %d (starts with line %d in the origin)\n", t.at(0), t.at(1), 1+t.at(2));
155 } 156 }
156 c2.report(8, new Inspector() { 157 c2.report(cset, new Inspector() {
157 158
158 public void next(LineInfo lineInfo) throws HgCallbackTargetException { 159 public void next(LineInfo lineInfo) throws HgCallbackTargetException {
159 System.out.printf("%3d:%3d: %s", lineInfo.getChangesetIndex(), lineInfo.getLineNumber(), new String(lineInfo.getContent())); 160 System.out.printf("%3d:%3d: %s", lineInfo.getChangesetIndex(), lineInfo.getOriginLineNumber(), new String(lineInfo.getContent()));
160 } 161 }
161 }, ProgressSupport.Factory.get(null), CancelSupport.Factory.get(null)); 162 }, ProgressSupport.Factory.get(null), CancelSupport.Factory.get(null));
162 } 163 }
163 } 164 }