Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

RevWalk.java 45KB

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