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.

RevWalk.java 45KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428
  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>, AutoCloseable {
  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. Use {@link #close()} instead.
  210. */
  211. @Deprecated
  212. public void release() {
  213. close();
  214. }
  215. /**
  216. * Release any resources used by this walker's reader.
  217. * <p>
  218. * A walker that has been released can be used again, but may need to be
  219. * released after the subsequent usage.
  220. *
  221. * @since 4.0
  222. */
  223. @Override
  224. public void close() {
  225. reader.close();
  226. }
  227. /**
  228. * Mark a commit to start graph traversal from.
  229. * <p>
  230. * Callers are encouraged to use {@link #parseCommit(AnyObjectId)} to obtain
  231. * the commit reference, rather than {@link #lookupCommit(AnyObjectId)}, as
  232. * this method requires the commit to be parsed before it can be added as a
  233. * root for the traversal.
  234. * <p>
  235. * The method will automatically parse an unparsed commit, but error
  236. * handling may be more difficult for the application to explain why a
  237. * RevCommit is not actually a commit. The object pool of this walker would
  238. * also be 'poisoned' by the non-commit RevCommit.
  239. *
  240. * @param c
  241. * the commit to start traversing from. The commit passed must be
  242. * from this same revision walker.
  243. * @throws MissingObjectException
  244. * the commit supplied is not available from the object
  245. * database. This usually indicates the supplied commit is
  246. * invalid, but the reference was constructed during an earlier
  247. * invocation to {@link #lookupCommit(AnyObjectId)}.
  248. * @throws IncorrectObjectTypeException
  249. * the object was not parsed yet and it was discovered during
  250. * parsing that it is not actually a commit. This usually
  251. * indicates the caller supplied a non-commit SHA-1 to
  252. * {@link #lookupCommit(AnyObjectId)}.
  253. * @throws IOException
  254. * a pack file or loose object could not be read.
  255. */
  256. public void markStart(final RevCommit c) throws MissingObjectException,
  257. IncorrectObjectTypeException, IOException {
  258. if ((c.flags & SEEN) != 0)
  259. return;
  260. if ((c.flags & PARSED) == 0)
  261. c.parseHeaders(this);
  262. c.flags |= SEEN;
  263. roots.add(c);
  264. queue.add(c);
  265. }
  266. /**
  267. * Mark commits to start graph traversal from.
  268. *
  269. * @param list
  270. * commits to start traversing from. The commits passed must be
  271. * from this same revision walker.
  272. * @throws MissingObjectException
  273. * one of the commits supplied is not available from the object
  274. * database. This usually indicates the supplied commit is
  275. * invalid, but the reference was constructed during an earlier
  276. * invocation to {@link #lookupCommit(AnyObjectId)}.
  277. * @throws IncorrectObjectTypeException
  278. * the object was not parsed yet and it was discovered during
  279. * parsing that it is not actually a commit. This usually
  280. * indicates the caller supplied a non-commit SHA-1 to
  281. * {@link #lookupCommit(AnyObjectId)}.
  282. * @throws IOException
  283. * a pack file or loose object could not be read.
  284. */
  285. public void markStart(final Collection<RevCommit> list)
  286. throws MissingObjectException, IncorrectObjectTypeException,
  287. IOException {
  288. for (final RevCommit c : list)
  289. markStart(c);
  290. }
  291. /**
  292. * Mark a commit to not produce in the output.
  293. * <p>
  294. * Uninteresting commits denote not just themselves but also their entire
  295. * ancestry chain, back until the merge base of an uninteresting commit and
  296. * an otherwise interesting commit.
  297. * <p>
  298. * Callers are encouraged to use {@link #parseCommit(AnyObjectId)} to obtain
  299. * the commit reference, rather than {@link #lookupCommit(AnyObjectId)}, as
  300. * this method requires the commit to be parsed before it can be added as a
  301. * root for the traversal.
  302. * <p>
  303. * The method will automatically parse an unparsed commit, but error
  304. * handling may be more difficult for the application to explain why a
  305. * RevCommit is not actually a commit. The object pool of this walker would
  306. * also be 'poisoned' by the non-commit RevCommit.
  307. *
  308. * @param c
  309. * the commit to start traversing from. The commit passed must be
  310. * from this same revision walker.
  311. * @throws MissingObjectException
  312. * the commit supplied is not available from the object
  313. * database. This usually indicates the supplied commit is
  314. * invalid, but the reference was constructed during an earlier
  315. * invocation to {@link #lookupCommit(AnyObjectId)}.
  316. * @throws IncorrectObjectTypeException
  317. * the object was not parsed yet and it was discovered during
  318. * parsing that it is not actually a commit. This usually
  319. * indicates the caller supplied a non-commit SHA-1 to
  320. * {@link #lookupCommit(AnyObjectId)}.
  321. * @throws IOException
  322. * a pack file or loose object could not be read.
  323. */
  324. public void markUninteresting(final RevCommit c)
  325. throws MissingObjectException, IncorrectObjectTypeException,
  326. IOException {
  327. c.flags |= UNINTERESTING;
  328. carryFlagsImpl(c);
  329. markStart(c);
  330. }
  331. /**
  332. * Determine if a commit is reachable from another commit.
  333. * <p>
  334. * A commit <code>base</code> is an ancestor of <code>tip</code> if we
  335. * can find a path of commits that leads from <code>tip</code> and ends at
  336. * <code>base</code>.
  337. * <p>
  338. * This utility function resets the walker, inserts the two supplied
  339. * commits, and then executes a walk until an answer can be obtained.
  340. * Currently allocated RevFlags that have been added to RevCommit instances
  341. * will be retained through the reset.
  342. *
  343. * @param base
  344. * commit the caller thinks is reachable from <code>tip</code>.
  345. * @param tip
  346. * commit to start iteration from, and which is most likely a
  347. * descendant (child) of <code>base</code>.
  348. * @return true if there is a path directly from <code>tip</code> to
  349. * <code>base</code> (and thus <code>base</code> is fully merged
  350. * into <code>tip</code>); false otherwise.
  351. * @throws MissingObjectException
  352. * one or or more of the next commit's parents are not available
  353. * from the object database, but were thought to be candidates
  354. * for traversal. This usually indicates a broken link.
  355. * @throws IncorrectObjectTypeException
  356. * one or or more of the next commit's parents are not actually
  357. * commit objects.
  358. * @throws IOException
  359. * a pack file or loose object could not be read.
  360. */
  361. public boolean isMergedInto(final RevCommit base, final RevCommit tip)
  362. throws MissingObjectException, IncorrectObjectTypeException,
  363. IOException {
  364. final RevFilter oldRF = filter;
  365. final TreeFilter oldTF = treeFilter;
  366. try {
  367. finishDelayedFreeFlags();
  368. reset(~freeFlags & APP_FLAGS);
  369. filter = RevFilter.MERGE_BASE;
  370. treeFilter = TreeFilter.ALL;
  371. markStart(tip);
  372. markStart(base);
  373. RevCommit mergeBase;
  374. while ((mergeBase = next()) != null)
  375. if (mergeBase == base)
  376. return true;
  377. return false;
  378. } finally {
  379. filter = oldRF;
  380. treeFilter = oldTF;
  381. }
  382. }
  383. /**
  384. * Pop the next most recent commit.
  385. *
  386. * @return next most recent commit; null if traversal is over.
  387. * @throws MissingObjectException
  388. * one or or more of the next commit's parents are not available
  389. * from the object database, but were thought to be candidates
  390. * for traversal. This usually indicates a broken link.
  391. * @throws IncorrectObjectTypeException
  392. * one or or more of the next commit's parents are not actually
  393. * commit objects.
  394. * @throws IOException
  395. * a pack file or loose object could not be read.
  396. */
  397. public RevCommit next() throws MissingObjectException,
  398. IncorrectObjectTypeException, IOException {
  399. return pending.next();
  400. }
  401. /**
  402. * Obtain the sort types applied to the commits returned.
  403. *
  404. * @return the sorting strategies employed. At least one strategy is always
  405. * used, but that strategy may be {@link RevSort#NONE}.
  406. */
  407. public EnumSet<RevSort> getRevSort() {
  408. return sorting.clone();
  409. }
  410. /**
  411. * Check whether the provided sorting strategy is enabled.
  412. *
  413. * @param sort
  414. * a sorting strategy to look for.
  415. * @return true if this strategy is enabled, false otherwise
  416. */
  417. public boolean hasRevSort(RevSort sort) {
  418. return sorting.contains(sort);
  419. }
  420. /**
  421. * Select a single sorting strategy for the returned commits.
  422. * <p>
  423. * Disables all sorting strategies, then enables only the single strategy
  424. * supplied by the caller.
  425. *
  426. * @param s
  427. * a sorting strategy to enable.
  428. */
  429. public void sort(final RevSort s) {
  430. assertNotStarted();
  431. sorting.clear();
  432. sorting.add(s);
  433. }
  434. /**
  435. * Add or remove a sorting strategy for the returned commits.
  436. * <p>
  437. * Multiple strategies can be applied at once, in which case some strategies
  438. * may take precedence over others. As an example, {@link RevSort#TOPO} must
  439. * take precedence over {@link RevSort#COMMIT_TIME_DESC}, otherwise it
  440. * cannot enforce its ordering.
  441. *
  442. * @param s
  443. * a sorting strategy to enable or disable.
  444. * @param use
  445. * true if this strategy should be used, false if it should be
  446. * removed.
  447. */
  448. public void sort(final RevSort s, final boolean use) {
  449. assertNotStarted();
  450. if (use)
  451. sorting.add(s);
  452. else
  453. sorting.remove(s);
  454. if (sorting.size() > 1)
  455. sorting.remove(RevSort.NONE);
  456. else if (sorting.size() == 0)
  457. sorting.add(RevSort.NONE);
  458. }
  459. /**
  460. * Get the currently configured commit filter.
  461. *
  462. * @return the current filter. Never null as a filter is always needed.
  463. */
  464. public RevFilter getRevFilter() {
  465. return filter;
  466. }
  467. /**
  468. * Set the commit filter for this walker.
  469. * <p>
  470. * Multiple filters may be combined by constructing an arbitrary tree of
  471. * <code>AndRevFilter</code> or <code>OrRevFilter</code> instances to
  472. * describe the boolean expression required by the application. Custom
  473. * filter implementations may also be constructed by applications.
  474. * <p>
  475. * Note that filters are not thread-safe and may not be shared by concurrent
  476. * RevWalk instances. Every RevWalk must be supplied its own unique filter,
  477. * unless the filter implementation specifically states it is (and always
  478. * will be) thread-safe. Callers may use {@link RevFilter#clone()} to create
  479. * a unique filter tree for this RevWalk instance.
  480. *
  481. * @param newFilter
  482. * the new filter. If null the special {@link RevFilter#ALL}
  483. * filter will be used instead, as it matches every commit.
  484. * @see org.eclipse.jgit.revwalk.filter.AndRevFilter
  485. * @see org.eclipse.jgit.revwalk.filter.OrRevFilter
  486. */
  487. public void setRevFilter(final RevFilter newFilter) {
  488. assertNotStarted();
  489. filter = newFilter != null ? newFilter : RevFilter.ALL;
  490. }
  491. /**
  492. * Get the tree filter used to simplify commits by modified paths.
  493. *
  494. * @return the current filter. Never null as a filter is always needed. If
  495. * no filter is being applied {@link TreeFilter#ALL} is returned.
  496. */
  497. public TreeFilter getTreeFilter() {
  498. return treeFilter;
  499. }
  500. /**
  501. * Set the tree filter used to simplify commits by modified paths.
  502. * <p>
  503. * If null or {@link TreeFilter#ALL} the path limiter is removed. Commits
  504. * will not be simplified.
  505. * <p>
  506. * If non-null and not {@link TreeFilter#ALL} then the tree filter will be
  507. * installed. Commits will have their ancestry simplified to hide commits that
  508. * do not contain tree entries matched by the filter, unless
  509. * {@code setRewriteParents(false)} is called.
  510. * <p>
  511. * Usually callers should be inserting a filter graph including
  512. * {@link TreeFilter#ANY_DIFF} along with one or more
  513. * {@link org.eclipse.jgit.treewalk.filter.PathFilter} instances.
  514. *
  515. * @param newFilter
  516. * new filter. If null the special {@link TreeFilter#ALL} filter
  517. * will be used instead, as it matches everything.
  518. * @see org.eclipse.jgit.treewalk.filter.PathFilter
  519. */
  520. public void setTreeFilter(final TreeFilter newFilter) {
  521. assertNotStarted();
  522. treeFilter = newFilter != null ? newFilter : TreeFilter.ALL;
  523. }
  524. /**
  525. * Set whether to rewrite parent pointers when filtering by modified paths.
  526. * <p>
  527. * By default, when {@link #setTreeFilter(TreeFilter)} is called with non-
  528. * null and non-{@link TreeFilter#ALL} filter, commits will have their
  529. * ancestry simplified and parents rewritten to hide commits that do not match
  530. * the filter.
  531. * <p>
  532. * This behavior can be bypassed by passing false to this method.
  533. *
  534. * @param rewrite
  535. * whether to rewrite parents; defaults to true.
  536. * @since 3.4
  537. */
  538. public void setRewriteParents(boolean rewrite) {
  539. rewriteParents = rewrite;
  540. }
  541. boolean getRewriteParents() {
  542. return rewriteParents;
  543. }
  544. /**
  545. * Should the body of a commit or tag be retained after parsing its headers?
  546. * <p>
  547. * Usually the body is always retained, but some application code might not
  548. * care and would prefer to discard the body of a commit as early as
  549. * possible, to reduce memory usage.
  550. *
  551. * @return true if the body should be retained; false it is discarded.
  552. */
  553. public boolean isRetainBody() {
  554. return retainBody;
  555. }
  556. /**
  557. * Set whether or not the body of a commit or tag is retained.
  558. * <p>
  559. * If a body of a commit or tag is not retained, the application must
  560. * call {@link #parseBody(RevObject)} before the body can be safely
  561. * accessed through the type specific access methods.
  562. *
  563. * @param retain true to retain bodies; false to discard them early.
  564. */
  565. public void setRetainBody(final boolean retain) {
  566. retainBody = retain;
  567. }
  568. /**
  569. * Locate a reference to a blob without loading it.
  570. * <p>
  571. * The blob may or may not exist in the repository. It is impossible to tell
  572. * from this method's return value.
  573. *
  574. * @param id
  575. * name of the blob object.
  576. * @return reference to the blob object. Never null.
  577. */
  578. public RevBlob lookupBlob(final AnyObjectId id) {
  579. RevBlob c = (RevBlob) objects.get(id);
  580. if (c == null) {
  581. c = new RevBlob(id);
  582. objects.add(c);
  583. }
  584. return c;
  585. }
  586. /**
  587. * Locate a reference to a tree without loading it.
  588. * <p>
  589. * The tree may or may not exist in the repository. It is impossible to tell
  590. * from this method's return value.
  591. *
  592. * @param id
  593. * name of the tree object.
  594. * @return reference to the tree object. Never null.
  595. */
  596. public RevTree lookupTree(final AnyObjectId id) {
  597. RevTree c = (RevTree) objects.get(id);
  598. if (c == null) {
  599. c = new RevTree(id);
  600. objects.add(c);
  601. }
  602. return c;
  603. }
  604. /**
  605. * Locate a reference to a commit without loading it.
  606. * <p>
  607. * The commit may or may not exist in the repository. It is impossible to
  608. * tell from this method's return value.
  609. * <p>
  610. * See {@link #parseHeaders(RevObject)} and {@link #parseBody(RevObject)}
  611. * for loading contents.
  612. *
  613. * @param id
  614. * name of the commit object.
  615. * @return reference to the commit object. Never null.
  616. */
  617. public RevCommit lookupCommit(final AnyObjectId id) {
  618. RevCommit c = (RevCommit) objects.get(id);
  619. if (c == null) {
  620. c = createCommit(id);
  621. objects.add(c);
  622. }
  623. return c;
  624. }
  625. /**
  626. * Locate a reference to a tag without loading it.
  627. * <p>
  628. * The tag may or may not exist in the repository. It is impossible to tell
  629. * from this method's return value.
  630. *
  631. * @param id
  632. * name of the tag object.
  633. * @return reference to the tag object. Never null.
  634. */
  635. public RevTag lookupTag(final AnyObjectId id) {
  636. RevTag c = (RevTag) objects.get(id);
  637. if (c == null) {
  638. c = new RevTag(id);
  639. objects.add(c);
  640. }
  641. return c;
  642. }
  643. /**
  644. * Locate a reference to any object without loading it.
  645. * <p>
  646. * The object may or may not exist in the repository. It is impossible to
  647. * tell from this method's return value.
  648. *
  649. * @param id
  650. * name of the object.
  651. * @param type
  652. * type of the object. Must be a valid Git object type.
  653. * @return reference to the object. Never null.
  654. */
  655. public RevObject lookupAny(final AnyObjectId id, final int type) {
  656. RevObject r = objects.get(id);
  657. if (r == null) {
  658. switch (type) {
  659. case Constants.OBJ_COMMIT:
  660. r = createCommit(id);
  661. break;
  662. case Constants.OBJ_TREE:
  663. r = new RevTree(id);
  664. break;
  665. case Constants.OBJ_BLOB:
  666. r = new RevBlob(id);
  667. break;
  668. case Constants.OBJ_TAG:
  669. r = new RevTag(id);
  670. break;
  671. default:
  672. throw new IllegalArgumentException(MessageFormat.format(
  673. JGitText.get().invalidGitType, Integer.valueOf(type)));
  674. }
  675. objects.add(r);
  676. }
  677. return r;
  678. }
  679. /**
  680. * Locate an object that was previously allocated in this walk.
  681. *
  682. * @param id
  683. * name of the object.
  684. * @return reference to the object if it has been previously located;
  685. * otherwise null.
  686. */
  687. public RevObject lookupOrNull(AnyObjectId id) {
  688. return objects.get(id);
  689. }
  690. /**
  691. * Locate a reference to a commit and immediately parse its content.
  692. * <p>
  693. * Unlike {@link #lookupCommit(AnyObjectId)} this method only returns
  694. * successfully if the commit object exists, is verified to be a commit, and
  695. * was parsed without error.
  696. *
  697. * @param id
  698. * name of the commit object.
  699. * @return reference to the commit object. Never null.
  700. * @throws MissingObjectException
  701. * the supplied commit does not exist.
  702. * @throws IncorrectObjectTypeException
  703. * the supplied id is not a commit or an annotated tag.
  704. * @throws IOException
  705. * a pack file or loose object could not be read.
  706. */
  707. public RevCommit parseCommit(final AnyObjectId id)
  708. throws MissingObjectException, IncorrectObjectTypeException,
  709. IOException {
  710. RevObject c = peel(parseAny(id));
  711. if (!(c instanceof RevCommit))
  712. throw new IncorrectObjectTypeException(id.toObjectId(),
  713. Constants.TYPE_COMMIT);
  714. return (RevCommit) c;
  715. }
  716. /**
  717. * Locate a reference to a tree.
  718. * <p>
  719. * This method only returns successfully if the tree object exists, is
  720. * verified to be a tree.
  721. *
  722. * @param id
  723. * name of the tree object, or a commit or annotated tag that may
  724. * reference a tree.
  725. * @return reference to the tree object. Never null.
  726. * @throws MissingObjectException
  727. * the supplied tree does not exist.
  728. * @throws IncorrectObjectTypeException
  729. * the supplied id is not a tree, a commit or an annotated tag.
  730. * @throws IOException
  731. * a pack file or loose object could not be read.
  732. */
  733. public RevTree parseTree(final AnyObjectId id)
  734. throws MissingObjectException, IncorrectObjectTypeException,
  735. IOException {
  736. RevObject c = peel(parseAny(id));
  737. final RevTree t;
  738. if (c instanceof RevCommit)
  739. t = ((RevCommit) c).getTree();
  740. else if (!(c instanceof RevTree))
  741. throw new IncorrectObjectTypeException(id.toObjectId(),
  742. Constants.TYPE_TREE);
  743. else
  744. t = (RevTree) c;
  745. parseHeaders(t);
  746. return t;
  747. }
  748. /**
  749. * Locate a reference to an annotated tag and immediately parse its content.
  750. * <p>
  751. * Unlike {@link #lookupTag(AnyObjectId)} this method only returns
  752. * successfully if the tag object exists, is verified to be a tag, and was
  753. * parsed without error.
  754. *
  755. * @param id
  756. * name of the tag object.
  757. * @return reference to the tag object. Never null.
  758. * @throws MissingObjectException
  759. * the supplied tag does not exist.
  760. * @throws IncorrectObjectTypeException
  761. * the supplied id is not a tag or an annotated tag.
  762. * @throws IOException
  763. * a pack file or loose object could not be read.
  764. */
  765. public RevTag parseTag(final AnyObjectId id) throws MissingObjectException,
  766. IncorrectObjectTypeException, IOException {
  767. RevObject c = parseAny(id);
  768. if (!(c instanceof RevTag))
  769. throw new IncorrectObjectTypeException(id.toObjectId(),
  770. Constants.TYPE_TAG);
  771. return (RevTag) c;
  772. }
  773. /**
  774. * Locate a reference to any object and immediately parse its headers.
  775. * <p>
  776. * This method only returns successfully if the object exists and was parsed
  777. * without error. Parsing an object can be expensive as the type must be
  778. * determined. For blobs this may mean the blob content was unpacked
  779. * unnecessarily, and thrown away.
  780. *
  781. * @param id
  782. * name of the object.
  783. * @return reference to the object. Never null.
  784. * @throws MissingObjectException
  785. * the supplied does not exist.
  786. * @throws IOException
  787. * a pack file or loose object could not be read.
  788. */
  789. public RevObject parseAny(final AnyObjectId id)
  790. throws MissingObjectException, IOException {
  791. RevObject r = objects.get(id);
  792. if (r == null)
  793. r = parseNew(id, reader.open(id));
  794. else
  795. parseHeaders(r);
  796. return r;
  797. }
  798. private RevObject parseNew(AnyObjectId id, ObjectLoader ldr)
  799. throws LargeObjectException, CorruptObjectException,
  800. MissingObjectException, IOException {
  801. RevObject r;
  802. int type = ldr.getType();
  803. switch (type) {
  804. case Constants.OBJ_COMMIT: {
  805. final RevCommit c = createCommit(id);
  806. c.parseCanonical(this, getCachedBytes(c, ldr));
  807. r = c;
  808. break;
  809. }
  810. case Constants.OBJ_TREE: {
  811. r = new RevTree(id);
  812. r.flags |= PARSED;
  813. break;
  814. }
  815. case Constants.OBJ_BLOB: {
  816. r = new RevBlob(id);
  817. r.flags |= PARSED;
  818. break;
  819. }
  820. case Constants.OBJ_TAG: {
  821. final RevTag t = new RevTag(id);
  822. t.parseCanonical(this, getCachedBytes(t, ldr));
  823. r = t;
  824. break;
  825. }
  826. default:
  827. throw new IllegalArgumentException(MessageFormat.format(
  828. JGitText.get().badObjectType, Integer.valueOf(type)));
  829. }
  830. objects.add(r);
  831. return r;
  832. }
  833. byte[] getCachedBytes(RevObject obj) throws LargeObjectException,
  834. MissingObjectException, IncorrectObjectTypeException, IOException {
  835. return getCachedBytes(obj, reader.open(obj, obj.getType()));
  836. }
  837. byte[] getCachedBytes(RevObject obj, ObjectLoader ldr)
  838. throws LargeObjectException, MissingObjectException, IOException {
  839. try {
  840. return ldr.getCachedBytes(5 * MB);
  841. } catch (LargeObjectException tooBig) {
  842. tooBig.setObjectId(obj);
  843. throw tooBig;
  844. }
  845. }
  846. /**
  847. * Asynchronous object parsing.
  848. *
  849. * @param <T>
  850. * any ObjectId type.
  851. * @param objectIds
  852. * objects to open from the object store. The supplied collection
  853. * must not be modified until the queue has finished.
  854. * @param reportMissing
  855. * if true missing objects are reported by calling failure with a
  856. * MissingObjectException. This may be more expensive for the
  857. * implementation to guarantee. If false the implementation may
  858. * choose to report MissingObjectException, or silently skip over
  859. * the object with no warning.
  860. * @return queue to read the objects from.
  861. */
  862. public <T extends ObjectId> AsyncRevObjectQueue parseAny(
  863. Iterable<T> objectIds, boolean reportMissing) {
  864. List<T> need = new ArrayList<T>();
  865. List<RevObject> have = new ArrayList<RevObject>();
  866. for (T id : objectIds) {
  867. RevObject r = objects.get(id);
  868. if (r != null && (r.flags & PARSED) != 0)
  869. have.add(r);
  870. else
  871. need.add(id);
  872. }
  873. final Iterator<RevObject> objItr = have.iterator();
  874. if (need.isEmpty()) {
  875. return new AsyncRevObjectQueue() {
  876. public RevObject next() {
  877. return objItr.hasNext() ? objItr.next() : null;
  878. }
  879. public boolean cancel(boolean mayInterruptIfRunning) {
  880. return true;
  881. }
  882. public void release() {
  883. // In-memory only, no action required.
  884. }
  885. };
  886. }
  887. final AsyncObjectLoaderQueue<T> lItr = reader.open(need, reportMissing);
  888. return new AsyncRevObjectQueue() {
  889. public RevObject next() throws MissingObjectException,
  890. IncorrectObjectTypeException, IOException {
  891. if (objItr.hasNext())
  892. return objItr.next();
  893. if (!lItr.next())
  894. return null;
  895. ObjectId id = lItr.getObjectId();
  896. ObjectLoader ldr = lItr.open();
  897. RevObject r = objects.get(id);
  898. if (r == null)
  899. r = parseNew(id, ldr);
  900. else if (r instanceof RevCommit) {
  901. byte[] raw = ldr.getCachedBytes();
  902. ((RevCommit) r).parseCanonical(RevWalk.this, raw);
  903. } else if (r instanceof RevTag) {
  904. byte[] raw = ldr.getCachedBytes();
  905. ((RevTag) r).parseCanonical(RevWalk.this, raw);
  906. } else
  907. r.flags |= PARSED;
  908. return r;
  909. }
  910. public boolean cancel(boolean mayInterruptIfRunning) {
  911. return lItr.cancel(mayInterruptIfRunning);
  912. }
  913. public void release() {
  914. lItr.release();
  915. }
  916. };
  917. }
  918. /**
  919. * Ensure the object's critical headers have been parsed.
  920. * <p>
  921. * This method only returns successfully if the object exists and was parsed
  922. * without error.
  923. *
  924. * @param obj
  925. * the object the caller needs to be parsed.
  926. * @throws MissingObjectException
  927. * the supplied does not exist.
  928. * @throws IOException
  929. * a pack file or loose object could not be read.
  930. */
  931. public void parseHeaders(final RevObject obj)
  932. throws MissingObjectException, IOException {
  933. if ((obj.flags & PARSED) == 0)
  934. obj.parseHeaders(this);
  935. }
  936. /**
  937. * Ensure the object's full body content is available.
  938. * <p>
  939. * This method only returns successfully if the object exists and was parsed
  940. * without error.
  941. *
  942. * @param obj
  943. * the object the caller needs to be parsed.
  944. * @throws MissingObjectException
  945. * the supplied does not exist.
  946. * @throws IOException
  947. * a pack file or loose object could not be read.
  948. */
  949. public void parseBody(final RevObject obj)
  950. throws MissingObjectException, IOException {
  951. obj.parseBody(this);
  952. }
  953. /**
  954. * Peel back annotated tags until a non-tag object is found.
  955. *
  956. * @param obj
  957. * the starting object.
  958. * @return If {@code obj} is not an annotated tag, {@code obj}. Otherwise
  959. * the first non-tag object that {@code obj} references. The
  960. * returned object's headers have been parsed.
  961. * @throws MissingObjectException
  962. * a referenced object cannot be found.
  963. * @throws IOException
  964. * a pack file or loose object could not be read.
  965. */
  966. public RevObject peel(RevObject obj) throws MissingObjectException,
  967. IOException {
  968. while (obj instanceof RevTag) {
  969. parseHeaders(obj);
  970. obj = ((RevTag) obj).getObject();
  971. }
  972. parseHeaders(obj);
  973. return obj;
  974. }
  975. /**
  976. * Create a new flag for application use during walking.
  977. * <p>
  978. * Applications are only assured to be able to create 24 unique flags on any
  979. * given revision walker instance. Any flags beyond 24 are offered only if
  980. * the implementation has extra free space within its internal storage.
  981. *
  982. * @param name
  983. * description of the flag, primarily useful for debugging.
  984. * @return newly constructed flag instance.
  985. * @throws IllegalArgumentException
  986. * too many flags have been reserved on this revision walker.
  987. */
  988. public RevFlag newFlag(final String name) {
  989. final int m = allocFlag();
  990. return new RevFlag(this, name, m);
  991. }
  992. int allocFlag() {
  993. if (freeFlags == 0)
  994. throw new IllegalArgumentException(MessageFormat.format(
  995. JGitText.get().flagsAlreadyCreated,
  996. Integer.valueOf(32 - RESERVED_FLAGS)));
  997. final int m = Integer.lowestOneBit(freeFlags);
  998. freeFlags &= ~m;
  999. return m;
  1000. }
  1001. /**
  1002. * Automatically carry a flag from a child commit to its parents.
  1003. * <p>
  1004. * A carried flag is copied from the child commit onto its parents when the
  1005. * child commit is popped from the lowest level of walk's internal graph.
  1006. *
  1007. * @param flag
  1008. * the flag to carry onto parents, if set on a descendant.
  1009. */
  1010. public void carry(final RevFlag flag) {
  1011. if ((freeFlags & flag.mask) != 0)
  1012. throw new IllegalArgumentException(MessageFormat.format(JGitText.get().flagIsDisposed, flag.name));
  1013. if (flag.walker != this)
  1014. throw new IllegalArgumentException(MessageFormat.format(JGitText.get().flagNotFromThis, flag.name));
  1015. carryFlags |= flag.mask;
  1016. }
  1017. /**
  1018. * Automatically carry flags from a child commit to its parents.
  1019. * <p>
  1020. * A carried flag is copied from the child commit onto its parents when the
  1021. * child commit is popped from the lowest level of walk's internal graph.
  1022. *
  1023. * @param set
  1024. * the flags to carry onto parents, if set on a descendant.
  1025. */
  1026. public void carry(final Collection<RevFlag> set) {
  1027. for (final RevFlag flag : set)
  1028. carry(flag);
  1029. }
  1030. /**
  1031. * Preserve a RevFlag during all {@code reset} methods.
  1032. * <p>
  1033. * Calling {@code retainOnReset(flag)} avoids needing to pass the flag
  1034. * during each {@code resetRetain()} invocation on this instance.
  1035. * <p>
  1036. * Clearing flags marked retainOnReset requires disposing of the flag with
  1037. * {@code #disposeFlag(RevFlag)} or disposing of the entire RevWalk by
  1038. * {@code #dispose()}.
  1039. *
  1040. * @param flag
  1041. * the flag to retain during all resets.
  1042. * @since 3.6
  1043. */
  1044. public final void retainOnReset(RevFlag flag) {
  1045. if ((freeFlags & flag.mask) != 0)
  1046. throw new IllegalArgumentException(MessageFormat.format(JGitText.get().flagIsDisposed, flag.name));
  1047. if (flag.walker != this)
  1048. throw new IllegalArgumentException(MessageFormat.format(JGitText.get().flagNotFromThis, flag.name));
  1049. retainOnReset |= flag.mask;
  1050. }
  1051. /**
  1052. * Preserve a set of RevFlags during all {@code reset} methods.
  1053. * <p>
  1054. * Calling {@code retainOnReset(set)} avoids needing to pass the flags
  1055. * during each {@code resetRetain()} invocation on this instance.
  1056. * <p>
  1057. * Clearing flags marked retainOnReset requires disposing of the flag with
  1058. * {@code #disposeFlag(RevFlag)} or disposing of the entire RevWalk by
  1059. * {@code #dispose()}.
  1060. *
  1061. * @param flags
  1062. * the flags to retain during all resets.
  1063. * @since 3.6
  1064. */
  1065. public final void retainOnReset(Collection<RevFlag> flags) {
  1066. for (RevFlag f : flags)
  1067. retainOnReset(f);
  1068. }
  1069. /**
  1070. * Allow a flag to be recycled for a different use.
  1071. * <p>
  1072. * Recycled flags always come back as a different Java object instance when
  1073. * assigned again by {@link #newFlag(String)}.
  1074. * <p>
  1075. * If the flag was previously being carried, the carrying request is
  1076. * removed. Disposing of a carried flag while a traversal is in progress has
  1077. * an undefined behavior.
  1078. *
  1079. * @param flag
  1080. * the to recycle.
  1081. */
  1082. public void disposeFlag(final RevFlag flag) {
  1083. freeFlag(flag.mask);
  1084. }
  1085. void freeFlag(final int mask) {
  1086. retainOnReset &= ~mask;
  1087. if (isNotStarted()) {
  1088. freeFlags |= mask;
  1089. carryFlags &= ~mask;
  1090. } else {
  1091. delayFreeFlags |= mask;
  1092. }
  1093. }
  1094. private void finishDelayedFreeFlags() {
  1095. if (delayFreeFlags != 0) {
  1096. freeFlags |= delayFreeFlags;
  1097. carryFlags &= ~delayFreeFlags;
  1098. delayFreeFlags = 0;
  1099. }
  1100. }
  1101. /**
  1102. * Resets internal state and allows this instance to be used again.
  1103. * <p>
  1104. * Unlike {@link #dispose()} previously acquired RevObject (and RevCommit)
  1105. * instances are not invalidated. RevFlag instances are not invalidated, but
  1106. * are removed from all RevObjects.
  1107. */
  1108. public final void reset() {
  1109. reset(0);
  1110. }
  1111. /**
  1112. * Resets internal state and allows this instance to be used again.
  1113. * <p>
  1114. * Unlike {@link #dispose()} previously acquired RevObject (and RevCommit)
  1115. * instances are not invalidated. RevFlag instances are not invalidated, but
  1116. * are removed from all RevObjects.
  1117. *
  1118. * @param retainFlags
  1119. * application flags that should <b>not</b> be cleared from
  1120. * existing commit objects.
  1121. */
  1122. public final void resetRetain(final RevFlagSet retainFlags) {
  1123. reset(retainFlags.mask);
  1124. }
  1125. /**
  1126. * Resets internal state and allows this instance to be used again.
  1127. * <p>
  1128. * Unlike {@link #dispose()} previously acquired RevObject (and RevCommit)
  1129. * instances are not invalidated. RevFlag instances are not invalidated, but
  1130. * are removed from all RevObjects.
  1131. * <p>
  1132. * See {@link #retainOnReset(RevFlag)} for an alternative that does not
  1133. * require passing the flags during each reset.
  1134. *
  1135. * @param retainFlags
  1136. * application flags that should <b>not</b> be cleared from
  1137. * existing commit objects.
  1138. */
  1139. public final void resetRetain(final RevFlag... retainFlags) {
  1140. int mask = 0;
  1141. for (final RevFlag flag : retainFlags)
  1142. mask |= flag.mask;
  1143. reset(mask);
  1144. }
  1145. /**
  1146. * Resets internal state and allows this instance to be used again.
  1147. * <p>
  1148. * Unlike {@link #dispose()} previously acquired RevObject (and RevCommit)
  1149. * instances are not invalidated. RevFlag instances are not invalidated, but
  1150. * are removed from all RevObjects.
  1151. *
  1152. * @param retainFlags
  1153. * application flags that should <b>not</b> be cleared from
  1154. * existing commit objects.
  1155. */
  1156. protected void reset(int retainFlags) {
  1157. finishDelayedFreeFlags();
  1158. retainFlags |= PARSED | retainOnReset;
  1159. final int clearFlags = ~retainFlags;
  1160. final FIFORevQueue q = new FIFORevQueue();
  1161. for (final RevCommit c : roots) {
  1162. if ((c.flags & clearFlags) == 0)
  1163. continue;
  1164. c.flags &= retainFlags;
  1165. c.reset();
  1166. q.add(c);
  1167. }
  1168. for (;;) {
  1169. final RevCommit c = q.next();
  1170. if (c == null)
  1171. break;
  1172. if (c.parents == null)
  1173. continue;
  1174. for (final RevCommit p : c.parents) {
  1175. if ((p.flags & clearFlags) == 0)
  1176. continue;
  1177. p.flags &= retainFlags;
  1178. p.reset();
  1179. q.add(p);
  1180. }
  1181. }
  1182. roots.clear();
  1183. queue = new DateRevQueue();
  1184. pending = new StartGenerator(this);
  1185. }
  1186. /**
  1187. * Dispose all internal state and invalidate all RevObject instances.
  1188. * <p>
  1189. * All RevObject (and thus RevCommit, etc.) instances previously acquired
  1190. * from this RevWalk are invalidated by a dispose call. Applications must
  1191. * not retain or use RevObject instances obtained prior to the dispose call.
  1192. * All RevFlag instances are also invalidated, and must not be reused.
  1193. */
  1194. public void dispose() {
  1195. reader.release();
  1196. freeFlags = APP_FLAGS;
  1197. delayFreeFlags = 0;
  1198. retainOnReset = 0;
  1199. carryFlags = UNINTERESTING;
  1200. objects.clear();
  1201. reader.release();
  1202. roots.clear();
  1203. queue = new DateRevQueue();
  1204. pending = new StartGenerator(this);
  1205. shallowCommitsInitialized = false;
  1206. }
  1207. /**
  1208. * Returns an Iterator over the commits of this walker.
  1209. * <p>
  1210. * The returned iterator is only useful for one walk. If this RevWalk gets
  1211. * reset a new iterator must be obtained to walk over the new results.
  1212. * <p>
  1213. * Applications must not use both the Iterator and the {@link #next()} API
  1214. * at the same time. Pick one API and use that for the entire walk.
  1215. * <p>
  1216. * If a checked exception is thrown during the walk (see {@link #next()})
  1217. * it is rethrown from the Iterator as a {@link RevWalkException}.
  1218. *
  1219. * @return an iterator over this walker's commits.
  1220. * @see RevWalkException
  1221. */
  1222. public Iterator<RevCommit> iterator() {
  1223. final RevCommit first;
  1224. try {
  1225. first = RevWalk.this.next();
  1226. } catch (MissingObjectException e) {
  1227. throw new RevWalkException(e);
  1228. } catch (IncorrectObjectTypeException e) {
  1229. throw new RevWalkException(e);
  1230. } catch (IOException e) {
  1231. throw new RevWalkException(e);
  1232. }
  1233. return new Iterator<RevCommit>() {
  1234. RevCommit next = first;
  1235. public boolean hasNext() {
  1236. return next != null;
  1237. }
  1238. public RevCommit next() {
  1239. try {
  1240. final RevCommit r = next;
  1241. next = RevWalk.this.next();
  1242. return r;
  1243. } catch (MissingObjectException e) {
  1244. throw new RevWalkException(e);
  1245. } catch (IncorrectObjectTypeException e) {
  1246. throw new RevWalkException(e);
  1247. } catch (IOException e) {
  1248. throw new RevWalkException(e);
  1249. }
  1250. }
  1251. public void remove() {
  1252. throw new UnsupportedOperationException();
  1253. }
  1254. };
  1255. }
  1256. /** Throws an exception if we have started producing output. */
  1257. protected void assertNotStarted() {
  1258. if (isNotStarted())
  1259. return;
  1260. throw new IllegalStateException(JGitText.get().outputHasAlreadyBeenStarted);
  1261. }
  1262. private boolean isNotStarted() {
  1263. return pending instanceof StartGenerator;
  1264. }
  1265. /**
  1266. * Create and return an {@link ObjectWalk} using the same objects.
  1267. * <p>
  1268. * Prior to using this method, the caller must reset this RevWalk to clean
  1269. * any flags that were used during the last traversal.
  1270. * <p>
  1271. * The returned ObjectWalk uses the same ObjectReader, internal object pool,
  1272. * and free RevFlags. Once the ObjectWalk is created, this RevWalk should
  1273. * not be used anymore.
  1274. *
  1275. * @return a new walk, using the exact same object pool.
  1276. */
  1277. public ObjectWalk toObjectWalkWithSameObjects() {
  1278. ObjectWalk ow = new ObjectWalk(reader);
  1279. RevWalk rw = ow;
  1280. rw.objects = objects;
  1281. rw.freeFlags = freeFlags;
  1282. return ow;
  1283. }
  1284. /**
  1285. * Construct a new unparsed commit for the given object.
  1286. *
  1287. * @param id
  1288. * the object this walker requires a commit reference for.
  1289. * @return a new unparsed reference for the object.
  1290. */
  1291. protected RevCommit createCommit(final AnyObjectId id) {
  1292. return new RevCommit(id);
  1293. }
  1294. void carryFlagsImpl(final RevCommit c) {
  1295. final int carry = c.flags & carryFlags;
  1296. if (carry != 0)
  1297. RevCommit.carryFlags(c, carry);
  1298. }
  1299. /**
  1300. * Assume additional commits are shallow (have no parents).
  1301. *
  1302. * @param ids
  1303. * commits that should be treated as shallow commits, in addition
  1304. * to any commits already known to be shallow by the repository.
  1305. * @since 3.3
  1306. */
  1307. public void assumeShallow(Collection<? extends ObjectId> ids) {
  1308. for (ObjectId id : ids)
  1309. lookupCommit(id).parents = RevCommit.NO_PARENTS;
  1310. }
  1311. void initializeShallowCommits() throws IOException {
  1312. if (shallowCommitsInitialized)
  1313. throw new IllegalStateException(
  1314. JGitText.get().shallowCommitsAlreadyInitialized);
  1315. shallowCommitsInitialized = true;
  1316. if (reader == null)
  1317. return;
  1318. for (ObjectId id : reader.getShallowCommits())
  1319. lookupCommit(id).parents = RevCommit.NO_PARENTS;
  1320. }
  1321. }