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.

RevWalkUtils.java 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * Copyright (C) 2011-2012, Robin Stocker <robin@nibor.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.util.ArrayList;
  46. import java.util.Collection;
  47. import java.util.List;
  48. import org.eclipse.jgit.errors.IncorrectObjectTypeException;
  49. import org.eclipse.jgit.errors.MissingObjectException;
  50. import org.eclipse.jgit.lib.Ref;
  51. /**
  52. * Utility methods for {@link org.eclipse.jgit.revwalk.RevWalk}.
  53. */
  54. public final class RevWalkUtils {
  55. private RevWalkUtils() {
  56. // Utility class
  57. }
  58. /**
  59. * Count the number of commits that are reachable from <code>start</code>
  60. * until a commit that is reachable from <code>end</code> is encountered. In
  61. * other words, count the number of commits that are in <code>start</code>,
  62. * but not in <code>end</code>.
  63. * <p>
  64. * Note that this method calls
  65. * {@link org.eclipse.jgit.revwalk.RevWalk#reset()} at the beginning. Also
  66. * note that the existing rev filter on the walk is left as-is, so be sure
  67. * to set the right rev filter before calling this method.
  68. *
  69. * @param walk
  70. * the rev walk to use
  71. * @param start
  72. * the commit to start counting from
  73. * @param end
  74. * the commit where counting should end, or null if counting
  75. * should be done until there are no more commits
  76. * @return the number of commits
  77. * @throws org.eclipse.jgit.errors.MissingObjectException
  78. * @throws org.eclipse.jgit.errors.IncorrectObjectTypeException
  79. * @throws java.io.IOException
  80. */
  81. public static int count(final RevWalk walk, final RevCommit start,
  82. final RevCommit end) throws MissingObjectException,
  83. IncorrectObjectTypeException, IOException {
  84. return find(walk, start, end).size();
  85. }
  86. /**
  87. * Find commits that are reachable from <code>start</code> until a commit
  88. * that is reachable from <code>end</code> is encountered. In other words,
  89. * Find of commits that are in <code>start</code>, but not in
  90. * <code>end</code>.
  91. * <p>
  92. * Note that this method calls
  93. * {@link org.eclipse.jgit.revwalk.RevWalk#reset()} at the beginning. Also
  94. * note that the existing rev filter on the walk is left as-is, so be sure
  95. * to set the right rev filter before calling this method.
  96. *
  97. * @param walk
  98. * the rev walk to use
  99. * @param start
  100. * the commit to start counting from
  101. * @param end
  102. * the commit where counting should end, or null if counting
  103. * should be done until there are no more commits
  104. * @return the commits found
  105. * @throws org.eclipse.jgit.errors.MissingObjectException
  106. * @throws org.eclipse.jgit.errors.IncorrectObjectTypeException
  107. * @throws java.io.IOException
  108. */
  109. public static List<RevCommit> find(final RevWalk walk,
  110. final RevCommit start, final RevCommit end)
  111. throws MissingObjectException, IncorrectObjectTypeException,
  112. IOException {
  113. walk.reset();
  114. walk.markStart(start);
  115. if (end != null)
  116. walk.markUninteresting(end);
  117. List<RevCommit> commits = new ArrayList<>();
  118. for (RevCommit c : walk)
  119. commits.add(c);
  120. return commits;
  121. }
  122. /**
  123. * Find the list of branches a given commit is reachable from when following
  124. * parents.
  125. * <p>
  126. * Note that this method calls
  127. * {@link org.eclipse.jgit.revwalk.RevWalk#reset()} at the beginning.
  128. * <p>
  129. * In order to improve performance this method assumes clock skew among
  130. * committers is never larger than 24 hours.
  131. *
  132. * @param commit
  133. * the commit we are looking at
  134. * @param revWalk
  135. * The RevWalk to be used.
  136. * @param refs
  137. * the set of branches we want to see reachability from
  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. */
  143. public static List<Ref> findBranchesReachableFrom(RevCommit commit,
  144. RevWalk revWalk, Collection<Ref> refs)
  145. throws MissingObjectException, IncorrectObjectTypeException,
  146. IOException {
  147. // Make sure commit is from the same RevWalk
  148. commit = revWalk.parseCommit(commit.getId());
  149. revWalk.reset();
  150. List<Ref> result = new ArrayList<>();
  151. final int SKEW = 24*3600; // one day clock skew
  152. for (Ref ref : refs) {
  153. RevObject maybehead = revWalk.parseAny(ref.getObjectId());
  154. if (!(maybehead instanceof RevCommit))
  155. continue;
  156. RevCommit headCommit = (RevCommit) maybehead;
  157. // if commit is in the ref branch, then the tip of ref should be
  158. // newer than the commit we are looking for. Allow for a large
  159. // clock skew.
  160. if (headCommit.getCommitTime() + SKEW < commit.getCommitTime())
  161. continue;
  162. if (revWalk.isMergedInto(commit, headCommit))
  163. result.add(ref);
  164. }
  165. return result;
  166. }
  167. }