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.

DepthWalk.java 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * Copyright (C) 2010, Garmin International
  3. * Copyright (C) 2010, Matt Fischer <matt.fischer@garmin.com>
  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.lib.AnyObjectId;
  49. import org.eclipse.jgit.lib.ObjectReader;
  50. import org.eclipse.jgit.lib.Repository;
  51. /** Interface for revision walkers that perform depth filtering. */
  52. public interface DepthWalk {
  53. /** @return Depth to filter to. */
  54. public int getDepth();
  55. /** @return flag marking commits that should become unshallow. */
  56. public RevFlag getUnshallowFlag();
  57. /** @return flag marking commits that are interesting again. */
  58. public RevFlag getReinterestingFlag();
  59. /** RevCommit with a depth (in commits) from a root. */
  60. public static class Commit extends RevCommit {
  61. /** Depth of this commit in the graph, via shortest path. */
  62. int depth;
  63. /** @return depth of this commit, as found by the shortest path. */
  64. public int getDepth() {
  65. return depth;
  66. }
  67. /**
  68. * Initialize a new commit.
  69. *
  70. * @param id
  71. * object name for the commit.
  72. */
  73. protected Commit(AnyObjectId id) {
  74. super(id);
  75. depth = -1;
  76. }
  77. }
  78. /** Subclass of RevWalk that performs depth filtering. */
  79. public class RevWalk extends org.eclipse.jgit.revwalk.RevWalk implements DepthWalk {
  80. private final int depth;
  81. private final RevFlag UNSHALLOW;
  82. private final RevFlag REINTERESTING;
  83. /**
  84. * @param repo Repository to walk
  85. * @param depth Maximum depth to return
  86. */
  87. public RevWalk(Repository repo, int depth) {
  88. super(repo);
  89. this.depth = depth;
  90. this.UNSHALLOW = newFlag("UNSHALLOW");
  91. this.REINTERESTING = newFlag("REINTERESTING");
  92. }
  93. /**
  94. * @param or ObjectReader to use
  95. * @param depth Maximum depth to return
  96. */
  97. public RevWalk(ObjectReader or, int depth) {
  98. super(or);
  99. this.depth = depth;
  100. this.UNSHALLOW = newFlag("UNSHALLOW");
  101. this.REINTERESTING = newFlag("REINTERESTING");
  102. }
  103. /**
  104. * Mark a root commit (i.e., one whose depth should be considered 0.)
  105. *
  106. * @param c
  107. * Commit to mark
  108. * @throws IOException
  109. * @throws IncorrectObjectTypeException
  110. * @throws MissingObjectException
  111. */
  112. public void markRoot(RevCommit c) throws MissingObjectException,
  113. IncorrectObjectTypeException, IOException {
  114. if (c instanceof Commit)
  115. ((Commit) c).depth = 0;
  116. super.markStart(c);
  117. }
  118. @Override
  119. protected RevCommit createCommit(AnyObjectId id) {
  120. return new Commit(id);
  121. }
  122. public int getDepth() {
  123. return depth;
  124. }
  125. public RevFlag getUnshallowFlag() {
  126. return UNSHALLOW;
  127. }
  128. public RevFlag getReinterestingFlag() {
  129. return REINTERESTING;
  130. }
  131. }
  132. /** Subclass of ObjectWalk that performs depth filtering. */
  133. public class ObjectWalk extends org.eclipse.jgit.revwalk.ObjectWalk implements DepthWalk {
  134. private final int depth;
  135. private final RevFlag UNSHALLOW;
  136. private final RevFlag REINTERESTING;
  137. /**
  138. * @param repo Repository to walk
  139. * @param depth Maximum depth to return
  140. */
  141. public ObjectWalk(Repository repo, int depth) {
  142. super(repo);
  143. this.depth = depth;
  144. this.UNSHALLOW = newFlag("UNSHALLOW");
  145. this.REINTERESTING = newFlag("REINTERESTING");
  146. }
  147. /**
  148. * @param or Object Reader
  149. * @param depth Maximum depth to return
  150. */
  151. public ObjectWalk(ObjectReader or, int depth) {
  152. super(or);
  153. this.depth = depth;
  154. this.UNSHALLOW = newFlag("UNSHALLOW");
  155. this.REINTERESTING = newFlag("REINTERESTING");
  156. }
  157. /**
  158. * Mark a root commit (i.e., one whose depth should be considered 0.)
  159. *
  160. * @param o
  161. * Commit to mark
  162. * @throws IOException
  163. * @throws IncorrectObjectTypeException
  164. * @throws MissingObjectException
  165. */
  166. public void markRoot(RevObject o) throws MissingObjectException,
  167. IncorrectObjectTypeException, IOException {
  168. RevObject c = o;
  169. while (c instanceof RevTag) {
  170. c = ((RevTag) c).getObject();
  171. parseHeaders(c);
  172. }
  173. if (c instanceof Commit)
  174. ((Commit) c).depth = 0;
  175. super.markStart(o);
  176. }
  177. /**
  178. * Mark an element which used to be shallow in the client, but which
  179. * should now be considered a full commit. Any ancestors of this commit
  180. * should be included in the walk, even if they are the ancestor of an
  181. * uninteresting commit.
  182. *
  183. * @param c
  184. * Commit to mark
  185. * @throws MissingObjectException
  186. * @throws IncorrectObjectTypeException
  187. * @throws IOException
  188. */
  189. public void markUnshallow(RevObject c) throws MissingObjectException,
  190. IncorrectObjectTypeException, IOException {
  191. if (c instanceof RevCommit)
  192. c.add(UNSHALLOW);
  193. super.markStart(c);
  194. }
  195. @Override
  196. protected RevCommit createCommit(AnyObjectId id) {
  197. return new Commit(id);
  198. }
  199. public int getDepth() {
  200. return depth;
  201. }
  202. public RevFlag getUnshallowFlag() {
  203. return UNSHALLOW;
  204. }
  205. public RevFlag getReinterestingFlag() {
  206. return REINTERESTING;
  207. }
  208. }
  209. }