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.

PendingGenerator.java 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * Copyright (C) 2009, Google Inc.
  3. * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
  4. * and other copyright owners as documented in the project's IP log.
  5. *
  6. * This program and the accompanying materials are made available
  7. * under the terms of the Eclipse Distribution License v1.0 which
  8. * accompanies this distribution, is reproduced below, and is
  9. * available at http://www.eclipse.org/org/documents/edl-v10.php
  10. *
  11. * All rights reserved.
  12. *
  13. * Redistribution and use in source and binary forms, with or
  14. * without modification, are permitted provided that the following
  15. * conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials provided
  23. * with the distribution.
  24. *
  25. * - Neither the name of the Eclipse Foundation, Inc. nor the
  26. * names of its contributors may be used to endorse or promote
  27. * products derived from this software without specific prior
  28. * written permission.
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  31. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  32. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  33. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  34. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  35. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  36. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  37. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  38. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  39. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  40. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  41. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  42. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  43. */
  44. package org.eclipse.jgit.revwalk;
  45. import java.io.IOException;
  46. import org.eclipse.jgit.errors.IncorrectObjectTypeException;
  47. import org.eclipse.jgit.errors.MissingObjectException;
  48. import org.eclipse.jgit.errors.StopWalkException;
  49. import org.eclipse.jgit.lib.ObjectId;
  50. import org.eclipse.jgit.revwalk.filter.RevFilter;
  51. /**
  52. * Default (and first pass) RevCommit Generator implementation for RevWalk.
  53. * <p>
  54. * This generator starts from a set of one or more commits and process them in
  55. * descending (newest to oldest) commit time order. Commits automatically cause
  56. * their parents to be enqueued for further processing, allowing the entire
  57. * commit graph to be walked. A {@link RevFilter} may be used to select a subset
  58. * of the commits and return them to the caller.
  59. */
  60. class PendingGenerator extends Generator {
  61. private static final int PARSED = RevWalk.PARSED;
  62. private static final int SEEN = RevWalk.SEEN;
  63. private static final int UNINTERESTING = RevWalk.UNINTERESTING;
  64. /**
  65. * Number of additional commits to scan after we think we are done.
  66. * <p>
  67. * This small buffer of commits is scanned to ensure we didn't miss anything
  68. * as a result of clock skew when the commits were made. We need to set our
  69. * constant to 1 additional commit due to the use of a pre-increment
  70. * operator when accessing the value.
  71. */
  72. static final int OVER_SCAN = 5 + 1;
  73. /** A commit near the end of time, to initialize {@link #last} with. */
  74. private static final RevCommit INIT_LAST;
  75. static {
  76. INIT_LAST = new RevCommit(ObjectId.zeroId());
  77. INIT_LAST.commitTime = Integer.MAX_VALUE;
  78. }
  79. private final RevWalk walker;
  80. private final DateRevQueue pending;
  81. private final RevFilter filter;
  82. private final int output;
  83. /** Last commit produced to the caller from {@link #next()}. */
  84. private RevCommit last = INIT_LAST;
  85. /**
  86. * Number of commits we have remaining in our over-scan allotment.
  87. * <p>
  88. * Only relevant if there are {@link #UNINTERESTING} commits in the
  89. * {@link #pending} queue.
  90. */
  91. private int overScan = OVER_SCAN;
  92. boolean canDispose;
  93. PendingGenerator(final RevWalk w, final DateRevQueue p,
  94. final RevFilter f, final int out) {
  95. walker = w;
  96. pending = p;
  97. filter = f;
  98. output = out;
  99. canDispose = true;
  100. }
  101. @Override
  102. int outputType() {
  103. return output | SORT_COMMIT_TIME_DESC;
  104. }
  105. @Override
  106. RevCommit next() throws MissingObjectException,
  107. IncorrectObjectTypeException, IOException {
  108. try {
  109. for (;;) {
  110. final RevCommit c = pending.next();
  111. if (c == null) {
  112. walker.reader.walkAdviceEnd();
  113. if (!(walker instanceof ObjectWalk))
  114. walker.reader.release();
  115. return null;
  116. }
  117. final boolean produce;
  118. if ((c.flags & UNINTERESTING) != 0)
  119. produce = false;
  120. else {
  121. c.parseBody(walker);
  122. produce = filter.include(walker, c);
  123. }
  124. for (final RevCommit p : c.parents) {
  125. if ((p.flags & SEEN) != 0)
  126. continue;
  127. if ((p.flags & PARSED) == 0)
  128. p.parseHeaders(walker);
  129. p.flags |= SEEN;
  130. pending.add(p);
  131. }
  132. walker.carryFlagsImpl(c);
  133. if ((c.flags & UNINTERESTING) != 0) {
  134. if (pending.everbodyHasFlag(UNINTERESTING)) {
  135. final RevCommit n = pending.peek();
  136. if (n != null && n.commitTime >= last.commitTime) {
  137. // This is too close to call. The next commit we
  138. // would pop is dated after the last one produced.
  139. // We have to keep going to ensure that we carry
  140. // flags as much as necessary.
  141. //
  142. overScan = OVER_SCAN;
  143. } else if (--overScan == 0)
  144. throw StopWalkException.INSTANCE;
  145. } else {
  146. overScan = OVER_SCAN;
  147. }
  148. if (canDispose)
  149. c.disposeBody();
  150. continue;
  151. }
  152. if (produce)
  153. return last = c;
  154. else if (canDispose)
  155. c.disposeBody();
  156. }
  157. } catch (StopWalkException swe) {
  158. walker.reader.walkAdviceEnd();
  159. walker.reader.release();
  160. pending.clear();
  161. return null;
  162. }
  163. }
  164. }