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

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