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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813
  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(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 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 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 pushTree(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 pushTree(obj);
  363. if (boundary)
  364. return pushTree(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. // The previous while loop should have exhausted the stack
  394. // of trees.
  395. assert currVisit == null;
  396. pushTree(o);
  397. }
  398. return o;
  399. }
  400. }
  401. }
  402. private static int findObjectId(byte[] buf, int ptr) {
  403. // Skip over the mode and name until the NUL before the ObjectId
  404. // can be located. Skip the NUL as the function returns.
  405. for (;;) {
  406. if (buf[++ptr] == 0) return ++ptr;
  407. if (buf[++ptr] == 0) return ++ptr;
  408. if (buf[++ptr] == 0) return ++ptr;
  409. if (buf[++ptr] == 0) return ++ptr;
  410. if (buf[++ptr] == 0) return ++ptr;
  411. if (buf[++ptr] == 0) return ++ptr;
  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. }
  423. }
  424. private static int parseMode(byte[] buf, int startPtr, int recEndPtr, TreeVisit tv) {
  425. int mode = buf[startPtr] - '0';
  426. for (;;) {
  427. byte c = buf[++startPtr];
  428. if (' ' == c)
  429. break;
  430. mode <<= 3;
  431. mode += c - '0';
  432. c = buf[++startPtr];
  433. if (' ' == c)
  434. break;
  435. mode <<= 3;
  436. mode += c - '0';
  437. c = buf[++startPtr];
  438. if (' ' == c)
  439. break;
  440. mode <<= 3;
  441. mode += c - '0';
  442. c = buf[++startPtr];
  443. if (' ' == c)
  444. break;
  445. mode <<= 3;
  446. mode += c - '0';
  447. c = buf[++startPtr];
  448. if (' ' == c)
  449. break;
  450. mode <<= 3;
  451. mode += c - '0';
  452. c = buf[++startPtr];
  453. if (' ' == c)
  454. break;
  455. mode <<= 3;
  456. mode += c - '0';
  457. c = buf[++startPtr];
  458. if (' ' == c)
  459. break;
  460. mode <<= 3;
  461. mode += c - '0';
  462. }
  463. tv.ptr = recEndPtr;
  464. tv.namePtr = startPtr + 1;
  465. tv.nameEnd = recEndPtr - (ID_SZ + 1);
  466. return mode;
  467. }
  468. /**
  469. * Verify all interesting objects are available, and reachable.
  470. * <p>
  471. * Callers should populate starting points and ending points with
  472. * {@link #markStart(RevObject)} and {@link #markUninteresting(RevObject)}
  473. * and then use this method to verify all objects between those two points
  474. * exist in the repository and are readable.
  475. * <p>
  476. * This method returns successfully if everything is connected; it throws an
  477. * exception if there is a connectivity problem. The exception message
  478. * provides some detail about the connectivity failure.
  479. *
  480. * @throws org.eclipse.jgit.errors.MissingObjectException
  481. * one or more of the next objects are not available from the
  482. * object database, but were thought to be candidates for
  483. * traversal. This usually indicates a broken link.
  484. * @throws org.eclipse.jgit.errors.IncorrectObjectTypeException
  485. * one or more of the objects in a tree do not match the type
  486. * indicated.
  487. * @throws java.io.IOException
  488. * a pack file or loose object could not be read.
  489. */
  490. public void checkConnectivity() throws MissingObjectException,
  491. IncorrectObjectTypeException, IOException {
  492. for (;;) {
  493. final RevCommit c = next();
  494. if (c == null)
  495. break;
  496. }
  497. for (;;) {
  498. final RevObject o = nextObject();
  499. if (o == null)
  500. break;
  501. if (o instanceof RevBlob && !reader.has(o))
  502. throw new MissingObjectException(o, OBJ_BLOB);
  503. }
  504. }
  505. /**
  506. * Get the current object's complete path.
  507. * <p>
  508. * This method is not very efficient and is primarily meant for debugging
  509. * and final output generation. Applications should try to avoid calling it,
  510. * and if invoked do so only once per interesting entry, where the name is
  511. * absolutely required for correct function.
  512. *
  513. * @return complete path of the current entry, from the root of the
  514. * repository. If the current entry is in a subtree there will be at
  515. * least one '/' in the returned string. Null if the current entry
  516. * has no path, such as for annotated tags or root level trees.
  517. */
  518. public String getPathString() {
  519. if (pathLen == 0) {
  520. pathLen = updatePathBuf(currVisit);
  521. if (pathLen == 0)
  522. return null;
  523. }
  524. return RawParseUtils.decode(pathBuf, 0, pathLen);
  525. }
  526. /**
  527. * Get the current object's path hash code.
  528. * <p>
  529. * This method computes a hash code on the fly for this path, the hash is
  530. * suitable to cluster objects that may have similar paths together.
  531. *
  532. * @return path hash code; any integer may be returned.
  533. */
  534. public int getPathHashCode() {
  535. TreeVisit tv = currVisit;
  536. if (tv == null)
  537. return 0;
  538. int nameEnd = tv.nameEnd;
  539. if (nameEnd == 0) {
  540. // When nameEnd == 0 the subtree is itself the current path
  541. // being visited. The name hash must be obtained from its
  542. // parent tree. If there is no parent, this is a root tree with
  543. // a hash code of 0.
  544. tv = tv.parent;
  545. if (tv == null)
  546. return 0;
  547. nameEnd = tv.nameEnd;
  548. }
  549. byte[] buf;
  550. int ptr;
  551. if (16 <= (nameEnd - tv.namePtr)) {
  552. buf = tv.buf;
  553. ptr = nameEnd - 16;
  554. } else {
  555. nameEnd = pathLen;
  556. if (nameEnd == 0) {
  557. nameEnd = updatePathBuf(currVisit);
  558. pathLen = nameEnd;
  559. }
  560. buf = pathBuf;
  561. ptr = Math.max(0, nameEnd - 16);
  562. }
  563. int hash = 0;
  564. for (; ptr < nameEnd; ptr++) {
  565. byte c = buf[ptr];
  566. if (c != ' ')
  567. hash = (hash >>> 2) + (c << 24);
  568. }
  569. return hash;
  570. }
  571. /**
  572. * Get the internal buffer holding the current path.
  573. *
  574. * @return the internal buffer holding the current path.
  575. */
  576. public byte[] getPathBuffer() {
  577. if (pathLen == 0)
  578. pathLen = updatePathBuf(currVisit);
  579. return pathBuf;
  580. }
  581. /**
  582. * Get length of the path in {@link #getPathBuffer()}.
  583. *
  584. * @return length of the path in {@link #getPathBuffer()}.
  585. */
  586. public int getPathLength() {
  587. if (pathLen == 0)
  588. pathLen = updatePathBuf(currVisit);
  589. return pathLen;
  590. }
  591. private int updatePathBuf(TreeVisit tv) {
  592. if (tv == null)
  593. return 0;
  594. // If nameEnd == 0 this tree has not yet contributed an entry.
  595. // Update only for the parent, which if null will be empty.
  596. int nameEnd = tv.nameEnd;
  597. if (nameEnd == 0)
  598. return updatePathBuf(tv.parent);
  599. int ptr = tv.pathLen;
  600. if (ptr == 0) {
  601. ptr = updatePathBuf(tv.parent);
  602. if (ptr == pathBuf.length)
  603. growPathBuf(ptr);
  604. if (ptr != 0)
  605. pathBuf[ptr++] = '/';
  606. tv.pathLen = ptr;
  607. }
  608. int namePtr = tv.namePtr;
  609. int nameLen = nameEnd - namePtr;
  610. int end = ptr + nameLen;
  611. while (pathBuf.length < end)
  612. growPathBuf(ptr);
  613. System.arraycopy(tv.buf, namePtr, pathBuf, ptr, nameLen);
  614. return end;
  615. }
  616. private void growPathBuf(int ptr) {
  617. byte[] newBuf = new byte[pathBuf.length << 1];
  618. System.arraycopy(pathBuf, 0, newBuf, 0, ptr);
  619. pathBuf = newBuf;
  620. }
  621. /** {@inheritDoc} */
  622. @Override
  623. public void dispose() {
  624. super.dispose();
  625. pendingObjects = new BlockObjQueue();
  626. currVisit = null;
  627. freeVisit = null;
  628. }
  629. /** {@inheritDoc} */
  630. @Override
  631. protected void reset(int retainFlags) {
  632. super.reset(retainFlags);
  633. for (RevObject obj : rootObjects)
  634. obj.flags &= ~IN_PENDING;
  635. rootObjects = new ArrayList<>();
  636. pendingObjects = new BlockObjQueue();
  637. currVisit = null;
  638. freeVisit = null;
  639. }
  640. private void addObject(RevObject o) {
  641. if ((o.flags & IN_PENDING) == 0) {
  642. o.flags |= IN_PENDING;
  643. rootObjects.add(o);
  644. pendingObjects.add(o);
  645. }
  646. }
  647. private void markTreeUninteresting(RevTree tree)
  648. throws MissingObjectException, IncorrectObjectTypeException,
  649. IOException {
  650. if ((tree.flags & UNINTERESTING) != 0)
  651. return;
  652. tree.flags |= UNINTERESTING;
  653. byte[] raw = reader.open(tree, OBJ_TREE).getCachedBytes();
  654. for (int ptr = 0; ptr < raw.length;) {
  655. byte c = raw[ptr];
  656. int mode = c - '0';
  657. for (;;) {
  658. c = raw[++ptr];
  659. if (' ' == c)
  660. break;
  661. mode <<= 3;
  662. mode += c - '0';
  663. }
  664. while (raw[++ptr] != 0) {
  665. // Skip entry name.
  666. }
  667. ptr++; // Skip NUL after entry name.
  668. switch (mode >>> TYPE_SHIFT) {
  669. case TYPE_FILE:
  670. case TYPE_SYMLINK:
  671. idBuffer.fromRaw(raw, ptr);
  672. lookupBlob(idBuffer).flags |= UNINTERESTING;
  673. break;
  674. case TYPE_TREE:
  675. idBuffer.fromRaw(raw, ptr);
  676. markTreeUninteresting(lookupTree(idBuffer));
  677. break;
  678. case TYPE_GITLINK:
  679. break;
  680. default:
  681. idBuffer.fromRaw(raw, ptr);
  682. throw new CorruptObjectException(MessageFormat.format(
  683. JGitText.get().corruptObjectInvalidMode3,
  684. String.format("%o", Integer.valueOf(mode)), //$NON-NLS-1$
  685. idBuffer.name(), "", tree)); //$NON-NLS-1$
  686. }
  687. ptr += ID_SZ;
  688. }
  689. }
  690. private RevObject pushTree(RevObject obj) throws LargeObjectException,
  691. MissingObjectException, IncorrectObjectTypeException, IOException {
  692. TreeVisit tv = freeVisit;
  693. if (tv != null) {
  694. freeVisit = tv.parent;
  695. tv.ptr = 0;
  696. tv.namePtr = 0;
  697. tv.nameEnd = 0;
  698. tv.pathLen = 0;
  699. } else {
  700. tv = new TreeVisit();
  701. }
  702. tv.obj = obj;
  703. tv.buf = reader.open(obj, OBJ_TREE).getCachedBytes();
  704. tv.parent = currVisit;
  705. currVisit = tv;
  706. return obj;
  707. }
  708. private void releaseTreeVisit(TreeVisit tv) {
  709. tv.buf = null;
  710. tv.parent = freeVisit;
  711. freeVisit = tv;
  712. }
  713. private static class TreeVisit {
  714. /** Parent tree visit that entered this tree, null if root tree. */
  715. TreeVisit parent;
  716. /** The RevTree currently being iterated through. */
  717. RevObject obj;
  718. /** Canonical encoding of the tree named by {@link #obj}. */
  719. byte[] buf;
  720. /** Index of next entry to parse in {@link #buf}. */
  721. int ptr;
  722. /** Start of the current name entry in {@link #buf}. */
  723. int namePtr;
  724. /** One past end of name, {@code nameEnd - namePtr} is the length. */
  725. int nameEnd;
  726. /** Number of bytes in the path leading up to this tree. */
  727. int pathLen;
  728. }
  729. }