You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ObjectWalkTest.java 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * Copyright (C) 2009-2010, Google Inc. and others
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Distribution License v. 1.0 which is available at
  6. * https://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. package org.eclipse.jgit.revwalk;
  11. import static org.junit.Assert.assertNull;
  12. import static org.junit.Assert.assertSame;
  13. import static org.junit.Assert.assertTrue;
  14. import org.eclipse.jgit.lib.FileMode;
  15. import org.eclipse.jgit.lib.ObjectId;
  16. import org.eclipse.jgit.lib.ObjectInserter;
  17. import org.eclipse.jgit.lib.TreeFormatter;
  18. import org.junit.Test;
  19. public class ObjectWalkTest extends RevWalkTestCase {
  20. protected ObjectWalk objw;
  21. @Override
  22. protected RevWalk createRevWalk() {
  23. return objw = new ObjectWalk(db);
  24. }
  25. @Test
  26. public void testNoCommits() throws Exception {
  27. assertNull(objw.next());
  28. assertNull(objw.nextObject());
  29. }
  30. @Test
  31. public void testTwoCommitsEmptyTree() throws Exception {
  32. final RevCommit a = commit();
  33. final RevCommit b = commit(a);
  34. markStart(b);
  35. assertCommit(b, objw.next());
  36. assertCommit(a, objw.next());
  37. assertNull(objw.next());
  38. assertSame(tree(), objw.nextObject());
  39. assertNull(objw.nextObject());
  40. }
  41. @Test
  42. public void testOneCommitOneTreeTwoBlob() throws Exception {
  43. final RevBlob f0 = blob("0");
  44. final RevBlob f1 = blob("1");
  45. final RevTree t = tree(file("0", f0), file("1", f1), file("2", f1));
  46. final RevCommit a = commit(t);
  47. markStart(a);
  48. assertCommit(a, objw.next());
  49. assertNull(objw.next());
  50. assertSame(t, objw.nextObject());
  51. assertSame(f0, objw.nextObject());
  52. assertSame(f1, objw.nextObject());
  53. assertNull(objw.nextObject());
  54. }
  55. @Test
  56. public void testTwoCommitTwoTreeTwoBlob() throws Exception {
  57. final RevBlob f0 = blob("0");
  58. final RevBlob f1 = blob("1");
  59. final RevBlob f2 = blob("0v2");
  60. final RevTree ta = tree(file("0", f0), file("1", f1), file("2", f1));
  61. final RevTree tb = tree(file("0", f2), file("1", f1), file("2", f1));
  62. final RevCommit a = commit(ta);
  63. final RevCommit b = commit(tb, a);
  64. markStart(b);
  65. assertCommit(b, objw.next());
  66. assertCommit(a, objw.next());
  67. assertNull(objw.next());
  68. assertSame(tb, objw.nextObject());
  69. assertSame(f2, objw.nextObject());
  70. assertSame(f1, objw.nextObject());
  71. assertSame(ta, objw.nextObject());
  72. assertSame(f0, objw.nextObject());
  73. assertNull(objw.nextObject());
  74. }
  75. @Test
  76. public void testTwoCommitDeepTree1() throws Exception {
  77. final RevBlob f0 = blob("0");
  78. final RevBlob f1 = blob("0v2");
  79. final RevTree ta = tree(file("a/b/0", f0));
  80. final RevTree tb = tree(file("a/b/1", f1));
  81. final RevCommit a = commit(ta);
  82. final RevCommit b = commit(tb, a);
  83. markStart(b);
  84. assertCommit(b, objw.next());
  85. assertCommit(a, objw.next());
  86. assertNull(objw.next());
  87. assertSame(tb, objw.nextObject());
  88. assertSame(get(tb, "a"), objw.nextObject());
  89. assertSame(get(tb, "a/b"), objw.nextObject());
  90. assertSame(f1, objw.nextObject());
  91. assertSame(ta, objw.nextObject());
  92. assertSame(get(ta, "a"), objw.nextObject());
  93. assertSame(get(ta, "a/b"), objw.nextObject());
  94. assertSame(f0, objw.nextObject());
  95. assertNull(objw.nextObject());
  96. }
  97. @Test
  98. public void testTwoCommitDeepTree2() throws Exception {
  99. final RevBlob f1 = blob("1");
  100. final RevTree ta = tree(file("a/b/0", f1), file("a/c/q", f1));
  101. final RevTree tb = tree(file("a/b/1", f1), file("a/c/q", f1));
  102. final RevCommit a = commit(ta);
  103. final RevCommit b = commit(tb, a);
  104. markStart(b);
  105. assertCommit(b, objw.next());
  106. assertCommit(a, objw.next());
  107. assertNull(objw.next());
  108. assertSame(tb, objw.nextObject());
  109. assertSame(get(tb, "a"), objw.nextObject());
  110. assertSame(get(tb, "a/b"), objw.nextObject());
  111. assertSame(f1, objw.nextObject());
  112. assertSame(get(tb, "a/c"), objw.nextObject());
  113. assertSame(ta, objw.nextObject());
  114. assertSame(get(ta, "a"), objw.nextObject());
  115. assertSame(get(ta, "a/b"), objw.nextObject());
  116. assertNull(objw.nextObject());
  117. }
  118. @Test
  119. public void testCull() throws Exception {
  120. final RevBlob f1 = blob("1");
  121. final RevBlob f2 = blob("2");
  122. final RevBlob f3 = blob("3");
  123. final RevBlob f4 = blob("4");
  124. final RevTree ta = tree(file("a/1", f1), file("c/3", f3));
  125. final RevCommit a = commit(ta);
  126. final RevTree tb = tree(file("a/1", f2), file("c/3", f3));
  127. final RevCommit b1 = commit(tb, a);
  128. final RevCommit b2 = commit(tb, b1);
  129. final RevTree tc = tree(file("a/1", f4));
  130. final RevCommit c1 = commit(tc, a);
  131. final RevCommit c2 = commit(tc, c1);
  132. markStart(b2);
  133. markUninteresting(c2);
  134. assertCommit(b2, objw.next());
  135. assertCommit(b1, objw.next());
  136. assertNull(objw.next());
  137. assertTrue(a.has(RevFlag.UNINTERESTING));
  138. assertTrue(ta.has(RevFlag.UNINTERESTING));
  139. assertTrue(f1.has(RevFlag.UNINTERESTING));
  140. assertTrue(f3.has(RevFlag.UNINTERESTING));
  141. assertSame(tb, objw.nextObject());
  142. assertSame(get(tb, "a"), objw.nextObject());
  143. assertSame(f2, objw.nextObject());
  144. assertNull(objw.nextObject());
  145. }
  146. @Test
  147. public void testEmptyTreeCorruption() throws Exception {
  148. ObjectId bId = ObjectId
  149. .fromString("abbbfafe3129f85747aba7bfac992af77134c607");
  150. final RevTree tree_root, tree_A, tree_AB;
  151. final RevCommit b;
  152. try (ObjectInserter inserter = db.newObjectInserter()) {
  153. ObjectId empty = inserter.insert(new TreeFormatter());
  154. TreeFormatter A = new TreeFormatter();
  155. A.append("A", FileMode.TREE, empty);
  156. A.append("B", FileMode.TREE, empty);
  157. ObjectId idA = inserter.insert(A);
  158. TreeFormatter root = new TreeFormatter();
  159. root.append("A", FileMode.TREE, idA);
  160. root.append("B", FileMode.REGULAR_FILE, bId);
  161. ObjectId idRoot = inserter.insert(root);
  162. inserter.flush();
  163. tree_root = objw.parseTree(idRoot);
  164. tree_A = objw.parseTree(idA);
  165. tree_AB = objw.parseTree(empty);
  166. b = commit(tree_root);
  167. }
  168. markStart(b);
  169. assertCommit(b, objw.next());
  170. assertNull(objw.next());
  171. assertSame(tree_root, objw.nextObject());
  172. assertSame(tree_A, objw.nextObject());
  173. assertSame(tree_AB, objw.nextObject());
  174. assertSame(rw.lookupBlob(bId), objw.nextObject());
  175. assertNull(objw.nextObject());
  176. }
  177. @Test
  178. public void testSkipTreeWhenStartFromBlob() throws Exception {
  179. final RevBlob f1 = blob("1");
  180. objw.markStart(f1);
  181. assertSame(f1, objw.nextObject());
  182. objw.skipTree();
  183. }
  184. }