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

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