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 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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"); //$NON-NLS-1$
  91. this.REINTERESTING = newFlag("REINTERESTING"); //$NON-NLS-1$
  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"); //$NON-NLS-1$
  101. this.REINTERESTING = newFlag("REINTERESTING"); //$NON-NLS-1$
  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. * @since 4.5
  133. */
  134. @Override
  135. public ObjectWalk toObjectWalkWithSameObjects() {
  136. ObjectWalk ow = new ObjectWalk(reader, depth);
  137. ow.objects = objects;
  138. ow.freeFlags = freeFlags;
  139. return ow;
  140. }
  141. }
  142. /** Subclass of ObjectWalk that performs depth filtering. */
  143. public class ObjectWalk extends org.eclipse.jgit.revwalk.ObjectWalk implements DepthWalk {
  144. private final int depth;
  145. private final RevFlag UNSHALLOW;
  146. private final RevFlag REINTERESTING;
  147. /**
  148. * @param repo Repository to walk
  149. * @param depth Maximum depth to return
  150. */
  151. public ObjectWalk(Repository repo, int depth) {
  152. super(repo);
  153. this.depth = depth;
  154. this.UNSHALLOW = newFlag("UNSHALLOW"); //$NON-NLS-1$
  155. this.REINTERESTING = newFlag("REINTERESTING"); //$NON-NLS-1$
  156. }
  157. /**
  158. * @param or Object Reader
  159. * @param depth Maximum depth to return
  160. */
  161. public ObjectWalk(ObjectReader or, int depth) {
  162. super(or);
  163. this.depth = depth;
  164. this.UNSHALLOW = newFlag("UNSHALLOW"); //$NON-NLS-1$
  165. this.REINTERESTING = newFlag("REINTERESTING"); //$NON-NLS-1$
  166. }
  167. /**
  168. * Mark a root commit (i.e., one whose depth should be considered 0.)
  169. *
  170. * @param o
  171. * Commit to mark
  172. * @throws IOException
  173. * @throws IncorrectObjectTypeException
  174. * @throws MissingObjectException
  175. */
  176. public void markRoot(RevObject o) throws MissingObjectException,
  177. IncorrectObjectTypeException, IOException {
  178. RevObject c = o;
  179. while (c instanceof RevTag) {
  180. c = ((RevTag) c).getObject();
  181. parseHeaders(c);
  182. }
  183. if (c instanceof Commit)
  184. ((Commit) c).depth = 0;
  185. super.markStart(o);
  186. }
  187. /**
  188. * Mark an element which used to be shallow in the client, but which
  189. * should now be considered a full commit. Any ancestors of this commit
  190. * should be included in the walk, even if they are the ancestor of an
  191. * uninteresting commit.
  192. *
  193. * @param c
  194. * Commit to mark
  195. * @throws MissingObjectException
  196. * @throws IncorrectObjectTypeException
  197. * @throws IOException
  198. */
  199. public void markUnshallow(RevObject c) throws MissingObjectException,
  200. IncorrectObjectTypeException, IOException {
  201. if (c instanceof RevCommit)
  202. c.add(UNSHALLOW);
  203. super.markStart(c);
  204. }
  205. @Override
  206. protected RevCommit createCommit(AnyObjectId id) {
  207. return new Commit(id);
  208. }
  209. public int getDepth() {
  210. return depth;
  211. }
  212. public RevFlag getUnshallowFlag() {
  213. return UNSHALLOW;
  214. }
  215. public RevFlag getReinterestingFlag() {
  216. return REINTERESTING;
  217. }
  218. }
  219. }