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

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