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.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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 org.eclipse.jgit.lib.FileTreeEntry;
  45. import org.eclipse.jgit.lib.ObjectId;
  46. import org.eclipse.jgit.lib.ObjectWriter;
  47. import org.eclipse.jgit.lib.Tree;
  48. public class ObjectWalkTest extends RevWalkTestCase {
  49. protected ObjectWalk objw;
  50. @Override
  51. protected RevWalk createRevWalk() {
  52. return objw = new ObjectWalk(db);
  53. }
  54. public void testNoCommits() throws Exception {
  55. assertNull(objw.next());
  56. assertNull(objw.nextObject());
  57. }
  58. public void testTwoCommitsEmptyTree() throws Exception {
  59. final RevCommit a = commit();
  60. final RevCommit b = commit(a);
  61. markStart(b);
  62. assertCommit(b, objw.next());
  63. assertCommit(a, objw.next());
  64. assertNull(objw.next());
  65. assertSame(tree(), objw.nextObject());
  66. assertNull(objw.nextObject());
  67. }
  68. public void testOneCommitOneTreeTwoBlob() throws Exception {
  69. final RevBlob f0 = blob("0");
  70. final RevBlob f1 = blob("1");
  71. final RevTree t = tree(file("0", f0), file("1", f1), file("2", f1));
  72. final RevCommit a = commit(t);
  73. markStart(a);
  74. assertCommit(a, objw.next());
  75. assertNull(objw.next());
  76. assertSame(t, objw.nextObject());
  77. assertSame(f0, objw.nextObject());
  78. assertSame(f1, objw.nextObject());
  79. assertNull(objw.nextObject());
  80. }
  81. public void testTwoCommitTwoTreeTwoBlob() throws Exception {
  82. final RevBlob f0 = blob("0");
  83. final RevBlob f1 = blob("1");
  84. final RevBlob f2 = blob("0v2");
  85. final RevTree ta = tree(file("0", f0), file("1", f1), file("2", f1));
  86. final RevTree tb = tree(file("0", f2), file("1", f1), file("2", f1));
  87. final RevCommit a = commit(ta);
  88. final RevCommit b = commit(tb, a);
  89. markStart(b);
  90. assertCommit(b, objw.next());
  91. assertCommit(a, objw.next());
  92. assertNull(objw.next());
  93. assertSame(tb, objw.nextObject());
  94. assertSame(f2, objw.nextObject());
  95. assertSame(f1, objw.nextObject());
  96. assertSame(ta, objw.nextObject());
  97. assertSame(f0, objw.nextObject());
  98. assertNull(objw.nextObject());
  99. }
  100. public void testTwoCommitDeepTree1() throws Exception {
  101. final RevBlob f0 = blob("0");
  102. final RevBlob f1 = blob("0v2");
  103. final RevTree ta = tree(file("a/b/0", f0));
  104. final RevTree tb = tree(file("a/b/1", f1));
  105. final RevCommit a = commit(ta);
  106. final RevCommit b = commit(tb, a);
  107. markStart(b);
  108. assertCommit(b, objw.next());
  109. assertCommit(a, objw.next());
  110. assertNull(objw.next());
  111. assertSame(tb, objw.nextObject());
  112. assertSame(get(tb, "a"), objw.nextObject());
  113. assertSame(get(tb, "a/b"), objw.nextObject());
  114. assertSame(f1, objw.nextObject());
  115. assertSame(ta, objw.nextObject());
  116. assertSame(get(ta, "a"), objw.nextObject());
  117. assertSame(get(ta, "a/b"), objw.nextObject());
  118. assertSame(f0, objw.nextObject());
  119. assertNull(objw.nextObject());
  120. }
  121. public void testTwoCommitDeepTree2() throws Exception {
  122. final RevBlob f1 = blob("1");
  123. final RevTree ta = tree(file("a/b/0", f1), file("a/c/q", f1));
  124. final RevTree tb = tree(file("a/b/1", f1), file("a/c/q", f1));
  125. final RevCommit a = commit(ta);
  126. final RevCommit b = commit(tb, a);
  127. markStart(b);
  128. assertCommit(b, objw.next());
  129. assertCommit(a, objw.next());
  130. assertNull(objw.next());
  131. assertSame(tb, objw.nextObject());
  132. assertSame(get(tb, "a"), objw.nextObject());
  133. assertSame(get(tb, "a/b"), objw.nextObject());
  134. assertSame(f1, objw.nextObject());
  135. assertSame(get(tb, "a/c"), objw.nextObject());
  136. assertSame(ta, objw.nextObject());
  137. assertSame(get(ta, "a"), objw.nextObject());
  138. assertSame(get(ta, "a/b"), objw.nextObject());
  139. assertNull(objw.nextObject());
  140. }
  141. public void testCull() throws Exception {
  142. final RevBlob f1 = blob("1");
  143. final RevBlob f2 = blob("2");
  144. final RevBlob f3 = blob("3");
  145. final RevBlob f4 = blob("4");
  146. final RevTree ta = tree(file("a/1", f1), file("c/3", f3));
  147. final RevCommit a = commit(ta);
  148. final RevTree tb = tree(file("a/1", f2), file("c/3", f3));
  149. final RevCommit b1 = commit(tb, a);
  150. final RevCommit b2 = commit(tb, b1);
  151. final RevTree tc = tree(file("a/1", f4));
  152. final RevCommit c1 = commit(tc, a);
  153. final RevCommit c2 = commit(tc, c1);
  154. markStart(b2);
  155. markUninteresting(c2);
  156. assertCommit(b2, objw.next());
  157. assertCommit(b1, objw.next());
  158. assertNull(objw.next());
  159. assertTrue(a.has(RevFlag.UNINTERESTING));
  160. assertTrue(ta.has(RevFlag.UNINTERESTING));
  161. assertTrue(f1.has(RevFlag.UNINTERESTING));
  162. assertTrue(f3.has(RevFlag.UNINTERESTING));
  163. assertSame(tb, objw.nextObject());
  164. assertSame(get(tb, "a"), objw.nextObject());
  165. assertSame(f2, objw.nextObject());
  166. assertNull(objw.nextObject());
  167. }
  168. public void testEmptyTreeCorruption() throws Exception {
  169. ObjectId bId = ObjectId
  170. .fromString("abbbfafe3129f85747aba7bfac992af77134c607");
  171. final RevTree tree_root, tree_A, tree_AB;
  172. final RevCommit b;
  173. {
  174. Tree root = new Tree(db);
  175. Tree A = root.addTree("A");
  176. FileTreeEntry B = root.addFile("B");
  177. B.setId(bId);
  178. Tree A_A = A.addTree("A");
  179. Tree A_B = A.addTree("B");
  180. final ObjectWriter ow = new ObjectWriter(db);
  181. A_A.setId(ow.writeTree(A_A));
  182. A_B.setId(ow.writeTree(A_B));
  183. A.setId(ow.writeTree(A));
  184. root.setId(ow.writeTree(root));
  185. tree_root = rw.parseTree(root.getId());
  186. tree_A = rw.parseTree(A.getId());
  187. tree_AB = rw.parseTree(A_A.getId());
  188. assertSame(tree_AB, rw.parseTree(A_B.getId()));
  189. b = commit(rw.parseTree(root.getId()));
  190. }
  191. markStart(b);
  192. assertCommit(b, objw.next());
  193. assertNull(objw.next());
  194. assertSame(tree_root, objw.nextObject());
  195. assertSame(tree_A, objw.nextObject());
  196. assertSame(tree_AB, objw.nextObject());
  197. assertSame(rw.lookupBlob(bId), objw.nextObject());
  198. assertNull(objw.nextObject());
  199. }
  200. }