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.

MergeBaseGenerator.java 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
  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 java.io.IOException;
  45. import org.eclipse.jgit.errors.IncorrectObjectTypeException;
  46. import org.eclipse.jgit.errors.MissingObjectException;
  47. /**
  48. * Computes the merge base(s) of the starting commits.
  49. * <p>
  50. * This generator is selected if the RevFilter is only
  51. * {@link org.eclipse.jgit.revwalk.filter.RevFilter#MERGE_BASE}.
  52. * <p>
  53. * To compute the merge base we assign a temporary flag to each of the starting
  54. * commits. The maximum number of starting commits is bounded by the number of
  55. * free flags available in the RevWalk when the generator is initialized. These
  56. * flags will be automatically released on the next reset of the RevWalk, but
  57. * not until then, as they are assigned to commits throughout the history.
  58. * <p>
  59. * Several internal flags are reused here for a different purpose, but this
  60. * should not have any impact as this generator should be run alone, and without
  61. * any other generators wrapped around it.
  62. */
  63. class MergeBaseGenerator extends Generator {
  64. private static final int PARSED = RevWalk.PARSED;
  65. private static final int IN_PENDING = RevWalk.SEEN;
  66. private static final int POPPED = RevWalk.TEMP_MARK;
  67. private static final int MERGE_BASE = RevWalk.REWRITE;
  68. private final RevWalk walker;
  69. private final DateRevQueue pending;
  70. private int branchMask;
  71. private int recarryTest;
  72. private int recarryMask;
  73. MergeBaseGenerator(final RevWalk w) {
  74. walker = w;
  75. pending = new DateRevQueue();
  76. }
  77. void init(final AbstractRevQueue p) {
  78. try {
  79. for (;;) {
  80. final RevCommit c = p.next();
  81. if (c == null)
  82. break;
  83. add(c);
  84. }
  85. } finally {
  86. // Always free the flags immediately. This ensures the flags
  87. // will be available for reuse when the walk resets.
  88. //
  89. walker.freeFlag(branchMask);
  90. // Setup the condition used by carryOntoOne to detect a late
  91. // merge base and produce it on the next round.
  92. //
  93. recarryTest = branchMask | POPPED;
  94. recarryMask = branchMask | POPPED | MERGE_BASE;
  95. }
  96. }
  97. private void add(final RevCommit c) {
  98. final int flag = walker.allocFlag();
  99. branchMask |= flag;
  100. if ((c.flags & branchMask) != 0) {
  101. // This should never happen. RevWalk ensures we get a
  102. // commit admitted to the initial queue only once. If
  103. // we see this marks aren't correctly erased.
  104. //
  105. throw new IllegalStateException("Stale RevFlags on " + c.name());
  106. }
  107. c.flags |= flag;
  108. pending.add(c);
  109. }
  110. @Override
  111. int outputType() {
  112. return 0;
  113. }
  114. @Override
  115. RevCommit next() throws MissingObjectException,
  116. IncorrectObjectTypeException, IOException {
  117. for (;;) {
  118. final RevCommit c = pending.next();
  119. if (c == null) {
  120. walker.curs.release();
  121. return null;
  122. }
  123. for (final RevCommit p : c.parents) {
  124. if ((p.flags & IN_PENDING) != 0)
  125. continue;
  126. if ((p.flags & PARSED) == 0)
  127. p.parseHeaders(walker);
  128. p.flags |= IN_PENDING;
  129. pending.add(p);
  130. }
  131. int carry = c.flags & branchMask;
  132. boolean mb = carry == branchMask;
  133. if (mb) {
  134. // If we are a merge base make sure our ancestors are
  135. // also flagged as being popped, so that they do not
  136. // generate to the caller.
  137. //
  138. carry |= MERGE_BASE;
  139. }
  140. carryOntoHistory(c, carry);
  141. if ((c.flags & MERGE_BASE) != 0) {
  142. // This commit is an ancestor of a merge base we already
  143. // popped back to the caller. If everyone in pending is
  144. // that way we are done traversing; if not we just need
  145. // to move to the next available commit and try again.
  146. //
  147. if (pending.everbodyHasFlag(MERGE_BASE))
  148. return null;
  149. continue;
  150. }
  151. c.flags |= POPPED;
  152. if (mb) {
  153. c.flags |= MERGE_BASE;
  154. return c;
  155. }
  156. }
  157. }
  158. private void carryOntoHistory(RevCommit c, final int carry) {
  159. for (;;) {
  160. final RevCommit[] pList = c.parents;
  161. if (pList == null)
  162. return;
  163. final int n = pList.length;
  164. if (n == 0)
  165. return;
  166. for (int i = 1; i < n; i++) {
  167. final RevCommit p = pList[i];
  168. if (!carryOntoOne(p, carry))
  169. carryOntoHistory(p, carry);
  170. }
  171. c = pList[0];
  172. if (carryOntoOne(c, carry))
  173. break;
  174. }
  175. }
  176. private boolean carryOntoOne(final RevCommit p, final int carry) {
  177. final boolean haveAll = (p.flags & carry) == carry;
  178. p.flags |= carry;
  179. if ((p.flags & recarryMask) == recarryTest) {
  180. // We were popped without being a merge base, but we just got
  181. // voted to be one. Inject ourselves back at the front of the
  182. // pending queue and tell all of our ancestors they are within
  183. // the merge base now.
  184. //
  185. p.flags &= ~POPPED;
  186. pending.add(p);
  187. carryOntoHistory(p, branchMask | MERGE_BASE);
  188. return true;
  189. }
  190. // If we already had all carried flags, our parents do too.
  191. // Return true to stop the caller from running down this leg
  192. // of the revision graph any further.
  193. //
  194. return haveAll;
  195. }
  196. }