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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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 java.util.Collections;
  47. import java.util.List;
  48. import java.util.Objects;
  49. import org.eclipse.jgit.errors.IncorrectObjectTypeException;
  50. import org.eclipse.jgit.errors.MissingObjectException;
  51. import org.eclipse.jgit.lib.AnyObjectId;
  52. import org.eclipse.jgit.lib.ObjectId;
  53. import org.eclipse.jgit.lib.ObjectReader;
  54. import org.eclipse.jgit.lib.Repository;
  55. /**
  56. * Interface for revision walkers that perform depth filtering.
  57. */
  58. public interface DepthWalk {
  59. /**
  60. * Get depth to filter to.
  61. *
  62. * @return Depth to filter to.
  63. */
  64. int getDepth();
  65. /**
  66. * @return the deepen-since value; if not 0, this walk only returns commits
  67. * whose commit time is at or after this limit
  68. * @since 5.2
  69. */
  70. default int getDeepenSince() {
  71. return 0;
  72. }
  73. /**
  74. * @return the objects specified by the client using --shallow-exclude
  75. * @since 5.2
  76. */
  77. default List<ObjectId> getDeepenNots() {
  78. return Collections.emptyList();
  79. }
  80. /** @return flag marking commits that should become unshallow. */
  81. /**
  82. * Get flag marking commits that should become unshallow.
  83. *
  84. * @return flag marking commits that should become unshallow.
  85. */
  86. RevFlag getUnshallowFlag();
  87. /**
  88. * Get flag marking commits that are interesting again.
  89. *
  90. * @return flag marking commits that are interesting again.
  91. */
  92. RevFlag getReinterestingFlag();
  93. /**
  94. * @return flag marking commits that are to be excluded because of --shallow-exclude
  95. * @since 5.2
  96. */
  97. RevFlag getDeepenNotFlag();
  98. /** RevCommit with a depth (in commits) from a root. */
  99. public static class Commit extends RevCommit {
  100. /** Depth of this commit in the graph, via shortest path. */
  101. int depth;
  102. boolean isBoundary;
  103. /**
  104. * True if this commit was excluded due to a shallow fetch
  105. * setting. All its children are thus boundary commits.
  106. */
  107. boolean makesChildBoundary;
  108. /** @return depth of this commit, as found by the shortest path. */
  109. public int getDepth() {
  110. return depth;
  111. }
  112. /**
  113. * @return true if at least one of this commit's parents was excluded
  114. * due to a shallow fetch setting, false otherwise
  115. * @since 5.2
  116. */
  117. public boolean isBoundary() {
  118. return isBoundary;
  119. }
  120. /**
  121. * Initialize a new commit.
  122. *
  123. * @param id
  124. * object name for the commit.
  125. */
  126. protected Commit(AnyObjectId id) {
  127. super(id);
  128. depth = -1;
  129. }
  130. }
  131. /** Subclass of RevWalk that performs depth filtering. */
  132. public class RevWalk extends org.eclipse.jgit.revwalk.RevWalk implements DepthWalk {
  133. private final int depth;
  134. private int deepenSince;
  135. private List<ObjectId> deepenNots;
  136. private final RevFlag UNSHALLOW;
  137. private final RevFlag REINTERESTING;
  138. private final RevFlag DEEPEN_NOT;
  139. /**
  140. * @param repo Repository to walk
  141. * @param depth Maximum depth to return
  142. */
  143. public RevWalk(Repository repo, int depth) {
  144. super(repo);
  145. this.depth = depth;
  146. this.deepenNots = Collections.emptyList();
  147. this.UNSHALLOW = newFlag("UNSHALLOW"); //$NON-NLS-1$
  148. this.REINTERESTING = newFlag("REINTERESTING"); //$NON-NLS-1$
  149. this.DEEPEN_NOT = newFlag("DEEPEN_NOT"); //$NON-NLS-1$
  150. }
  151. /**
  152. * @param or ObjectReader to use
  153. * @param depth Maximum depth to return
  154. */
  155. public RevWalk(ObjectReader or, int depth) {
  156. super(or);
  157. this.depth = depth;
  158. this.deepenNots = Collections.emptyList();
  159. this.UNSHALLOW = newFlag("UNSHALLOW"); //$NON-NLS-1$
  160. this.REINTERESTING = newFlag("REINTERESTING"); //$NON-NLS-1$
  161. this.DEEPEN_NOT = newFlag("DEEPEN_NOT"); //$NON-NLS-1$
  162. }
  163. /**
  164. * Mark a root commit (i.e., one whose depth should be considered 0.)
  165. *
  166. * @param c
  167. * Commit to mark
  168. * @throws IOException
  169. * @throws IncorrectObjectTypeException
  170. * @throws MissingObjectException
  171. */
  172. public void markRoot(RevCommit c) throws MissingObjectException,
  173. IncorrectObjectTypeException, IOException {
  174. if (c instanceof Commit)
  175. ((Commit) c).depth = 0;
  176. super.markStart(c);
  177. }
  178. @Override
  179. protected RevCommit createCommit(AnyObjectId id) {
  180. return new Commit(id);
  181. }
  182. @Override
  183. public int getDepth() {
  184. return depth;
  185. }
  186. @Override
  187. public int getDeepenSince() {
  188. return deepenSince;
  189. }
  190. /**
  191. * Sets the deepen-since value.
  192. *
  193. * @param limit
  194. * new deepen-since value
  195. * @since 5.2
  196. */
  197. public void setDeepenSince(int limit) {
  198. deepenSince = limit;
  199. }
  200. @Override
  201. public List<ObjectId> getDeepenNots() {
  202. return deepenNots;
  203. }
  204. /**
  205. * Mark objects that the client specified using
  206. * --shallow-exclude. Objects that are not commits have no
  207. * effect.
  208. *
  209. * @param deepenNots specified objects
  210. * @since 5.2
  211. */
  212. public void setDeepenNots(List<ObjectId> deepenNots) {
  213. this.deepenNots = Objects.requireNonNull(deepenNots);
  214. }
  215. @Override
  216. public RevFlag getUnshallowFlag() {
  217. return UNSHALLOW;
  218. }
  219. @Override
  220. public RevFlag getReinterestingFlag() {
  221. return REINTERESTING;
  222. }
  223. @Override
  224. public RevFlag getDeepenNotFlag() {
  225. return DEEPEN_NOT;
  226. }
  227. /**
  228. * @since 4.5
  229. */
  230. @Override
  231. public ObjectWalk toObjectWalkWithSameObjects() {
  232. ObjectWalk ow = new ObjectWalk(reader, depth);
  233. ow.deepenSince = deepenSince;
  234. ow.deepenNots = deepenNots;
  235. ow.objects = objects;
  236. ow.freeFlags = freeFlags;
  237. return ow;
  238. }
  239. }
  240. /** Subclass of ObjectWalk that performs depth filtering. */
  241. public class ObjectWalk extends org.eclipse.jgit.revwalk.ObjectWalk implements DepthWalk {
  242. private final int depth;
  243. private int deepenSince;
  244. private List<ObjectId> deepenNots;
  245. private final RevFlag UNSHALLOW;
  246. private final RevFlag REINTERESTING;
  247. private final RevFlag DEEPEN_NOT;
  248. /**
  249. * @param repo Repository to walk
  250. * @param depth Maximum depth to return
  251. */
  252. public ObjectWalk(Repository repo, int depth) {
  253. super(repo);
  254. this.depth = depth;
  255. this.deepenNots = Collections.emptyList();
  256. this.UNSHALLOW = newFlag("UNSHALLOW"); //$NON-NLS-1$
  257. this.REINTERESTING = newFlag("REINTERESTING"); //$NON-NLS-1$
  258. this.DEEPEN_NOT = newFlag("DEEPEN_NOT"); //$NON-NLS-1$
  259. }
  260. /**
  261. * @param or Object Reader
  262. * @param depth Maximum depth to return
  263. */
  264. public ObjectWalk(ObjectReader or, int depth) {
  265. super(or);
  266. this.depth = depth;
  267. this.deepenNots = Collections.emptyList();
  268. this.UNSHALLOW = newFlag("UNSHALLOW"); //$NON-NLS-1$
  269. this.REINTERESTING = newFlag("REINTERESTING"); //$NON-NLS-1$
  270. this.DEEPEN_NOT = newFlag("DEEPEN_NOT"); //$NON-NLS-1$
  271. }
  272. /**
  273. * Mark a root commit (i.e., one whose depth should be considered 0.)
  274. *
  275. * @param o
  276. * Commit to mark
  277. * @throws IOException
  278. * @throws IncorrectObjectTypeException
  279. * @throws MissingObjectException
  280. */
  281. public void markRoot(RevObject o) throws MissingObjectException,
  282. IncorrectObjectTypeException, IOException {
  283. RevObject c = o;
  284. while (c instanceof RevTag) {
  285. c = ((RevTag) c).getObject();
  286. parseHeaders(c);
  287. }
  288. if (c instanceof Commit)
  289. ((Commit) c).depth = 0;
  290. super.markStart(o);
  291. }
  292. /**
  293. * Mark an element which used to be shallow in the client, but which
  294. * should now be considered a full commit. Any ancestors of this commit
  295. * should be included in the walk, even if they are the ancestor of an
  296. * uninteresting commit.
  297. *
  298. * @param c
  299. * Commit to mark
  300. * @throws MissingObjectException
  301. * @throws IncorrectObjectTypeException
  302. * @throws IOException
  303. */
  304. public void markUnshallow(RevObject c) throws MissingObjectException,
  305. IncorrectObjectTypeException, IOException {
  306. if (c instanceof RevCommit)
  307. c.add(UNSHALLOW);
  308. super.markStart(c);
  309. }
  310. @Override
  311. protected RevCommit createCommit(AnyObjectId id) {
  312. return new Commit(id);
  313. }
  314. @Override
  315. public int getDepth() {
  316. return depth;
  317. }
  318. @Override
  319. public int getDeepenSince() {
  320. return deepenSince;
  321. }
  322. @Override
  323. public List<ObjectId> getDeepenNots() {
  324. return deepenNots;
  325. }
  326. @Override
  327. public RevFlag getUnshallowFlag() {
  328. return UNSHALLOW;
  329. }
  330. @Override
  331. public RevFlag getReinterestingFlag() {
  332. return REINTERESTING;
  333. }
  334. @Override
  335. public RevFlag getDeepenNotFlag() {
  336. return DEEPEN_NOT;
  337. }
  338. }
  339. }