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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. }