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.

ObjectWalk.java 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. /*
  2. * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.revwalk;
  44. import java.io.IOException;
  45. import java.text.MessageFormat;
  46. import org.eclipse.jgit.JGitText;
  47. import org.eclipse.jgit.errors.CorruptObjectException;
  48. import org.eclipse.jgit.errors.IncorrectObjectTypeException;
  49. import org.eclipse.jgit.errors.MissingObjectException;
  50. import org.eclipse.jgit.lib.AnyObjectId;
  51. import org.eclipse.jgit.lib.Constants;
  52. import org.eclipse.jgit.lib.FileMode;
  53. import org.eclipse.jgit.lib.ObjectReader;
  54. import org.eclipse.jgit.lib.Repository;
  55. import org.eclipse.jgit.treewalk.CanonicalTreeParser;
  56. /**
  57. * Specialized subclass of RevWalk to include trees, blobs and tags.
  58. * <p>
  59. * Unlike RevWalk this subclass is able to remember starting roots that include
  60. * annotated tags, or arbitrary trees or blobs. Once commit generation is
  61. * complete and all commits have been popped by the application, individual
  62. * annotated tag, tree and blob objects can be popped through the additional
  63. * method {@link #nextObject()}.
  64. * <p>
  65. * Tree and blob objects reachable from interesting commits are automatically
  66. * scheduled for inclusion in the results of {@link #nextObject()}, returning
  67. * each object exactly once. Objects are sorted and returned according to the
  68. * the commits that reference them and the order they appear within a tree.
  69. * Ordering can be affected by changing the {@link RevSort} used to order the
  70. * commits that are returned first.
  71. */
  72. public class ObjectWalk extends RevWalk {
  73. /**
  74. * Indicates a non-RevCommit is in {@link #pendingObjects}.
  75. * <p>
  76. * We can safely reuse {@link RevWalk#REWRITE} here for the same value as it
  77. * is only set on RevCommit and {@link #pendingObjects} never has RevCommit
  78. * instances inserted into it.
  79. */
  80. private static final int IN_PENDING = RevWalk.REWRITE;
  81. private CanonicalTreeParser treeWalk;
  82. private BlockObjQueue pendingObjects;
  83. private RevTree currentTree;
  84. private RevObject last;
  85. private RevCommit firstCommit;
  86. private RevCommit lastCommit;
  87. /**
  88. * Create a new revision and object walker for a given repository.
  89. *
  90. * @param repo
  91. * the repository the walker will obtain data from.
  92. */
  93. public ObjectWalk(final Repository repo) {
  94. this(repo.newObjectReader());
  95. }
  96. /**
  97. * Create a new revision and object walker for a given repository.
  98. *
  99. * @param or
  100. * the reader the walker will obtain data from. The reader should
  101. * be released by the caller when the walker is no longer
  102. * required.
  103. */
  104. public ObjectWalk(ObjectReader or) {
  105. super(or);
  106. pendingObjects = new BlockObjQueue();
  107. treeWalk = new CanonicalTreeParser();
  108. }
  109. /**
  110. * Mark an object or commit to start graph traversal from.
  111. * <p>
  112. * Callers are encouraged to use {@link RevWalk#parseAny(AnyObjectId)}
  113. * instead of {@link RevWalk#lookupAny(AnyObjectId, int)}, as this method
  114. * requires the object to be parsed before it can be added as a root for the
  115. * traversal.
  116. * <p>
  117. * The method will automatically parse an unparsed object, but error
  118. * handling may be more difficult for the application to explain why a
  119. * RevObject is not actually valid. The object pool of this walker would
  120. * also be 'poisoned' by the invalid RevObject.
  121. * <p>
  122. * This method will automatically call {@link RevWalk#markStart(RevCommit)}
  123. * if passed RevCommit instance, or a RevTag that directly (or indirectly)
  124. * references a RevCommit.
  125. *
  126. * @param o
  127. * the object to start traversing from. The object passed must be
  128. * from this same revision walker.
  129. * @throws MissingObjectException
  130. * the object supplied is not available from the object
  131. * database. This usually indicates the supplied object is
  132. * invalid, but the reference was constructed during an earlier
  133. * invocation to {@link RevWalk#lookupAny(AnyObjectId, int)}.
  134. * @throws IncorrectObjectTypeException
  135. * the object was not parsed yet and it was discovered during
  136. * parsing that it is not actually the type of the instance
  137. * passed in. This usually indicates the caller used the wrong
  138. * type in a {@link RevWalk#lookupAny(AnyObjectId, int)} call.
  139. * @throws IOException
  140. * a pack file or loose object could not be read.
  141. */
  142. public void markStart(RevObject o) throws MissingObjectException,
  143. IncorrectObjectTypeException, IOException {
  144. while (o instanceof RevTag) {
  145. addObject(o);
  146. o = ((RevTag) o).getObject();
  147. parseHeaders(o);
  148. }
  149. if (o instanceof RevCommit)
  150. super.markStart((RevCommit) o);
  151. else
  152. addObject(o);
  153. }
  154. /**
  155. * Mark an object to not produce in the output.
  156. * <p>
  157. * Uninteresting objects denote not just themselves but also their entire
  158. * reachable chain, back until the merge base of an uninteresting commit and
  159. * an otherwise interesting commit.
  160. * <p>
  161. * Callers are encouraged to use {@link RevWalk#parseAny(AnyObjectId)}
  162. * instead of {@link RevWalk#lookupAny(AnyObjectId, int)}, as this method
  163. * requires the object to be parsed before it can be added as a root for the
  164. * traversal.
  165. * <p>
  166. * The method will automatically parse an unparsed object, but error
  167. * handling may be more difficult for the application to explain why a
  168. * RevObject is not actually valid. The object pool of this walker would
  169. * also be 'poisoned' by the invalid RevObject.
  170. * <p>
  171. * This method will automatically call {@link RevWalk#markStart(RevCommit)}
  172. * if passed RevCommit instance, or a RevTag that directly (or indirectly)
  173. * references a RevCommit.
  174. *
  175. * @param o
  176. * the object to start traversing from. The object passed must be
  177. * @throws MissingObjectException
  178. * the object supplied is not available from the object
  179. * database. This usually indicates the supplied object is
  180. * invalid, but the reference was constructed during an earlier
  181. * invocation to {@link RevWalk#lookupAny(AnyObjectId, int)}.
  182. * @throws IncorrectObjectTypeException
  183. * the object was not parsed yet and it was discovered during
  184. * parsing that it is not actually the type of the instance
  185. * passed in. This usually indicates the caller used the wrong
  186. * type in a {@link RevWalk#lookupAny(AnyObjectId, int)} call.
  187. * @throws IOException
  188. * a pack file or loose object could not be read.
  189. */
  190. public void markUninteresting(RevObject o) throws MissingObjectException,
  191. IncorrectObjectTypeException, IOException {
  192. while (o instanceof RevTag) {
  193. o.flags |= UNINTERESTING;
  194. if (hasRevSort(RevSort.BOUNDARY))
  195. addObject(o);
  196. o = ((RevTag) o).getObject();
  197. parseHeaders(o);
  198. }
  199. if (o instanceof RevCommit)
  200. super.markUninteresting((RevCommit) o);
  201. else if (o instanceof RevTree)
  202. markTreeUninteresting((RevTree) o);
  203. else
  204. o.flags |= UNINTERESTING;
  205. if (o.getType() != Constants.OBJ_COMMIT && hasRevSort(RevSort.BOUNDARY)) {
  206. addObject(o);
  207. }
  208. }
  209. @Override
  210. public RevCommit next() throws MissingObjectException,
  211. IncorrectObjectTypeException, IOException {
  212. for (;;) {
  213. final RevCommit r = super.next();
  214. if (r == null)
  215. return null;
  216. if ((r.flags & UNINTERESTING) != 0) {
  217. markTreeUninteresting(r.getTree());
  218. if (hasRevSort(RevSort.BOUNDARY)) {
  219. pendingObjects.add(r.getTree());
  220. return r;
  221. }
  222. continue;
  223. }
  224. if (firstCommit == null)
  225. firstCommit = r;
  226. lastCommit = r;
  227. pendingObjects.add(r.getTree());
  228. return r;
  229. }
  230. }
  231. /**
  232. * Pop the next most recent object.
  233. *
  234. * @return next most recent object; null if traversal is over.
  235. * @throws MissingObjectException
  236. * one or or more of the next objects are not available from the
  237. * object database, but were thought to be candidates for
  238. * traversal. This usually indicates a broken link.
  239. * @throws IncorrectObjectTypeException
  240. * one or or more of the objects in a tree do not match the type
  241. * indicated.
  242. * @throws IOException
  243. * a pack file or loose object could not be read.
  244. */
  245. public RevObject nextObject() throws MissingObjectException,
  246. IncorrectObjectTypeException, IOException {
  247. if (last != null)
  248. treeWalk = last instanceof RevTree ? enter(last) : treeWalk.next();
  249. while (!treeWalk.eof()) {
  250. final FileMode mode = treeWalk.getEntryFileMode();
  251. switch (mode.getObjectType()) {
  252. case Constants.OBJ_BLOB: {
  253. treeWalk.getEntryObjectId(idBuffer);
  254. final RevBlob o = lookupBlob(idBuffer);
  255. if ((o.flags & SEEN) != 0)
  256. break;
  257. o.flags |= SEEN;
  258. if (shouldSkipObject(o))
  259. break;
  260. last = o;
  261. return o;
  262. }
  263. case Constants.OBJ_TREE: {
  264. treeWalk.getEntryObjectId(idBuffer);
  265. final RevTree o = lookupTree(idBuffer);
  266. if ((o.flags & SEEN) != 0)
  267. break;
  268. o.flags |= SEEN;
  269. if (shouldSkipObject(o))
  270. break;
  271. last = o;
  272. return o;
  273. }
  274. default:
  275. if (FileMode.GITLINK.equals(mode))
  276. break;
  277. treeWalk.getEntryObjectId(idBuffer);
  278. throw new CorruptObjectException(MessageFormat.format(JGitText.get().corruptObjectInvalidMode3
  279. , mode , idBuffer.name() , treeWalk.getEntryPathString() , currentTree.name()));
  280. }
  281. treeWalk = treeWalk.next();
  282. }
  283. if (firstCommit != null) {
  284. reader.walkAdviceBeginTrees(this, firstCommit, lastCommit);
  285. firstCommit = null;
  286. lastCommit = null;
  287. }
  288. last = null;
  289. for (;;) {
  290. final RevObject o = pendingObjects.next();
  291. if (o == null) {
  292. reader.walkAdviceEnd();
  293. return null;
  294. }
  295. if ((o.flags & SEEN) != 0)
  296. continue;
  297. o.flags |= SEEN;
  298. if (shouldSkipObject(o))
  299. continue;
  300. if (o instanceof RevTree) {
  301. currentTree = (RevTree) o;
  302. treeWalk = treeWalk.resetRoot(reader, currentTree);
  303. }
  304. return o;
  305. }
  306. }
  307. private CanonicalTreeParser enter(RevObject tree) throws IOException {
  308. CanonicalTreeParser p = treeWalk.createSubtreeIterator0(reader, tree);
  309. if (p.eof()) {
  310. // We can't tolerate the subtree being an empty tree, as
  311. // that will break us out early before we visit all names.
  312. // If it is, advance to the parent's next record.
  313. //
  314. return treeWalk.next();
  315. }
  316. return p;
  317. }
  318. private final boolean shouldSkipObject(final RevObject o) {
  319. return (o.flags & UNINTERESTING) != 0 && !hasRevSort(RevSort.BOUNDARY);
  320. }
  321. /**
  322. * Verify all interesting objects are available, and reachable.
  323. * <p>
  324. * Callers should populate starting points and ending points with
  325. * {@link #markStart(RevObject)} and {@link #markUninteresting(RevObject)}
  326. * and then use this method to verify all objects between those two points
  327. * exist in the repository and are readable.
  328. * <p>
  329. * This method returns successfully if everything is connected; it throws an
  330. * exception if there is a connectivity problem. The exception message
  331. * provides some detail about the connectivity failure.
  332. *
  333. * @throws MissingObjectException
  334. * one or or more of the next objects are not available from the
  335. * object database, but were thought to be candidates for
  336. * traversal. This usually indicates a broken link.
  337. * @throws IncorrectObjectTypeException
  338. * one or or more of the objects in a tree do not match the type
  339. * indicated.
  340. * @throws IOException
  341. * a pack file or loose object could not be read.
  342. */
  343. public void checkConnectivity() throws MissingObjectException,
  344. IncorrectObjectTypeException, IOException {
  345. for (;;) {
  346. final RevCommit c = next();
  347. if (c == null)
  348. break;
  349. }
  350. for (;;) {
  351. final RevObject o = nextObject();
  352. if (o == null)
  353. break;
  354. if (o instanceof RevBlob && !reader.has(o))
  355. throw new MissingObjectException(o, Constants.TYPE_BLOB);
  356. }
  357. }
  358. /**
  359. * Get the current object's complete path.
  360. * <p>
  361. * This method is not very efficient and is primarily meant for debugging
  362. * and final output generation. Applications should try to avoid calling it,
  363. * and if invoked do so only once per interesting entry, where the name is
  364. * absolutely required for correct function.
  365. *
  366. * @return complete path of the current entry, from the root of the
  367. * repository. If the current entry is in a subtree there will be at
  368. * least one '/' in the returned string. Null if the current entry
  369. * has no path, such as for annotated tags or root level trees.
  370. */
  371. public String getPathString() {
  372. return last != null ? treeWalk.getEntryPathString() : null;
  373. }
  374. /**
  375. * Get the current object's path hash code.
  376. * <p>
  377. * This method computes a hash code on the fly for this path, the hash is
  378. * suitable to cluster objects that may have similar paths together.
  379. *
  380. * @return path hash code; any integer may be returned.
  381. */
  382. public int getPathHashCode() {
  383. return last != null ? treeWalk.getEntryPathHashCode() : 0;
  384. }
  385. @Override
  386. public void dispose() {
  387. super.dispose();
  388. pendingObjects = new BlockObjQueue();
  389. treeWalk = new CanonicalTreeParser();
  390. currentTree = null;
  391. last = null;
  392. firstCommit = null;
  393. lastCommit = null;
  394. }
  395. @Override
  396. protected void reset(final int retainFlags) {
  397. super.reset(retainFlags);
  398. pendingObjects = new BlockObjQueue();
  399. treeWalk = new CanonicalTreeParser();
  400. currentTree = null;
  401. last = null;
  402. firstCommit = null;
  403. lastCommit = null;
  404. }
  405. private void addObject(final RevObject o) {
  406. if ((o.flags & IN_PENDING) == 0) {
  407. o.flags |= IN_PENDING;
  408. pendingObjects.add(o);
  409. }
  410. }
  411. private void markTreeUninteresting(final RevTree tree)
  412. throws MissingObjectException, IncorrectObjectTypeException,
  413. IOException {
  414. if ((tree.flags & UNINTERESTING) != 0)
  415. return;
  416. tree.flags |= UNINTERESTING;
  417. treeWalk = treeWalk.resetRoot(reader, tree);
  418. while (!treeWalk.eof()) {
  419. final FileMode mode = treeWalk.getEntryFileMode();
  420. final int sType = mode.getObjectType();
  421. switch (sType) {
  422. case Constants.OBJ_BLOB: {
  423. treeWalk.getEntryObjectId(idBuffer);
  424. lookupBlob(idBuffer).flags |= UNINTERESTING;
  425. break;
  426. }
  427. case Constants.OBJ_TREE: {
  428. treeWalk.getEntryObjectId(idBuffer);
  429. final RevTree t = lookupTree(idBuffer);
  430. if ((t.flags & UNINTERESTING) == 0) {
  431. t.flags |= UNINTERESTING;
  432. treeWalk = treeWalk.createSubtreeIterator0(reader, t);
  433. continue;
  434. }
  435. break;
  436. }
  437. default:
  438. if (FileMode.GITLINK.equals(mode))
  439. break;
  440. treeWalk.getEntryObjectId(idBuffer);
  441. throw new CorruptObjectException(MessageFormat.format(JGitText.get().corruptObjectInvalidMode3
  442. , mode , idBuffer.name() , treeWalk.getEntryPathString() , tree));
  443. }
  444. treeWalk = treeWalk.next();
  445. }
  446. }
  447. }