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

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