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.

TreeWalk.java 31KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925
  1. /*
  2. * Copyright (C) 2008-2009, Google Inc.
  3. * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
  4. * and other copyright owners as documented in the project's IP log.
  5. *
  6. * This program and the accompanying materials are made available
  7. * under the terms of the Eclipse Distribution License v1.0 which
  8. * accompanies this distribution, is reproduced below, and is
  9. * available at http://www.eclipse.org/org/documents/edl-v10.php
  10. *
  11. * All rights reserved.
  12. *
  13. * Redistribution and use in source and binary forms, with or
  14. * without modification, are permitted provided that the following
  15. * conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above copyright
  18. * notice, this list of conditions and the following disclaimer.
  19. *
  20. * - Redistributions in binary form must reproduce the above
  21. * copyright notice, this list of conditions and the following
  22. * disclaimer in the documentation and/or other materials provided
  23. * with the distribution.
  24. *
  25. * - Neither the name of the Eclipse Foundation, Inc. nor the
  26. * names of its contributors may be used to endorse or promote
  27. * products derived from this software without specific prior
  28. * written permission.
  29. *
  30. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  31. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  32. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  33. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  34. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  35. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  36. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  37. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  38. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  39. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  40. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  41. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  42. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  43. */
  44. package org.eclipse.jgit.treewalk;
  45. import java.io.IOException;
  46. import java.util.Collections;
  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.errors.StopWalkException;
  51. import org.eclipse.jgit.lib.AnyObjectId;
  52. import org.eclipse.jgit.lib.Constants;
  53. import org.eclipse.jgit.lib.FileMode;
  54. import org.eclipse.jgit.lib.MutableObjectId;
  55. import org.eclipse.jgit.lib.ObjectId;
  56. import org.eclipse.jgit.lib.Repository;
  57. import org.eclipse.jgit.lib.WindowCursor;
  58. import org.eclipse.jgit.revwalk.RevTree;
  59. import org.eclipse.jgit.treewalk.filter.PathFilterGroup;
  60. import org.eclipse.jgit.treewalk.filter.TreeFilter;
  61. import org.eclipse.jgit.util.RawParseUtils;
  62. /**
  63. * Walks one or more {@link AbstractTreeIterator}s in parallel.
  64. * <p>
  65. * This class can perform n-way differences across as many trees as necessary.
  66. * <p>
  67. * Each tree added must have the same root as existing trees in the walk.
  68. * <p>
  69. * A TreeWalk instance can only be used once to generate results. Running a
  70. * second time requires creating a new TreeWalk instance, or invoking
  71. * {@link #reset()} and adding new trees before starting again. Resetting an
  72. * existing instance may be faster for some applications as some internal
  73. * buffers may be recycled.
  74. * <p>
  75. * TreeWalk instances are not thread-safe. Applications must either restrict
  76. * usage of a TreeWalk instance to a single thread, or implement their own
  77. * synchronization at a higher level.
  78. * <p>
  79. * Multiple simultaneous TreeWalk instances per {@link Repository} are
  80. * permitted, even from concurrent threads.
  81. */
  82. public class TreeWalk {
  83. /**
  84. * Open a tree walk and filter to exactly one path.
  85. * <p>
  86. * The returned tree walk is already positioned on the requested path, so
  87. * the caller should not need to invoke {@link #next()} unless they are
  88. * looking for a possible directory/file name conflict.
  89. *
  90. * @param db
  91. * repository to read tree object data from.
  92. * @param path
  93. * single path to advance the tree walk instance into.
  94. * @param trees
  95. * one or more trees to walk through, all with the same root.
  96. * @return a new tree walk configured for exactly this one path; null if no
  97. * path was found in any of the trees.
  98. * @throws IOException
  99. * reading a pack file or loose object failed.
  100. * @throws CorruptObjectException
  101. * an tree object could not be read as its data stream did not
  102. * appear to be a tree, or could not be inflated.
  103. * @throws IncorrectObjectTypeException
  104. * an object we expected to be a tree was not a tree.
  105. * @throws MissingObjectException
  106. * a tree object was not found.
  107. */
  108. public static TreeWalk forPath(final Repository db, final String path,
  109. final AnyObjectId... trees) throws MissingObjectException,
  110. IncorrectObjectTypeException, CorruptObjectException, IOException {
  111. final TreeWalk r = new TreeWalk(db);
  112. r.setFilter(PathFilterGroup.createFromStrings(Collections
  113. .singleton(path)));
  114. r.setRecursive(r.getFilter().shouldBeRecursive());
  115. r.reset(trees);
  116. return r.next() ? r : null;
  117. }
  118. /**
  119. * Open a tree walk and filter to exactly one path.
  120. * <p>
  121. * The returned tree walk is already positioned on the requested path, so
  122. * the caller should not need to invoke {@link #next()} unless they are
  123. * looking for a possible directory/file name conflict.
  124. *
  125. * @param db
  126. * repository to read tree object data from.
  127. * @param path
  128. * single path to advance the tree walk instance into.
  129. * @param tree
  130. * the single tree to walk through.
  131. * @return a new tree walk configured for exactly this one path; null if no
  132. * path was found in any of the trees.
  133. * @throws IOException
  134. * reading a pack file or loose object failed.
  135. * @throws CorruptObjectException
  136. * an tree object could not be read as its data stream did not
  137. * appear to be a tree, or could not be inflated.
  138. * @throws IncorrectObjectTypeException
  139. * an object we expected to be a tree was not a tree.
  140. * @throws MissingObjectException
  141. * a tree object was not found.
  142. */
  143. public static TreeWalk forPath(final Repository db, final String path,
  144. final RevTree tree) throws MissingObjectException,
  145. IncorrectObjectTypeException, CorruptObjectException, IOException {
  146. return forPath(db, path, new ObjectId[] { tree });
  147. }
  148. private final Repository db;
  149. private final MutableObjectId idBuffer = new MutableObjectId();
  150. private final WindowCursor curs = new WindowCursor();
  151. private TreeFilter filter;
  152. AbstractTreeIterator[] trees;
  153. private boolean recursive;
  154. private boolean postOrderTraversal;
  155. private int depth;
  156. private boolean advance;
  157. private boolean postChildren;
  158. AbstractTreeIterator currentHead;
  159. /**
  160. * Create a new tree walker for a given repository.
  161. *
  162. * @param repo
  163. * the repository the walker will obtain data from.
  164. */
  165. public TreeWalk(final Repository repo) {
  166. db = repo;
  167. filter = TreeFilter.ALL;
  168. trees = new AbstractTreeIterator[] { new EmptyTreeIterator() };
  169. }
  170. /**
  171. * Get the repository this tree walker is reading from.
  172. *
  173. * @return the repository configured when the walker was created.
  174. */
  175. public Repository getRepository() {
  176. return db;
  177. }
  178. /**
  179. * Get the currently configured filter.
  180. *
  181. * @return the current filter. Never null as a filter is always needed.
  182. */
  183. public TreeFilter getFilter() {
  184. return filter;
  185. }
  186. /**
  187. * Set the tree entry filter for this walker.
  188. * <p>
  189. * Multiple filters may be combined by constructing an arbitrary tree of
  190. * <code>AndTreeFilter</code> or <code>OrTreeFilter</code> instances to
  191. * describe the boolean expression required by the application. Custom
  192. * filter implementations may also be constructed by applications.
  193. * <p>
  194. * Note that filters are not thread-safe and may not be shared by concurrent
  195. * TreeWalk instances. Every TreeWalk must be supplied its own unique
  196. * filter, unless the filter implementation specifically states it is (and
  197. * always will be) thread-safe. Callers may use {@link TreeFilter#clone()}
  198. * to create a unique filter tree for this TreeWalk instance.
  199. *
  200. * @param newFilter
  201. * the new filter. If null the special {@link TreeFilter#ALL}
  202. * filter will be used instead, as it matches every entry.
  203. * @see org.eclipse.jgit.treewalk.filter.AndTreeFilter
  204. * @see org.eclipse.jgit.treewalk.filter.OrTreeFilter
  205. */
  206. public void setFilter(final TreeFilter newFilter) {
  207. filter = newFilter != null ? newFilter : TreeFilter.ALL;
  208. }
  209. /**
  210. * Is this walker automatically entering into subtrees?
  211. * <p>
  212. * If the walker is recursive then the caller will not see a subtree node
  213. * and instead will only receive file nodes in all relevant subtrees.
  214. *
  215. * @return true if automatically entering subtrees is enabled.
  216. */
  217. public boolean isRecursive() {
  218. return recursive;
  219. }
  220. /**
  221. * Set the walker to enter (or not enter) subtrees automatically.
  222. * <p>
  223. * If recursive mode is enabled the walker will hide subtree nodes from the
  224. * calling application and will produce only file level nodes. If a tree
  225. * (directory) is deleted then all of the file level nodes will appear to be
  226. * deleted, recursively, through as many levels as necessary to account for
  227. * all entries.
  228. *
  229. * @param b
  230. * true to skip subtree nodes and only obtain files nodes.
  231. */
  232. public void setRecursive(final boolean b) {
  233. recursive = b;
  234. }
  235. /**
  236. * Does this walker return a tree entry after it exits the subtree?
  237. * <p>
  238. * If post order traversal is enabled then the walker will return a subtree
  239. * after it has returned the last entry within that subtree. This may cause
  240. * a subtree to be seen by the application twice if {@link #isRecursive()}
  241. * is false, as the application will see it once, call
  242. * {@link #enterSubtree()}, and then see it again as it leaves the subtree.
  243. * <p>
  244. * If an application does not enable {@link #isRecursive()} and it does not
  245. * call {@link #enterSubtree()} then the tree is returned only once as none
  246. * of the children were processed.
  247. *
  248. * @return true if subtrees are returned after entries within the subtree.
  249. */
  250. public boolean isPostOrderTraversal() {
  251. return postOrderTraversal;
  252. }
  253. /**
  254. * Set the walker to return trees after their children.
  255. *
  256. * @param b
  257. * true to get trees after their children.
  258. * @see #isPostOrderTraversal()
  259. */
  260. public void setPostOrderTraversal(final boolean b) {
  261. postOrderTraversal = b;
  262. }
  263. /** Reset this walker so new tree iterators can be added to it. */
  264. public void reset() {
  265. trees = new AbstractTreeIterator[0];
  266. advance = false;
  267. depth = 0;
  268. }
  269. /**
  270. * Reset this walker to run over a single existing tree.
  271. *
  272. * @param id
  273. * the tree we need to parse. The walker will execute over this
  274. * single tree if the reset is successful.
  275. * @throws MissingObjectException
  276. * the given tree object does not exist in this repository.
  277. * @throws IncorrectObjectTypeException
  278. * the given object id does not denote a tree, but instead names
  279. * some other non-tree type of object. Note that commits are not
  280. * trees, even if they are sometimes called a "tree-ish".
  281. * @throws CorruptObjectException
  282. * the object claimed to be a tree, but its contents did not
  283. * appear to be a tree. The repository may have data corruption.
  284. * @throws IOException
  285. * a loose object or pack file could not be read.
  286. */
  287. public void reset(final AnyObjectId id) throws MissingObjectException,
  288. IncorrectObjectTypeException, CorruptObjectException, IOException {
  289. if (trees.length == 1) {
  290. AbstractTreeIterator o = trees[0];
  291. while (o.parent != null)
  292. o = o.parent;
  293. if (o instanceof CanonicalTreeParser) {
  294. o.matches = null;
  295. o.matchShift = 0;
  296. ((CanonicalTreeParser) o).reset(db, id, curs);
  297. trees[0] = o;
  298. } else {
  299. trees[0] = parserFor(id);
  300. }
  301. } else {
  302. trees = new AbstractTreeIterator[] { parserFor(id) };
  303. }
  304. advance = false;
  305. depth = 0;
  306. }
  307. /**
  308. * Reset this walker to run over a set of existing trees.
  309. *
  310. * @param ids
  311. * the trees we need to parse. The walker will execute over this
  312. * many parallel trees if the reset is successful.
  313. * @throws MissingObjectException
  314. * the given tree object does not exist in this repository.
  315. * @throws IncorrectObjectTypeException
  316. * the given object id does not denote a tree, but instead names
  317. * some other non-tree type of object. Note that commits are not
  318. * trees, even if they are sometimes called a "tree-ish".
  319. * @throws CorruptObjectException
  320. * the object claimed to be a tree, but its contents did not
  321. * appear to be a tree. The repository may have data corruption.
  322. * @throws IOException
  323. * a loose object or pack file could not be read.
  324. */
  325. public void reset(final AnyObjectId[] ids) throws MissingObjectException,
  326. IncorrectObjectTypeException, CorruptObjectException, IOException {
  327. final int oldLen = trees.length;
  328. final int newLen = ids.length;
  329. final AbstractTreeIterator[] r = newLen == oldLen ? trees
  330. : new AbstractTreeIterator[newLen];
  331. for (int i = 0; i < newLen; i++) {
  332. AbstractTreeIterator o;
  333. if (i < oldLen) {
  334. o = trees[i];
  335. while (o.parent != null)
  336. o = o.parent;
  337. if (o instanceof CanonicalTreeParser && o.pathOffset == 0) {
  338. o.matches = null;
  339. o.matchShift = 0;
  340. ((CanonicalTreeParser) o).reset(db, ids[i], curs);
  341. r[i] = o;
  342. continue;
  343. }
  344. }
  345. o = parserFor(ids[i]);
  346. r[i] = o;
  347. }
  348. trees = r;
  349. advance = false;
  350. depth = 0;
  351. }
  352. /**
  353. * Add an already existing tree object for walking.
  354. * <p>
  355. * The position of this tree is returned to the caller, in case the caller
  356. * has lost track of the order they added the trees into the walker.
  357. * <p>
  358. * The tree must have the same root as existing trees in the walk.
  359. *
  360. * @param id
  361. * identity of the tree object the caller wants walked.
  362. * @return position of this tree within the walker.
  363. * @throws MissingObjectException
  364. * the given tree object does not exist in this repository.
  365. * @throws IncorrectObjectTypeException
  366. * the given object id does not denote a tree, but instead names
  367. * some other non-tree type of object. Note that commits are not
  368. * trees, even if they are sometimes called a "tree-ish".
  369. * @throws CorruptObjectException
  370. * the object claimed to be a tree, but its contents did not
  371. * appear to be a tree. The repository may have data corruption.
  372. * @throws IOException
  373. * a loose object or pack file could not be read.
  374. */
  375. public int addTree(final AnyObjectId id) throws MissingObjectException,
  376. IncorrectObjectTypeException, CorruptObjectException, IOException {
  377. return addTree(parserFor(id));
  378. }
  379. /**
  380. * Add an already created tree iterator for walking.
  381. * <p>
  382. * The position of this tree is returned to the caller, in case the caller
  383. * has lost track of the order they added the trees into the walker.
  384. * <p>
  385. * The tree which the iterator operates on must have the same root as
  386. * existing trees in the walk.
  387. *
  388. * @param p
  389. * an iterator to walk over. The iterator should be new, with no
  390. * parent, and should still be positioned before the first entry.
  391. * The tree which the iterator operates on must have the same root
  392. * as other trees in the walk.
  393. *
  394. * @return position of this tree within the walker.
  395. * @throws CorruptObjectException
  396. * the iterator was unable to obtain its first entry, due to
  397. * possible data corruption within the backing data store.
  398. */
  399. public int addTree(final AbstractTreeIterator p)
  400. throws CorruptObjectException {
  401. final int n = trees.length;
  402. final AbstractTreeIterator[] newTrees = new AbstractTreeIterator[n + 1];
  403. System.arraycopy(trees, 0, newTrees, 0, n);
  404. newTrees[n] = p;
  405. p.matches = null;
  406. p.matchShift = 0;
  407. trees = newTrees;
  408. return n;
  409. }
  410. /**
  411. * Get the number of trees known to this walker.
  412. *
  413. * @return the total number of trees this walker is iterating over.
  414. */
  415. public int getTreeCount() {
  416. return trees.length;
  417. }
  418. /**
  419. * Advance this walker to the next relevant entry.
  420. *
  421. * @return true if there is an entry available; false if all entries have
  422. * been walked and the walk of this set of tree iterators is over.
  423. * @throws MissingObjectException
  424. * {@link #isRecursive()} was enabled, a subtree was found, but
  425. * the subtree object does not exist in this repository. The
  426. * repository may be missing objects.
  427. * @throws IncorrectObjectTypeException
  428. * {@link #isRecursive()} was enabled, a subtree was found, and
  429. * the subtree id does not denote a tree, but instead names some
  430. * other non-tree type of object. The repository may have data
  431. * corruption.
  432. * @throws CorruptObjectException
  433. * the contents of a tree did not appear to be a tree. The
  434. * repository may have data corruption.
  435. * @throws IOException
  436. * a loose object or pack file could not be read.
  437. */
  438. public boolean next() throws MissingObjectException,
  439. IncorrectObjectTypeException, CorruptObjectException, IOException {
  440. try {
  441. if (advance) {
  442. advance = false;
  443. postChildren = false;
  444. popEntriesEqual();
  445. }
  446. for (;;) {
  447. final AbstractTreeIterator t = min();
  448. if (t.eof()) {
  449. if (depth > 0) {
  450. exitSubtree();
  451. if (postOrderTraversal) {
  452. advance = true;
  453. postChildren = true;
  454. return true;
  455. }
  456. popEntriesEqual();
  457. continue;
  458. }
  459. return false;
  460. }
  461. currentHead = t;
  462. if (!filter.include(this)) {
  463. skipEntriesEqual();
  464. continue;
  465. }
  466. if (recursive && FileMode.TREE.equals(t.mode)) {
  467. enterSubtree();
  468. continue;
  469. }
  470. advance = true;
  471. return true;
  472. }
  473. } catch (StopWalkException stop) {
  474. for (final AbstractTreeIterator t : trees)
  475. t.stopWalk();
  476. return false;
  477. }
  478. }
  479. /**
  480. * Obtain the tree iterator for the current entry.
  481. * <p>
  482. * Entering into (or exiting out of) a subtree causes the current tree
  483. * iterator instance to be changed for the nth tree. This allows the tree
  484. * iterators to manage only one list of items, with the diving handled by
  485. * recursive trees.
  486. *
  487. * @param <T>
  488. * type of the tree iterator expected by the caller.
  489. * @param nth
  490. * tree to obtain the current iterator of.
  491. * @param clazz
  492. * type of the tree iterator expected by the caller.
  493. * @return r the current iterator of the requested type; null if the tree
  494. * has no entry to match the current path.
  495. */
  496. public <T extends AbstractTreeIterator> T getTree(final int nth,
  497. final Class<T> clazz) {
  498. final AbstractTreeIterator t = trees[nth];
  499. return t.matches == currentHead ? (T) t : null;
  500. }
  501. /**
  502. * Obtain the raw {@link FileMode} bits for the current entry.
  503. * <p>
  504. * Every added tree supplies mode bits, even if the tree does not contain
  505. * the current entry. In the latter case {@link FileMode#MISSING}'s mode
  506. * bits (0) are returned.
  507. *
  508. * @param nth
  509. * tree to obtain the mode bits from.
  510. * @return mode bits for the current entry of the nth tree.
  511. * @see FileMode#fromBits(int)
  512. */
  513. public int getRawMode(final int nth) {
  514. final AbstractTreeIterator t = trees[nth];
  515. return t.matches == currentHead ? t.mode : 0;
  516. }
  517. /**
  518. * Obtain the {@link FileMode} for the current entry.
  519. * <p>
  520. * Every added tree supplies a mode, even if the tree does not contain the
  521. * current entry. In the latter case {@link FileMode#MISSING} is returned.
  522. *
  523. * @param nth
  524. * tree to obtain the mode from.
  525. * @return mode for the current entry of the nth tree.
  526. */
  527. public FileMode getFileMode(final int nth) {
  528. return FileMode.fromBits(getRawMode(nth));
  529. }
  530. /**
  531. * Obtain the ObjectId for the current entry.
  532. * <p>
  533. * Using this method to compare ObjectId values between trees of this walker
  534. * is very inefficient. Applications should try to use
  535. * {@link #idEqual(int, int)} or {@link #getObjectId(MutableObjectId, int)}
  536. * whenever possible.
  537. * <p>
  538. * Every tree supplies an object id, even if the tree does not contain the
  539. * current entry. In the latter case {@link ObjectId#zeroId()} is returned.
  540. *
  541. * @param nth
  542. * tree to obtain the object identifier from.
  543. * @return object identifier for the current tree entry.
  544. * @see #getObjectId(MutableObjectId, int)
  545. * @see #idEqual(int, int)
  546. */
  547. public ObjectId getObjectId(final int nth) {
  548. final AbstractTreeIterator t = trees[nth];
  549. return t.matches == currentHead ? t.getEntryObjectId() : ObjectId
  550. .zeroId();
  551. }
  552. /**
  553. * Obtain the ObjectId for the current entry.
  554. * <p>
  555. * Every tree supplies an object id, even if the tree does not contain the
  556. * current entry. In the latter case {@link ObjectId#zeroId()} is supplied.
  557. * <p>
  558. * Applications should try to use {@link #idEqual(int, int)} when possible
  559. * as it avoids conversion overheads.
  560. *
  561. * @param out
  562. * buffer to copy the object id into.
  563. * @param nth
  564. * tree to obtain the object identifier from.
  565. * @see #idEqual(int, int)
  566. */
  567. public void getObjectId(final MutableObjectId out, final int nth) {
  568. final AbstractTreeIterator t = trees[nth];
  569. if (t.matches == currentHead)
  570. t.getEntryObjectId(out);
  571. else
  572. out.clear();
  573. }
  574. /**
  575. * Compare two tree's current ObjectId values for equality.
  576. *
  577. * @param nthA
  578. * first tree to compare the object id from.
  579. * @param nthB
  580. * second tree to compare the object id from.
  581. * @return result of
  582. * <code>getObjectId(nthA).equals(getObjectId(nthB))</code>.
  583. * @see #getObjectId(int)
  584. */
  585. public boolean idEqual(final int nthA, final int nthB) {
  586. final AbstractTreeIterator ch = currentHead;
  587. final AbstractTreeIterator a = trees[nthA];
  588. final AbstractTreeIterator b = trees[nthB];
  589. if (a.matches == ch && b.matches == ch)
  590. return a.idEqual(b);
  591. if (a.matches != ch && b.matches != ch) {
  592. // If neither tree matches the current path node then neither
  593. // tree has this entry. In such case the ObjectId is zero(),
  594. // and zero() is always equal to zero().
  595. //
  596. return true;
  597. }
  598. return false;
  599. }
  600. /**
  601. * Get the current entry's name within its parent tree.
  602. * <p>
  603. * This method is not very efficient and is primarily meant for debugging
  604. * and final output generation. Applications should try to avoid calling it,
  605. * and if invoked do so only once per interesting entry, where the name is
  606. * absolutely required for correct function.
  607. *
  608. * @return name of the current entry within the parent tree (or directory).
  609. * The name never includes a '/'.
  610. */
  611. public String getNameString() {
  612. final AbstractTreeIterator t = currentHead;
  613. final int off = t.pathOffset;
  614. final int end = t.pathLen;
  615. return RawParseUtils.decode(Constants.CHARSET, t.path, off, end);
  616. }
  617. /**
  618. * Get the current entry's complete path.
  619. * <p>
  620. * This method is not very efficient and is primarily meant for debugging
  621. * and final output generation. Applications should try to avoid calling it,
  622. * and if invoked do so only once per interesting entry, where the name is
  623. * absolutely required for correct function.
  624. *
  625. * @return complete path of the current entry, from the root of the
  626. * repository. If the current entry is in a subtree there will be at
  627. * least one '/' in the returned string.
  628. */
  629. public String getPathString() {
  630. return pathOf(currentHead);
  631. }
  632. /**
  633. * Get the current entry's complete path as a UTF-8 byte array.
  634. *
  635. * @return complete path of the current entry, from the root of the
  636. * repository. If the current entry is in a subtree there will be at
  637. * least one '/' in the returned string.
  638. */
  639. public byte[] getRawPath() {
  640. final AbstractTreeIterator t = currentHead;
  641. final int n = t.pathLen;
  642. final byte[] r = new byte[n];
  643. System.arraycopy(t.path, 0, r, 0, n);
  644. return r;
  645. }
  646. /**
  647. * Test if the supplied path matches the current entry's path.
  648. * <p>
  649. * This method tests that the supplied path is exactly equal to the current
  650. * entry, or is one of its parent directories. It is faster to use this
  651. * method then to use {@link #getPathString()} to first create a String
  652. * object, then test <code>startsWith</code> or some other type of string
  653. * match function.
  654. *
  655. * @param p
  656. * path buffer to test. Callers should ensure the path does not
  657. * end with '/' prior to invocation.
  658. * @param pLen
  659. * number of bytes from <code>buf</code> to test.
  660. * @return < 0 if p is before the current path; 0 if p matches the current
  661. * path; 1 if the current path is past p and p will never match
  662. * again on this tree walk.
  663. */
  664. public int isPathPrefix(final byte[] p, final int pLen) {
  665. final AbstractTreeIterator t = currentHead;
  666. final byte[] c = t.path;
  667. final int cLen = t.pathLen;
  668. int ci;
  669. for (ci = 0; ci < cLen && ci < pLen; ci++) {
  670. final int c_value = (c[ci] & 0xff) - (p[ci] & 0xff);
  671. if (c_value != 0)
  672. return c_value;
  673. }
  674. if (ci < cLen) {
  675. // Ran out of pattern but we still had current data.
  676. // If c[ci] == '/' then pattern matches the subtree.
  677. // Otherwise we cannot be certain so we return -1.
  678. //
  679. return c[ci] == '/' ? 0 : -1;
  680. }
  681. if (ci < pLen) {
  682. // Ran out of current, but we still have pattern data.
  683. // If p[ci] == '/' then pattern matches this subtree,
  684. // otherwise we cannot be certain so we return -1.
  685. //
  686. return p[ci] == '/' ? 0 : -1;
  687. }
  688. // Both strings are identical.
  689. //
  690. return 0;
  691. }
  692. /**
  693. * Test if the supplied path matches (being suffix of) the current entry's
  694. * path.
  695. * <p>
  696. * This method tests that the supplied path is exactly equal to the current
  697. * entry, or is relative to one of entry's parent directories. It is faster
  698. * to use this method then to use {@link #getPathString()} to first create
  699. * a String object, then test <code>endsWith</code> or some other type of
  700. * string match function.
  701. *
  702. * @param p
  703. * path buffer to test.
  704. * @param pLen
  705. * number of bytes from <code>buf</code> to test.
  706. * @return true if p is suffix of the current path;
  707. * false if otherwise
  708. */
  709. public boolean isPathSuffix(final byte[] p, final int pLen) {
  710. final AbstractTreeIterator t = currentHead;
  711. final byte[] c = t.path;
  712. final int cLen = t.pathLen;
  713. int ci;
  714. for (ci = 1; ci < cLen && ci < pLen; ci++) {
  715. if (c[cLen-ci] != p[pLen-ci])
  716. return false;
  717. }
  718. return true;
  719. }
  720. /**
  721. * Get the current subtree depth of this walker.
  722. *
  723. * @return the current subtree depth of this walker.
  724. */
  725. public int getDepth() {
  726. return depth;
  727. }
  728. /**
  729. * Is the current entry a subtree?
  730. * <p>
  731. * This method is faster then testing the raw mode bits of all trees to see
  732. * if any of them are a subtree. If at least one is a subtree then this
  733. * method will return true.
  734. *
  735. * @return true if {@link #enterSubtree()} will work on the current node.
  736. */
  737. public boolean isSubtree() {
  738. return FileMode.TREE.equals(currentHead.mode);
  739. }
  740. /**
  741. * Is the current entry a subtree returned after its children?
  742. *
  743. * @return true if the current node is a tree that has been returned after
  744. * its children were already processed.
  745. * @see #isPostOrderTraversal()
  746. */
  747. public boolean isPostChildren() {
  748. return postChildren && isSubtree();
  749. }
  750. /**
  751. * Enter into the current subtree.
  752. * <p>
  753. * If the current entry is a subtree this method arranges for its children
  754. * to be returned before the next sibling following the subtree is returned.
  755. *
  756. * @throws MissingObjectException
  757. * a subtree was found, but the subtree object does not exist in
  758. * this repository. The repository may be missing objects.
  759. * @throws IncorrectObjectTypeException
  760. * a subtree was found, and the subtree id does not denote a
  761. * tree, but instead names some other non-tree type of object.
  762. * The repository may have data corruption.
  763. * @throws CorruptObjectException
  764. * the contents of a tree did not appear to be a tree. The
  765. * repository may have data corruption.
  766. * @throws IOException
  767. * a loose object or pack file could not be read.
  768. */
  769. public void enterSubtree() throws MissingObjectException,
  770. IncorrectObjectTypeException, CorruptObjectException, IOException {
  771. final AbstractTreeIterator ch = currentHead;
  772. final AbstractTreeIterator[] tmp = new AbstractTreeIterator[trees.length];
  773. for (int i = 0; i < trees.length; i++) {
  774. final AbstractTreeIterator t = trees[i];
  775. final AbstractTreeIterator n;
  776. if (t.matches == ch && !t.eof() && FileMode.TREE.equals(t.mode))
  777. n = t.createSubtreeIterator(db, idBuffer, curs);
  778. else
  779. n = t.createEmptyTreeIterator();
  780. tmp[i] = n;
  781. }
  782. depth++;
  783. advance = false;
  784. System.arraycopy(tmp, 0, trees, 0, trees.length);
  785. }
  786. AbstractTreeIterator min() throws CorruptObjectException {
  787. int i = 0;
  788. AbstractTreeIterator minRef = trees[i];
  789. while (minRef.eof() && ++i < trees.length)
  790. minRef = trees[i];
  791. if (minRef.eof())
  792. return minRef;
  793. minRef.matches = minRef;
  794. while (++i < trees.length) {
  795. final AbstractTreeIterator t = trees[i];
  796. if (t.eof())
  797. continue;
  798. final int cmp = t.pathCompare(minRef);
  799. if (cmp < 0) {
  800. t.matches = t;
  801. minRef = t;
  802. } else if (cmp == 0) {
  803. t.matches = minRef;
  804. }
  805. }
  806. return minRef;
  807. }
  808. void popEntriesEqual() throws CorruptObjectException {
  809. final AbstractTreeIterator ch = currentHead;
  810. for (int i = 0; i < trees.length; i++) {
  811. final AbstractTreeIterator t = trees[i];
  812. if (t.matches == ch) {
  813. t.next(1);
  814. t.matches = null;
  815. }
  816. }
  817. }
  818. void skipEntriesEqual() throws CorruptObjectException {
  819. final AbstractTreeIterator ch = currentHead;
  820. for (int i = 0; i < trees.length; i++) {
  821. final AbstractTreeIterator t = trees[i];
  822. if (t.matches == ch) {
  823. t.skip();
  824. t.matches = null;
  825. }
  826. }
  827. }
  828. private void exitSubtree() {
  829. depth--;
  830. for (int i = 0; i < trees.length; i++)
  831. trees[i] = trees[i].parent;
  832. AbstractTreeIterator minRef = null;
  833. for (final AbstractTreeIterator t : trees) {
  834. if (t.matches != t)
  835. continue;
  836. if (minRef == null || t.pathCompare(minRef) < 0)
  837. minRef = t;
  838. }
  839. currentHead = minRef;
  840. }
  841. private CanonicalTreeParser parserFor(final AnyObjectId id)
  842. throws IncorrectObjectTypeException, IOException {
  843. final CanonicalTreeParser p = new CanonicalTreeParser();
  844. p.reset(db, id, curs);
  845. return p;
  846. }
  847. static String pathOf(final AbstractTreeIterator t) {
  848. return RawParseUtils.decode(Constants.CHARSET, t.path, 0, t.pathLen);
  849. }
  850. static String pathOf(final byte[] buf, int pos, int end) {
  851. return RawParseUtils.decode(Constants.CHARSET, buf, pos, end);
  852. }
  853. }