Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414
  1. /*
  2. * Copyright (C) 2007, Robin Rosenberg <robin.rosenberg@dewire.com>
  3. * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
  4. * Copyright (C) 2014, Gustaf Lundh <gustaf.lundh@sonymobile.com>
  5. * and other copyright owners as documented in the project's IP log.
  6. *
  7. * This program and the accompanying materials are made available
  8. * under the terms of the Eclipse Distribution License v1.0 which
  9. * accompanies this distribution, is reproduced below, and is
  10. * available at http://www.eclipse.org/org/documents/edl-v10.php
  11. *
  12. * All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or
  15. * without modification, are permitted provided that the following
  16. * conditions are met:
  17. *
  18. * - Redistributions of source code must retain the above copyright
  19. * notice, this list of conditions and the following disclaimer.
  20. *
  21. * - Redistributions in binary form must reproduce the above
  22. * copyright notice, this list of conditions and the following
  23. * disclaimer in the documentation and/or other materials provided
  24. * with the distribution.
  25. *
  26. * - Neither the name of the Eclipse Foundation, Inc. nor the
  27. * names of its contributors may be used to endorse or promote
  28. * products derived from this software without specific prior
  29. * written permission.
  30. *
  31. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  32. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  33. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  34. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  35. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  36. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  37. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  38. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  39. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  40. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  41. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  42. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  43. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  44. */
  45. package org.eclipse.jgit.revwalk;
  46. import java.io.IOException;
  47. import java.text.MessageFormat;
  48. import java.util.ArrayList;
  49. import java.util.Collection;
  50. import java.util.EnumSet;
  51. import java.util.Iterator;
  52. import java.util.List;
  53. import org.eclipse.jgit.errors.CorruptObjectException;
  54. import org.eclipse.jgit.errors.IncorrectObjectTypeException;
  55. import org.eclipse.jgit.errors.LargeObjectException;
  56. import org.eclipse.jgit.errors.MissingObjectException;
  57. import org.eclipse.jgit.errors.RevWalkException;
  58. import org.eclipse.jgit.internal.JGitText;
  59. import org.eclipse.jgit.lib.AnyObjectId;
  60. import org.eclipse.jgit.lib.AsyncObjectLoaderQueue;
  61. import org.eclipse.jgit.lib.Constants;
  62. import org.eclipse.jgit.lib.MutableObjectId;
  63. import org.eclipse.jgit.lib.ObjectId;
  64. import org.eclipse.jgit.lib.ObjectIdOwnerMap;
  65. import org.eclipse.jgit.lib.ObjectLoader;
  66. import org.eclipse.jgit.lib.ObjectReader;
  67. import org.eclipse.jgit.lib.Repository;
  68. import org.eclipse.jgit.revwalk.filter.RevFilter;
  69. import org.eclipse.jgit.treewalk.filter.TreeFilter;
  70. /**
  71. * Walks a commit graph and produces the matching commits in order.
  72. * <p>
  73. * A RevWalk instance can only be used once to generate results. Running a
  74. * second time requires creating a new RevWalk instance, or invoking
  75. * {@link #reset()} before starting again. Resetting an existing instance may be
  76. * faster for some applications as commit body parsing can be avoided on the
  77. * later invocations.
  78. * <p>
  79. * RevWalk instances are not thread-safe. Applications must either restrict
  80. * usage of a RevWalk instance to a single thread, or implement their own
  81. * synchronization at a higher level.
  82. * <p>
  83. * Multiple simultaneous RevWalk instances per {@link Repository} are permitted,
  84. * even from concurrent threads. Equality of {@link RevCommit}s from two
  85. * different RevWalk instances is never true, even if their {@link ObjectId}s
  86. * are equal (and thus they describe the same commit).
  87. * <p>
  88. * The offered iterator is over the list of RevCommits described by the
  89. * configuration of this instance. Applications should restrict themselves to
  90. * using either the provided Iterator or {@link #next()}, but never use both on
  91. * the same RevWalk at the same time. The Iterator may buffer RevCommits, while
  92. * {@link #next()} does not.
  93. */
  94. public class RevWalk implements Iterable<RevCommit> {
  95. private static final int MB = 1 << 20;
  96. /**
  97. * Set on objects whose important header data has been loaded.
  98. * <p>
  99. * For a RevCommit this indicates we have pulled apart the tree and parent
  100. * references from the raw bytes available in the repository and translated
  101. * those to our own local RevTree and RevCommit instances. The raw buffer is
  102. * also available for message and other header filtering.
  103. * <p>
  104. * For a RevTag this indicates we have pulled part the tag references to
  105. * find out who the tag refers to, and what that object's type is.
  106. */
  107. static final int PARSED = 1 << 0;
  108. /**
  109. * Set on RevCommit instances added to our {@link #pending} queue.
  110. * <p>
  111. * We use this flag to avoid adding the same commit instance twice to our
  112. * queue, especially if we reached it by more than one path.
  113. */
  114. static final int SEEN = 1 << 1;
  115. /**
  116. * Set on RevCommit instances the caller does not want output.
  117. * <p>
  118. * We flag commits as uninteresting if the caller does not want commits
  119. * reachable from a commit given to {@link #markUninteresting(RevCommit)}.
  120. * This flag is always carried into the commit's parents and is a key part
  121. * of the "rev-list B --not A" feature; A is marked UNINTERESTING.
  122. */
  123. static final int UNINTERESTING = 1 << 2;
  124. /**
  125. * Set on a RevCommit that can collapse out of the history.
  126. * <p>
  127. * If the {@link #treeFilter} concluded that this commit matches his
  128. * parents' for all of the paths that the filter is interested in then we
  129. * mark the commit REWRITE. Later we can rewrite the parents of a REWRITE
  130. * child to remove chains of REWRITE commits before we produce the child to
  131. * the application.
  132. *
  133. * @see RewriteGenerator
  134. */
  135. static final int REWRITE = 1 << 3;
  136. /**
  137. * Temporary mark for use within generators or filters.
  138. * <p>
  139. * This mark is only for local use within a single scope. If someone sets
  140. * the mark they must unset it before any other code can see the mark.
  141. */
  142. static final int TEMP_MARK = 1 << 4;
  143. /**
  144. * Temporary mark for use within {@link TopoSortGenerator}.
  145. * <p>
  146. * This mark indicates the commit could not produce when it wanted to, as at
  147. * least one child was behind it. Commits with this flag are delayed until
  148. * all children have been output first.
  149. */
  150. static final int TOPO_DELAY = 1 << 5;
  151. /** Number of flag bits we keep internal for our own use. See above flags. */
  152. static final int RESERVED_FLAGS = 6;
  153. private static final int APP_FLAGS = -1 & ~((1 << RESERVED_FLAGS) - 1);
  154. final ObjectReader reader;
  155. final MutableObjectId idBuffer;
  156. ObjectIdOwnerMap<RevObject> objects;
  157. private int freeFlags = APP_FLAGS;
  158. private int delayFreeFlags;
  159. private int retainOnReset;
  160. int carryFlags = UNINTERESTING;
  161. final ArrayList<RevCommit> roots;
  162. AbstractRevQueue queue;
  163. Generator pending;
  164. private final EnumSet<RevSort> sorting;
  165. private RevFilter filter;
  166. private TreeFilter treeFilter;
  167. private boolean retainBody;
  168. private boolean rewriteParents = true;
  169. boolean shallowCommitsInitialized;
  170. /**
  171. * Create a new revision walker for a given repository.
  172. *
  173. * @param repo
  174. * the repository the walker will obtain data from. An
  175. * ObjectReader will be created by the walker, and must be
  176. * released by the caller.
  177. */
  178. public RevWalk(final Repository repo) {
  179. this(repo.newObjectReader());
  180. }
  181. /**
  182. * Create a new revision walker for a given repository.
  183. *
  184. * @param or
  185. * the reader the walker will obtain data from. The reader should
  186. * be released by the caller when the walker is no longer
  187. * required.
  188. */
  189. public RevWalk(ObjectReader or) {
  190. reader = or;
  191. idBuffer = new MutableObjectId();
  192. objects = new ObjectIdOwnerMap<RevObject>();
  193. roots = new ArrayList<RevCommit>();
  194. queue = new DateRevQueue();
  195. pending = new StartGenerator(this);
  196. sorting = EnumSet.of(RevSort.NONE);
  197. filter = RevFilter.ALL;
  198. treeFilter = TreeFilter.ALL;
  199. retainBody = true;
  200. }
  201. /** @return the reader this walker is using to load objects. */
  202. public ObjectReader getObjectReader() {
  203. return reader;
  204. }
  205. /**
  206. * Release any resources used by this walker's reader.
  207. * <p>
  208. * A walker that has been released can be used again, but may need to be
  209. * released after the subsequent usage.
  210. */
  211. public void release() {
  212. reader.release();
  213. }
  214. /**
  215. * Mark a commit to start graph traversal from.
  216. * <p>
  217. * Callers are encouraged to use {@link #parseCommit(AnyObjectId)} to obtain
  218. * the commit reference, rather than {@link #lookupCommit(AnyObjectId)}, as
  219. * this method requires the commit to be parsed before it can be added as a
  220. * root for the traversal.
  221. * <p>
  222. * The method will automatically parse an unparsed commit, but error
  223. * handling may be more difficult for the application to explain why a
  224. * RevCommit is not actually a commit. The object pool of this walker would
  225. * also be 'poisoned' by the non-commit RevCommit.
  226. *
  227. * @param c
  228. * the commit to start traversing from. The commit passed must be
  229. * from this same revision walker.
  230. * @throws MissingObjectException
  231. * the commit supplied is not available from the object
  232. * database. This usually indicates the supplied commit is
  233. * invalid, but the reference was constructed during an earlier
  234. * invocation to {@link #lookupCommit(AnyObjectId)}.
  235. * @throws IncorrectObjectTypeException
  236. * the object was not parsed yet and it was discovered during
  237. * parsing that it is not actually a commit. This usually
  238. * indicates the caller supplied a non-commit SHA-1 to
  239. * {@link #lookupCommit(AnyObjectId)}.
  240. * @throws IOException
  241. * a pack file or loose object could not be read.
  242. */
  243. public void markStart(final RevCommit c) throws MissingObjectException,
  244. IncorrectObjectTypeException, IOException {
  245. if ((c.flags & SEEN) != 0)
  246. return;
  247. if ((c.flags & PARSED) == 0)
  248. c.parseHeaders(this);
  249. c.flags |= SEEN;
  250. roots.add(c);
  251. queue.add(c);
  252. }
  253. /**
  254. * Mark commits to start graph traversal from.
  255. *
  256. * @param list
  257. * commits to start traversing from. The commits passed must be
  258. * from this same revision walker.
  259. * @throws MissingObjectException
  260. * one of the commits supplied is not available from the object
  261. * database. This usually indicates the supplied commit is
  262. * invalid, but the reference was constructed during an earlier
  263. * invocation to {@link #lookupCommit(AnyObjectId)}.
  264. * @throws IncorrectObjectTypeException
  265. * the object was not parsed yet and it was discovered during
  266. * parsing that it is not actually a commit. This usually
  267. * indicates the caller supplied a non-commit SHA-1 to
  268. * {@link #lookupCommit(AnyObjectId)}.
  269. * @throws IOException
  270. * a pack file or loose object could not be read.
  271. */
  272. public void markStart(final Collection<RevCommit> list)
  273. throws MissingObjectException, IncorrectObjectTypeException,
  274. IOException {
  275. for (final RevCommit c : list)
  276. markStart(c);
  277. }
  278. /**
  279. * Mark a commit to not produce in the output.
  280. * <p>
  281. * Uninteresting commits denote not just themselves but also their entire
  282. * ancestry chain, back until the merge base of an uninteresting commit and
  283. * an otherwise interesting commit.
  284. * <p>
  285. * Callers are encouraged to use {@link #parseCommit(AnyObjectId)} to obtain
  286. * the commit reference, rather than {@link #lookupCommit(AnyObjectId)}, as
  287. * this method requires the commit to be parsed before it can be added as a
  288. * root for the traversal.
  289. * <p>
  290. * The method will automatically parse an unparsed commit, but error
  291. * handling may be more difficult for the application to explain why a
  292. * RevCommit is not actually a commit. The object pool of this walker would
  293. * also be 'poisoned' by the non-commit RevCommit.
  294. *
  295. * @param c
  296. * the commit to start traversing from. The commit passed must be
  297. * from this same revision walker.
  298. * @throws MissingObjectException
  299. * the commit supplied is not available from the object
  300. * database. This usually indicates the supplied commit is
  301. * invalid, but the reference was constructed during an earlier
  302. * invocation to {@link #lookupCommit(AnyObjectId)}.
  303. * @throws IncorrectObjectTypeException
  304. * the object was not parsed yet and it was discovered during
  305. * parsing that it is not actually a commit. This usually
  306. * indicates the caller supplied a non-commit SHA-1 to
  307. * {@link #lookupCommit(AnyObjectId)}.
  308. * @throws IOException
  309. * a pack file or loose object could not be read.
  310. */
  311. public void markUninteresting(final RevCommit c)
  312. throws MissingObjectException, IncorrectObjectTypeException,
  313. IOException {
  314. c.flags |= UNINTERESTING;
  315. carryFlagsImpl(c);
  316. markStart(c);
  317. }
  318. /**
  319. * Determine if a commit is reachable from another commit.
  320. * <p>
  321. * A commit <code>base</code> is an ancestor of <code>tip</code> if we
  322. * can find a path of commits that leads from <code>tip</code> and ends at
  323. * <code>base</code>.
  324. * <p>
  325. * This utility function resets the walker, inserts the two supplied
  326. * commits, and then executes a walk until an answer can be obtained.
  327. * Currently allocated RevFlags that have been added to RevCommit instances
  328. * will be retained through the reset.
  329. *
  330. * @param base
  331. * commit the caller thinks is reachable from <code>tip</code>.
  332. * @param tip
  333. * commit to start iteration from, and which is most likely a
  334. * descendant (child) of <code>base</code>.
  335. * @return true if there is a path directly from <code>tip</code> to
  336. * <code>base</code> (and thus <code>base</code> is fully merged
  337. * into <code>tip</code>); false otherwise.
  338. * @throws MissingObjectException
  339. * one or or more of the next commit's parents are not available
  340. * from the object database, but were thought to be candidates
  341. * for traversal. This usually indicates a broken link.
  342. * @throws IncorrectObjectTypeException
  343. * one or or more of the next commit's parents are not actually
  344. * commit objects.
  345. * @throws IOException
  346. * a pack file or loose object could not be read.
  347. */
  348. public boolean isMergedInto(final RevCommit base, final RevCommit tip)
  349. throws MissingObjectException, IncorrectObjectTypeException,
  350. IOException {
  351. final RevFilter oldRF = filter;
  352. final TreeFilter oldTF = treeFilter;
  353. try {
  354. finishDelayedFreeFlags();
  355. reset(~freeFlags & APP_FLAGS);
  356. filter = RevFilter.MERGE_BASE;
  357. treeFilter = TreeFilter.ALL;
  358. markStart(tip);
  359. markStart(base);
  360. RevCommit mergeBase;
  361. while ((mergeBase = next()) != null)
  362. if (mergeBase == base)
  363. return true;
  364. return false;
  365. } finally {
  366. filter = oldRF;
  367. treeFilter = oldTF;
  368. }
  369. }
  370. /**
  371. * Pop the next most recent commit.
  372. *
  373. * @return next most recent commit; null if traversal is over.
  374. * @throws MissingObjectException
  375. * one or or more of the next commit's parents are not available
  376. * from the object database, but were thought to be candidates
  377. * for traversal. This usually indicates a broken link.
  378. * @throws IncorrectObjectTypeException
  379. * one or or more of the next commit's parents are not actually
  380. * commit objects.
  381. * @throws IOException
  382. * a pack file or loose object could not be read.
  383. */
  384. public RevCommit next() throws MissingObjectException,
  385. IncorrectObjectTypeException, IOException {
  386. return pending.next();
  387. }
  388. /**
  389. * Obtain the sort types applied to the commits returned.
  390. *
  391. * @return the sorting strategies employed. At least one strategy is always
  392. * used, but that strategy may be {@link RevSort#NONE}.
  393. */
  394. public EnumSet<RevSort> getRevSort() {
  395. return sorting.clone();
  396. }
  397. /**
  398. * Check whether the provided sorting strategy is enabled.
  399. *
  400. * @param sort
  401. * a sorting strategy to look for.
  402. * @return true if this strategy is enabled, false otherwise
  403. */
  404. public boolean hasRevSort(RevSort sort) {
  405. return sorting.contains(sort);
  406. }
  407. /**
  408. * Select a single sorting strategy for the returned commits.
  409. * <p>
  410. * Disables all sorting strategies, then enables only the single strategy
  411. * supplied by the caller.
  412. *
  413. * @param s
  414. * a sorting strategy to enable.
  415. */
  416. public void sort(final RevSort s) {
  417. assertNotStarted();
  418. sorting.clear();
  419. sorting.add(s);
  420. }
  421. /**
  422. * Add or remove a sorting strategy for the returned commits.
  423. * <p>
  424. * Multiple strategies can be applied at once, in which case some strategies
  425. * may take precedence over others. As an example, {@link RevSort#TOPO} must
  426. * take precedence over {@link RevSort#COMMIT_TIME_DESC}, otherwise it
  427. * cannot enforce its ordering.
  428. *
  429. * @param s
  430. * a sorting strategy to enable or disable.
  431. * @param use
  432. * true if this strategy should be used, false if it should be
  433. * removed.
  434. */
  435. public void sort(final RevSort s, final boolean use) {
  436. assertNotStarted();
  437. if (use)
  438. sorting.add(s);
  439. else
  440. sorting.remove(s);
  441. if (sorting.size() > 1)
  442. sorting.remove(RevSort.NONE);
  443. else if (sorting.size() == 0)
  444. sorting.add(RevSort.NONE);
  445. }
  446. /**
  447. * Get the currently configured commit filter.
  448. *
  449. * @return the current filter. Never null as a filter is always needed.
  450. */
  451. public RevFilter getRevFilter() {
  452. return filter;
  453. }
  454. /**
  455. * Set the commit filter for this walker.
  456. * <p>
  457. * Multiple filters may be combined by constructing an arbitrary tree of
  458. * <code>AndRevFilter</code> or <code>OrRevFilter</code> instances to
  459. * describe the boolean expression required by the application. Custom
  460. * filter implementations may also be constructed by applications.
  461. * <p>
  462. * Note that filters are not thread-safe and may not be shared by concurrent
  463. * RevWalk instances. Every RevWalk must be supplied its own unique filter,
  464. * unless the filter implementation specifically states it is (and always
  465. * will be) thread-safe. Callers may use {@link RevFilter#clone()} to create
  466. * a unique filter tree for this RevWalk instance.
  467. *
  468. * @param newFilter
  469. * the new filter. If null the special {@link RevFilter#ALL}
  470. * filter will be used instead, as it matches every commit.
  471. * @see org.eclipse.jgit.revwalk.filter.AndRevFilter
  472. * @see org.eclipse.jgit.revwalk.filter.OrRevFilter
  473. */
  474. public void setRevFilter(final RevFilter newFilter) {
  475. assertNotStarted();
  476. filter = newFilter != null ? newFilter : RevFilter.ALL;
  477. }
  478. /**
  479. * Get the tree filter used to simplify commits by modified paths.
  480. *
  481. * @return the current filter. Never null as a filter is always needed. If
  482. * no filter is being applied {@link TreeFilter#ALL} is returned.
  483. */
  484. public TreeFilter getTreeFilter() {
  485. return treeFilter;
  486. }
  487. /**
  488. * Set the tree filter used to simplify commits by modified paths.
  489. * <p>
  490. * If null or {@link TreeFilter#ALL} the path limiter is removed. Commits
  491. * will not be simplified.
  492. * <p>
  493. * If non-null and not {@link TreeFilter#ALL} then the tree filter will be
  494. * installed. Commits will have their ancestry simplified to hide commits that
  495. * do not contain tree entries matched by the filter, unless
  496. * {@code setRewriteParents(false)} is called.
  497. * <p>
  498. * Usually callers should be inserting a filter graph including
  499. * {@link TreeFilter#ANY_DIFF} along with one or more
  500. * {@link org.eclipse.jgit.treewalk.filter.PathFilter} instances.
  501. *
  502. * @param newFilter
  503. * new filter. If null the special {@link TreeFilter#ALL} filter
  504. * will be used instead, as it matches everything.
  505. * @see org.eclipse.jgit.treewalk.filter.PathFilter
  506. */
  507. public void setTreeFilter(final TreeFilter newFilter) {
  508. assertNotStarted();
  509. treeFilter = newFilter != null ? newFilter : TreeFilter.ALL;
  510. }
  511. /**
  512. * Set whether to rewrite parent pointers when filtering by modified paths.
  513. * <p>
  514. * By default, when {@link #setTreeFilter(TreeFilter)} is called with non-
  515. * null and non-{@link TreeFilter#ALL} filter, commits will have their
  516. * ancestry simplified and parents rewritten to hide commits that do not match
  517. * the filter.
  518. * <p>
  519. * This behavior can be bypassed by passing false to this method.
  520. *
  521. * @param rewrite
  522. * whether to rewrite parents; defaults to true.
  523. * @since 3.4
  524. */
  525. public void setRewriteParents(boolean rewrite) {
  526. rewriteParents = rewrite;
  527. }
  528. boolean getRewriteParents() {
  529. return rewriteParents;
  530. }
  531. /**
  532. * Should the body of a commit or tag be retained after parsing its headers?
  533. * <p>
  534. * Usually the body is always retained, but some application code might not
  535. * care and would prefer to discard the body of a commit as early as
  536. * possible, to reduce memory usage.
  537. *
  538. * @return true if the body should be retained; false it is discarded.
  539. */
  540. public boolean isRetainBody() {
  541. return retainBody;
  542. }
  543. /**
  544. * Set whether or not the body of a commit or tag is retained.
  545. * <p>
  546. * If a body of a commit or tag is not retained, the application must
  547. * call {@link #parseBody(RevObject)} before the body can be safely
  548. * accessed through the type specific access methods.
  549. *
  550. * @param retain true to retain bodies; false to discard them early.
  551. */
  552. public void setRetainBody(final boolean retain) {
  553. retainBody = retain;
  554. }
  555. /**
  556. * Locate a reference to a blob without loading it.
  557. * <p>
  558. * The blob may or may not exist in the repository. It is impossible to tell
  559. * from this method's return value.
  560. *
  561. * @param id
  562. * name of the blob object.
  563. * @return reference to the blob object. Never null.
  564. */
  565. public RevBlob lookupBlob(final AnyObjectId id) {
  566. RevBlob c = (RevBlob) objects.get(id);
  567. if (c == null) {
  568. c = new RevBlob(id);
  569. objects.add(c);
  570. }
  571. return c;
  572. }
  573. /**
  574. * Locate a reference to a tree without loading it.
  575. * <p>
  576. * The tree may or may not exist in the repository. It is impossible to tell
  577. * from this method's return value.
  578. *
  579. * @param id
  580. * name of the tree object.
  581. * @return reference to the tree object. Never null.
  582. */
  583. public RevTree lookupTree(final AnyObjectId id) {
  584. RevTree c = (RevTree) objects.get(id);
  585. if (c == null) {
  586. c = new RevTree(id);
  587. objects.add(c);
  588. }
  589. return c;
  590. }
  591. /**
  592. * Locate a reference to a commit without loading it.
  593. * <p>
  594. * The commit may or may not exist in the repository. It is impossible to
  595. * tell from this method's return value.
  596. * <p>
  597. * See {@link #parseHeaders(RevObject)} and {@link #parseBody(RevObject)}
  598. * for loading contents.
  599. *
  600. * @param id
  601. * name of the commit object.
  602. * @return reference to the commit object. Never null.
  603. */
  604. public RevCommit lookupCommit(final AnyObjectId id) {
  605. RevCommit c = (RevCommit) objects.get(id);
  606. if (c == null) {
  607. c = createCommit(id);
  608. objects.add(c);
  609. }
  610. return c;
  611. }
  612. /**
  613. * Locate a reference to a tag without loading it.
  614. * <p>
  615. * The tag may or may not exist in the repository. It is impossible to tell
  616. * from this method's return value.
  617. *
  618. * @param id
  619. * name of the tag object.
  620. * @return reference to the tag object. Never null.
  621. */
  622. public RevTag lookupTag(final AnyObjectId id) {
  623. RevTag c = (RevTag) objects.get(id);
  624. if (c == null) {
  625. c = new RevTag(id);
  626. objects.add(c);
  627. }
  628. return c;
  629. }
  630. /**
  631. * Locate a reference to any object without loading it.
  632. * <p>
  633. * The object may or may not exist in the repository. It is impossible to
  634. * tell from this method's return value.
  635. *
  636. * @param id
  637. * name of the object.
  638. * @param type
  639. * type of the object. Must be a valid Git object type.
  640. * @return reference to the object. Never null.
  641. */
  642. public RevObject lookupAny(final AnyObjectId id, final int type) {
  643. RevObject r = objects.get(id);
  644. if (r == null) {
  645. switch (type) {
  646. case Constants.OBJ_COMMIT:
  647. r = createCommit(id);
  648. break;
  649. case Constants.OBJ_TREE:
  650. r = new RevTree(id);
  651. break;
  652. case Constants.OBJ_BLOB:
  653. r = new RevBlob(id);
  654. break;
  655. case Constants.OBJ_TAG:
  656. r = new RevTag(id);
  657. break;
  658. default:
  659. throw new IllegalArgumentException(MessageFormat.format(
  660. JGitText.get().invalidGitType, Integer.valueOf(type)));
  661. }
  662. objects.add(r);
  663. }
  664. return r;
  665. }
  666. /**
  667. * Locate an object that was previously allocated in this walk.
  668. *
  669. * @param id
  670. * name of the object.
  671. * @return reference to the object if it has been previously located;
  672. * otherwise null.
  673. */
  674. public RevObject lookupOrNull(AnyObjectId id) {
  675. return objects.get(id);
  676. }
  677. /**
  678. * Locate a reference to a commit and immediately parse its content.
  679. * <p>
  680. * Unlike {@link #lookupCommit(AnyObjectId)} this method only returns
  681. * successfully if the commit object exists, is verified to be a commit, and
  682. * was parsed without error.
  683. *
  684. * @param id
  685. * name of the commit object.
  686. * @return reference to the commit object. Never null.
  687. * @throws MissingObjectException
  688. * the supplied commit does not exist.
  689. * @throws IncorrectObjectTypeException
  690. * the supplied id is not a commit or an annotated tag.
  691. * @throws IOException
  692. * a pack file or loose object could not be read.
  693. */
  694. public RevCommit parseCommit(final AnyObjectId id)
  695. throws MissingObjectException, IncorrectObjectTypeException,
  696. IOException {
  697. RevObject c = peel(parseAny(id));
  698. if (!(c instanceof RevCommit))
  699. throw new IncorrectObjectTypeException(id.toObjectId(),
  700. Constants.TYPE_COMMIT);
  701. return (RevCommit) c;
  702. }
  703. /**
  704. * Locate a reference to a tree.
  705. * <p>
  706. * This method only returns successfully if the tree object exists, is
  707. * verified to be a tree.
  708. *
  709. * @param id
  710. * name of the tree object, or a commit or annotated tag that may
  711. * reference a tree.
  712. * @return reference to the tree object. Never null.
  713. * @throws MissingObjectException
  714. * the supplied tree does not exist.
  715. * @throws IncorrectObjectTypeException
  716. * the supplied id is not a tree, a commit or an annotated tag.
  717. * @throws IOException
  718. * a pack file or loose object could not be read.
  719. */
  720. public RevTree parseTree(final AnyObjectId id)
  721. throws MissingObjectException, IncorrectObjectTypeException,
  722. IOException {
  723. RevObject c = peel(parseAny(id));
  724. final RevTree t;
  725. if (c instanceof RevCommit)
  726. t = ((RevCommit) c).getTree();
  727. else if (!(c instanceof RevTree))
  728. throw new IncorrectObjectTypeException(id.toObjectId(),
  729. Constants.TYPE_TREE);
  730. else
  731. t = (RevTree) c;
  732. parseHeaders(t);
  733. return t;
  734. }
  735. /**
  736. * Locate a reference to an annotated tag and immediately parse its content.
  737. * <p>
  738. * Unlike {@link #lookupTag(AnyObjectId)} this method only returns
  739. * successfully if the tag object exists, is verified to be a tag, and was
  740. * parsed without error.
  741. *
  742. * @param id
  743. * name of the tag object.
  744. * @return reference to the tag object. Never null.
  745. * @throws MissingObjectException
  746. * the supplied tag does not exist.
  747. * @throws IncorrectObjectTypeException
  748. * the supplied id is not a tag or an annotated tag.
  749. * @throws IOException
  750. * a pack file or loose object could not be read.
  751. */
  752. public RevTag parseTag(final AnyObjectId id) throws MissingObjectException,
  753. IncorrectObjectTypeException, IOException {
  754. RevObject c = parseAny(id);
  755. if (!(c instanceof RevTag))
  756. throw new IncorrectObjectTypeException(id.toObjectId(),
  757. Constants.TYPE_TAG);
  758. return (RevTag) c;
  759. }
  760. /**
  761. * Locate a reference to any object and immediately parse its headers.
  762. * <p>
  763. * This method only returns successfully if the object exists and was parsed
  764. * without error. Parsing an object can be expensive as the type must be
  765. * determined. For blobs this may mean the blob content was unpacked
  766. * unnecessarily, and thrown away.
  767. *
  768. * @param id
  769. * name of the object.
  770. * @return reference to the object. Never null.
  771. * @throws MissingObjectException
  772. * the supplied does not exist.
  773. * @throws IOException
  774. * a pack file or loose object could not be read.
  775. */
  776. public RevObject parseAny(final AnyObjectId id)
  777. throws MissingObjectException, IOException {
  778. RevObject r = objects.get(id);
  779. if (r == null)
  780. r = parseNew(id, reader.open(id));
  781. else
  782. parseHeaders(r);
  783. return r;
  784. }
  785. private RevObject parseNew(AnyObjectId id, ObjectLoader ldr)
  786. throws LargeObjectException, CorruptObjectException,
  787. MissingObjectException, IOException {
  788. RevObject r;
  789. int type = ldr.getType();
  790. switch (type) {
  791. case Constants.OBJ_COMMIT: {
  792. final RevCommit c = createCommit(id);
  793. c.parseCanonical(this, getCachedBytes(c, ldr));
  794. r = c;
  795. break;
  796. }
  797. case Constants.OBJ_TREE: {
  798. r = new RevTree(id);
  799. r.flags |= PARSED;
  800. break;
  801. }
  802. case Constants.OBJ_BLOB: {
  803. r = new RevBlob(id);
  804. r.flags |= PARSED;
  805. break;
  806. }
  807. case Constants.OBJ_TAG: {
  808. final RevTag t = new RevTag(id);
  809. t.parseCanonical(this, getCachedBytes(t, ldr));
  810. r = t;
  811. break;
  812. }
  813. default:
  814. throw new IllegalArgumentException(MessageFormat.format(
  815. JGitText.get().badObjectType, Integer.valueOf(type)));
  816. }
  817. objects.add(r);
  818. return r;
  819. }
  820. byte[] getCachedBytes(RevObject obj) throws LargeObjectException,
  821. MissingObjectException, IncorrectObjectTypeException, IOException {
  822. return getCachedBytes(obj, reader.open(obj, obj.getType()));
  823. }
  824. byte[] getCachedBytes(RevObject obj, ObjectLoader ldr)
  825. throws LargeObjectException, MissingObjectException, IOException {
  826. try {
  827. return ldr.getCachedBytes(5 * MB);
  828. } catch (LargeObjectException tooBig) {
  829. tooBig.setObjectId(obj);
  830. throw tooBig;
  831. }
  832. }
  833. /**
  834. * Asynchronous object parsing.
  835. *
  836. * @param <T>
  837. * any ObjectId type.
  838. * @param objectIds
  839. * objects to open from the object store. The supplied collection
  840. * must not be modified until the queue has finished.
  841. * @param reportMissing
  842. * if true missing objects are reported by calling failure with a
  843. * MissingObjectException. This may be more expensive for the
  844. * implementation to guarantee. If false the implementation may
  845. * choose to report MissingObjectException, or silently skip over
  846. * the object with no warning.
  847. * @return queue to read the objects from.
  848. */
  849. public <T extends ObjectId> AsyncRevObjectQueue parseAny(
  850. Iterable<T> objectIds, boolean reportMissing) {
  851. List<T> need = new ArrayList<T>();
  852. List<RevObject> have = new ArrayList<RevObject>();
  853. for (T id : objectIds) {
  854. RevObject r = objects.get(id);
  855. if (r != null && (r.flags & PARSED) != 0)
  856. have.add(r);
  857. else
  858. need.add(id);
  859. }
  860. final Iterator<RevObject> objItr = have.iterator();
  861. if (need.isEmpty()) {
  862. return new AsyncRevObjectQueue() {
  863. public RevObject next() {
  864. return objItr.hasNext() ? objItr.next() : null;
  865. }
  866. public boolean cancel(boolean mayInterruptIfRunning) {
  867. return true;
  868. }
  869. public void release() {
  870. // In-memory only, no action required.
  871. }
  872. };
  873. }
  874. final AsyncObjectLoaderQueue<T> lItr = reader.open(need, reportMissing);
  875. return new AsyncRevObjectQueue() {
  876. public RevObject next() throws MissingObjectException,
  877. IncorrectObjectTypeException, IOException {
  878. if (objItr.hasNext())
  879. return objItr.next();
  880. if (!lItr.next())
  881. return null;
  882. ObjectId id = lItr.getObjectId();
  883. ObjectLoader ldr = lItr.open();
  884. RevObject r = objects.get(id);
  885. if (r == null)
  886. r = parseNew(id, ldr);
  887. else if (r instanceof RevCommit) {
  888. byte[] raw = ldr.getCachedBytes();
  889. ((RevCommit) r).parseCanonical(RevWalk.this, raw);
  890. } else if (r instanceof RevTag) {
  891. byte[] raw = ldr.getCachedBytes();
  892. ((RevTag) r).parseCanonical(RevWalk.this, raw);
  893. } else
  894. r.flags |= PARSED;
  895. return r;
  896. }
  897. public boolean cancel(boolean mayInterruptIfRunning) {
  898. return lItr.cancel(mayInterruptIfRunning);
  899. }
  900. public void release() {
  901. lItr.release();
  902. }
  903. };
  904. }
  905. /**
  906. * Ensure the object's critical headers have been parsed.
  907. * <p>
  908. * This method only returns successfully if the object exists and was parsed
  909. * without error.
  910. *
  911. * @param obj
  912. * the object the caller needs to be parsed.
  913. * @throws MissingObjectException
  914. * the supplied does not exist.
  915. * @throws IOException
  916. * a pack file or loose object could not be read.
  917. */
  918. public void parseHeaders(final RevObject obj)
  919. throws MissingObjectException, IOException {
  920. if ((obj.flags & PARSED) == 0)
  921. obj.parseHeaders(this);
  922. }
  923. /**
  924. * Ensure the object's full body content is available.
  925. * <p>
  926. * This method only returns successfully if the object exists and was parsed
  927. * without error.
  928. *
  929. * @param obj
  930. * the object the caller needs to be parsed.
  931. * @throws MissingObjectException
  932. * the supplied does not exist.
  933. * @throws IOException
  934. * a pack file or loose object could not be read.
  935. */
  936. public void parseBody(final RevObject obj)
  937. throws MissingObjectException, IOException {
  938. obj.parseBody(this);
  939. }
  940. /**
  941. * Peel back annotated tags until a non-tag object is found.
  942. *
  943. * @param obj
  944. * the starting object.
  945. * @return If {@code obj} is not an annotated tag, {@code obj}. Otherwise
  946. * the first non-tag object that {@code obj} references. The
  947. * returned object's headers have been parsed.
  948. * @throws MissingObjectException
  949. * a referenced object cannot be found.
  950. * @throws IOException
  951. * a pack file or loose object could not be read.
  952. */
  953. public RevObject peel(RevObject obj) throws MissingObjectException,
  954. IOException {
  955. while (obj instanceof RevTag) {
  956. parseHeaders(obj);
  957. obj = ((RevTag) obj).getObject();
  958. }
  959. parseHeaders(obj);
  960. return obj;
  961. }
  962. /**
  963. * Create a new flag for application use during walking.
  964. * <p>
  965. * Applications are only assured to be able to create 24 unique flags on any
  966. * given revision walker instance. Any flags beyond 24 are offered only if
  967. * the implementation has extra free space within its internal storage.
  968. *
  969. * @param name
  970. * description of the flag, primarily useful for debugging.
  971. * @return newly constructed flag instance.
  972. * @throws IllegalArgumentException
  973. * too many flags have been reserved on this revision walker.
  974. */
  975. public RevFlag newFlag(final String name) {
  976. final int m = allocFlag();
  977. return new RevFlag(this, name, m);
  978. }
  979. int allocFlag() {
  980. if (freeFlags == 0)
  981. throw new IllegalArgumentException(MessageFormat.format(
  982. JGitText.get().flagsAlreadyCreated,
  983. Integer.valueOf(32 - RESERVED_FLAGS)));
  984. final int m = Integer.lowestOneBit(freeFlags);
  985. freeFlags &= ~m;
  986. return m;
  987. }
  988. /**
  989. * Automatically carry a flag from a child commit to its parents.
  990. * <p>
  991. * A carried flag is copied from the child commit onto its parents when the
  992. * child commit is popped from the lowest level of walk's internal graph.
  993. *
  994. * @param flag
  995. * the flag to carry onto parents, if set on a descendant.
  996. */
  997. public void carry(final RevFlag flag) {
  998. if ((freeFlags & flag.mask) != 0)
  999. throw new IllegalArgumentException(MessageFormat.format(JGitText.get().flagIsDisposed, flag.name));
  1000. if (flag.walker != this)
  1001. throw new IllegalArgumentException(MessageFormat.format(JGitText.get().flagNotFromThis, flag.name));
  1002. carryFlags |= flag.mask;
  1003. }
  1004. /**
  1005. * Automatically carry flags from a child commit to its parents.
  1006. * <p>
  1007. * A carried flag is copied from the child commit onto its parents when the
  1008. * child commit is popped from the lowest level of walk's internal graph.
  1009. *
  1010. * @param set
  1011. * the flags to carry onto parents, if set on a descendant.
  1012. */
  1013. public void carry(final Collection<RevFlag> set) {
  1014. for (final RevFlag flag : set)
  1015. carry(flag);
  1016. }
  1017. /**
  1018. * Preserve a RevFlag during all {@code reset} methods.
  1019. * <p>
  1020. * Calling {@code retainOnReset(flag)} avoids needing to pass the flag
  1021. * during each {@code resetRetain()} invocation on this instance.
  1022. * <p>
  1023. * Clearing flags marked retainOnReset requires disposing of the flag with
  1024. * {@code #disposeFlag(RevFlag)} or disposing of the entire RevWalk by
  1025. * {@code #dispose()}.
  1026. *
  1027. * @param flag
  1028. * the flag to retain during all resets.
  1029. * @since 3.6
  1030. */
  1031. public final void retainOnReset(RevFlag flag) {
  1032. if ((freeFlags & flag.mask) != 0)
  1033. throw new IllegalArgumentException(MessageFormat.format(JGitText.get().flagIsDisposed, flag.name));
  1034. if (flag.walker != this)
  1035. throw new IllegalArgumentException(MessageFormat.format(JGitText.get().flagNotFromThis, flag.name));
  1036. retainOnReset |= flag.mask;
  1037. }
  1038. /**
  1039. * Preserve a set of RevFlags during all {@code reset} methods.
  1040. * <p>
  1041. * Calling {@code retainOnReset(set)} avoids needing to pass the flags
  1042. * during each {@code resetRetain()} invocation on this instance.
  1043. * <p>
  1044. * Clearing flags marked retainOnReset requires disposing of the flag with
  1045. * {@code #disposeFlag(RevFlag)} or disposing of the entire RevWalk by
  1046. * {@code #dispose()}.
  1047. *
  1048. * @param flags
  1049. * the flags to retain during all resets.
  1050. * @since 3.6
  1051. */
  1052. public final void retainOnReset(Collection<RevFlag> flags) {
  1053. for (RevFlag f : flags)
  1054. retainOnReset(f);
  1055. }
  1056. /**
  1057. * Allow a flag to be recycled for a different use.
  1058. * <p>
  1059. * Recycled flags always come back as a different Java object instance when
  1060. * assigned again by {@link #newFlag(String)}.
  1061. * <p>
  1062. * If the flag was previously being carried, the carrying request is
  1063. * removed. Disposing of a carried flag while a traversal is in progress has
  1064. * an undefined behavior.
  1065. *
  1066. * @param flag
  1067. * the to recycle.
  1068. */
  1069. public void disposeFlag(final RevFlag flag) {
  1070. freeFlag(flag.mask);
  1071. }
  1072. void freeFlag(final int mask) {
  1073. retainOnReset &= ~mask;
  1074. if (isNotStarted()) {
  1075. freeFlags |= mask;
  1076. carryFlags &= ~mask;
  1077. } else {
  1078. delayFreeFlags |= mask;
  1079. }
  1080. }
  1081. private void finishDelayedFreeFlags() {
  1082. if (delayFreeFlags != 0) {
  1083. freeFlags |= delayFreeFlags;
  1084. carryFlags &= ~delayFreeFlags;
  1085. delayFreeFlags = 0;
  1086. }
  1087. }
  1088. /**
  1089. * Resets internal state and allows this instance to be used again.
  1090. * <p>
  1091. * Unlike {@link #dispose()} previously acquired RevObject (and RevCommit)
  1092. * instances are not invalidated. RevFlag instances are not invalidated, but
  1093. * are removed from all RevObjects.
  1094. */
  1095. public final void reset() {
  1096. reset(0);
  1097. }
  1098. /**
  1099. * Resets internal state and allows this instance to be used again.
  1100. * <p>
  1101. * Unlike {@link #dispose()} previously acquired RevObject (and RevCommit)
  1102. * instances are not invalidated. RevFlag instances are not invalidated, but
  1103. * are removed from all RevObjects.
  1104. *
  1105. * @param retainFlags
  1106. * application flags that should <b>not</b> be cleared from
  1107. * existing commit objects.
  1108. */
  1109. public final void resetRetain(final RevFlagSet retainFlags) {
  1110. reset(retainFlags.mask);
  1111. }
  1112. /**
  1113. * Resets internal state and allows this instance to be used again.
  1114. * <p>
  1115. * Unlike {@link #dispose()} previously acquired RevObject (and RevCommit)
  1116. * instances are not invalidated. RevFlag instances are not invalidated, but
  1117. * are removed from all RevObjects.
  1118. * <p>
  1119. * See {@link #retainOnReset(RevFlag)} for an alternative that does not
  1120. * require passing the flags during each reset.
  1121. *
  1122. * @param retainFlags
  1123. * application flags that should <b>not</b> be cleared from
  1124. * existing commit objects.
  1125. */
  1126. public final void resetRetain(final RevFlag... retainFlags) {
  1127. int mask = 0;
  1128. for (final RevFlag flag : retainFlags)
  1129. mask |= flag.mask;
  1130. reset(mask);
  1131. }
  1132. /**
  1133. * Resets internal state and allows this instance to be used again.
  1134. * <p>
  1135. * Unlike {@link #dispose()} previously acquired RevObject (and RevCommit)
  1136. * instances are not invalidated. RevFlag instances are not invalidated, but
  1137. * are removed from all RevObjects.
  1138. *
  1139. * @param retainFlags
  1140. * application flags that should <b>not</b> be cleared from
  1141. * existing commit objects.
  1142. */
  1143. protected void reset(int retainFlags) {
  1144. finishDelayedFreeFlags();
  1145. retainFlags |= PARSED | retainOnReset;
  1146. final int clearFlags = ~retainFlags;
  1147. final FIFORevQueue q = new FIFORevQueue();
  1148. for (final RevCommit c : roots) {
  1149. if ((c.flags & clearFlags) == 0)
  1150. continue;
  1151. c.flags &= retainFlags;
  1152. c.reset();
  1153. q.add(c);
  1154. }
  1155. for (;;) {
  1156. final RevCommit c = q.next();
  1157. if (c == null)
  1158. break;
  1159. if (c.parents == null)
  1160. continue;
  1161. for (final RevCommit p : c.parents) {
  1162. if ((p.flags & clearFlags) == 0)
  1163. continue;
  1164. p.flags &= retainFlags;
  1165. p.reset();
  1166. q.add(p);
  1167. }
  1168. }
  1169. roots.clear();
  1170. queue = new DateRevQueue();
  1171. pending = new StartGenerator(this);
  1172. }
  1173. /**
  1174. * Dispose all internal state and invalidate all RevObject instances.
  1175. * <p>
  1176. * All RevObject (and thus RevCommit, etc.) instances previously acquired
  1177. * from this RevWalk are invalidated by a dispose call. Applications must
  1178. * not retain or use RevObject instances obtained prior to the dispose call.
  1179. * All RevFlag instances are also invalidated, and must not be reused.
  1180. */
  1181. public void dispose() {
  1182. reader.release();
  1183. freeFlags = APP_FLAGS;
  1184. delayFreeFlags = 0;
  1185. retainOnReset = 0;
  1186. carryFlags = UNINTERESTING;
  1187. objects.clear();
  1188. reader.release();
  1189. roots.clear();
  1190. queue = new DateRevQueue();
  1191. pending = new StartGenerator(this);
  1192. shallowCommitsInitialized = false;
  1193. }
  1194. /**
  1195. * Returns an Iterator over the commits of this walker.
  1196. * <p>
  1197. * The returned iterator is only useful for one walk. If this RevWalk gets
  1198. * reset a new iterator must be obtained to walk over the new results.
  1199. * <p>
  1200. * Applications must not use both the Iterator and the {@link #next()} API
  1201. * at the same time. Pick one API and use that for the entire walk.
  1202. * <p>
  1203. * If a checked exception is thrown during the walk (see {@link #next()})
  1204. * it is rethrown from the Iterator as a {@link RevWalkException}.
  1205. *
  1206. * @return an iterator over this walker's commits.
  1207. * @see RevWalkException
  1208. */
  1209. public Iterator<RevCommit> iterator() {
  1210. final RevCommit first;
  1211. try {
  1212. first = RevWalk.this.next();
  1213. } catch (MissingObjectException e) {
  1214. throw new RevWalkException(e);
  1215. } catch (IncorrectObjectTypeException e) {
  1216. throw new RevWalkException(e);
  1217. } catch (IOException e) {
  1218. throw new RevWalkException(e);
  1219. }
  1220. return new Iterator<RevCommit>() {
  1221. RevCommit next = first;
  1222. public boolean hasNext() {
  1223. return next != null;
  1224. }
  1225. public RevCommit next() {
  1226. try {
  1227. final RevCommit r = next;
  1228. next = RevWalk.this.next();
  1229. return r;
  1230. } catch (MissingObjectException e) {
  1231. throw new RevWalkException(e);
  1232. } catch (IncorrectObjectTypeException e) {
  1233. throw new RevWalkException(e);
  1234. } catch (IOException e) {
  1235. throw new RevWalkException(e);
  1236. }
  1237. }
  1238. public void remove() {
  1239. throw new UnsupportedOperationException();
  1240. }
  1241. };
  1242. }
  1243. /** Throws an exception if we have started producing output. */
  1244. protected void assertNotStarted() {
  1245. if (isNotStarted())
  1246. return;
  1247. throw new IllegalStateException(JGitText.get().outputHasAlreadyBeenStarted);
  1248. }
  1249. private boolean isNotStarted() {
  1250. return pending instanceof StartGenerator;
  1251. }
  1252. /**
  1253. * Create and return an {@link ObjectWalk} using the same objects.
  1254. * <p>
  1255. * Prior to using this method, the caller must reset this RevWalk to clean
  1256. * any flags that were used during the last traversal.
  1257. * <p>
  1258. * The returned ObjectWalk uses the same ObjectReader, internal object pool,
  1259. * and free RevFlags. Once the ObjectWalk is created, this RevWalk should
  1260. * not be used anymore.
  1261. *
  1262. * @return a new walk, using the exact same object pool.
  1263. */
  1264. public ObjectWalk toObjectWalkWithSameObjects() {
  1265. ObjectWalk ow = new ObjectWalk(reader);
  1266. RevWalk rw = ow;
  1267. rw.objects = objects;
  1268. rw.freeFlags = freeFlags;
  1269. return ow;
  1270. }
  1271. /**
  1272. * Construct a new unparsed commit for the given object.
  1273. *
  1274. * @param id
  1275. * the object this walker requires a commit reference for.
  1276. * @return a new unparsed reference for the object.
  1277. */
  1278. protected RevCommit createCommit(final AnyObjectId id) {
  1279. return new RevCommit(id);
  1280. }
  1281. void carryFlagsImpl(final RevCommit c) {
  1282. final int carry = c.flags & carryFlags;
  1283. if (carry != 0)
  1284. RevCommit.carryFlags(c, carry);
  1285. }
  1286. /**
  1287. * Assume additional commits are shallow (have no parents).
  1288. *
  1289. * @param ids
  1290. * commits that should be treated as shallow commits, in addition
  1291. * to any commits already known to be shallow by the repository.
  1292. * @since 3.3
  1293. */
  1294. public void assumeShallow(Collection<? extends ObjectId> ids) {
  1295. for (ObjectId id : ids)
  1296. lookupCommit(id).parents = RevCommit.NO_PARENTS;
  1297. }
  1298. void initializeShallowCommits() throws IOException {
  1299. if (shallowCommitsInitialized)
  1300. throw new IllegalStateException(
  1301. JGitText.get().shallowCommitsAlreadyInitialized);
  1302. shallowCommitsInitialized = true;
  1303. if (reader == null)
  1304. return;
  1305. for (ObjectId id : reader.getShallowCommits())
  1306. lookupCommit(id).parents = RevCommit.NO_PARENTS;
  1307. }
  1308. }