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

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