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.

TreeRevFilterTest.java 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * Copyright (C) 2014, 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.assertEquals;
  45. import static org.junit.Assert.assertNull;
  46. import java.util.Collections;
  47. import org.eclipse.jgit.revwalk.filter.OrRevFilter;
  48. import org.eclipse.jgit.revwalk.filter.RevFilter;
  49. import org.eclipse.jgit.revwalk.filter.SkipRevFilter;
  50. import org.eclipse.jgit.treewalk.filter.AndTreeFilter;
  51. import org.eclipse.jgit.treewalk.filter.PathFilterGroup;
  52. import org.eclipse.jgit.treewalk.filter.TreeFilter;
  53. import org.junit.Test;
  54. public class TreeRevFilterTest extends RevWalkTestCase {
  55. private RevFilter treeRevFilter(String path) {
  56. return new TreeRevFilter(rw, treeFilter(path));
  57. }
  58. private static TreeFilter treeFilter(String path) {
  59. return AndTreeFilter.create(
  60. PathFilterGroup.createFromStrings(Collections.singleton(path)),
  61. TreeFilter.ANY_DIFF);
  62. }
  63. @Test
  64. public void testStringOfPearls_FilePath1()
  65. throws Exception {
  66. RevCommit a = commit(tree(file("d/f", blob("a"))));
  67. RevCommit b = commit(tree(file("d/f", blob("a"))), a);
  68. RevCommit c = commit(tree(file("d/f", blob("b"))), b);
  69. rw.setRevFilter(treeRevFilter("d/f"));
  70. markStart(c);
  71. assertCommit(c, rw.next());
  72. assertEquals(1, c.getParentCount());
  73. assertCommit(b, c.getParent(0));
  74. assertCommit(a, rw.next()); // b was skipped
  75. assertEquals(0, a.getParentCount());
  76. assertNull(rw.next());
  77. }
  78. @Test
  79. public void testStringOfPearls_FilePath2() throws Exception {
  80. RevCommit a = commit(tree(file("d/f", blob("a"))));
  81. RevCommit b = commit(tree(file("d/f", blob("a"))), a);
  82. RevCommit c = commit(tree(file("d/f", blob("b"))), b);
  83. RevCommit d = commit(tree(file("d/f", blob("b"))), c);
  84. rw.setRevFilter(treeRevFilter("d/f"));
  85. markStart(d);
  86. // d was skipped
  87. assertCommit(c, rw.next());
  88. assertEquals(1, c.getParentCount());
  89. assertCommit(b, c.getParent(0));
  90. // b was skipped
  91. assertCommit(a, rw.next());
  92. assertEquals(0, a.getParentCount());
  93. assertNull(rw.next());
  94. }
  95. @Test
  96. public void testStringOfPearls_DirPath2() throws Exception {
  97. RevCommit a = commit(tree(file("d/f", blob("a"))));
  98. RevCommit b = commit(tree(file("d/f", blob("a"))), a);
  99. RevCommit c = commit(tree(file("d/f", blob("b"))), b);
  100. RevCommit d = commit(tree(file("d/f", blob("b"))), c);
  101. rw.setRevFilter(treeRevFilter("d"));
  102. markStart(d);
  103. // d was skipped
  104. assertCommit(c, rw.next());
  105. assertEquals(1, c.getParentCount());
  106. assertCommit(b, c.getParent(0));
  107. // b was skipped
  108. assertCommit(a, rw.next());
  109. assertEquals(0, a.getParentCount());
  110. assertNull(rw.next());
  111. }
  112. @Test
  113. public void testStringOfPearls_FilePath3() throws Exception {
  114. RevCommit a = commit(tree(file("d/f", blob("a"))));
  115. RevCommit b = commit(tree(file("d/f", blob("a"))), a);
  116. RevCommit c = commit(tree(file("d/f", blob("b"))), b);
  117. RevCommit d = commit(tree(file("d/f", blob("b"))), c);
  118. RevCommit e = commit(tree(file("d/f", blob("b"))), d);
  119. RevCommit f = commit(tree(file("d/f", blob("b"))), e);
  120. RevCommit g = commit(tree(file("d/f", blob("b"))), f);
  121. RevCommit h = commit(tree(file("d/f", blob("b"))), g);
  122. RevCommit i = commit(tree(file("d/f", blob("c"))), h);
  123. rw.setRevFilter(treeRevFilter("d/f"));
  124. markStart(i);
  125. assertCommit(i, rw.next());
  126. assertEquals(1, i.getParentCount());
  127. assertCommit(h, i.getParent(0));
  128. // h..d was skipped
  129. assertCommit(c, rw.next());
  130. assertEquals(1, c.getParentCount());
  131. assertCommit(b, c.getParent(0));
  132. // b was skipped
  133. assertCommit(a, rw.next());
  134. assertEquals(0, a.getParentCount());
  135. assertNull(rw.next());
  136. }
  137. @Test
  138. public void testPathFilterOrOtherFilter() throws Exception {
  139. RevFilter pathFilter = treeRevFilter("d/f");
  140. RevFilter skipFilter = SkipRevFilter.create(1);
  141. RevFilter orFilter = OrRevFilter.create(skipFilter, pathFilter);
  142. RevCommit a = parseBody(commit(5, tree(file("d/f", blob("a")))));
  143. RevCommit b = parseBody(commit(5, tree(file("d/f", blob("a"))), a));
  144. RevCommit c = parseBody(commit(5, tree(file("d/f", blob("b"))), b));
  145. // Path filter matches c, a.
  146. rw.setRevFilter(pathFilter);
  147. markStart(c);
  148. assertCommit(c, rw.next());
  149. assertCommit(a, rw.next());
  150. // Skip filter matches b, a.
  151. rw.reset();
  152. rw.setRevFilter(skipFilter);
  153. markStart(c);
  154. assertCommit(b, rw.next());
  155. assertCommit(a, rw.next());
  156. // (Path OR Skip) matches c, b, a.
  157. rw.reset();
  158. rw.setRevFilter(orFilter);
  159. markStart(c);
  160. assertCommit(c, rw.next());
  161. assertCommit(b, rw.next());
  162. assertCommit(a, rw.next());
  163. }
  164. }