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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  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 java.util.ArrayList;
  47. import java.util.List;
  48. import org.eclipse.jgit.JGitText;
  49. import org.eclipse.jgit.errors.CorruptObjectException;
  50. import org.eclipse.jgit.errors.IncorrectObjectTypeException;
  51. import org.eclipse.jgit.errors.MissingObjectException;
  52. import org.eclipse.jgit.lib.AnyObjectId;
  53. import org.eclipse.jgit.lib.Constants;
  54. import org.eclipse.jgit.lib.FileMode;
  55. import org.eclipse.jgit.lib.ObjectReader;
  56. import org.eclipse.jgit.lib.Repository;
  57. import org.eclipse.jgit.treewalk.CanonicalTreeParser;
  58. /**
  59. * Specialized subclass of RevWalk to include trees, blobs and tags.
  60. * <p>
  61. * Unlike RevWalk this subclass is able to remember starting roots that include
  62. * annotated tags, or arbitrary trees or blobs. Once commit generation is
  63. * complete and all commits have been popped by the application, individual
  64. * annotated tag, tree and blob objects can be popped through the additional
  65. * method {@link #nextObject()}.
  66. * <p>
  67. * Tree and blob objects reachable from interesting commits are automatically
  68. * scheduled for inclusion in the results of {@link #nextObject()}, returning
  69. * each object exactly once. Objects are sorted and returned according to the
  70. * the commits that reference them and the order they appear within a tree.
  71. * Ordering can be affected by changing the {@link RevSort} used to order the
  72. * commits that are returned first.
  73. */
  74. public class ObjectWalk extends RevWalk {
  75. /**
  76. * Indicates a non-RevCommit is in {@link #pendingObjects}.
  77. * <p>
  78. * We can safely reuse {@link RevWalk#REWRITE} here for the same value as it
  79. * is only set on RevCommit and {@link #pendingObjects} never has RevCommit
  80. * instances inserted into it.
  81. */
  82. private static final int IN_PENDING = RevWalk.REWRITE;
  83. private static final byte[] EMPTY_PATH = {};
  84. private CanonicalTreeParser treeWalk;
  85. private List<RevObject> rootObjects;
  86. private BlockObjQueue pendingObjects;
  87. private RevTree currentTree;
  88. private RevObject last;
  89. private RevCommit firstCommit;
  90. private RevCommit lastCommit;
  91. /**
  92. * Create a new revision and object walker for a given repository.
  93. *
  94. * @param repo
  95. * the repository the walker will obtain data from.
  96. */
  97. public ObjectWalk(final Repository repo) {
  98. this(repo.newObjectReader());
  99. }
  100. /**
  101. * Create a new revision and object walker for a given repository.
  102. *
  103. * @param or
  104. * the reader the walker will obtain data from. The reader should
  105. * be released by the caller when the walker is no longer
  106. * required.
  107. */
  108. public ObjectWalk(ObjectReader or) {
  109. super(or);
  110. rootObjects = new ArrayList<RevObject>();
  111. pendingObjects = new BlockObjQueue();
  112. treeWalk = new CanonicalTreeParser();
  113. }
  114. /**
  115. * Mark an object or commit to start graph traversal from.
  116. * <p>
  117. * Callers are encouraged to use {@link RevWalk#parseAny(AnyObjectId)}
  118. * instead of {@link RevWalk#lookupAny(AnyObjectId, int)}, as this method
  119. * requires the object to be parsed before it can be added as a root for the
  120. * traversal.
  121. * <p>
  122. * The method will automatically parse an unparsed object, but error
  123. * handling may be more difficult for the application to explain why a
  124. * RevObject is not actually valid. The object pool of this walker would
  125. * also be 'poisoned' by the invalid RevObject.
  126. * <p>
  127. * This method will automatically call {@link RevWalk#markStart(RevCommit)}
  128. * if passed RevCommit instance, or a RevTag that directly (or indirectly)
  129. * references a RevCommit.
  130. *
  131. * @param o
  132. * the object to start traversing from. The object passed must be
  133. * from this same revision walker.
  134. * @throws MissingObjectException
  135. * the object supplied is not available from the object
  136. * database. This usually indicates the supplied object is
  137. * invalid, but the reference was constructed during an earlier
  138. * invocation to {@link RevWalk#lookupAny(AnyObjectId, int)}.
  139. * @throws IncorrectObjectTypeException
  140. * the object was not parsed yet and it was discovered during
  141. * parsing that it is not actually the type of the instance
  142. * passed in. This usually indicates the caller used the wrong
  143. * type in a {@link RevWalk#lookupAny(AnyObjectId, int)} call.
  144. * @throws IOException
  145. * a pack file or loose object could not be read.
  146. */
  147. public void markStart(RevObject o) throws MissingObjectException,
  148. IncorrectObjectTypeException, IOException {
  149. while (o instanceof RevTag) {
  150. addObject(o);
  151. o = ((RevTag) o).getObject();
  152. parseHeaders(o);
  153. }
  154. if (o instanceof RevCommit)
  155. super.markStart((RevCommit) o);
  156. else
  157. addObject(o);
  158. }
  159. /**
  160. * Mark an object to not produce in the output.
  161. * <p>
  162. * Uninteresting objects denote not just themselves but also their entire
  163. * reachable chain, back until the merge base of an uninteresting commit and
  164. * an otherwise interesting commit.
  165. * <p>
  166. * Callers are encouraged to use {@link RevWalk#parseAny(AnyObjectId)}
  167. * instead of {@link RevWalk#lookupAny(AnyObjectId, int)}, as this method
  168. * requires the object to be parsed before it can be added as a root for the
  169. * traversal.
  170. * <p>
  171. * The method will automatically parse an unparsed object, but error
  172. * handling may be more difficult for the application to explain why a
  173. * RevObject is not actually valid. The object pool of this walker would
  174. * also be 'poisoned' by the invalid RevObject.
  175. * <p>
  176. * This method will automatically call {@link RevWalk#markStart(RevCommit)}
  177. * if passed RevCommit instance, or a RevTag that directly (or indirectly)
  178. * references a RevCommit.
  179. *
  180. * @param o
  181. * the object to start traversing from. The object passed must be
  182. * @throws MissingObjectException
  183. * the object supplied is not available from the object
  184. * database. This usually indicates the supplied object is
  185. * invalid, but the reference was constructed during an earlier
  186. * invocation to {@link RevWalk#lookupAny(AnyObjectId, int)}.
  187. * @throws IncorrectObjectTypeException
  188. * the object was not parsed yet and it was discovered during
  189. * parsing that it is not actually the type of the instance
  190. * passed in. This usually indicates the caller used the wrong
  191. * type in a {@link RevWalk#lookupAny(AnyObjectId, int)} call.
  192. * @throws IOException
  193. * a pack file or loose object could not be read.
  194. */
  195. public void markUninteresting(RevObject o) throws MissingObjectException,
  196. IncorrectObjectTypeException, IOException {
  197. while (o instanceof RevTag) {
  198. o.flags |= UNINTERESTING;
  199. if (hasRevSort(RevSort.BOUNDARY))
  200. addObject(o);
  201. o = ((RevTag) o).getObject();
  202. parseHeaders(o);
  203. }
  204. if (o instanceof RevCommit)
  205. super.markUninteresting((RevCommit) o);
  206. else if (o instanceof RevTree)
  207. markTreeUninteresting((RevTree) o);
  208. else
  209. o.flags |= UNINTERESTING;
  210. if (o.getType() != Constants.OBJ_COMMIT && hasRevSort(RevSort.BOUNDARY)) {
  211. addObject(o);
  212. }
  213. }
  214. @Override
  215. public RevCommit next() throws MissingObjectException,
  216. IncorrectObjectTypeException, IOException {
  217. for (;;) {
  218. final RevCommit r = super.next();
  219. if (r == null)
  220. return null;
  221. if ((r.flags & UNINTERESTING) != 0) {
  222. markTreeUninteresting(r.getTree());
  223. if (hasRevSort(RevSort.BOUNDARY))
  224. return r;
  225. continue;
  226. }
  227. if (firstCommit == null)
  228. firstCommit = r;
  229. lastCommit = r;
  230. pendingObjects.add(r.getTree());
  231. return r;
  232. }
  233. }
  234. /**
  235. * Pop the next most recent object.
  236. *
  237. * @return next most recent object; null if traversal is over.
  238. * @throws MissingObjectException
  239. * one or or more of the next objects are not available from the
  240. * object database, but were thought to be candidates for
  241. * traversal. This usually indicates a broken link.
  242. * @throws IncorrectObjectTypeException
  243. * one or or more of the objects in a tree do not match the type
  244. * indicated.
  245. * @throws IOException
  246. * a pack file or loose object could not be read.
  247. */
  248. public RevObject nextObject() throws MissingObjectException,
  249. IncorrectObjectTypeException, IOException {
  250. if (last != null)
  251. treeWalk = last instanceof RevTree ? enter(last) : treeWalk.next();
  252. while (!treeWalk.eof()) {
  253. final FileMode mode = treeWalk.getEntryFileMode();
  254. switch (mode.getObjectType()) {
  255. case Constants.OBJ_BLOB: {
  256. treeWalk.getEntryObjectId(idBuffer);
  257. final RevBlob o = lookupBlob(idBuffer);
  258. if ((o.flags & SEEN) != 0)
  259. break;
  260. o.flags |= SEEN;
  261. if (shouldSkipObject(o))
  262. break;
  263. last = o;
  264. return o;
  265. }
  266. case Constants.OBJ_TREE: {
  267. treeWalk.getEntryObjectId(idBuffer);
  268. final RevTree o = lookupTree(idBuffer);
  269. if ((o.flags & SEEN) != 0)
  270. break;
  271. o.flags |= SEEN;
  272. if (shouldSkipObject(o))
  273. break;
  274. last = o;
  275. return o;
  276. }
  277. default:
  278. if (FileMode.GITLINK.equals(mode))
  279. break;
  280. treeWalk.getEntryObjectId(idBuffer);
  281. throw new CorruptObjectException(MessageFormat.format(JGitText.get().corruptObjectInvalidMode3
  282. , mode , idBuffer.name() , treeWalk.getEntryPathString() , currentTree.name()));
  283. }
  284. treeWalk = treeWalk.next();
  285. }
  286. if (firstCommit != null) {
  287. reader.walkAdviceBeginTrees(this, firstCommit, lastCommit);
  288. firstCommit = null;
  289. lastCommit = null;
  290. }
  291. last = null;
  292. for (;;) {
  293. final RevObject o = pendingObjects.next();
  294. if (o == null) {
  295. reader.walkAdviceEnd();
  296. return null;
  297. }
  298. if ((o.flags & SEEN) != 0)
  299. continue;
  300. o.flags |= SEEN;
  301. if (shouldSkipObject(o))
  302. continue;
  303. if (o instanceof RevTree) {
  304. currentTree = (RevTree) o;
  305. treeWalk = treeWalk.resetRoot(reader, currentTree);
  306. }
  307. return o;
  308. }
  309. }
  310. private CanonicalTreeParser enter(RevObject tree) throws IOException {
  311. CanonicalTreeParser p = treeWalk.createSubtreeIterator0(reader, tree);
  312. if (p.eof()) {
  313. // We can't tolerate the subtree being an empty tree, as
  314. // that will break us out early before we visit all names.
  315. // If it is, advance to the parent's next record.
  316. //
  317. return treeWalk.next();
  318. }
  319. return p;
  320. }
  321. private final boolean shouldSkipObject(final RevObject o) {
  322. return (o.flags & UNINTERESTING) != 0 && !hasRevSort(RevSort.BOUNDARY);
  323. }
  324. /**
  325. * Verify all interesting objects are available, and reachable.
  326. * <p>
  327. * Callers should populate starting points and ending points with
  328. * {@link #markStart(RevObject)} and {@link #markUninteresting(RevObject)}
  329. * and then use this method to verify all objects between those two points
  330. * exist in the repository and are readable.
  331. * <p>
  332. * This method returns successfully if everything is connected; it throws an
  333. * exception if there is a connectivity problem. The exception message
  334. * provides some detail about the connectivity failure.
  335. *
  336. * @throws MissingObjectException
  337. * one or or more of the next objects are not available from the
  338. * object database, but were thought to be candidates for
  339. * traversal. This usually indicates a broken link.
  340. * @throws IncorrectObjectTypeException
  341. * one or or more of the objects in a tree do not match the type
  342. * indicated.
  343. * @throws IOException
  344. * a pack file or loose object could not be read.
  345. */
  346. public void checkConnectivity() throws MissingObjectException,
  347. IncorrectObjectTypeException, IOException {
  348. for (;;) {
  349. final RevCommit c = next();
  350. if (c == null)
  351. break;
  352. }
  353. for (;;) {
  354. final RevObject o = nextObject();
  355. if (o == null)
  356. break;
  357. if (o instanceof RevBlob && !reader.has(o))
  358. throw new MissingObjectException(o, Constants.TYPE_BLOB);
  359. }
  360. }
  361. /**
  362. * Get the current object's complete path.
  363. * <p>
  364. * This method is not very efficient and is primarily meant for debugging
  365. * and final output generation. Applications should try to avoid calling it,
  366. * and if invoked do so only once per interesting entry, where the name is
  367. * absolutely required for correct function.
  368. *
  369. * @return complete path of the current entry, from the root of the
  370. * repository. If the current entry is in a subtree there will be at
  371. * least one '/' in the returned string. Null if the current entry
  372. * has no path, such as for annotated tags or root level trees.
  373. */
  374. public String getPathString() {
  375. return last != null ? treeWalk.getEntryPathString() : null;
  376. }
  377. /**
  378. * Get the current object's path hash code.
  379. * <p>
  380. * This method computes a hash code on the fly for this path, the hash is
  381. * suitable to cluster objects that may have similar paths together.
  382. *
  383. * @return path hash code; any integer may be returned.
  384. */
  385. public int getPathHashCode() {
  386. return last != null ? treeWalk.getEntryPathHashCode() : 0;
  387. }
  388. /** @return the internal buffer holding the current path. */
  389. public byte[] getPathBuffer() {
  390. return last != null ? treeWalk.getEntryPathBuffer() : EMPTY_PATH;
  391. }
  392. /** @return length of the path in {@link #getPathBuffer()}. */
  393. public int getPathLength() {
  394. return last != null ? treeWalk.getEntryPathLength() : 0;
  395. }
  396. @Override
  397. public void dispose() {
  398. super.dispose();
  399. pendingObjects = new BlockObjQueue();
  400. treeWalk = new CanonicalTreeParser();
  401. currentTree = null;
  402. last = null;
  403. firstCommit = null;
  404. lastCommit = null;
  405. }
  406. @Override
  407. protected void reset(final int retainFlags) {
  408. super.reset(retainFlags);
  409. for (RevObject obj : rootObjects)
  410. obj.flags &= ~IN_PENDING;
  411. rootObjects = new ArrayList<RevObject>();
  412. pendingObjects = new BlockObjQueue();
  413. treeWalk = new CanonicalTreeParser();
  414. currentTree = null;
  415. last = null;
  416. firstCommit = null;
  417. lastCommit = null;
  418. }
  419. private void addObject(final RevObject o) {
  420. if ((o.flags & IN_PENDING) == 0) {
  421. o.flags |= IN_PENDING;
  422. rootObjects.add(o);
  423. pendingObjects.add(o);
  424. }
  425. }
  426. private void markTreeUninteresting(final RevTree tree)
  427. throws MissingObjectException, IncorrectObjectTypeException,
  428. IOException {
  429. if ((tree.flags & UNINTERESTING) != 0)
  430. return;
  431. tree.flags |= UNINTERESTING;
  432. treeWalk = treeWalk.resetRoot(reader, tree);
  433. while (!treeWalk.eof()) {
  434. final FileMode mode = treeWalk.getEntryFileMode();
  435. final int sType = mode.getObjectType();
  436. switch (sType) {
  437. case Constants.OBJ_BLOB: {
  438. treeWalk.getEntryObjectId(idBuffer);
  439. lookupBlob(idBuffer).flags |= UNINTERESTING;
  440. break;
  441. }
  442. case Constants.OBJ_TREE: {
  443. treeWalk.getEntryObjectId(idBuffer);
  444. final RevTree t = lookupTree(idBuffer);
  445. if ((t.flags & UNINTERESTING) == 0) {
  446. t.flags |= UNINTERESTING;
  447. treeWalk = treeWalk.createSubtreeIterator0(reader, t);
  448. continue;
  449. }
  450. break;
  451. }
  452. default:
  453. if (FileMode.GITLINK.equals(mode))
  454. break;
  455. treeWalk.getEntryObjectId(idBuffer);
  456. throw new CorruptObjectException(MessageFormat.format(JGitText.get().corruptObjectInvalidMode3
  457. , mode , idBuffer.name() , treeWalk.getEntryPathString() , tree));
  458. }
  459. treeWalk = treeWalk.next();
  460. }
  461. }
  462. }