DelayRevQueue.java

  1. /*
  2.  * Copyright (C) 2009, Google Inc.
  3.  * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org> and others
  4.  *
  5.  * This program and the accompanying materials are made available under the
  6.  * terms of the Eclipse Distribution License v. 1.0 which is available at
  7.  * https://www.eclipse.org/org/documents/edl-v10.php.
  8.  *
  9.  * SPDX-License-Identifier: BSD-3-Clause
  10.  */

  11. package org.eclipse.jgit.revwalk;

  12. import java.io.IOException;

  13. import org.eclipse.jgit.errors.IncorrectObjectTypeException;
  14. import org.eclipse.jgit.errors.MissingObjectException;

  15. /**
  16.  * Delays commits to be at least {@link PendingGenerator#OVER_SCAN} late.
  17.  * <p>
  18.  * This helps to "fix up" weird corner cases resulting from clock skew, by
  19.  * slowing down what we produce to the caller we get a better chance to ensure
  20.  * PendingGenerator reached back far enough in the graph to correctly mark
  21.  * commits {@link RevWalk#UNINTERESTING} if necessary.
  22.  * <p>
  23.  * This generator should appear before {@link FixUninterestingGenerator} if the
  24.  * lower level {@link #pending} isn't already fully buffered.
  25.  */
  26. final class DelayRevQueue extends Generator {
  27.     private static final int OVER_SCAN = PendingGenerator.OVER_SCAN;

  28.     private final Generator pending;

  29.     private final FIFORevQueue delay;

  30.     private int size;

  31.     DelayRevQueue(Generator g) {
  32.         super(g.firstParent);
  33.         pending = g;
  34.         delay = new FIFORevQueue();
  35.     }

  36.     @Override
  37.     int outputType() {
  38.         return pending.outputType();
  39.     }

  40.     @Override
  41.     RevCommit next() throws MissingObjectException,
  42.             IncorrectObjectTypeException, IOException {
  43.         while (size < OVER_SCAN) {
  44.             final RevCommit c = pending.next();
  45.             if (c == null)
  46.                 break;
  47.             delay.add(c);
  48.             size++;
  49.         }

  50.         final RevCommit c = delay.next();
  51.         if (c == null)
  52.             return null;
  53.         size--;
  54.         return c;
  55.     }
  56. }