comparison test/org/tmatesoft/hg/test/TestBlame.java @ 554:a5fd757d1b5d

Access to content of annotated files through BlockData interface
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Thu, 21 Feb 2013 21:53:55 +0100
parents 093a2022dad5
children e623aa2ca526
comparison
equal deleted inserted replaced
553:093a2022dad5 554:a5fd757d1b5d
34 import org.tmatesoft.hg.core.HgIterateDirection; 34 import org.tmatesoft.hg.core.HgIterateDirection;
35 import org.tmatesoft.hg.internal.AnnotateFacility; 35 import org.tmatesoft.hg.internal.AnnotateFacility;
36 import org.tmatesoft.hg.internal.IntVector; 36 import org.tmatesoft.hg.internal.IntVector;
37 import org.tmatesoft.hg.internal.AnnotateFacility.AddBlock; 37 import org.tmatesoft.hg.internal.AnnotateFacility.AddBlock;
38 import org.tmatesoft.hg.internal.AnnotateFacility.Block; 38 import org.tmatesoft.hg.internal.AnnotateFacility.Block;
39 import org.tmatesoft.hg.internal.AnnotateFacility.BlockData;
39 import org.tmatesoft.hg.internal.AnnotateFacility.ChangeBlock; 40 import org.tmatesoft.hg.internal.AnnotateFacility.ChangeBlock;
40 import org.tmatesoft.hg.internal.AnnotateFacility.DeleteBlock; 41 import org.tmatesoft.hg.internal.AnnotateFacility.DeleteBlock;
41 import org.tmatesoft.hg.internal.AnnotateFacility.EqualBlock; 42 import org.tmatesoft.hg.internal.AnnotateFacility.EqualBlock;
42 import org.tmatesoft.hg.internal.AnnotateFacility.LineDescriptor; 43 import org.tmatesoft.hg.internal.AnnotateFacility.LineDescriptor;
43 import org.tmatesoft.hg.repo.HgDataFile; 44 import org.tmatesoft.hg.repo.HgDataFile;
192 dump.needRevisions(true); 193 dump.needRevisions(true);
193 af.annotate(df, checkChangeset, dump, HgIterateDirection.OldToNew); 194 af.annotate(df, checkChangeset, dump, HgIterateDirection.OldToNew);
194 } 195 }
195 196
196 private void ccc() throws Exception { 197 private void ccc() throws Exception {
197 HgRepository repo = new HgLookup().detect("/home/artem/hg/test-annotate/"); 198 HgRepository repo = new HgLookup().detect("/home/artem/hg/junit-test-repos/test-annotate/");
198 HgDataFile df = repo.getFileNode("file1"); 199 HgDataFile df = repo.getFileNode("file1");
199 AnnotateFacility af = new AnnotateFacility(); 200 AnnotateFacility af = new AnnotateFacility();
200 DiffOutInspector dump = new DiffOutInspector(System.out); 201 DiffOutInspector dump = new DiffOutInspector(System.out);
201 dump.needRevisions(true); 202 dump.needRevisions(true);
202 af.annotate(df, TIP, dump, HgIterateDirection.OldToNew); 203 af.annotate(df, TIP, dump, HgIterateDirection.OldToNew);
203 System.out.println(); 204 System.out.println();
205 af.annotate(df, TIP, new LineDumpInspector(true), HgIterateDirection.NewToOld);
206 System.out.println();
207 af.annotate(df, TIP, new LineDumpInspector(false), HgIterateDirection.NewToOld);
208 System.out.println();
204 FileAnnotateInspector fa = new FileAnnotateInspector(); 209 FileAnnotateInspector fa = new FileAnnotateInspector();
205 af.annotate(df, TIP, fa); 210 af.annotate(df, TIP, fa);
206 for (int i = 0; i < fa.lineRevisions.length; i++) { 211 for (int i = 0; i < fa.lineRevisions.length; i++) {
207 System.out.printf("%d: LINE %d\n", fa.lineRevisions[i], i+1); 212 System.out.printf("%d: LINE %d\n", fa.lineRevisions[i], i+1);
208 } 213 }
214 // System.out.println(Arrays.equals(new String[] { "a", "bc" }, splitLines("a\nbc"))); 219 // System.out.println(Arrays.equals(new String[] { "a", "bc" }, splitLines("a\nbc")));
215 // System.out.println(Arrays.equals(new String[] { "a", "bc" }, splitLines("a\nbc\n"))); 220 // System.out.println(Arrays.equals(new String[] { "a", "bc" }, splitLines("a\nbc\n")));
216 new TestBlame().ccc(); 221 new TestBlame().ccc();
217 } 222 }
218 223
219 static class DiffOutInspector implements AnnotateFacility.BlockInspector { 224 private static class DiffOutInspector implements AnnotateFacility.BlockInspector {
220 private final PrintStream out; 225 private final PrintStream out;
221 private boolean dumpRevs; 226 private boolean dumpRevs;
222 private IntVector reportedRevisionPairs = new IntVector(); 227 private IntVector reportedRevisionPairs = new IntVector();
223 228
224 DiffOutInspector(PrintStream ps) { 229 DiffOutInspector(PrintStream ps) {
225 out = ps; 230 out = ps;
226 } 231 }
227 232
233 // Note, true makes output incompatible with 'hg diff'
228 public void needRevisions(boolean dumpRevs) { 234 public void needRevisions(boolean dumpRevs) {
229 // Note, true makes output incompatible with 'hg diff'
230 this.dumpRevs = dumpRevs; 235 this.dumpRevs = dumpRevs;
231 } 236 }
232 237
233 private void printRevs(Block b) { 238 private void printRevs(Block b) {
234 if (dumpRevs) { 239 if (dumpRevs) {
255 } 260 }
256 261
257 public void deleted(DeleteBlock block) { 262 public void deleted(DeleteBlock block) {
258 printRevs(block); 263 printRevs(block);
259 out.printf("@@ -%d,%d +%d,0 @@\n", block.firstRemovedLine() + 1, block.totalRemovedLines(), block.removedAt()); 264 out.printf("@@ -%d,%d +%d,0 @@\n", block.firstRemovedLine() + 1, block.totalRemovedLines(), block.removedAt());
260 // String[] lines = block.removedLines();
261 // assert lines.length == block.totalRemovedLines();
262 // for (int i = 0, ln = block.firstRemovedLine(); i < lines.length; i++, ln++) {
263 // linesOld.put(ln, String.format("%3d:---:%s", ln, lines[i]));
264 // }
265 } 265 }
266 266
267 public void changed(ChangeBlock block) { 267 public void changed(ChangeBlock block) {
268 // deleted(block);
269 // added(block);
270 printRevs(block); 268 printRevs(block);
271 out.printf("@@ -%d,%d +%d,%d @@\n", block.firstRemovedLine() + 1, block.totalRemovedLines(), block.firstAddedLine() + 1, block.totalAddedLines()); 269 out.printf("@@ -%d,%d +%d,%d @@\n", block.firstRemovedLine() + 1, block.totalRemovedLines(), block.firstAddedLine() + 1, block.totalAddedLines());
272 } 270 }
273 271
274 public void added(AddBlock block) { 272 public void added(AddBlock block) {
275 printRevs(block); 273 printRevs(block);
276 out.printf("@@ -%d,0 +%d,%d @@\n", block.insertedAt(), block.firstAddedLine() + 1, block.totalAddedLines()); 274 out.printf("@@ -%d,0 +%d,%d @@\n", block.insertedAt(), block.firstAddedLine() + 1, block.totalAddedLines());
277 // String[] addedLines = block.addedLines();
278 // assert addedLines.length == block.totalAddedLines();
279 // for (int i = 0, ln = block.firstAddedLine(), x = addedLines.length; i < x; i++, ln++) {
280 // linesNew.put(ln, String.format("%3d:+++:%s", ln, addedLines[i]));
281 // }
282 } 275 }
283 } 276 }
284 277
285 public static class LineGrepOutputParser implements OutputParser { 278 public static class LineGrepOutputParser implements OutputParser {
286 279
329 } 322 }
330 lineRevisions[lineNumber] = changesetRevIndex; 323 lineRevisions[lineNumber] = changesetRevIndex;
331 } 324 }
332 } 325 }
333 326
327 private static class LineDumpInspector implements AnnotateFacility.BlockInspector {
328
329 private final boolean lineByLine;
330
331 public LineDumpInspector(boolean lineByLine) {
332 this.lineByLine = lineByLine;
333 }
334
335 public void same(EqualBlock block) {
336 }
337
338 public void added(AddBlock block) {
339 BlockData lines = block.addedLines();
340 printBlock(lines, block.targetChangesetIndex(), block.firstAddedLine(), block.totalAddedLines(), "+++");
341 }
342
343 public void changed(ChangeBlock block) {
344 deleted(block);
345 added(block);
346 }
347
348 public void deleted(DeleteBlock block) {
349 BlockData lines = block.removedLines();
350 assert lines.elementCount() == block.totalRemovedLines();
351 printBlock(lines, block.originChangesetIndex(), block.firstRemovedLine(), block.totalRemovedLines(), "---");
352 }
353
354 private void printBlock(BlockData lines, int cset, int first, int length, String marker) {
355 assert lines.elementCount() == length;
356 if (lineByLine) {
357 for (int i = 0, ln = first; i < length; i++, ln++) {
358 String line = new String(lines.elementAt(i).asArray());
359 System.out.printf("%3d:%3d:%s:%s", cset, ln, marker, line);
360 }
361 } else {
362 String content = new String(lines.asArray());
363 System.out.printf("%3d:%s:[%d..%d):\n%s", cset, marker, first, first+length, content);
364 }
365 }
366 }
334 } 367 }