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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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.assertEquals;
  12. import static org.junit.Assert.assertNull;
  13. import static org.junit.Assert.assertTrue;
  14. import static org.junit.Assert.fail;
  15. import org.eclipse.jgit.internal.JGitText;
  16. import org.junit.Test;
  17. public class RevWalkSortTest extends RevWalkTestCase {
  18. @Test
  19. public void testSort_Default() throws Exception {
  20. final RevCommit a = commit();
  21. final RevCommit b = commit(1, a);
  22. final RevCommit c = commit(1, b);
  23. final RevCommit d = commit(1, c);
  24. markStart(d);
  25. assertCommit(d, rw.next());
  26. assertCommit(c, rw.next());
  27. assertCommit(b, rw.next());
  28. assertCommit(a, rw.next());
  29. assertNull(rw.next());
  30. }
  31. @Test
  32. public void testSort_COMMIT_TIME_DESC() throws Exception {
  33. final RevCommit a = commit();
  34. final RevCommit b = commit(a);
  35. final RevCommit c = commit(b);
  36. final RevCommit d = commit(c);
  37. rw.sort(RevSort.COMMIT_TIME_DESC);
  38. markStart(d);
  39. assertCommit(d, rw.next());
  40. assertCommit(c, rw.next());
  41. assertCommit(b, rw.next());
  42. assertCommit(a, rw.next());
  43. assertNull(rw.next());
  44. }
  45. @Test
  46. public void testSort_REVERSE() throws Exception {
  47. final RevCommit a = commit();
  48. final RevCommit b = commit(a);
  49. final RevCommit c = commit(b);
  50. final RevCommit d = commit(c);
  51. rw.sort(RevSort.REVERSE);
  52. markStart(d);
  53. assertCommit(a, rw.next());
  54. assertCommit(b, rw.next());
  55. assertCommit(c, rw.next());
  56. assertCommit(d, rw.next());
  57. assertNull(rw.next());
  58. }
  59. @Test
  60. public void testSort_COMMIT_TIME_DESC_OutOfOrder1() throws Exception {
  61. // Despite being out of order time-wise, a strand-of-pearls must
  62. // still maintain topological order.
  63. //
  64. final RevCommit a = commit();
  65. final RevCommit b = commit(a);
  66. final RevCommit c = commit(-5, b);
  67. final RevCommit d = commit(10, c);
  68. assertTrue(parseBody(a).getCommitTime() < parseBody(d).getCommitTime());
  69. assertTrue(parseBody(c).getCommitTime() < parseBody(b).getCommitTime());
  70. rw.sort(RevSort.COMMIT_TIME_DESC);
  71. markStart(d);
  72. assertCommit(d, rw.next());
  73. assertCommit(c, rw.next());
  74. assertCommit(b, rw.next());
  75. assertCommit(a, rw.next());
  76. assertNull(rw.next());
  77. }
  78. @Test
  79. public void testSort_COMMIT_TIME_DESC_OutOfOrder2() throws Exception {
  80. // c1 is back dated before its parent.
  81. //
  82. final RevCommit a = commit();
  83. final RevCommit b = commit(a);
  84. final RevCommit c1 = commit(-5, b);
  85. final RevCommit c2 = commit(10, b);
  86. final RevCommit d = commit(c1, c2);
  87. rw.sort(RevSort.COMMIT_TIME_DESC);
  88. markStart(d);
  89. assertCommit(d, rw.next());
  90. assertCommit(c2, rw.next());
  91. assertCommit(b, rw.next());
  92. assertCommit(a, rw.next());
  93. assertCommit(c1, rw.next());
  94. assertNull(rw.next());
  95. }
  96. @Test
  97. public void testSort_TOPO() throws Exception {
  98. // c1 is back dated before its parent.
  99. //
  100. final RevCommit a = commit();
  101. final RevCommit b = commit(a);
  102. final RevCommit c1 = commit(-5, b);
  103. final RevCommit c2 = commit(10, b);
  104. final RevCommit d = commit(c1, c2);
  105. rw.sort(RevSort.TOPO);
  106. markStart(d);
  107. assertCommit(d, rw.next());
  108. assertCommit(c2, rw.next());
  109. assertCommit(c1, rw.next());
  110. assertCommit(b, rw.next());
  111. assertCommit(a, rw.next());
  112. assertNull(rw.next());
  113. }
  114. @Test
  115. public void testSort_TOPO_REVERSE() throws Exception {
  116. // c1 is back dated before its parent.
  117. //
  118. final RevCommit a = commit();
  119. final RevCommit b = commit(a);
  120. final RevCommit c1 = commit(-5, b);
  121. final RevCommit c2 = commit(10, b);
  122. final RevCommit d = commit(c1, c2);
  123. rw.sort(RevSort.TOPO);
  124. rw.sort(RevSort.REVERSE, true);
  125. markStart(d);
  126. assertCommit(a, rw.next());
  127. assertCommit(b, rw.next());
  128. assertCommit(c1, rw.next());
  129. assertCommit(c2, rw.next());
  130. assertCommit(d, rw.next());
  131. assertNull(rw.next());
  132. }
  133. @Test
  134. public void testSort_TOPO_NON_INTERMIX() throws Exception {
  135. // c1 is back dated before its parent.
  136. //
  137. final RevCommit a = commit();
  138. final RevCommit b = commit(a);
  139. final RevCommit c1 = commit(-5, b);
  140. final RevCommit c2 = commit(10, b);
  141. final RevCommit d = commit(c1, c2);
  142. rw.sort(RevSort.TOPO_KEEP_BRANCH_TOGETHER);
  143. markStart(d);
  144. assertCommit(d, rw.next());
  145. assertCommit(c2, rw.next());
  146. assertCommit(c1, rw.next());
  147. assertCommit(b, rw.next());
  148. assertCommit(a, rw.next());
  149. assertNull(rw.next());
  150. }
  151. @Test
  152. public void testSort_TOPO_NON_INTERMIX_OutOfOrderCommitTimes()
  153. throws Exception {
  154. // b is committed before c2 in a different line of history.
  155. //
  156. final RevCommit a = commit();
  157. final RevCommit c1 = commit(a);
  158. final RevCommit b = commit(a);
  159. final RevCommit c2 = commit(c1);
  160. final RevCommit d = commit(b, c2);
  161. rw.sort(RevSort.TOPO_KEEP_BRANCH_TOGETHER);
  162. markStart(d);
  163. assertCommit(d, rw.next());
  164. assertCommit(c2, rw.next());
  165. assertCommit(c1, rw.next());
  166. assertCommit(b, rw.next());
  167. assertCommit(a, rw.next());
  168. assertNull(rw.next());
  169. }
  170. @Test
  171. public void testSort_TOPO_NON_INTERMIX_MultipleLinesOfHistory()
  172. throws Exception {
  173. final RevCommit a1 = commit();
  174. final RevCommit b1 = commit(a1);
  175. final RevCommit a2 = commit(a1, b1);
  176. final RevCommit b2 = commit(b1);
  177. final RevCommit b3 = commit(b1);
  178. final RevCommit a3 = commit(a2, b2);
  179. final RevCommit a4 = commit(a3, b3);
  180. rw.sort(RevSort.TOPO_KEEP_BRANCH_TOGETHER);
  181. markStart(a4);
  182. assertCommit(a4, rw.next());
  183. assertCommit(b3, rw.next());
  184. assertCommit(a3, rw.next());
  185. assertCommit(b2, rw.next());
  186. assertCommit(a2, rw.next());
  187. assertCommit(b1, rw.next());
  188. assertCommit(a1, rw.next());
  189. assertNull(rw.next());
  190. }
  191. @Test
  192. public void testSort_TOPO_NON_INTERMIX_REVERSE() throws Exception {
  193. // c1 is back dated before its parent.
  194. //
  195. final RevCommit a = commit();
  196. final RevCommit b = commit(a);
  197. final RevCommit c1 = commit(-5, b);
  198. final RevCommit c2 = commit(10, b);
  199. final RevCommit d = commit(c1, c2);
  200. rw.sort(RevSort.TOPO_KEEP_BRANCH_TOGETHER);
  201. rw.sort(RevSort.REVERSE, true);
  202. markStart(d);
  203. assertCommit(a, rw.next());
  204. assertCommit(b, rw.next());
  205. assertCommit(c1, rw.next());
  206. assertCommit(c2, rw.next());
  207. assertCommit(d, rw.next());
  208. assertNull(rw.next());
  209. }
  210. @Test
  211. public void testSort_TOPO_NON_INTERMIX_REVERSE_MultipleLinesOfHistory()
  212. throws Exception {
  213. final RevCommit a1 = commit();
  214. final RevCommit b1 = commit(a1);
  215. final RevCommit a2 = commit(a1, b1);
  216. final RevCommit b2 = commit(b1);
  217. final RevCommit b3 = commit(b1);
  218. final RevCommit a3 = commit(a2, b2);
  219. final RevCommit a4 = commit(a3, b3);
  220. rw.sort(RevSort.TOPO_KEEP_BRANCH_TOGETHER);
  221. rw.sort(RevSort.REVERSE, true);
  222. markStart(a4);
  223. assertCommit(a1, rw.next());
  224. assertCommit(b1, rw.next());
  225. assertCommit(a2, rw.next());
  226. assertCommit(b2, rw.next());
  227. assertCommit(a3, rw.next());
  228. assertCommit(b3, rw.next());
  229. assertCommit(a4, rw.next());
  230. assertNull(rw.next());
  231. }
  232. @Test
  233. public void testSort_TOPO_NON_INTERMIX_ParentOfMultipleStartChildren()
  234. throws Exception {
  235. final RevCommit a = commit();
  236. final RevCommit b = commit(a);
  237. final RevCommit c = commit(a);
  238. final RevCommit d1 = commit(a);
  239. final RevCommit d2 = commit(d1);
  240. final RevCommit e = commit(a);
  241. rw.sort(RevSort.TOPO_KEEP_BRANCH_TOGETHER);
  242. markStart(b);
  243. markStart(c);
  244. markStart(d2);
  245. markStart(e);
  246. assertCommit(e, rw.next());
  247. assertCommit(d2, rw.next());
  248. assertCommit(d1, rw.next());
  249. assertCommit(c, rw.next());
  250. assertCommit(b, rw.next());
  251. assertCommit(a, rw.next());
  252. assertNull(rw.next());
  253. }
  254. @Test
  255. public void testSort_TOPO_NON_INTERMIX_Uninteresting() throws Exception {
  256. final RevCommit a1 = commit();
  257. final RevCommit a2 = commit(a1);
  258. final RevCommit a3 = commit(a2);
  259. final RevCommit b = commit(a1);
  260. final RevCommit a4 = commit(a3, b);
  261. rw.sort(RevSort.TOPO_KEEP_BRANCH_TOGETHER);
  262. markStart(a4);
  263. markUninteresting(a2);
  264. assertCommit(a4, rw.next());
  265. assertCommit(b, rw.next());
  266. assertCommit(a3, rw.next());
  267. assertNull(rw.next());
  268. }
  269. @Test
  270. public void testSort_TOPO_NON_INTERMIX_and_TOPO_throws() throws Exception {
  271. final RevCommit a = commit();
  272. rw.sort(RevSort.TOPO_KEEP_BRANCH_TOGETHER);
  273. rw.sort(RevSort.TOPO, true);
  274. markStart(a);
  275. try {
  276. rw.next();
  277. fail("did not throw IllegalStateException");
  278. } catch (IllegalStateException e) {
  279. assertEquals(
  280. JGitText.get().cannotCombineTopoSortWithTopoKeepBranchTogetherSort,
  281. e.getMessage());
  282. }
  283. }
  284. }