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

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