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.

RevWalkMergeBaseTest.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * Copyright (C) 2009, 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.fail;
  13. import org.eclipse.jgit.revwalk.filter.RevFilter;
  14. import org.eclipse.jgit.treewalk.filter.TreeFilter;
  15. import org.junit.Test;
  16. public class RevWalkMergeBaseTest extends RevWalkTestCase {
  17. @Test
  18. public void testNone() throws Exception {
  19. final RevCommit c1 = commit(commit(commit()));
  20. final RevCommit c2 = commit(commit(commit()));
  21. rw.setRevFilter(RevFilter.MERGE_BASE);
  22. markStart(c1);
  23. markStart(c2);
  24. assertNull(rw.next());
  25. }
  26. @Test
  27. public void testDisallowTreeFilter() throws Exception {
  28. final RevCommit c1 = commit();
  29. final RevCommit c2 = commit();
  30. rw.setRevFilter(RevFilter.MERGE_BASE);
  31. rw.setTreeFilter(TreeFilter.ANY_DIFF);
  32. markStart(c1);
  33. markStart(c2);
  34. try {
  35. assertNull(rw.next());
  36. fail("did not throw IllegalStateException");
  37. } catch (IllegalStateException ise) {
  38. // expected result
  39. }
  40. }
  41. @Test
  42. public void testSimple() throws Exception {
  43. final RevCommit a = commit();
  44. final RevCommit b = commit(a);
  45. final RevCommit c1 = commit(commit(commit(commit(commit(b)))));
  46. final RevCommit c2 = commit(commit(commit(commit(commit(b)))));
  47. rw.setRevFilter(RevFilter.MERGE_BASE);
  48. markStart(c1);
  49. markStart(c2);
  50. assertCommit(b, rw.next());
  51. assertNull(rw.next());
  52. }
  53. @Test
  54. public void testMultipleHeads_SameBase1() throws Exception {
  55. final RevCommit a = commit();
  56. final RevCommit b = commit(a);
  57. final RevCommit c1 = commit(commit(commit(commit(commit(b)))));
  58. final RevCommit c2 = commit(commit(commit(commit(commit(b)))));
  59. final RevCommit c3 = commit(commit(commit(b)));
  60. rw.setRevFilter(RevFilter.MERGE_BASE);
  61. markStart(c1);
  62. markStart(c2);
  63. markStart(c3);
  64. assertCommit(b, rw.next());
  65. assertNull(rw.next());
  66. }
  67. @Test
  68. public void testMultipleHeads_SameBase2() throws Exception {
  69. final RevCommit a = commit();
  70. final RevCommit b = commit(a);
  71. final RevCommit c = commit(b);
  72. final RevCommit d1 = commit(commit(commit(commit(commit(b)))));
  73. final RevCommit d2 = commit(commit(commit(commit(commit(c)))));
  74. final RevCommit d3 = commit(commit(commit(c)));
  75. rw.setRevFilter(RevFilter.MERGE_BASE);
  76. markStart(d1);
  77. markStart(d2);
  78. markStart(d3);
  79. assertCommit(b, rw.next());
  80. assertNull(rw.next());
  81. }
  82. @Test
  83. public void testCrissCross() throws Exception {
  84. // See http://marc.info/?l=git&m=111463358500362&w=2 for a nice
  85. // description of what this test is creating. We don't have a
  86. // clean merge base for d,e as they each merged the parents b,c
  87. // in different orders.
  88. //
  89. final RevCommit a = commit();
  90. final RevCommit b = commit(a);
  91. final RevCommit c = commit(a);
  92. final RevCommit d = commit(b, c);
  93. final RevCommit e = commit(c, b);
  94. rw.setRevFilter(RevFilter.MERGE_BASE);
  95. markStart(d);
  96. markStart(e);
  97. assertCommit(c, rw.next());
  98. assertCommit(b, rw.next());
  99. assertNull(rw.next());
  100. }
  101. @Test
  102. public void testInconsistentCommitTimes() throws Exception {
  103. // When commit times are inconsistent (a parent is younger than a child)
  104. // make sure that not both, parent and child, are reported as merge
  105. // base. In the following repo the merge base between C,D should be B.
  106. // But when A is younger than B the MergeBaseGenerator used to generate
  107. // A before it detected that B is also a merge base.
  108. //
  109. // +---C
  110. // / /
  111. // A---B---D
  112. final RevCommit a = commit(2);
  113. final RevCommit b = commit(-1, a);
  114. final RevCommit c = commit(2, b, a);
  115. final RevCommit d = commit(1, b);
  116. rw.setRevFilter(RevFilter.MERGE_BASE);
  117. markStart(d);
  118. markStart(c);
  119. assertCommit(b, rw.next());
  120. assertNull(rw.next());
  121. }
  122. }