RevWalkUtils.java

  1. /*
  2.  * Copyright (C) 2011-2012, Robin Stocker <robin@nibor.org> and others
  3.  *
  4.  * This program and the accompanying materials are made available under the
  5.  * terms of the Eclipse Distribution License v. 1.0 which is available at
  6.  * https://www.eclipse.org/org/documents/edl-v10.php.
  7.  *
  8.  * SPDX-License-Identifier: BSD-3-Clause
  9.  */

  10. package org.eclipse.jgit.revwalk;

  11. import java.io.IOException;
  12. import java.util.ArrayList;
  13. import java.util.Collection;
  14. import java.util.List;

  15. import org.eclipse.jgit.errors.IncorrectObjectTypeException;
  16. import org.eclipse.jgit.errors.MissingObjectException;
  17. import org.eclipse.jgit.internal.JGitText;
  18. import org.eclipse.jgit.lib.NullProgressMonitor;
  19. import org.eclipse.jgit.lib.ProgressMonitor;
  20. import org.eclipse.jgit.lib.Ref;

  21. /**
  22.  * Utility methods for {@link org.eclipse.jgit.revwalk.RevWalk}.
  23.  */
  24. public final class RevWalkUtils {

  25.     private RevWalkUtils() {
  26.         // Utility class
  27.     }

  28.     /**
  29.      * Count the number of commits that are reachable from <code>start</code>
  30.      * until a commit that is reachable from <code>end</code> is encountered. In
  31.      * other words, count the number of commits that are in <code>start</code>,
  32.      * but not in <code>end</code>.
  33.      * <p>
  34.      * Note that this method calls
  35.      * {@link org.eclipse.jgit.revwalk.RevWalk#reset()} at the beginning. Also
  36.      * note that the existing rev filter on the walk is left as-is, so be sure
  37.      * to set the right rev filter before calling this method.
  38.      *
  39.      * @param walk
  40.      *            the rev walk to use
  41.      * @param start
  42.      *            the commit to start counting from
  43.      * @param end
  44.      *            the commit where counting should end, or null if counting
  45.      *            should be done until there are no more commits
  46.      * @return the number of commits
  47.      * @throws org.eclipse.jgit.errors.MissingObjectException
  48.      * @throws org.eclipse.jgit.errors.IncorrectObjectTypeException
  49.      * @throws java.io.IOException
  50.      */
  51.     public static int count(final RevWalk walk, final RevCommit start,
  52.             final RevCommit end) throws MissingObjectException,
  53.             IncorrectObjectTypeException, IOException {
  54.         return find(walk, start, end).size();
  55.     }

  56.     /**
  57.      * Find commits that are reachable from <code>start</code> until a commit
  58.      * that is reachable from <code>end</code> is encountered. In other words,
  59.      * Find of commits that are in <code>start</code>, but not in
  60.      * <code>end</code>.
  61.      * <p>
  62.      * Note that this method calls
  63.      * {@link org.eclipse.jgit.revwalk.RevWalk#reset()} at the beginning. Also
  64.      * note that the existing rev filter on the walk is left as-is, so be sure
  65.      * to set the right rev filter before calling this method.
  66.      *
  67.      * @param walk
  68.      *            the rev walk to use
  69.      * @param start
  70.      *            the commit to start counting from
  71.      * @param end
  72.      *            the commit where counting should end, or null if counting
  73.      *            should be done until there are no more commits
  74.      * @return the commits found
  75.      * @throws org.eclipse.jgit.errors.MissingObjectException
  76.      * @throws org.eclipse.jgit.errors.IncorrectObjectTypeException
  77.      * @throws java.io.IOException
  78.      */
  79.     public static List<RevCommit> find(final RevWalk walk,
  80.             final RevCommit start, final RevCommit end)
  81.             throws MissingObjectException, IncorrectObjectTypeException,
  82.             IOException {
  83.         walk.reset();
  84.         walk.markStart(start);
  85.         if (end != null)
  86.             walk.markUninteresting(end);

  87.         List<RevCommit> commits = new ArrayList<>();
  88.         for (RevCommit c : walk)
  89.             commits.add(c);
  90.         return commits;
  91.     }

  92.     /**
  93.      * Find the list of branches a given commit is reachable from when following
  94.      * parents.
  95.      * <p>
  96.      * Note that this method calls
  97.      * {@link org.eclipse.jgit.revwalk.RevWalk#reset()} at the beginning.
  98.      * <p>
  99.      * In order to improve performance this method assumes clock skew among
  100.      * committers is never larger than 24 hours.
  101.      *
  102.      * @param commit
  103.      *            the commit we are looking at
  104.      * @param revWalk
  105.      *            The RevWalk to be used.
  106.      * @param refs
  107.      *            the set of branches we want to see reachability from
  108.      * @return the list of branches a given commit is reachable from
  109.      * @throws org.eclipse.jgit.errors.MissingObjectException
  110.      * @throws org.eclipse.jgit.errors.IncorrectObjectTypeException
  111.      * @throws java.io.IOException
  112.      */
  113.     public static List<Ref> findBranchesReachableFrom(RevCommit commit,
  114.             RevWalk revWalk, Collection<Ref> refs)
  115.             throws MissingObjectException, IncorrectObjectTypeException,
  116.             IOException {
  117.         return findBranchesReachableFrom(commit, revWalk, refs,
  118.                 NullProgressMonitor.INSTANCE);
  119.     }

  120.     /**
  121.      * Find the list of branches a given commit is reachable from when following
  122.      * parents.
  123.      * <p>
  124.      * Note that this method calls
  125.      * {@link org.eclipse.jgit.revwalk.RevWalk#reset()} at the beginning.
  126.      * <p>
  127.      * In order to improve performance this method assumes clock skew among
  128.      * committers is never larger than 24 hours.
  129.      *
  130.      * @param commit
  131.      *            the commit we are looking at
  132.      * @param revWalk
  133.      *            The RevWalk to be used.
  134.      * @param refs
  135.      *            the set of branches we want to see reachability from
  136.      * @param monitor
  137.      *            the callback for progress and cancellation
  138.      * @return the list of branches a given commit is reachable from
  139.      * @throws org.eclipse.jgit.errors.MissingObjectException
  140.      * @throws org.eclipse.jgit.errors.IncorrectObjectTypeException
  141.      * @throws java.io.IOException
  142.      * @since 5.4
  143.      */
  144.     public static List<Ref> findBranchesReachableFrom(RevCommit commit,
  145.             RevWalk revWalk, Collection<Ref> refs, ProgressMonitor monitor)
  146.             throws MissingObjectException, IncorrectObjectTypeException,
  147.             IOException {

  148.         // Make sure commit is from the same RevWalk
  149.         commit = revWalk.parseCommit(commit.getId());
  150.         revWalk.reset();
  151.         List<Ref> result = new ArrayList<>();
  152.         monitor.beginTask(JGitText.get().searchForReachableBranches,
  153.                 refs.size());
  154.         final int SKEW = 24*3600; // one day clock skew

  155.         for (Ref ref : refs) {
  156.             if (monitor.isCancelled())
  157.                 return result;
  158.             monitor.update(1);
  159.             RevObject maybehead = revWalk.parseAny(ref.getObjectId());
  160.             if (!(maybehead instanceof RevCommit))
  161.                 continue;
  162.             RevCommit headCommit = (RevCommit) maybehead;

  163.             // if commit is in the ref branch, then the tip of ref should be
  164.             // newer than the commit we are looking for. Allow for a large
  165.             // clock skew.
  166.             if (headCommit.getCommitTime() + SKEW < commit.getCommitTime())
  167.                 continue;

  168.             if (revWalk.isMergedInto(commit, headCommit))
  169.                 result.add(ref);
  170.         }
  171.         monitor.endTask();
  172.         return result;
  173.     }

  174. }