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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  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 static org.eclipse.jgit.lib.Constants.OBJ_BLOB;
  45. import static org.eclipse.jgit.lib.Constants.OBJ_COMMIT;
  46. import static org.eclipse.jgit.lib.Constants.OBJ_TREE;
  47. import java.io.IOException;
  48. import java.text.MessageFormat;
  49. import java.util.ArrayList;
  50. import java.util.List;
  51. import org.eclipse.jgit.errors.CorruptObjectException;
  52. import org.eclipse.jgit.errors.IncorrectObjectTypeException;
  53. import org.eclipse.jgit.errors.LargeObjectException;
  54. import org.eclipse.jgit.errors.MissingObjectException;
  55. import org.eclipse.jgit.internal.JGitText;
  56. import org.eclipse.jgit.lib.AnyObjectId;
  57. import org.eclipse.jgit.lib.ObjectReader;
  58. import org.eclipse.jgit.lib.Repository;
  59. import org.eclipse.jgit.revwalk.filter.ObjectFilter;
  60. import org.eclipse.jgit.util.RawParseUtils;
  61. /**
  62. * Specialized subclass of RevWalk to include trees, blobs and tags.
  63. * <p>
  64. * Unlike RevWalk this subclass is able to remember starting roots that include
  65. * annotated tags, or arbitrary trees or blobs. Once commit generation is
  66. * complete and all commits have been popped by the application, individual
  67. * annotated tag, tree and blob objects can be popped through the additional
  68. * method {@link #nextObject()}.
  69. * <p>
  70. * Tree and blob objects reachable from interesting commits are automatically
  71. * scheduled for inclusion in the results of {@link #nextObject()}, returning
  72. * each object exactly once. Objects are sorted and returned according to the
  73. * the commits that reference them and the order they appear within a tree.
  74. * Ordering can be affected by changing the
  75. * {@link org.eclipse.jgit.revwalk.RevSort} used to order the commits that are
  76. * returned first.
  77. */
  78. public class ObjectWalk extends RevWalk {
  79. private static final int ID_SZ = 20;
  80. private static final int TYPE_SHIFT = 12;
  81. private static final int TYPE_TREE = 0040000 >>> TYPE_SHIFT;
  82. private static final int TYPE_SYMLINK = 0120000 >>> TYPE_SHIFT;
  83. private static final int TYPE_FILE = 0100000 >>> TYPE_SHIFT;
  84. private static final int TYPE_GITLINK = 0160000 >>> TYPE_SHIFT;
  85. /**
  86. * Indicates a non-RevCommit is in {@link #pendingObjects}.
  87. * <p>
  88. * We can safely reuse {@link RevWalk#REWRITE} here for the same value as it
  89. * is only set on RevCommit and {@link #pendingObjects} never has RevCommit
  90. * instances inserted into it.
  91. */
  92. private static final int IN_PENDING = RevWalk.REWRITE;
  93. private List<RevObject> rootObjects;
  94. private BlockObjQueue pendingObjects;
  95. private ObjectFilter objectFilter;
  96. private TreeVisit freeVisit;
  97. private TreeVisit currVisit;
  98. private byte[] pathBuf;
  99. private int pathLen;
  100. private boolean boundary;
  101. /**
  102. * Create a new revision and object walker for a given repository.
  103. *
  104. * @param repo
  105. * the repository the walker will obtain data from.
  106. */
  107. public ObjectWalk(final Repository repo) {
  108. this(repo.newObjectReader());
  109. }
  110. /**
  111. * Create a new revision and object walker for a given repository.
  112. *
  113. * @param or
  114. * the reader the walker will obtain data from. The reader should
  115. * be released by the caller when the walker is no longer
  116. * required.
  117. */
  118. public ObjectWalk(ObjectReader or) {
  119. super(or);
  120. setRetainBody(false);
  121. rootObjects = new ArrayList<>();
  122. pendingObjects = new BlockObjQueue();
  123. objectFilter = ObjectFilter.ALL;
  124. pathBuf = new byte[256];
  125. }
  126. /**
  127. * Mark an object or commit to start graph traversal from.
  128. * <p>
  129. * Callers are encouraged to use
  130. * {@link org.eclipse.jgit.revwalk.RevWalk#parseAny(AnyObjectId)} instead of
  131. * {@link org.eclipse.jgit.revwalk.RevWalk#lookupAny(AnyObjectId, int)}, as
  132. * this method requires the object to be parsed before it can be added as a
  133. * root for the traversal.
  134. * <p>
  135. * The method will automatically parse an unparsed object, but error
  136. * handling may be more difficult for the application to explain why a
  137. * RevObject is not actually valid. The object pool of this walker would
  138. * also be 'poisoned' by the invalid RevObject.
  139. * <p>
  140. * This method will automatically call
  141. * {@link org.eclipse.jgit.revwalk.RevWalk#markStart(RevCommit)} if passed
  142. * RevCommit instance, or a RevTag that directly (or indirectly) references
  143. * a RevCommit.
  144. *
  145. * @param o
  146. * the object to start traversing from. The object passed must be
  147. * from this same revision walker.
  148. * @throws org.eclipse.jgit.errors.MissingObjectException
  149. * the object supplied is not available from the object
  150. * database. This usually indicates the supplied object is
  151. * invalid, but the reference was constructed during an earlier
  152. * invocation to
  153. * {@link org.eclipse.jgit.revwalk.RevWalk#lookupAny(AnyObjectId, int)}.
  154. * @throws org.eclipse.jgit.errors.IncorrectObjectTypeException
  155. * the object was not parsed yet and it was discovered during
  156. * parsing that it is not actually the type of the instance
  157. * passed in. This usually indicates the caller used the wrong
  158. * type in a
  159. * {@link org.eclipse.jgit.revwalk.RevWalk#lookupAny(AnyObjectId, int)}
  160. * call.
  161. * @throws java.io.IOException
  162. * a pack file or loose object could not be read.
  163. */
  164. public void markStart(RevObject o) throws MissingObjectException,
  165. IncorrectObjectTypeException, IOException {
  166. while (o instanceof RevTag) {
  167. addObject(o);
  168. o = ((RevTag) o).getObject();
  169. parseHeaders(o);
  170. }
  171. if (o instanceof RevCommit)
  172. super.markStart((RevCommit) o);
  173. else
  174. addObject(o);
  175. }
  176. /**
  177. * Mark an object to not produce in the output.
  178. * <p>
  179. * Uninteresting objects denote not just themselves but also their entire
  180. * reachable chain, back until the merge base of an uninteresting commit and
  181. * an otherwise interesting commit.
  182. * <p>
  183. * Callers are encouraged to use
  184. * {@link org.eclipse.jgit.revwalk.RevWalk#parseAny(AnyObjectId)} instead of
  185. * {@link org.eclipse.jgit.revwalk.RevWalk#lookupAny(AnyObjectId, int)}, as
  186. * this method requires the object to be parsed before it can be added as a
  187. * root for the traversal.
  188. * <p>
  189. * The method will automatically parse an unparsed object, but error
  190. * handling may be more difficult for the application to explain why a
  191. * RevObject is not actually valid. The object pool of this walker would
  192. * also be 'poisoned' by the invalid RevObject.
  193. * <p>
  194. * This method will automatically call
  195. * {@link org.eclipse.jgit.revwalk.RevWalk#markStart(RevCommit)} if passed
  196. * RevCommit instance, or a RevTag that directly (or indirectly) references
  197. * a RevCommit.
  198. *
  199. * @param o
  200. * the object to start traversing from. The object passed must be
  201. * @throws org.eclipse.jgit.errors.MissingObjectException
  202. * the object supplied is not available from the object
  203. * database. This usually indicates the supplied object is
  204. * invalid, but the reference was constructed during an earlier
  205. * invocation to
  206. * {@link org.eclipse.jgit.revwalk.RevWalk#lookupAny(AnyObjectId, int)}.
  207. * @throws org.eclipse.jgit.errors.IncorrectObjectTypeException
  208. * the object was not parsed yet and it was discovered during
  209. * parsing that it is not actually the type of the instance
  210. * passed in. This usually indicates the caller used the wrong
  211. * type in a
  212. * {@link org.eclipse.jgit.revwalk.RevWalk#lookupAny(AnyObjectId, int)}
  213. * call.
  214. * @throws java.io.IOException
  215. * a pack file or loose object could not be read.
  216. */
  217. public void markUninteresting(RevObject o) throws MissingObjectException,
  218. IncorrectObjectTypeException, IOException {
  219. while (o instanceof RevTag) {
  220. o.flags |= UNINTERESTING;
  221. if (boundary)
  222. addObject(o);
  223. o = ((RevTag) o).getObject();
  224. parseHeaders(o);
  225. }
  226. if (o instanceof RevCommit)
  227. super.markUninteresting((RevCommit) o);
  228. else if (o instanceof RevTree)
  229. markTreeUninteresting((RevTree) o);
  230. else
  231. o.flags |= UNINTERESTING;
  232. if (o.getType() != OBJ_COMMIT && boundary)
  233. addObject(o);
  234. }
  235. /** {@inheritDoc} */
  236. @Override
  237. public void sort(RevSort s) {
  238. super.sort(s);
  239. boundary = hasRevSort(RevSort.BOUNDARY);
  240. }
  241. /** {@inheritDoc} */
  242. @Override
  243. public void sort(RevSort s, boolean use) {
  244. super.sort(s, use);
  245. boundary = hasRevSort(RevSort.BOUNDARY);
  246. }
  247. /**
  248. * Get the currently configured object filter.
  249. *
  250. * @return the current filter. Never null as a filter is always needed.
  251. * @since 4.0
  252. */
  253. public ObjectFilter getObjectFilter() {
  254. return objectFilter;
  255. }
  256. /**
  257. * Set the object filter for this walker. This filter affects the objects
  258. * visited by {@link #nextObject()}. It does not affect the commits listed
  259. * by {@link #next()}.
  260. * <p>
  261. * If the filter returns false for an object, then that object is skipped
  262. * and objects reachable from it are not enqueued to be walked recursively.
  263. * This can be used to speed up the object walk by skipping subtrees that
  264. * are known to be uninteresting.
  265. *
  266. * @param newFilter
  267. * the new filter. If null the special
  268. * {@link org.eclipse.jgit.revwalk.filter.ObjectFilter#ALL}
  269. * filter will be used instead, as it matches every object.
  270. * @since 4.0
  271. */
  272. public void setObjectFilter(ObjectFilter newFilter) {
  273. assertNotStarted();
  274. objectFilter = newFilter != null ? newFilter : ObjectFilter.ALL;
  275. }
  276. /** {@inheritDoc} */
  277. @Override
  278. public RevCommit next() throws MissingObjectException,
  279. IncorrectObjectTypeException, IOException {
  280. for (;;) {
  281. final RevCommit r = super.next();
  282. if (r == null) {
  283. return null;
  284. }
  285. final RevTree t = r.getTree();
  286. if ((r.flags & UNINTERESTING) != 0) {
  287. if (objectFilter.include(this, t)) {
  288. markTreeUninteresting(t);
  289. }
  290. if (boundary) {
  291. return r;
  292. }
  293. continue;
  294. }
  295. if (objectFilter.include(this, t)) {
  296. pendingObjects.add(t);
  297. }
  298. return r;
  299. }
  300. }
  301. /**
  302. * Pop the next most recent object.
  303. *
  304. * @return next most recent object; null if traversal is over.
  305. * @throws org.eclipse.jgit.errors.MissingObjectException
  306. * one or or more of the next objects are not available from the
  307. * object database, but were thought to be candidates for
  308. * traversal. This usually indicates a broken link.
  309. * @throws org.eclipse.jgit.errors.IncorrectObjectTypeException
  310. * one or or more of the objects in a tree do not match the type
  311. * indicated.
  312. * @throws java.io.IOException
  313. * a pack file or loose object could not be read.
  314. */
  315. public RevObject nextObject() throws MissingObjectException,
  316. IncorrectObjectTypeException, IOException {
  317. pathLen = 0;
  318. TreeVisit tv = currVisit;
  319. while (tv != null) {
  320. byte[] buf = tv.buf;
  321. for (int ptr = tv.ptr; ptr < buf.length;) {
  322. int startPtr = ptr;
  323. ptr = findObjectId(buf, ptr);
  324. idBuffer.fromRaw(buf, ptr);
  325. ptr += ID_SZ;
  326. if (!objectFilter.include(this, idBuffer)) {
  327. continue;
  328. }
  329. RevObject obj = objects.get(idBuffer);
  330. if (obj != null && (obj.flags & SEEN) != 0)
  331. continue;
  332. int mode = parseMode(buf, startPtr, ptr, tv);
  333. int flags;
  334. switch (mode >>> TYPE_SHIFT) {
  335. case TYPE_FILE:
  336. case TYPE_SYMLINK:
  337. if (obj == null) {
  338. obj = new RevBlob(idBuffer);
  339. obj.flags = SEEN;
  340. objects.add(obj);
  341. return obj;
  342. }
  343. if (!(obj instanceof RevBlob))
  344. throw new IncorrectObjectTypeException(obj, OBJ_BLOB);
  345. obj.flags = flags = obj.flags | SEEN;
  346. if ((flags & UNINTERESTING) == 0)
  347. return obj;
  348. if (boundary)
  349. return obj;
  350. continue;
  351. case TYPE_TREE:
  352. if (obj == null) {
  353. obj = new RevTree(idBuffer);
  354. obj.flags = SEEN;
  355. objects.add(obj);
  356. return enterTree(obj);
  357. }
  358. if (!(obj instanceof RevTree))
  359. throw new IncorrectObjectTypeException(obj, OBJ_TREE);
  360. obj.flags = flags = obj.flags | SEEN;
  361. if ((flags & UNINTERESTING) == 0)
  362. return enterTree(obj);
  363. if (boundary)
  364. return enterTree(obj);
  365. continue;
  366. case TYPE_GITLINK:
  367. continue;
  368. default:
  369. throw new CorruptObjectException(MessageFormat.format(
  370. JGitText.get().corruptObjectInvalidMode3,
  371. String.format("%o", Integer.valueOf(mode)), //$NON-NLS-1$
  372. idBuffer.name(),
  373. RawParseUtils.decode(buf, tv.namePtr, tv.nameEnd),
  374. tv.obj));
  375. }
  376. }
  377. currVisit = tv.parent;
  378. releaseTreeVisit(tv);
  379. tv = currVisit;
  380. }
  381. for (;;) {
  382. RevObject o = pendingObjects.next();
  383. if (o == null) {
  384. return null;
  385. }
  386. int flags = o.flags;
  387. if ((flags & SEEN) != 0)
  388. continue;
  389. flags |= SEEN;
  390. o.flags = flags;
  391. if ((flags & UNINTERESTING) == 0 | boundary) {
  392. if (o instanceof RevTree) {
  393. tv = newTreeVisit(o);
  394. tv.parent = null;
  395. currVisit = tv;
  396. }
  397. return o;
  398. }
  399. }
  400. }
  401. private RevObject enterTree(RevObject obj) throws MissingObjectException,
  402. IncorrectObjectTypeException, IOException {
  403. TreeVisit tv = newTreeVisit(obj);
  404. tv.parent = currVisit;
  405. currVisit = tv;
  406. return obj;
  407. }
  408. private static int findObjectId(byte[] buf, int ptr) {
  409. // Skip over the mode and name until the NUL before the ObjectId
  410. // can be located. Skip the NUL as the function returns.
  411. for (;;) {
  412. if (buf[++ptr] == 0) return ++ptr;
  413. if (buf[++ptr] == 0) return ++ptr;
  414. if (buf[++ptr] == 0) return ++ptr;
  415. if (buf[++ptr] == 0) return ++ptr;
  416. if (buf[++ptr] == 0) return ++ptr;
  417. if (buf[++ptr] == 0) return ++ptr;
  418. if (buf[++ptr] == 0) return ++ptr;
  419. if (buf[++ptr] == 0) return ++ptr;
  420. if (buf[++ptr] == 0) return ++ptr;
  421. if (buf[++ptr] == 0) return ++ptr;
  422. if (buf[++ptr] == 0) return ++ptr;
  423. if (buf[++ptr] == 0) return ++ptr;
  424. if (buf[++ptr] == 0) return ++ptr;
  425. if (buf[++ptr] == 0) return ++ptr;
  426. if (buf[++ptr] == 0) return ++ptr;
  427. if (buf[++ptr] == 0) return ++ptr;
  428. }
  429. }
  430. private static int parseMode(byte[] buf, int startPtr, int recEndPtr, TreeVisit tv) {
  431. int mode = buf[startPtr] - '0';
  432. for (;;) {
  433. byte c = buf[++startPtr];
  434. if (' ' == c)
  435. break;
  436. mode <<= 3;
  437. mode += c - '0';
  438. c = buf[++startPtr];
  439. if (' ' == c)
  440. break;
  441. mode <<= 3;
  442. mode += c - '0';
  443. c = buf[++startPtr];
  444. if (' ' == c)
  445. break;
  446. mode <<= 3;
  447. mode += c - '0';
  448. c = buf[++startPtr];
  449. if (' ' == c)
  450. break;
  451. mode <<= 3;
  452. mode += c - '0';
  453. c = buf[++startPtr];
  454. if (' ' == c)
  455. break;
  456. mode <<= 3;
  457. mode += c - '0';
  458. c = buf[++startPtr];
  459. if (' ' == c)
  460. break;
  461. mode <<= 3;
  462. mode += c - '0';
  463. c = buf[++startPtr];
  464. if (' ' == c)
  465. break;
  466. mode <<= 3;
  467. mode += c - '0';
  468. }
  469. tv.ptr = recEndPtr;
  470. tv.namePtr = startPtr + 1;
  471. tv.nameEnd = recEndPtr - (ID_SZ + 1);
  472. return mode;
  473. }
  474. /**
  475. * Verify all interesting objects are available, and reachable.
  476. * <p>
  477. * Callers should populate starting points and ending points with
  478. * {@link #markStart(RevObject)} and {@link #markUninteresting(RevObject)}
  479. * and then use this method to verify all objects between those two points
  480. * exist in the repository and are readable.
  481. * <p>
  482. * This method returns successfully if everything is connected; it throws an
  483. * exception if there is a connectivity problem. The exception message
  484. * provides some detail about the connectivity failure.
  485. *
  486. * @throws org.eclipse.jgit.errors.MissingObjectException
  487. * one or or more of the next objects are not available from the
  488. * object database, but were thought to be candidates for
  489. * traversal. This usually indicates a broken link.
  490. * @throws org.eclipse.jgit.errors.IncorrectObjectTypeException
  491. * one or or more of the objects in a tree do not match the type
  492. * indicated.
  493. * @throws java.io.IOException
  494. * a pack file or loose object could not be read.
  495. */
  496. public void checkConnectivity() throws MissingObjectException,
  497. IncorrectObjectTypeException, IOException {
  498. for (;;) {
  499. final RevCommit c = next();
  500. if (c == null)
  501. break;
  502. }
  503. for (;;) {
  504. final RevObject o = nextObject();
  505. if (o == null)
  506. break;
  507. if (o instanceof RevBlob && !reader.has(o))
  508. throw new MissingObjectException(o, OBJ_BLOB);
  509. }
  510. }
  511. /**
  512. * Get the current object's complete path.
  513. * <p>
  514. * This method is not very efficient and is primarily meant for debugging
  515. * and final output generation. Applications should try to avoid calling it,
  516. * and if invoked do so only once per interesting entry, where the name is
  517. * absolutely required for correct function.
  518. *
  519. * @return complete path of the current entry, from the root of the
  520. * repository. If the current entry is in a subtree there will be at
  521. * least one '/' in the returned string. Null if the current entry
  522. * has no path, such as for annotated tags or root level trees.
  523. */
  524. public String getPathString() {
  525. if (pathLen == 0) {
  526. pathLen = updatePathBuf(currVisit);
  527. if (pathLen == 0)
  528. return null;
  529. }
  530. return RawParseUtils.decode(pathBuf, 0, pathLen);
  531. }
  532. /**
  533. * Get the current object's path hash code.
  534. * <p>
  535. * This method computes a hash code on the fly for this path, the hash is
  536. * suitable to cluster objects that may have similar paths together.
  537. *
  538. * @return path hash code; any integer may be returned.
  539. */
  540. public int getPathHashCode() {
  541. TreeVisit tv = currVisit;
  542. if (tv == null)
  543. return 0;
  544. int nameEnd = tv.nameEnd;
  545. if (nameEnd == 0) {
  546. // When nameEnd == 0 the subtree is itself the current path
  547. // being visited. The name hash must be obtained from its
  548. // parent tree. If there is no parent, this is a root tree with
  549. // a hash code of 0.
  550. tv = tv.parent;
  551. if (tv == null)
  552. return 0;
  553. nameEnd = tv.nameEnd;
  554. }
  555. byte[] buf;
  556. int ptr;
  557. if (16 <= (nameEnd - tv.namePtr)) {
  558. buf = tv.buf;
  559. ptr = nameEnd - 16;
  560. } else {
  561. nameEnd = pathLen;
  562. if (nameEnd == 0) {
  563. nameEnd = updatePathBuf(currVisit);
  564. pathLen = nameEnd;
  565. }
  566. buf = pathBuf;
  567. ptr = Math.max(0, nameEnd - 16);
  568. }
  569. int hash = 0;
  570. for (; ptr < nameEnd; ptr++) {
  571. byte c = buf[ptr];
  572. if (c != ' ')
  573. hash = (hash >>> 2) + (c << 24);
  574. }
  575. return hash;
  576. }
  577. /**
  578. * Get the internal buffer holding the current path.
  579. *
  580. * @return the internal buffer holding the current path.
  581. */
  582. public byte[] getPathBuffer() {
  583. if (pathLen == 0)
  584. pathLen = updatePathBuf(currVisit);
  585. return pathBuf;
  586. }
  587. /**
  588. * Get length of the path in {@link #getPathBuffer()}.
  589. *
  590. * @return length of the path in {@link #getPathBuffer()}.
  591. */
  592. public int getPathLength() {
  593. if (pathLen == 0)
  594. pathLen = updatePathBuf(currVisit);
  595. return pathLen;
  596. }
  597. private int updatePathBuf(TreeVisit tv) {
  598. if (tv == null)
  599. return 0;
  600. // If nameEnd == 0 this tree has not yet contributed an entry.
  601. // Update only for the parent, which if null will be empty.
  602. int nameEnd = tv.nameEnd;
  603. if (nameEnd == 0)
  604. return updatePathBuf(tv.parent);
  605. int ptr = tv.pathLen;
  606. if (ptr == 0) {
  607. ptr = updatePathBuf(tv.parent);
  608. if (ptr == pathBuf.length)
  609. growPathBuf(ptr);
  610. if (ptr != 0)
  611. pathBuf[ptr++] = '/';
  612. tv.pathLen = ptr;
  613. }
  614. int namePtr = tv.namePtr;
  615. int nameLen = nameEnd - namePtr;
  616. int end = ptr + nameLen;
  617. while (pathBuf.length < end)
  618. growPathBuf(ptr);
  619. System.arraycopy(tv.buf, namePtr, pathBuf, ptr, nameLen);
  620. return end;
  621. }
  622. private void growPathBuf(int ptr) {
  623. byte[] newBuf = new byte[pathBuf.length << 1];
  624. System.arraycopy(pathBuf, 0, newBuf, 0, ptr);
  625. pathBuf = newBuf;
  626. }
  627. /** {@inheritDoc} */
  628. @Override
  629. public void dispose() {
  630. super.dispose();
  631. pendingObjects = new BlockObjQueue();
  632. currVisit = null;
  633. freeVisit = null;
  634. }
  635. /** {@inheritDoc} */
  636. @Override
  637. protected void reset(final int retainFlags) {
  638. super.reset(retainFlags);
  639. for (RevObject obj : rootObjects)
  640. obj.flags &= ~IN_PENDING;
  641. rootObjects = new ArrayList<>();
  642. pendingObjects = new BlockObjQueue();
  643. currVisit = null;
  644. freeVisit = null;
  645. }
  646. private void addObject(final RevObject o) {
  647. if ((o.flags & IN_PENDING) == 0) {
  648. o.flags |= IN_PENDING;
  649. rootObjects.add(o);
  650. pendingObjects.add(o);
  651. }
  652. }
  653. private void markTreeUninteresting(final RevTree tree)
  654. throws MissingObjectException, IncorrectObjectTypeException,
  655. IOException {
  656. if ((tree.flags & UNINTERESTING) != 0)
  657. return;
  658. tree.flags |= UNINTERESTING;
  659. byte[] raw = reader.open(tree, OBJ_TREE).getCachedBytes();
  660. for (int ptr = 0; ptr < raw.length;) {
  661. byte c = raw[ptr];
  662. int mode = c - '0';
  663. for (;;) {
  664. c = raw[++ptr];
  665. if (' ' == c)
  666. break;
  667. mode <<= 3;
  668. mode += c - '0';
  669. }
  670. while (raw[++ptr] != 0) {
  671. // Skip entry name.
  672. }
  673. ptr++; // Skip NUL after entry name.
  674. switch (mode >>> TYPE_SHIFT) {
  675. case TYPE_FILE:
  676. case TYPE_SYMLINK:
  677. idBuffer.fromRaw(raw, ptr);
  678. lookupBlob(idBuffer).flags |= UNINTERESTING;
  679. break;
  680. case TYPE_TREE:
  681. idBuffer.fromRaw(raw, ptr);
  682. markTreeUninteresting(lookupTree(idBuffer));
  683. break;
  684. case TYPE_GITLINK:
  685. break;
  686. default:
  687. idBuffer.fromRaw(raw, ptr);
  688. throw new CorruptObjectException(MessageFormat.format(
  689. JGitText.get().corruptObjectInvalidMode3,
  690. String.format("%o", Integer.valueOf(mode)), //$NON-NLS-1$
  691. idBuffer.name(), "", tree)); //$NON-NLS-1$
  692. }
  693. ptr += ID_SZ;
  694. }
  695. }
  696. private TreeVisit newTreeVisit(RevObject obj) throws LargeObjectException,
  697. MissingObjectException, IncorrectObjectTypeException, IOException {
  698. TreeVisit tv = freeVisit;
  699. if (tv != null) {
  700. freeVisit = tv.parent;
  701. tv.ptr = 0;
  702. tv.namePtr = 0;
  703. tv.nameEnd = 0;
  704. tv.pathLen = 0;
  705. } else {
  706. tv = new TreeVisit();
  707. }
  708. tv.obj = obj;
  709. tv.buf = reader.open(obj, OBJ_TREE).getCachedBytes();
  710. return tv;
  711. }
  712. private void releaseTreeVisit(TreeVisit tv) {
  713. tv.buf = null;
  714. tv.parent = freeVisit;
  715. freeVisit = tv;
  716. }
  717. private static class TreeVisit {
  718. /** Parent tree visit that entered this tree, null if root tree. */
  719. TreeVisit parent;
  720. /** The RevTree currently being iterated through. */
  721. RevObject obj;
  722. /** Canonical encoding of the tree named by {@link #obj}. */
  723. byte[] buf;
  724. /** Index of next entry to parse in {@link #buf}. */
  725. int ptr;
  726. /** Start of the current name entry in {@link #buf}. */
  727. int namePtr;
  728. /** One past end of name, {@code nameEnd - namePtr} is the length. */
  729. int nameEnd;
  730. /** Number of bytes in the path leading up to this tree. */
  731. int pathLen;
  732. }
  733. }