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 5.9KB

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