comparison test/org/tmatesoft/hg/test/TestBlame.java @ 630:72c979555cb8

HgDiffCommand. Do not use deprecated code. Javadoc
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Thu, 23 May 2013 19:44:28 +0200
parents 5f52074707b2
children 54e16ab771ec
comparison
equal deleted inserted replaced
629:5f52074707b2 630:72c979555cb8
40 import org.junit.Assert; 40 import org.junit.Assert;
41 import org.junit.Rule; 41 import org.junit.Rule;
42 import org.junit.Test; 42 import org.junit.Test;
43 import org.tmatesoft.hg.core.HgAnnotateCommand; 43 import org.tmatesoft.hg.core.HgAnnotateCommand;
44 import org.tmatesoft.hg.core.HgAnnotateCommand.LineInfo; 44 import org.tmatesoft.hg.core.HgAnnotateCommand.LineInfo;
45 import org.tmatesoft.hg.core.HgBlameInspector;
45 import org.tmatesoft.hg.core.HgCallbackTargetException; 46 import org.tmatesoft.hg.core.HgCallbackTargetException;
46 import org.tmatesoft.hg.core.HgIterateDirection; 47 import org.tmatesoft.hg.core.HgDiffCommand;
47 import org.tmatesoft.hg.core.HgRepoFacade; 48 import org.tmatesoft.hg.core.HgRepoFacade;
48 import org.tmatesoft.hg.core.Nodeid; 49 import org.tmatesoft.hg.core.Nodeid;
49 import org.tmatesoft.hg.internal.FileAnnotation; 50 import org.tmatesoft.hg.internal.FileAnnotation;
50 import org.tmatesoft.hg.internal.FileAnnotation.LineDescriptor; 51 import org.tmatesoft.hg.internal.FileAnnotation.LineDescriptor;
51 import org.tmatesoft.hg.internal.FileAnnotation.LineInspector; 52 import org.tmatesoft.hg.internal.FileAnnotation.LineInspector;
52 import org.tmatesoft.hg.internal.IntVector; 53 import org.tmatesoft.hg.internal.IntVector;
53 import org.tmatesoft.hg.repo.HgBlameInspector;
54 import org.tmatesoft.hg.repo.HgChangelog; 54 import org.tmatesoft.hg.repo.HgChangelog;
55 import org.tmatesoft.hg.repo.HgDataFile; 55 import org.tmatesoft.hg.repo.HgDataFile;
56 import org.tmatesoft.hg.repo.HgLookup; 56 import org.tmatesoft.hg.repo.HgLookup;
57 import org.tmatesoft.hg.repo.HgRepository; 57 import org.tmatesoft.hg.repo.HgRepository;
58 import org.tmatesoft.hg.util.Path; 58 import org.tmatesoft.hg.util.Path;
73 HgRepository repo = new HgLookup().detectFromWorkingDir(); 73 HgRepository repo = new HgLookup().detectFromWorkingDir();
74 final String fname = "src/org/tmatesoft/hg/internal/PatchGenerator.java"; 74 final String fname = "src/org/tmatesoft/hg/internal/PatchGenerator.java";
75 final int checkChangeset = repo.getChangelog().getRevisionIndex(Nodeid.fromAscii("946b131962521f9199e1fedbdc2487d3aaef5e46")); // 539 75 final int checkChangeset = repo.getChangelog().getRevisionIndex(Nodeid.fromAscii("946b131962521f9199e1fedbdc2487d3aaef5e46")); // 539
76 HgDataFile df = repo.getFileNode(fname); 76 HgDataFile df = repo.getFileNode(fname);
77 ByteArrayOutputStream bos = new ByteArrayOutputStream(); 77 ByteArrayOutputStream bos = new ByteArrayOutputStream();
78 df.annotateSingleRevision(checkChangeset, new DiffOutInspector(new PrintStream(bos))); 78 HgDiffCommand diffCmd = new HgDiffCommand(repo);
79 diffCmd.file(df).changeset(checkChangeset);
80 diffCmd.executeAnnotateSingleRevision(new DiffOutInspector(new PrintStream(bos)));
79 LineGrepOutputParser gp = new LineGrepOutputParser("^@@.+"); 81 LineGrepOutputParser gp = new LineGrepOutputParser("^@@.+");
80 ExecHelper eh = new ExecHelper(gp, null); 82 ExecHelper eh = new ExecHelper(gp, null);
81 eh.run("hg", "diff", "-c", String.valueOf(checkChangeset), "-U", "0", fname); 83 eh.run("hg", "diff", "-c", String.valueOf(checkChangeset), "-U", "0", fname);
82 // 84 //
83 String[] apiResult = splitLines(bos.toString()); 85 String[] apiResult = splitLines(bos.toString());
90 HgRepository repo = new HgLookup().detectFromWorkingDir(); 92 HgRepository repo = new HgLookup().detectFromWorkingDir();
91 final String fname = "src/org/tmatesoft/hg/internal/PatchGenerator.java"; 93 final String fname = "src/org/tmatesoft/hg/internal/PatchGenerator.java";
92 HgDataFile df = repo.getFileNode(fname); 94 HgDataFile df = repo.getFileNode(fname);
93 AnnotateRunner ar = new AnnotateRunner(df.getPath(), null); 95 AnnotateRunner ar = new AnnotateRunner(df.getPath(), null);
94 96
97 final HgDiffCommand diffCmd = new HgDiffCommand(repo);
98 diffCmd.file(df).order(NewToOld);
95 final HgChangelog clog = repo.getChangelog(); 99 final HgChangelog clog = repo.getChangelog();
96 final int[] toTest = new int[] { 100 final int[] toTest = new int[] {
97 clog.getRevisionIndex(Nodeid.fromAscii("946b131962521f9199e1fedbdc2487d3aaef5e46")), // 539 101 clog.getRevisionIndex(Nodeid.fromAscii("946b131962521f9199e1fedbdc2487d3aaef5e46")), // 539
98 clog.getRevisionIndex(Nodeid.fromAscii("1e95f48d9886abe79b9711ab371bc877ca5e773e")), // 541 102 clog.getRevisionIndex(Nodeid.fromAscii("1e95f48d9886abe79b9711ab371bc877ca5e773e")), // 541
99 /*, TIP */}; 103 /*, TIP */};
100 for (int cs : toTest) { 104 for (int cs : toTest) {
101 ar.run(cs, false); 105 ar.run(cs, false);
102 FileAnnotateInspector fa = new FileAnnotateInspector(); 106 FileAnnotateInspector fa = new FileAnnotateInspector();
103 FileAnnotation.annotate(df, cs, fa); 107 diffCmd.range(0, cs);
108 diffCmd.executeAnnotate(new FileAnnotation(fa));
104 doAnnotateLineCheck(cs, ar.getLines(), Arrays.asList(fa.lineRevisions), Arrays.asList(fa.lines)); 109 doAnnotateLineCheck(cs, ar.getLines(), Arrays.asList(fa.lineRevisions), Arrays.asList(fa.lines));
105 } 110 }
106 } 111 }
107 112
108 @Test 113 @Test
109 public void testFileLineAnnotate2() throws Exception { 114 public void testFileLineAnnotate2() throws Exception {
110 HgRepository repo = Configuration.get().find("test-annotate"); 115 HgRepository repo = Configuration.get().find("test-annotate");
111 HgDataFile df = repo.getFileNode("file1"); 116 HgDataFile df = repo.getFileNode("file1");
112 AnnotateRunner ar = new AnnotateRunner(df.getPath(), repo.getWorkingDir()); 117 AnnotateRunner ar = new AnnotateRunner(df.getPath(), repo.getWorkingDir());
113 118
119 final HgDiffCommand diffCmd = new HgDiffCommand(repo).file(df).order(NewToOld);
114 for (int cs : new int[] { 4, 6 /*, 8 see below*/, TIP}) { 120 for (int cs : new int[] { 4, 6 /*, 8 see below*/, TIP}) {
115 ar.run(cs, false); 121 ar.run(cs, false);
116 FileAnnotateInspector fa = new FileAnnotateInspector(); 122 FileAnnotateInspector fa = new FileAnnotateInspector();
117 FileAnnotation.annotate(df, cs, fa); 123 diffCmd.range(0, cs);
124 diffCmd.executeAnnotate(new FileAnnotation(fa));
118 doAnnotateLineCheck(cs, ar.getLines(), Arrays.asList(fa.lineRevisions), Arrays.asList(fa.lines)); 125 doAnnotateLineCheck(cs, ar.getLines(), Arrays.asList(fa.lineRevisions), Arrays.asList(fa.lines));
119 } 126 }
120 /*`hg annotate -r 8` and HgBlameFacility give different result 127 /*`hg annotate -r 8` and HgBlameFacility give different result
121 * for "r0, line 5" line, which was deleted in rev2 and restored back in 128 * for "r0, line 5" line, which was deleted in rev2 and restored back in
122 * rev4 (both in default branch), while branch with r3 and r6 kept the line intact. 129 * rev4 (both in default branch), while branch with r3 and r6 kept the line intact.
136 public void testComplexHistoryAnnotate() throws Exception { 143 public void testComplexHistoryAnnotate() throws Exception {
137 HgRepository repo = Configuration.get().find("test-annotate"); 144 HgRepository repo = Configuration.get().find("test-annotate");
138 HgDataFile df = repo.getFileNode("file1"); 145 HgDataFile df = repo.getFileNode("file1");
139 ByteArrayOutputStream bos = new ByteArrayOutputStream(); 146 ByteArrayOutputStream bos = new ByteArrayOutputStream();
140 DiffOutInspector dump = new DiffOutInspector(new PrintStream(bos)); 147 DiffOutInspector dump = new DiffOutInspector(new PrintStream(bos));
141 df.annotate(0, TIP, dump, HgIterateDirection.OldToNew); 148 HgDiffCommand diffCmd = new HgDiffCommand(repo);
149 diffCmd.file(df).range(0, TIP).order(OldToNew);
150 diffCmd.executeAnnotate(dump);
142 LinkedList<String> apiResult = new LinkedList<String>(Arrays.asList(splitLines(bos.toString()))); 151 LinkedList<String> apiResult = new LinkedList<String>(Arrays.asList(splitLines(bos.toString())));
143 152
144 /* 153 /*
145 * FIXME this is an ugly hack to deal with the way `hg diff -c <mergeRev>` describes the change 154 * 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 155 * and our merge handling approach. For merged revision m, and lines changed both in p1 and p2
204 })); 213 }));
205 // rev6 changes rev4, rev4 changes rev3. Plus, anything changed 214 // rev6 changes rev4, rev4 changes rev3. Plus, anything changed
206 // earlier than rev2 shall be reported as new from change3 215 // earlier than rev2 shall be reported as new from change3
207 int[] change_2_8_new2old = new int[] {4, 6, 3, 4, -1, 3}; 216 int[] change_2_8_new2old = new int[] {4, 6, 3, 4, -1, 3};
208 int[] change_2_8_old2new = new int[] {-1, 3, 3, 4, 4, 6 }; 217 int[] change_2_8_old2new = new int[] {-1, 3, 3, 4, 4, 6 };
209 df.annotate(2, 8, insp, NewToOld); 218 final HgDiffCommand cmd = new HgDiffCommand(repo);
219 cmd.file(df);
220 cmd.range(2, 8).order(NewToOld);
221 cmd.executeAnnotate(insp);
210 Assert.assertArrayEquals(change_2_8_new2old, insp.getReportedRevisionPairs()); 222 Assert.assertArrayEquals(change_2_8_new2old, insp.getReportedRevisionPairs());
211 insp.reset(); 223 insp.reset();
212 df.annotate(2, 8, insp, OldToNew); 224 cmd.order(OldToNew).executeAnnotate(insp);
213 Assert.assertArrayEquals(change_2_8_old2new, insp.getReportedRevisionPairs()); 225 Assert.assertArrayEquals(change_2_8_old2new, insp.getReportedRevisionPairs());
214 // same as 2 to 8, with addition of rev9 changes rev7 (rev6 to rev7 didn't change content, only name) 226 // same as 2 to 8, with addition of rev9 changes rev7 (rev6 to rev7 didn't change content, only name)
215 int[] change_3_9_new2old = new int[] {7, 9, 4, 6, 3, 4, -1, 3 }; 227 int[] change_3_9_new2old = new int[] {7, 9, 4, 6, 3, 4, -1, 3 };
216 int[] change_3_9_old2new = new int[] {-1, 3, 3, 4, 4, 6, 7, 9 }; 228 int[] change_3_9_old2new = new int[] {-1, 3, 3, 4, 4, 6, 7, 9 };
217 insp.reset(); 229 insp.reset();
218 df.annotate(3, 9, insp, NewToOld); 230 cmd.range(3, 9).order(NewToOld).executeAnnotate(insp);
219 Assert.assertArrayEquals(change_3_9_new2old, insp.getReportedRevisionPairs()); 231 Assert.assertArrayEquals(change_3_9_new2old, insp.getReportedRevisionPairs());
220 insp.reset(); 232 insp.reset();
221 df.annotate(3, 9, insp, OldToNew); 233 cmd.order(OldToNew).executeAnnotate(insp);
222 Assert.assertArrayEquals(change_3_9_old2new, insp.getReportedRevisionPairs()); 234 Assert.assertArrayEquals(change_3_9_old2new, insp.getReportedRevisionPairs());
223 } 235 }
224 236
225 @Test 237 @Test
226 public void testAnnotateCmdFollowNoFollow() throws Exception { 238 public void testAnnotateCmdFollowNoFollow() throws Exception {
267 HgRepository repo = Configuration.get().find("test-annotate"); 279 HgRepository repo = Configuration.get().find("test-annotate");
268 HgDataFile df = repo.getFileNode("file1"); 280 HgDataFile df = repo.getFileNode("file1");
269 LineGrepOutputParser gp = new LineGrepOutputParser("^@@.+"); 281 LineGrepOutputParser gp = new LineGrepOutputParser("^@@.+");
270 ExecHelper eh = new ExecHelper(gp, repo.getWorkingDir()); 282 ExecHelper eh = new ExecHelper(gp, repo.getWorkingDir());
271 int[] toTest = { 3, 4, 5 }; // p1 ancestry line, p2 ancestry line, not in ancestry line 283 int[] toTest = { 3, 4, 5 }; // p1 ancestry line, p2 ancestry line, not in ancestry line
284 final HgDiffCommand diffCmd = new HgDiffCommand(repo).file(df);
272 for (int cs : toTest) { 285 for (int cs : toTest) {
273 ByteArrayOutputStream bos = new ByteArrayOutputStream(); 286 ByteArrayOutputStream bos = new ByteArrayOutputStream();
274 df.diff(cs, 8, new DiffOutInspector(new PrintStream(bos))); 287 diffCmd.range(cs, 8).executeDiff(new DiffOutInspector(new PrintStream(bos)));
275 eh.run("hg", "diff", "-r", String.valueOf(cs), "-r", "8", "-U", "0", df.getPath().toString()); 288 eh.run("hg", "diff", "-r", String.valueOf(cs), "-r", "8", "-U", "0", df.getPath().toString());
276 // 289 //
277 String[] apiResult = splitLines(bos.toString()); 290 String[] apiResult = splitLines(bos.toString());
278 String[] expected = splitLines(gp.result()); 291 String[] expected = splitLines(gp.result());
279 Assert.assertArrayEquals("diff -r " + cs + " - r 8", expected, apiResult); 292 Assert.assertArrayEquals("diff -r " + cs + "-r 8", expected, apiResult);
280 gp.reset(); 293 gp.reset();
281 } 294 }
282 } 295 }
283 296
284 /** 297 /**
291 LineGrepOutputParser gp = new LineGrepOutputParser("^@@.+"); 304 LineGrepOutputParser gp = new LineGrepOutputParser("^@@.+");
292 ExecHelper eh = new ExecHelper(gp, repo.getWorkingDir()); 305 ExecHelper eh = new ExecHelper(gp, repo.getWorkingDir());
293 eh.run("hg", "diff", "-c", "0", "-U", "0", df.getPath().toString()); 306 eh.run("hg", "diff", "-c", "0", "-U", "0", df.getPath().toString());
294 // 307 //
295 ByteArrayOutputStream bos = new ByteArrayOutputStream(); 308 ByteArrayOutputStream bos = new ByteArrayOutputStream();
296 df.annotateSingleRevision(0, new DiffOutInspector(new PrintStream(bos))); 309 HgDiffCommand diffCmd = new HgDiffCommand(repo).file(df);
310 diffCmd.changeset(0).executeAnnotateSingleRevision(new DiffOutInspector(new PrintStream(bos)));
297 // 311 //
298 String[] apiResult = splitLines(bos.toString()); 312 String[] apiResult = splitLines(bos.toString());
299 String[] expected = splitLines(gp.result()); 313 String[] expected = splitLines(gp.result());
300 Assert.assertArrayEquals(expected, apiResult); 314 Assert.assertArrayEquals(expected, apiResult);
301 } 315 }
333 private void ccc() throws Throwable { 347 private void ccc() throws Throwable {
334 HgRepository repo = new HgLookup().detect("/home/artem/hg/hgtest-annotate-merge/"); 348 HgRepository repo = new HgLookup().detect("/home/artem/hg/hgtest-annotate-merge/");
335 HgDataFile df = repo.getFileNode("file.txt"); 349 HgDataFile df = repo.getFileNode("file.txt");
336 DiffOutInspector dump = new DiffOutInspector(System.out); 350 DiffOutInspector dump = new DiffOutInspector(System.out);
337 dump.needRevisions(true); 351 dump.needRevisions(true);
338 df.annotate(0, 8, dump, HgIterateDirection.NewToOld); 352 HgDiffCommand diffCmd = new HgDiffCommand(repo);
353 diffCmd.file(df);
354 diffCmd.range(0, 8).order(NewToOld);
355 diffCmd.executeAnnotate(dump);
339 // af.annotateSingleRevision(df, 113, dump); 356 // af.annotateSingleRevision(df, 113, dump);
340 // System.out.println(); 357 // System.out.println();
341 // af.annotate(df, TIP, new LineDumpInspector(true), HgIterateDirection.NewToOld); 358 // af.annotate(df, TIP, new LineDumpInspector(true), HgIterateDirection.NewToOld);
342 // System.out.println(); 359 // System.out.println();
343 // af.annotate(df, TIP, new LineDumpInspector(false), HgIterateDirection.NewToOld); 360 // af.annotate(df, TIP, new LineDumpInspector(false), HgIterateDirection.NewToOld);
349 doLineAnnotateTest(df, cs, op); 366 doLineAnnotateTest(df, cs, op);
350 } 367 }
351 errorCollector.verify(); 368 errorCollector.verify();
352 */ 369 */
353 FileAnnotateInspector fa = new FileAnnotateInspector(); 370 FileAnnotateInspector fa = new FileAnnotateInspector();
354 FileAnnotation.annotate(df, 8, fa); 371 diffCmd.range(0, 8).order(NewToOld);
372 diffCmd.executeAnnotate(new FileAnnotation(fa));
355 for (int i = 0; i < fa.lineRevisions.length; i++) { 373 for (int i = 0; i < fa.lineRevisions.length; i++) {
356 System.out.printf("%d: %s", fa.lineRevisions[i], fa.line(i) == null ? "null\n" : fa.line(i)); 374 System.out.printf("%d: %s", fa.lineRevisions[i], fa.line(i) == null ? "null\n" : fa.line(i));
357 } 375 }
358 } 376 }
359 377