comparison test/org/tmatesoft/hg/test/TestBlame.java @ 603:707b5c7c6fa4

Refactor HgBlameFacility: relevant action methods moved to proper home (HgDataFile), as facility doesn't provide anything but packaging of relevant methods/interfaces
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Mon, 06 May 2013 18:29:57 +0200
parents e49f9d9513fa
children 507602cb4fb3
comparison
equal deleted inserted replaced
602:e3717fc7d26f 603:707b5c7c6fa4
47 import org.tmatesoft.hg.core.HgRepoFacade; 47 import org.tmatesoft.hg.core.HgRepoFacade;
48 import org.tmatesoft.hg.internal.FileAnnotation; 48 import org.tmatesoft.hg.internal.FileAnnotation;
49 import org.tmatesoft.hg.internal.FileAnnotation.LineDescriptor; 49 import org.tmatesoft.hg.internal.FileAnnotation.LineDescriptor;
50 import org.tmatesoft.hg.internal.FileAnnotation.LineInspector; 50 import org.tmatesoft.hg.internal.FileAnnotation.LineInspector;
51 import org.tmatesoft.hg.internal.IntVector; 51 import org.tmatesoft.hg.internal.IntVector;
52 import org.tmatesoft.hg.repo.HgBlameFacility; 52 import org.tmatesoft.hg.repo.HgBlameInspector;
53 import org.tmatesoft.hg.repo.HgBlameFacility.AddBlock; 53 import org.tmatesoft.hg.repo.HgBlameInspector.BlockData;
54 import org.tmatesoft.hg.repo.HgBlameFacility.Block;
55 import org.tmatesoft.hg.repo.HgBlameFacility.BlockData;
56 import org.tmatesoft.hg.repo.HgBlameFacility.ChangeBlock;
57 import org.tmatesoft.hg.repo.HgBlameFacility.DeleteBlock;
58 import org.tmatesoft.hg.repo.HgBlameFacility.EqualBlock;
59 import org.tmatesoft.hg.repo.HgDataFile; 54 import org.tmatesoft.hg.repo.HgDataFile;
60 import org.tmatesoft.hg.repo.HgLookup; 55 import org.tmatesoft.hg.repo.HgLookup;
61 import org.tmatesoft.hg.repo.HgRepository; 56 import org.tmatesoft.hg.repo.HgRepository;
62 import org.tmatesoft.hg.util.Path; 57 import org.tmatesoft.hg.util.Path;
63 58
77 HgRepository repo = new HgLookup().detectFromWorkingDir(); 72 HgRepository repo = new HgLookup().detectFromWorkingDir();
78 final String fname = "src/org/tmatesoft/hg/internal/PatchGenerator.java"; 73 final String fname = "src/org/tmatesoft/hg/internal/PatchGenerator.java";
79 final int checkChangeset = 539; 74 final int checkChangeset = 539;
80 HgDataFile df = repo.getFileNode(fname); 75 HgDataFile df = repo.getFileNode(fname);
81 ByteArrayOutputStream bos = new ByteArrayOutputStream(); 76 ByteArrayOutputStream bos = new ByteArrayOutputStream();
82 new HgBlameFacility(df).annotateSingleRevision(checkChangeset, new DiffOutInspector(new PrintStream(bos))); 77 df.annotateSingleRevision(checkChangeset, new DiffOutInspector(new PrintStream(bos)));
83 LineGrepOutputParser gp = new LineGrepOutputParser("^@@.+"); 78 LineGrepOutputParser gp = new LineGrepOutputParser("^@@.+");
84 ExecHelper eh = new ExecHelper(gp, null); 79 ExecHelper eh = new ExecHelper(gp, null);
85 eh.run("hg", "diff", "-c", String.valueOf(checkChangeset), "-U", "0", fname); 80 eh.run("hg", "diff", "-c", String.valueOf(checkChangeset), "-U", "0", fname);
86 // 81 //
87 String[] apiResult = splitLines(bos.toString()); 82 String[] apiResult = splitLines(bos.toString());
133 128
134 @Test 129 @Test
135 public void testComplexHistoryAnnotate() throws Exception { 130 public void testComplexHistoryAnnotate() throws Exception {
136 HgRepository repo = Configuration.get().find("test-annotate"); 131 HgRepository repo = Configuration.get().find("test-annotate");
137 HgDataFile df = repo.getFileNode("file1"); 132 HgDataFile df = repo.getFileNode("file1");
138 HgBlameFacility af = new HgBlameFacility(df);
139 ByteArrayOutputStream bos = new ByteArrayOutputStream(); 133 ByteArrayOutputStream bos = new ByteArrayOutputStream();
140 DiffOutInspector dump = new DiffOutInspector(new PrintStream(bos)); 134 DiffOutInspector dump = new DiffOutInspector(new PrintStream(bos));
141 af.annotate(TIP, dump, HgIterateDirection.OldToNew); 135 df.annotate(TIP, dump, HgIterateDirection.OldToNew);
142 LinkedList<String> apiResult = new LinkedList<String>(Arrays.asList(splitLines(bos.toString()))); 136 LinkedList<String> apiResult = new LinkedList<String>(Arrays.asList(splitLines(bos.toString())));
143 137
144 /* 138 /*
145 * FIXME this is an ugly hack to deal with the way `hg diff -c <mergeRev>` describes the change 139 * FIXME this is an ugly hack to deal with the way `hg diff -c <mergeRev>` describes the change
146 * and our merge handling approach. For merged revision m, and lines changed both in p1 and p2 140 * and our merge handling approach. For merged revision m, and lines changed both in p1 and p2
194 @Test 188 @Test
195 public void testPartialHistoryFollow() throws Exception { 189 public void testPartialHistoryFollow() throws Exception {
196 HgRepository repo = Configuration.get().find("test-annotate2"); 190 HgRepository repo = Configuration.get().find("test-annotate2");
197 HgDataFile df = repo.getFileNode("file1b.txt"); 191 HgDataFile df = repo.getFileNode("file1b.txt");
198 // rev3: file1 -> file1a, rev7: file1a -> file1b, tip: rev10 192 // rev3: file1 -> file1a, rev7: file1a -> file1b, tip: rev10
199 HgBlameFacility bf = new HgBlameFacility(df);
200 DiffOutInspector insp = new DiffOutInspector(new PrintStream(new OutputStream() { 193 DiffOutInspector insp = new DiffOutInspector(new PrintStream(new OutputStream() {
201 @Override 194 @Override
202 public void write(int b) throws IOException { 195 public void write(int b) throws IOException {
203 // NULL OutputStream 196 // NULL OutputStream
204 } 197 }
205 })); 198 }));
206 // rev6 changes rev4, rev4 changes rev3. Plus, anything changed 199 // rev6 changes rev4, rev4 changes rev3. Plus, anything changed
207 // earlier than rev2 shall be reported as new from change3 200 // earlier than rev2 shall be reported as new from change3
208 int[] change_2_8_new2old = new int[] {4, 6, 3, 4, -1, 3}; 201 int[] change_2_8_new2old = new int[] {4, 6, 3, 4, -1, 3};
209 int[] change_2_8_old2new = new int[] {-1, 3, 3, 4, 4, 6 }; 202 int[] change_2_8_old2new = new int[] {-1, 3, 3, 4, 4, 6 };
210 bf.annotate(2, 8, insp, NewToOld); 203 df.annotate(2, 8, insp, NewToOld);
211 Assert.assertArrayEquals(change_2_8_new2old, insp.getReportedRevisionPairs()); 204 Assert.assertArrayEquals(change_2_8_new2old, insp.getReportedRevisionPairs());
212 insp.reset(); 205 insp.reset();
213 bf.annotate(2, 8, insp, OldToNew); 206 df.annotate(2, 8, insp, OldToNew);
214 Assert.assertArrayEquals(change_2_8_old2new, insp.getReportedRevisionPairs()); 207 Assert.assertArrayEquals(change_2_8_old2new, insp.getReportedRevisionPairs());
215 // same as 2 to 8, with addition of rev9 changes rev7 (rev6 to rev7 didn't change content, only name) 208 // same as 2 to 8, with addition of rev9 changes rev7 (rev6 to rev7 didn't change content, only name)
216 int[] change_3_9_new2old = new int[] {7, 9, 4, 6, 3, 4, -1, 3 }; 209 int[] change_3_9_new2old = new int[] {7, 9, 4, 6, 3, 4, -1, 3 };
217 int[] change_3_9_old2new = new int[] {-1, 3, 3, 4, 4, 6, 7, 9 }; 210 int[] change_3_9_old2new = new int[] {-1, 3, 3, 4, 4, 6, 7, 9 };
218 insp.reset(); 211 insp.reset();
219 bf.annotate(3, 9, insp, NewToOld); 212 df.annotate(3, 9, insp, NewToOld);
220 Assert.assertArrayEquals(change_3_9_new2old, insp.getReportedRevisionPairs()); 213 Assert.assertArrayEquals(change_3_9_new2old, insp.getReportedRevisionPairs());
221 insp.reset(); 214 insp.reset();
222 bf.annotate(3, 9, insp, OldToNew); 215 df.annotate(3, 9, insp, OldToNew);
223 Assert.assertArrayEquals(change_3_9_old2new, insp.getReportedRevisionPairs()); 216 Assert.assertArrayEquals(change_3_9_old2new, insp.getReportedRevisionPairs());
224 } 217 }
225 218
226 @Test 219 @Test
227 public void testAnnotateCmdFollowNoFollow() throws Exception { 220 public void testAnnotateCmdFollowNoFollow() throws Exception {
295 private void aaa() throws Exception { 288 private void aaa() throws Exception {
296 HgRepository repo = new HgLookup().detectFromWorkingDir(); 289 HgRepository repo = new HgLookup().detectFromWorkingDir();
297 final String fname = "src/org/tmatesoft/hg/internal/PatchGenerator.java"; 290 final String fname = "src/org/tmatesoft/hg/internal/PatchGenerator.java";
298 final int checkChangeset = 539; 291 final int checkChangeset = 539;
299 HgDataFile df = repo.getFileNode(fname); 292 HgDataFile df = repo.getFileNode(fname);
300 HgBlameFacility af = new HgBlameFacility(df);
301 DiffOutInspector dump = new DiffOutInspector(System.out); 293 DiffOutInspector dump = new DiffOutInspector(System.out);
302 System.out.println("541 -> 543"); 294 System.out.println("541 -> 543");
303 af.annotateSingleRevision(543, dump); 295 df.annotateSingleRevision(543, dump);
304 System.out.println("539 -> 541"); 296 System.out.println("539 -> 541");
305 af.annotateSingleRevision(541, dump); 297 df.annotateSingleRevision(541, dump);
306 System.out.println("536 -> 539"); 298 System.out.println("536 -> 539");
307 af.annotateSingleRevision(checkChangeset, dump); 299 df.annotateSingleRevision(checkChangeset, dump);
308 System.out.println("531 -> 536"); 300 System.out.println("531 -> 536");
309 af.annotateSingleRevision(536, dump); 301 df.annotateSingleRevision(536, dump);
310 System.out.println(" -1 -> 531"); 302 System.out.println(" -1 -> 531");
311 af.annotateSingleRevision(531, dump); 303 df.annotateSingleRevision(531, dump);
312 304
313 FileAnnotateInspector fai = new FileAnnotateInspector(); 305 FileAnnotateInspector fai = new FileAnnotateInspector();
314 FileAnnotation.annotate(df, 541, fai); 306 FileAnnotation.annotate(df, 541, fai);
315 for (int i = 0; i < fai.lineRevisions.length; i++) { 307 for (int i = 0; i < fai.lineRevisions.length; i++) {
316 System.out.printf("%3d: LINE %d\n", fai.lineRevisions[i], i+1); 308 System.out.printf("%3d: LINE %d\n", fai.lineRevisions[i], i+1);
320 private void bbb() throws Exception { 312 private void bbb() throws Exception {
321 HgRepository repo = new HgLookup().detectFromWorkingDir(); 313 HgRepository repo = new HgLookup().detectFromWorkingDir();
322 final String fname = "src/org/tmatesoft/hg/repo/HgManifest.java"; 314 final String fname = "src/org/tmatesoft/hg/repo/HgManifest.java";
323 final int checkChangeset = 415; 315 final int checkChangeset = 415;
324 HgDataFile df = repo.getFileNode(fname); 316 HgDataFile df = repo.getFileNode(fname);
325 HgBlameFacility af = new HgBlameFacility(df);
326 DiffOutInspector dump = new DiffOutInspector(System.out); 317 DiffOutInspector dump = new DiffOutInspector(System.out);
327 // System.out.println("413 -> 415"); 318 // System.out.println("413 -> 415");
328 // af.diff(df, 413, 415, dump); 319 // af.diff(df, 413, 415, dump);
329 // System.out.println("408 -> 415"); 320 // System.out.println("408 -> 415");
330 // af.diff(df, 408, 415, dump); 321 // af.diff(df, 408, 415, dump);
331 // System.out.println("Combined (with merge):"); 322 // System.out.println("Combined (with merge):");
332 // dump.needRevisions(true); 323 // dump.needRevisions(true);
333 // af.annotateChange(df, checkChangeset, dump); 324 // af.annotateChange(df, checkChangeset, dump);
334 dump.needRevisions(true); 325 dump.needRevisions(true);
335 af.annotate(checkChangeset, dump, HgIterateDirection.OldToNew); 326 df.annotate(checkChangeset, dump, HgIterateDirection.OldToNew);
336 } 327 }
337 328
338 private void ccc() throws Throwable { 329 private void ccc() throws Throwable {
339 HgRepository repo = new HgLookup().detect("/home/artem/hg/hgtest-annotate-merge/"); 330 HgRepository repo = new HgLookup().detect("/home/artem/hg/hgtest-annotate-merge/");
340 HgDataFile df = repo.getFileNode("file.txt"); 331 HgDataFile df = repo.getFileNode("file.txt");
341 HgBlameFacility af = new HgBlameFacility(df);
342 DiffOutInspector dump = new DiffOutInspector(System.out); 332 DiffOutInspector dump = new DiffOutInspector(System.out);
343 dump.needRevisions(true); 333 dump.needRevisions(true);
344 af.annotate(8, dump, HgIterateDirection.NewToOld); 334 df.annotate(8, dump, HgIterateDirection.NewToOld);
345 // af.annotateSingleRevision(df, 113, dump); 335 // af.annotateSingleRevision(df, 113, dump);
346 // System.out.println(); 336 // System.out.println();
347 // af.annotate(df, TIP, new LineDumpInspector(true), HgIterateDirection.NewToOld); 337 // af.annotate(df, TIP, new LineDumpInspector(true), HgIterateDirection.NewToOld);
348 // System.out.println(); 338 // System.out.println();
349 // af.annotate(df, TIP, new LineDumpInspector(false), HgIterateDirection.NewToOld); 339 // af.annotate(df, TIP, new LineDumpInspector(false), HgIterateDirection.NewToOld);
370 // System.out.println(Arrays.equals(new String[] { "a", "bc" }, splitLines("a\nbc\n"))); 360 // System.out.println(Arrays.equals(new String[] { "a", "bc" }, splitLines("a\nbc\n")));
371 TestBlame tt = new TestBlame(); 361 TestBlame tt = new TestBlame();
372 tt.ccc(); 362 tt.ccc();
373 } 363 }
374 364
375 private static class DiffOutInspector implements HgBlameFacility.Inspector { 365 private static class DiffOutInspector implements HgBlameInspector {
376 private final PrintStream out; 366 private final PrintStream out;
377 private boolean dumpRevs; 367 private boolean dumpRevs;
378 private IntVector reportedRevisionPairs = new IntVector(); 368 private IntVector reportedRevisionPairs = new IntVector();
379 369
380 DiffOutInspector(PrintStream ps) { 370 DiffOutInspector(PrintStream ps) {
488 public String line(int i) { 478 public String line(int i) {
489 return lines[i]; 479 return lines[i];
490 } 480 }
491 } 481 }
492 482
493 private static class LineDumpInspector implements HgBlameFacility.Inspector { 483 @SuppressWarnings("unused")
484 private static class LineDumpInspector implements HgBlameInspector {
494 485
495 private final boolean lineByLine; 486 private final boolean lineByLine;
496 487
497 public LineDumpInspector(boolean lineByLine) { 488 public LineDumpInspector(boolean lineByLine) {
498 this.lineByLine = lineByLine; 489 this.lineByLine = lineByLine;