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

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