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.

RevWalkPathFilter1Test.java 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * Copyright (C) 2009, 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.assertEquals;
  12. import static org.junit.Assert.assertNull;
  13. import java.util.Collections;
  14. import org.eclipse.jgit.treewalk.filter.AndTreeFilter;
  15. import org.eclipse.jgit.treewalk.filter.PathFilterGroup;
  16. import org.eclipse.jgit.treewalk.filter.TreeFilter;
  17. import org.junit.Test;
  18. public class RevWalkPathFilter1Test extends RevWalkTestCase {
  19. protected void filter(String path) {
  20. rw.setTreeFilter(AndTreeFilter.create(PathFilterGroup
  21. .createFromStrings(Collections.singleton(path)),
  22. TreeFilter.ANY_DIFF));
  23. }
  24. @Test
  25. public void testEmpty_EmptyTree() throws Exception {
  26. final RevCommit a = commit();
  27. filter("a");
  28. markStart(a);
  29. assertNull(rw.next());
  30. }
  31. @Test
  32. public void testEmpty_NoMatch() throws Exception {
  33. final RevCommit a = commit(tree(file("0", blob("0"))));
  34. filter("a");
  35. markStart(a);
  36. assertNull(rw.next());
  37. }
  38. @Test
  39. public void testSimple1() throws Exception {
  40. final RevCommit a = commit(tree(file("0", blob("0"))));
  41. filter("0");
  42. markStart(a);
  43. assertCommit(a, rw.next());
  44. assertNull(rw.next());
  45. }
  46. @Test
  47. public void testEdits_MatchNone() throws Exception {
  48. final RevCommit a = commit(tree(file("0", blob("a"))));
  49. final RevCommit b = commit(tree(file("0", blob("b"))), a);
  50. final RevCommit c = commit(tree(file("0", blob("c"))), b);
  51. final RevCommit d = commit(tree(file("0", blob("d"))), c);
  52. filter("a");
  53. markStart(d);
  54. assertNull(rw.next());
  55. }
  56. @Test
  57. public void testEdits_MatchAll() throws Exception {
  58. final RevCommit a = commit(tree(file("0", blob("a"))));
  59. final RevCommit b = commit(tree(file("0", blob("b"))), a);
  60. final RevCommit c = commit(tree(file("0", blob("c"))), b);
  61. final RevCommit d = commit(tree(file("0", blob("d"))), c);
  62. filter("0");
  63. markStart(d);
  64. assertCommit(d, rw.next());
  65. assertCommit(c, rw.next());
  66. assertCommit(b, rw.next());
  67. assertCommit(a, rw.next());
  68. assertNull(rw.next());
  69. }
  70. @Test
  71. public void testStringOfPearls_FilePath1() throws Exception {
  72. final RevCommit a = commit(tree(file("d/f", blob("a"))));
  73. final RevCommit b = commit(tree(file("d/f", blob("a"))), a);
  74. final RevCommit c = commit(tree(file("d/f", blob("b"))), b);
  75. filter("d/f");
  76. markStart(c);
  77. assertCommit(c, rw.next());
  78. assertEquals(1, c.getParentCount());
  79. assertCommit(a, c.getParent(0)); // b was skipped
  80. assertCommit(a, rw.next());
  81. assertEquals(0, a.getParentCount());
  82. assertNull(rw.next());
  83. }
  84. @Test
  85. public void testStringOfPearls_FilePath1_NoParentRewriting()
  86. throws Exception {
  87. final RevCommit a = commit(tree(file("d/f", blob("a"))));
  88. final RevCommit b = commit(tree(file("d/f", blob("a"))), a);
  89. final RevCommit c = commit(tree(file("d/f", blob("b"))), b);
  90. filter("d/f");
  91. markStart(c);
  92. rw.setRewriteParents(false);
  93. assertCommit(c, rw.next());
  94. assertEquals(1, c.getParentCount());
  95. assertCommit(b, c.getParent(0));
  96. assertCommit(a, rw.next()); // b was skipped
  97. assertEquals(0, a.getParentCount());
  98. assertNull(rw.next());
  99. }
  100. @Test
  101. public void testStringOfPearls_FilePath2() throws Exception {
  102. final RevCommit a = commit(tree(file("d/f", blob("a"))));
  103. final RevCommit b = commit(tree(file("d/f", blob("a"))), a);
  104. final RevCommit c = commit(tree(file("d/f", blob("b"))), b);
  105. final RevCommit d = commit(tree(file("d/f", blob("b"))), c);
  106. filter("d/f");
  107. markStart(d);
  108. // d was skipped
  109. assertCommit(c, rw.next());
  110. assertEquals(1, c.getParentCount());
  111. assertCommit(a, c.getParent(0)); // b was skipped
  112. assertCommit(a, rw.next());
  113. assertEquals(0, a.getParentCount());
  114. assertNull(rw.next());
  115. }
  116. @Test
  117. public void testStringOfPearls_FilePath2_NoParentRewriting()
  118. throws Exception {
  119. final RevCommit a = commit(tree(file("d/f", blob("a"))));
  120. final RevCommit b = commit(tree(file("d/f", blob("a"))), a);
  121. final RevCommit c = commit(tree(file("d/f", blob("b"))), b);
  122. final RevCommit d = commit(tree(file("d/f", blob("b"))), c);
  123. filter("d/f");
  124. markStart(d);
  125. rw.setRewriteParents(false);
  126. // d was skipped
  127. assertCommit(c, rw.next());
  128. assertEquals(1, c.getParentCount());
  129. assertCommit(b, c.getParent(0));
  130. // b was skipped
  131. assertCommit(a, rw.next());
  132. assertEquals(0, a.getParentCount());
  133. assertNull(rw.next());
  134. }
  135. @Test
  136. public void testStringOfPearls_DirPath2() throws Exception {
  137. final RevCommit a = commit(tree(file("d/f", blob("a"))));
  138. final RevCommit b = commit(tree(file("d/f", blob("a"))), a);
  139. final RevCommit c = commit(tree(file("d/f", blob("b"))), b);
  140. final RevCommit d = commit(tree(file("d/f", blob("b"))), c);
  141. filter("d");
  142. markStart(d);
  143. // d was skipped
  144. assertCommit(c, rw.next());
  145. assertEquals(1, c.getParentCount());
  146. assertCommit(a, c.getParent(0)); // b was skipped
  147. assertCommit(a, rw.next());
  148. assertEquals(0, a.getParentCount());
  149. assertNull(rw.next());
  150. }
  151. @Test
  152. public void testStringOfPearls_DirPath2_NoParentRewriting()
  153. throws Exception {
  154. final RevCommit a = commit(tree(file("d/f", blob("a"))));
  155. final RevCommit b = commit(tree(file("d/f", blob("a"))), a);
  156. final RevCommit c = commit(tree(file("d/f", blob("b"))), b);
  157. final RevCommit d = commit(tree(file("d/f", blob("b"))), c);
  158. filter("d");
  159. markStart(d);
  160. rw.setRewriteParents(false);
  161. // d was skipped
  162. assertCommit(c, rw.next());
  163. assertEquals(1, c.getParentCount());
  164. assertCommit(b, c.getParent(0));
  165. // b was skipped
  166. assertCommit(a, rw.next());
  167. assertEquals(0, a.getParentCount());
  168. assertNull(rw.next());
  169. }
  170. @Test
  171. public void testStringOfPearls_FilePath3() throws Exception {
  172. final RevCommit a = commit(tree(file("d/f", blob("a"))));
  173. final RevCommit b = commit(tree(file("d/f", blob("a"))), a);
  174. final RevCommit c = commit(tree(file("d/f", blob("b"))), b);
  175. final RevCommit d = commit(tree(file("d/f", blob("b"))), c);
  176. final RevCommit e = commit(tree(file("d/f", blob("b"))), d);
  177. final RevCommit f = commit(tree(file("d/f", blob("b"))), e);
  178. final RevCommit g = commit(tree(file("d/f", blob("b"))), f);
  179. final RevCommit h = commit(tree(file("d/f", blob("b"))), g);
  180. final RevCommit i = commit(tree(file("d/f", blob("c"))), h);
  181. filter("d/f");
  182. markStart(i);
  183. assertCommit(i, rw.next());
  184. assertEquals(1, i.getParentCount());
  185. assertCommit(c, i.getParent(0)); // h..d was skipped
  186. assertCommit(c, rw.next());
  187. assertEquals(1, c.getParentCount());
  188. assertCommit(a, c.getParent(0)); // b was skipped
  189. assertCommit(a, rw.next());
  190. assertEquals(0, a.getParentCount());
  191. assertNull(rw.next());
  192. }
  193. @Test
  194. public void testStringOfPearls_FilePath3_NoParentRewriting()
  195. throws Exception {
  196. final RevCommit a = commit(tree(file("d/f", blob("a"))));
  197. final RevCommit b = commit(tree(file("d/f", blob("a"))), a);
  198. final RevCommit c = commit(tree(file("d/f", blob("b"))), b);
  199. final RevCommit d = commit(tree(file("d/f", blob("b"))), c);
  200. final RevCommit e = commit(tree(file("d/f", blob("b"))), d);
  201. final RevCommit f = commit(tree(file("d/f", blob("b"))), e);
  202. final RevCommit g = commit(tree(file("d/f", blob("b"))), f);
  203. final RevCommit h = commit(tree(file("d/f", blob("b"))), g);
  204. final RevCommit i = commit(tree(file("d/f", blob("c"))), h);
  205. filter("d/f");
  206. markStart(i);
  207. rw.setRewriteParents(false);
  208. assertCommit(i, rw.next());
  209. assertEquals(1, i.getParentCount());
  210. assertCommit(h, i.getParent(0));
  211. // h..d was skipped
  212. assertCommit(c, rw.next());
  213. assertEquals(1, c.getParentCount());
  214. assertCommit(b, c.getParent(0));
  215. // b was skipped
  216. assertCommit(a, rw.next());
  217. assertEquals(0, a.getParentCount());
  218. assertNull(rw.next());
  219. }
  220. }