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

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