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

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