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

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