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.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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 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 {@link RevWalk#reset()} at the beginning.
  65. * Also note that the existing rev filter on the walk is left as-is, so be
  66. * sure to set the right rev filter before calling this method.
  67. *
  68. * @param walk
  69. * the rev walk to use
  70. * @param start
  71. * the commit to start counting from
  72. * @param end
  73. * the commit where counting should end, or null if counting
  74. * should be done until there are no more commits
  75. *
  76. * @return the number of commits
  77. * @throws MissingObjectException
  78. * @throws IncorrectObjectTypeException
  79. * @throws 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 {@link RevWalk#reset()} at the beginning.
  93. * Also note that the existing rev filter on the walk is left as-is, so be
  94. * sure to set the right rev filter before calling this method.
  95. *
  96. * @param walk
  97. * the rev walk to use
  98. * @param start
  99. * the commit to start counting from
  100. * @param end
  101. * the commit where counting should end, or null if counting
  102. * should be done until there are no more commits
  103. * @return the commits found
  104. * @throws MissingObjectException
  105. * @throws IncorrectObjectTypeException
  106. * @throws IOException
  107. */
  108. public static List<RevCommit> find(final RevWalk walk,
  109. final RevCommit start, final RevCommit end)
  110. throws MissingObjectException, IncorrectObjectTypeException,
  111. IOException {
  112. walk.reset();
  113. walk.markStart(start);
  114. if (end != null)
  115. walk.markUninteresting(end);
  116. List<RevCommit> commits = new ArrayList<RevCommit>();
  117. for (RevCommit c : walk)
  118. commits.add(c);
  119. return commits;
  120. }
  121. /**
  122. * Find the list of branches a given commit is reachable from when following
  123. * parent.s
  124. * <p>
  125. * Note that this method calls {@link 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. * @return the list of branches a given commit is reachable from
  137. * @throws MissingObjectException
  138. * @throws IncorrectObjectTypeException
  139. * @throws IOException
  140. */
  141. public static List<Ref> findBranchesReachableFrom(RevCommit commit,
  142. RevWalk revWalk, Collection<Ref> refs)
  143. throws MissingObjectException, IncorrectObjectTypeException,
  144. IOException {
  145. // Make sure commit is from the same RevWalk
  146. commit = revWalk.parseCommit(commit.getId());
  147. revWalk.reset();
  148. List<Ref> result = new ArrayList<Ref>();
  149. final int SKEW = 24*3600; // one day clock skew
  150. for (Ref ref : refs) {
  151. RevObject maybehead = revWalk.parseAny(ref.getObjectId());
  152. if (!(maybehead instanceof RevCommit))
  153. continue;
  154. RevCommit headCommit = (RevCommit) maybehead;
  155. // if commit is in the ref branch, then the tip of ref should be
  156. // newer than the commit we are looking for. Allow for a large
  157. // clock skew.
  158. if (headCommit.getCommitTime() + SKEW < commit.getCommitTime())
  159. continue;
  160. if (revWalk.isMergedInto(commit, headCommit))
  161. result.add(ref);
  162. }
  163. return result;
  164. }
  165. }