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.6KB

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