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

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