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.

RevWalkFilterTest.java 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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 java.io.IOException;
  45. import java.util.Date;
  46. import org.eclipse.jgit.errors.IncorrectObjectTypeException;
  47. import org.eclipse.jgit.errors.MissingObjectException;
  48. import org.eclipse.jgit.errors.StopWalkException;
  49. import org.eclipse.jgit.revwalk.filter.AndRevFilter;
  50. import org.eclipse.jgit.revwalk.filter.CommitTimeRevFilter;
  51. import org.eclipse.jgit.revwalk.filter.NotRevFilter;
  52. import org.eclipse.jgit.revwalk.filter.OrRevFilter;
  53. import org.eclipse.jgit.revwalk.filter.RevFilter;
  54. public class RevWalkFilterTest extends RevWalkTestCase {
  55. private static final MyAll MY_ALL = new MyAll();
  56. public void testFilter_ALL() throws Exception {
  57. final RevCommit a = commit();
  58. final RevCommit b = commit(a);
  59. final RevCommit c = commit(b);
  60. rw.setRevFilter(RevFilter.ALL);
  61. markStart(c);
  62. assertCommit(c, rw.next());
  63. assertCommit(b, rw.next());
  64. assertCommit(a, rw.next());
  65. assertNull(rw.next());
  66. }
  67. public void testFilter_Negate_ALL() throws Exception {
  68. final RevCommit a = commit();
  69. final RevCommit b = commit(a);
  70. final RevCommit c = commit(b);
  71. rw.setRevFilter(RevFilter.ALL.negate());
  72. markStart(c);
  73. assertNull(rw.next());
  74. }
  75. public void testFilter_NOT_ALL() throws Exception {
  76. final RevCommit a = commit();
  77. final RevCommit b = commit(a);
  78. final RevCommit c = commit(b);
  79. rw.setRevFilter(NotRevFilter.create(RevFilter.ALL));
  80. markStart(c);
  81. assertNull(rw.next());
  82. }
  83. public void testFilter_NONE() throws Exception {
  84. final RevCommit a = commit();
  85. final RevCommit b = commit(a);
  86. final RevCommit c = commit(b);
  87. rw.setRevFilter(RevFilter.NONE);
  88. markStart(c);
  89. assertNull(rw.next());
  90. }
  91. public void testFilter_NOT_NONE() throws Exception {
  92. final RevCommit a = commit();
  93. final RevCommit b = commit(a);
  94. final RevCommit c = commit(b);
  95. rw.setRevFilter(NotRevFilter.create(RevFilter.NONE));
  96. markStart(c);
  97. assertCommit(c, rw.next());
  98. assertCommit(b, rw.next());
  99. assertCommit(a, rw.next());
  100. assertNull(rw.next());
  101. }
  102. public void testFilter_ALL_And_NONE() throws Exception {
  103. final RevCommit a = commit();
  104. final RevCommit b = commit(a);
  105. final RevCommit c = commit(b);
  106. rw.setRevFilter(AndRevFilter.create(RevFilter.ALL, RevFilter.NONE));
  107. markStart(c);
  108. assertNull(rw.next());
  109. }
  110. public void testFilter_NONE_And_ALL() throws Exception {
  111. final RevCommit a = commit();
  112. final RevCommit b = commit(a);
  113. final RevCommit c = commit(b);
  114. rw.setRevFilter(AndRevFilter.create(RevFilter.NONE, RevFilter.ALL));
  115. markStart(c);
  116. assertNull(rw.next());
  117. }
  118. public void testFilter_ALL_Or_NONE() throws Exception {
  119. final RevCommit a = commit();
  120. final RevCommit b = commit(a);
  121. final RevCommit c = commit(b);
  122. rw.setRevFilter(OrRevFilter.create(RevFilter.ALL, RevFilter.NONE));
  123. markStart(c);
  124. assertCommit(c, rw.next());
  125. assertCommit(b, rw.next());
  126. assertCommit(a, rw.next());
  127. assertNull(rw.next());
  128. }
  129. public void testFilter_NONE_Or_ALL() throws Exception {
  130. final RevCommit a = commit();
  131. final RevCommit b = commit(a);
  132. final RevCommit c = commit(b);
  133. rw.setRevFilter(OrRevFilter.create(RevFilter.NONE, RevFilter.ALL));
  134. markStart(c);
  135. assertCommit(c, rw.next());
  136. assertCommit(b, rw.next());
  137. assertCommit(a, rw.next());
  138. assertNull(rw.next());
  139. }
  140. public void testFilter_MY_ALL_And_NONE() throws Exception {
  141. final RevCommit a = commit();
  142. final RevCommit b = commit(a);
  143. final RevCommit c = commit(b);
  144. rw.setRevFilter(AndRevFilter.create(MY_ALL, RevFilter.NONE));
  145. markStart(c);
  146. assertNull(rw.next());
  147. }
  148. public void testFilter_NONE_And_MY_ALL() throws Exception {
  149. final RevCommit a = commit();
  150. final RevCommit b = commit(a);
  151. final RevCommit c = commit(b);
  152. rw.setRevFilter(AndRevFilter.create(RevFilter.NONE, MY_ALL));
  153. markStart(c);
  154. assertNull(rw.next());
  155. }
  156. public void testFilter_MY_ALL_Or_NONE() throws Exception {
  157. final RevCommit a = commit();
  158. final RevCommit b = commit(a);
  159. final RevCommit c = commit(b);
  160. rw.setRevFilter(OrRevFilter.create(MY_ALL, RevFilter.NONE));
  161. markStart(c);
  162. assertCommit(c, rw.next());
  163. assertCommit(b, rw.next());
  164. assertCommit(a, rw.next());
  165. assertNull(rw.next());
  166. }
  167. public void testFilter_NONE_Or_MY_ALL() throws Exception {
  168. final RevCommit a = commit();
  169. final RevCommit b = commit(a);
  170. final RevCommit c = commit(b);
  171. rw.setRevFilter(OrRevFilter.create(RevFilter.NONE, MY_ALL));
  172. markStart(c);
  173. assertCommit(c, rw.next());
  174. assertCommit(b, rw.next());
  175. assertCommit(a, rw.next());
  176. assertNull(rw.next());
  177. }
  178. public void testFilter_NO_MERGES() throws Exception {
  179. final RevCommit a = commit();
  180. final RevCommit b = commit(a);
  181. final RevCommit c1 = commit(b);
  182. final RevCommit c2 = commit(b);
  183. final RevCommit d = commit(c1, c2);
  184. final RevCommit e = commit(d);
  185. rw.setRevFilter(RevFilter.NO_MERGES);
  186. markStart(e);
  187. assertCommit(e, rw.next());
  188. assertCommit(c2, rw.next());
  189. assertCommit(c1, rw.next());
  190. assertCommit(b, rw.next());
  191. assertCommit(a, rw.next());
  192. assertNull(rw.next());
  193. }
  194. public void testCommitTimeRevFilter() throws Exception {
  195. final RevCommit a = commit();
  196. tick(100);
  197. final RevCommit b = commit(a);
  198. tick(100);
  199. Date since = getClock();
  200. final RevCommit c1 = commit(b);
  201. tick(100);
  202. final RevCommit c2 = commit(b);
  203. tick(100);
  204. Date until = getClock();
  205. final RevCommit d = commit(c1, c2);
  206. tick(100);
  207. final RevCommit e = commit(d);
  208. {
  209. RevFilter after = CommitTimeRevFilter.after(since);
  210. assertNotNull(after);
  211. rw.setRevFilter(after);
  212. markStart(e);
  213. assertCommit(e, rw.next());
  214. assertCommit(d, rw.next());
  215. assertCommit(c2, rw.next());
  216. assertCommit(c1, rw.next());
  217. assertNull(rw.next());
  218. }
  219. {
  220. RevFilter before = CommitTimeRevFilter.before(until);
  221. assertNotNull(before);
  222. rw.reset();
  223. rw.setRevFilter(before);
  224. markStart(e);
  225. assertCommit(c2, rw.next());
  226. assertCommit(c1, rw.next());
  227. assertCommit(b, rw.next());
  228. assertCommit(a, rw.next());
  229. assertNull(rw.next());
  230. }
  231. {
  232. RevFilter between = CommitTimeRevFilter.between(since, until);
  233. assertNotNull(between);
  234. rw.reset();
  235. rw.setRevFilter(between);
  236. markStart(e);
  237. assertCommit(c2, rw.next());
  238. assertCommit(c1, rw.next());
  239. assertNull(rw.next());
  240. }
  241. }
  242. private static class MyAll extends RevFilter {
  243. @Override
  244. public RevFilter clone() {
  245. return this;
  246. }
  247. @Override
  248. public boolean include(RevWalk walker, RevCommit cmit)
  249. throws StopWalkException, MissingObjectException,
  250. IncorrectObjectTypeException, IOException {
  251. return true;
  252. }
  253. }
  254. }