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 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. * Copyright (C) 2009-2010, Google Inc.
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.revwalk;
  44. import static org.junit.Assert.assertNull;
  45. import static org.junit.Assert.assertSame;
  46. import static org.junit.Assert.assertTrue;
  47. import org.eclipse.jgit.lib.Constants;
  48. import org.eclipse.jgit.lib.FileTreeEntry;
  49. import org.eclipse.jgit.lib.ObjectId;
  50. import org.eclipse.jgit.lib.ObjectInserter;
  51. import org.eclipse.jgit.lib.Tree;
  52. import org.junit.Test;
  53. @SuppressWarnings("deprecation")
  54. public class ObjectWalkTest extends RevWalkTestCase {
  55. protected ObjectWalk objw;
  56. @Override
  57. protected RevWalk createRevWalk() {
  58. return objw = new ObjectWalk(db);
  59. }
  60. @Test
  61. public void testNoCommits() throws Exception {
  62. assertNull(objw.next());
  63. assertNull(objw.nextObject());
  64. }
  65. @Test
  66. public void testTwoCommitsEmptyTree() throws Exception {
  67. final RevCommit a = commit();
  68. final RevCommit b = commit(a);
  69. markStart(b);
  70. assertCommit(b, objw.next());
  71. assertCommit(a, objw.next());
  72. assertNull(objw.next());
  73. assertSame(tree(), objw.nextObject());
  74. assertNull(objw.nextObject());
  75. }
  76. @Test
  77. public void testOneCommitOneTreeTwoBlob() throws Exception {
  78. final RevBlob f0 = blob("0");
  79. final RevBlob f1 = blob("1");
  80. final RevTree t = tree(file("0", f0), file("1", f1), file("2", f1));
  81. final RevCommit a = commit(t);
  82. markStart(a);
  83. assertCommit(a, objw.next());
  84. assertNull(objw.next());
  85. assertSame(t, objw.nextObject());
  86. assertSame(f0, objw.nextObject());
  87. assertSame(f1, objw.nextObject());
  88. assertNull(objw.nextObject());
  89. }
  90. @Test
  91. public void testTwoCommitTwoTreeTwoBlob() throws Exception {
  92. final RevBlob f0 = blob("0");
  93. final RevBlob f1 = blob("1");
  94. final RevBlob f2 = blob("0v2");
  95. final RevTree ta = tree(file("0", f0), file("1", f1), file("2", f1));
  96. final RevTree tb = tree(file("0", f2), file("1", f1), file("2", f1));
  97. final RevCommit a = commit(ta);
  98. final RevCommit b = commit(tb, a);
  99. markStart(b);
  100. assertCommit(b, objw.next());
  101. assertCommit(a, objw.next());
  102. assertNull(objw.next());
  103. assertSame(tb, objw.nextObject());
  104. assertSame(f2, objw.nextObject());
  105. assertSame(f1, objw.nextObject());
  106. assertSame(ta, objw.nextObject());
  107. assertSame(f0, objw.nextObject());
  108. assertNull(objw.nextObject());
  109. }
  110. @Test
  111. public void testTwoCommitDeepTree1() throws Exception {
  112. final RevBlob f0 = blob("0");
  113. final RevBlob f1 = blob("0v2");
  114. final RevTree ta = tree(file("a/b/0", f0));
  115. final RevTree tb = tree(file("a/b/1", f1));
  116. final RevCommit a = commit(ta);
  117. final RevCommit b = commit(tb, a);
  118. markStart(b);
  119. assertCommit(b, objw.next());
  120. assertCommit(a, objw.next());
  121. assertNull(objw.next());
  122. assertSame(tb, objw.nextObject());
  123. assertSame(get(tb, "a"), objw.nextObject());
  124. assertSame(get(tb, "a/b"), objw.nextObject());
  125. assertSame(f1, objw.nextObject());
  126. assertSame(ta, objw.nextObject());
  127. assertSame(get(ta, "a"), objw.nextObject());
  128. assertSame(get(ta, "a/b"), objw.nextObject());
  129. assertSame(f0, objw.nextObject());
  130. assertNull(objw.nextObject());
  131. }
  132. @Test
  133. public void testTwoCommitDeepTree2() throws Exception {
  134. final RevBlob f1 = blob("1");
  135. final RevTree ta = tree(file("a/b/0", f1), file("a/c/q", f1));
  136. final RevTree tb = tree(file("a/b/1", f1), file("a/c/q", f1));
  137. final RevCommit a = commit(ta);
  138. final RevCommit b = commit(tb, a);
  139. markStart(b);
  140. assertCommit(b, objw.next());
  141. assertCommit(a, objw.next());
  142. assertNull(objw.next());
  143. assertSame(tb, objw.nextObject());
  144. assertSame(get(tb, "a"), objw.nextObject());
  145. assertSame(get(tb, "a/b"), objw.nextObject());
  146. assertSame(f1, objw.nextObject());
  147. assertSame(get(tb, "a/c"), objw.nextObject());
  148. assertSame(ta, objw.nextObject());
  149. assertSame(get(ta, "a"), objw.nextObject());
  150. assertSame(get(ta, "a/b"), objw.nextObject());
  151. assertNull(objw.nextObject());
  152. }
  153. @Test
  154. public void testCull() throws Exception {
  155. final RevBlob f1 = blob("1");
  156. final RevBlob f2 = blob("2");
  157. final RevBlob f3 = blob("3");
  158. final RevBlob f4 = blob("4");
  159. final RevTree ta = tree(file("a/1", f1), file("c/3", f3));
  160. final RevCommit a = commit(ta);
  161. final RevTree tb = tree(file("a/1", f2), file("c/3", f3));
  162. final RevCommit b1 = commit(tb, a);
  163. final RevCommit b2 = commit(tb, b1);
  164. final RevTree tc = tree(file("a/1", f4));
  165. final RevCommit c1 = commit(tc, a);
  166. final RevCommit c2 = commit(tc, c1);
  167. markStart(b2);
  168. markUninteresting(c2);
  169. assertCommit(b2, objw.next());
  170. assertCommit(b1, objw.next());
  171. assertNull(objw.next());
  172. assertTrue(a.has(RevFlag.UNINTERESTING));
  173. assertTrue(ta.has(RevFlag.UNINTERESTING));
  174. assertTrue(f1.has(RevFlag.UNINTERESTING));
  175. assertTrue(f3.has(RevFlag.UNINTERESTING));
  176. assertSame(tb, objw.nextObject());
  177. assertSame(get(tb, "a"), objw.nextObject());
  178. assertSame(f2, objw.nextObject());
  179. assertNull(objw.nextObject());
  180. }
  181. @Test
  182. public void testEmptyTreeCorruption() throws Exception {
  183. ObjectId bId = ObjectId
  184. .fromString("abbbfafe3129f85747aba7bfac992af77134c607");
  185. final RevTree tree_root, tree_A, tree_AB;
  186. final RevCommit b;
  187. {
  188. Tree root = new Tree(db);
  189. Tree A = root.addTree("A");
  190. FileTreeEntry B = root.addFile("B");
  191. B.setId(bId);
  192. Tree A_A = A.addTree("A");
  193. Tree A_B = A.addTree("B");
  194. try (final ObjectInserter inserter = db.newObjectInserter()) {
  195. A_A.setId(inserter.insert(Constants.OBJ_TREE, A_A.format()));
  196. A_B.setId(inserter.insert(Constants.OBJ_TREE, A_B.format()));
  197. A.setId(inserter.insert(Constants.OBJ_TREE, A.format()));
  198. root.setId(inserter.insert(Constants.OBJ_TREE, root.format()));
  199. inserter.flush();
  200. }
  201. tree_root = rw.parseTree(root.getId());
  202. tree_A = rw.parseTree(A.getId());
  203. tree_AB = rw.parseTree(A_A.getId());
  204. assertSame(tree_AB, rw.parseTree(A_B.getId()));
  205. b = commit(rw.parseTree(root.getId()));
  206. }
  207. markStart(b);
  208. assertCommit(b, objw.next());
  209. assertNull(objw.next());
  210. assertSame(tree_root, objw.nextObject());
  211. assertSame(tree_A, objw.nextObject());
  212. assertSame(tree_AB, objw.nextObject());
  213. assertSame(rw.lookupBlob(bId), objw.nextObject());
  214. assertNull(objw.nextObject());
  215. }
  216. }