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.

RevWalkSortTest.java 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. * Copyright (C) 2009-2010, 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.assertNull;
  12. import static org.junit.Assert.assertTrue;
  13. import org.junit.Test;
  14. public class RevWalkSortTest extends RevWalkTestCase {
  15. @Test
  16. public void testSort_Default() throws Exception {
  17. final RevCommit a = commit();
  18. final RevCommit b = commit(1, a);
  19. final RevCommit c = commit(1, b);
  20. final RevCommit d = commit(1, c);
  21. markStart(d);
  22. assertCommit(d, rw.next());
  23. assertCommit(c, rw.next());
  24. assertCommit(b, rw.next());
  25. assertCommit(a, rw.next());
  26. assertNull(rw.next());
  27. }
  28. @Test
  29. public void testSort_COMMIT_TIME_DESC() throws Exception {
  30. final RevCommit a = commit();
  31. final RevCommit b = commit(a);
  32. final RevCommit c = commit(b);
  33. final RevCommit d = commit(c);
  34. rw.sort(RevSort.COMMIT_TIME_DESC);
  35. markStart(d);
  36. assertCommit(d, rw.next());
  37. assertCommit(c, rw.next());
  38. assertCommit(b, rw.next());
  39. assertCommit(a, rw.next());
  40. assertNull(rw.next());
  41. }
  42. @Test
  43. public void testSort_REVERSE() throws Exception {
  44. final RevCommit a = commit();
  45. final RevCommit b = commit(a);
  46. final RevCommit c = commit(b);
  47. final RevCommit d = commit(c);
  48. rw.sort(RevSort.REVERSE);
  49. markStart(d);
  50. assertCommit(a, rw.next());
  51. assertCommit(b, rw.next());
  52. assertCommit(c, rw.next());
  53. assertCommit(d, rw.next());
  54. assertNull(rw.next());
  55. }
  56. @Test
  57. public void testSort_COMMIT_TIME_DESC_OutOfOrder1() throws Exception {
  58. // Despite being out of order time-wise, a strand-of-pearls must
  59. // still maintain topological order.
  60. //
  61. final RevCommit a = commit();
  62. final RevCommit b = commit(a);
  63. final RevCommit c = commit(-5, b);
  64. final RevCommit d = commit(10, c);
  65. assertTrue(parseBody(a).getCommitTime() < parseBody(d).getCommitTime());
  66. assertTrue(parseBody(c).getCommitTime() < parseBody(b).getCommitTime());
  67. rw.sort(RevSort.COMMIT_TIME_DESC);
  68. markStart(d);
  69. assertCommit(d, rw.next());
  70. assertCommit(c, rw.next());
  71. assertCommit(b, rw.next());
  72. assertCommit(a, rw.next());
  73. assertNull(rw.next());
  74. }
  75. @Test
  76. public void testSort_COMMIT_TIME_DESC_OutOfOrder2() throws Exception {
  77. // c1 is back dated before its parent.
  78. //
  79. final RevCommit a = commit();
  80. final RevCommit b = commit(a);
  81. final RevCommit c1 = commit(-5, b);
  82. final RevCommit c2 = commit(10, b);
  83. final RevCommit d = commit(c1, c2);
  84. rw.sort(RevSort.COMMIT_TIME_DESC);
  85. markStart(d);
  86. assertCommit(d, rw.next());
  87. assertCommit(c2, rw.next());
  88. assertCommit(b, rw.next());
  89. assertCommit(a, rw.next());
  90. assertCommit(c1, rw.next());
  91. assertNull(rw.next());
  92. }
  93. @Test
  94. public void testSort_TOPO() throws Exception {
  95. // c1 is back dated before its parent.
  96. //
  97. final RevCommit a = commit();
  98. final RevCommit b = commit(a);
  99. final RevCommit c1 = commit(-5, b);
  100. final RevCommit c2 = commit(10, b);
  101. final RevCommit d = commit(c1, c2);
  102. rw.sort(RevSort.TOPO);
  103. markStart(d);
  104. assertCommit(d, rw.next());
  105. assertCommit(c2, rw.next());
  106. assertCommit(c1, rw.next());
  107. assertCommit(b, rw.next());
  108. assertCommit(a, rw.next());
  109. assertNull(rw.next());
  110. }
  111. @Test
  112. public void testSort_TOPO_REVERSE() throws Exception {
  113. // c1 is back dated before its parent.
  114. //
  115. final RevCommit a = commit();
  116. final RevCommit b = commit(a);
  117. final RevCommit c1 = commit(-5, b);
  118. final RevCommit c2 = commit(10, b);
  119. final RevCommit d = commit(c1, c2);
  120. rw.sort(RevSort.TOPO);
  121. rw.sort(RevSort.REVERSE, true);
  122. markStart(d);
  123. assertCommit(a, rw.next());
  124. assertCommit(b, rw.next());
  125. assertCommit(c1, rw.next());
  126. assertCommit(c2, rw.next());
  127. assertCommit(d, rw.next());
  128. assertNull(rw.next());
  129. }
  130. @Test
  131. public void testSort_TOPO_OutOfOrderCommitTimes() throws Exception {
  132. // b is committed before c2 in a different line of history.
  133. //
  134. final RevCommit a = commit();
  135. final RevCommit c1 = commit(a);
  136. final RevCommit b = commit(a);
  137. final RevCommit c2 = commit(c1);
  138. final RevCommit d = commit(b, c2);
  139. rw.sort(RevSort.TOPO);
  140. markStart(d);
  141. assertCommit(d, rw.next());
  142. assertCommit(c2, rw.next());
  143. assertCommit(c1, rw.next());
  144. assertCommit(b, rw.next());
  145. assertCommit(a, rw.next());
  146. assertNull(rw.next());
  147. }
  148. @Test
  149. public void testSort_TOPO_MultipleLinesOfHistory() throws Exception {
  150. final RevCommit a1 = commit();
  151. final RevCommit b1 = commit(a1);
  152. final RevCommit a2 = commit(a1, b1);
  153. final RevCommit b2 = commit(b1);
  154. final RevCommit b3 = commit(b1);
  155. final RevCommit a3 = commit(a2, b2);
  156. final RevCommit a4 = commit(a3, b3);
  157. rw.sort(RevSort.TOPO);
  158. markStart(a4);
  159. assertCommit(a4, rw.next());
  160. assertCommit(b3, rw.next());
  161. assertCommit(a3, rw.next());
  162. assertCommit(b2, rw.next());
  163. assertCommit(a2, rw.next());
  164. assertCommit(b1, rw.next());
  165. assertCommit(a1, rw.next());
  166. assertNull(rw.next());
  167. }
  168. @Test
  169. public void testSort_TOPO_REVERSE_MultipleLinesOfHistory()
  170. throws Exception {
  171. final RevCommit a1 = commit();
  172. final RevCommit b1 = commit(a1);
  173. final RevCommit a2 = commit(a1, b1);
  174. final RevCommit b2 = commit(b1);
  175. final RevCommit b3 = commit(b1);
  176. final RevCommit a3 = commit(a2, b2);
  177. final RevCommit a4 = commit(a3, b3);
  178. rw.sort(RevSort.TOPO);
  179. rw.sort(RevSort.REVERSE, true);
  180. markStart(a4);
  181. assertCommit(a1, rw.next());
  182. assertCommit(b1, rw.next());
  183. assertCommit(a2, rw.next());
  184. assertCommit(b2, rw.next());
  185. assertCommit(a3, rw.next());
  186. assertCommit(b3, rw.next());
  187. assertCommit(a4, rw.next());
  188. assertNull(rw.next());
  189. }
  190. @Test
  191. public void testSort_TOPO_ParentOfMultipleStartChildren() throws Exception {
  192. final RevCommit a = commit();
  193. final RevCommit b = commit(a);
  194. final RevCommit c = commit(a);
  195. final RevCommit d1 = commit(a);
  196. final RevCommit d2 = commit(d1);
  197. final RevCommit e = commit(a);
  198. rw.sort(RevSort.TOPO);
  199. markStart(b);
  200. markStart(c);
  201. markStart(d2);
  202. markStart(e);
  203. assertCommit(e, rw.next());
  204. assertCommit(d2, rw.next());
  205. assertCommit(d1, rw.next());
  206. assertCommit(c, rw.next());
  207. assertCommit(b, rw.next());
  208. assertCommit(a, rw.next());
  209. assertNull(rw.next());
  210. }
  211. @Test
  212. public void testSort_TOPO_Uninteresting() throws Exception {
  213. final RevCommit a1 = commit();
  214. final RevCommit a2 = commit(a1);
  215. final RevCommit a3 = commit(a2);
  216. final RevCommit b = commit(a1);
  217. final RevCommit a4 = commit(a3, b);
  218. rw.sort(RevSort.TOPO);
  219. markStart(a4);
  220. markUninteresting(a2);
  221. assertCommit(a4, rw.next());
  222. assertCommit(b, rw.next());
  223. assertCommit(a3, rw.next());
  224. assertNull(rw.next());
  225. }
  226. }