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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977
  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.ObjectReader;
  57. import org.eclipse.jgit.lib.Repository;
  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 reader
  91. * the reader the walker will obtain tree 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 ObjectReader reader, final String path,
  109. final AnyObjectId... trees) throws MissingObjectException,
  110. IncorrectObjectTypeException, CorruptObjectException, IOException {
  111. final TreeWalk r = new TreeWalk(reader);
  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 trees
  130. * one or more trees to walk through, all with the same root.
  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 AnyObjectId... trees) throws MissingObjectException,
  145. IncorrectObjectTypeException, CorruptObjectException, IOException {
  146. ObjectReader reader = db.newObjectReader();
  147. try {
  148. return forPath(reader, path, trees);
  149. } finally {
  150. reader.release();
  151. }
  152. }
  153. /**
  154. * Open a tree walk and filter to exactly one path.
  155. * <p>
  156. * The returned tree walk is already positioned on the requested path, so
  157. * the caller should not need to invoke {@link #next()} unless they are
  158. * looking for a possible directory/file name conflict.
  159. *
  160. * @param db
  161. * repository to read tree object data from.
  162. * @param path
  163. * single path to advance the tree walk instance into.
  164. * @param tree
  165. * the single tree to walk through.
  166. * @return a new tree walk configured for exactly this one path; null if no
  167. * path was found in any of the trees.
  168. * @throws IOException
  169. * reading a pack file or loose object failed.
  170. * @throws CorruptObjectException
  171. * an tree object could not be read as its data stream did not
  172. * appear to be a tree, or could not be inflated.
  173. * @throws IncorrectObjectTypeException
  174. * an object we expected to be a tree was not a tree.
  175. * @throws MissingObjectException
  176. * a tree object was not found.
  177. */
  178. public static TreeWalk forPath(final Repository db, final String path,
  179. final RevTree tree) throws MissingObjectException,
  180. IncorrectObjectTypeException, CorruptObjectException, IOException {
  181. return forPath(db, path, new ObjectId[] { tree });
  182. }
  183. private final ObjectReader reader;
  184. private final MutableObjectId idBuffer = new MutableObjectId();
  185. private TreeFilter filter;
  186. AbstractTreeIterator[] trees;
  187. private boolean recursive;
  188. private boolean postOrderTraversal;
  189. private int depth;
  190. private boolean advance;
  191. private boolean postChildren;
  192. AbstractTreeIterator currentHead;
  193. /**
  194. * Create a new tree walker for a given repository.
  195. *
  196. * @param repo
  197. * the repository the walker will obtain data from.
  198. */
  199. public TreeWalk(final Repository repo) {
  200. this(repo.newObjectReader());
  201. }
  202. /**
  203. * Create a new tree walker for a given repository.
  204. *
  205. * @param or
  206. * the reader the walker will obtain tree data from.
  207. */
  208. public TreeWalk(final ObjectReader or) {
  209. reader = or;
  210. filter = TreeFilter.ALL;
  211. trees = new AbstractTreeIterator[] { new EmptyTreeIterator() };
  212. }
  213. /** @return the reader this walker is using to load objects. */
  214. public ObjectReader getObjectReader() {
  215. return reader;
  216. }
  217. /**
  218. * Release any resources used by this walker's reader.
  219. * <p>
  220. * A walker that has been released can be used again, but may need to be
  221. * released after the subsequent usage.
  222. */
  223. public void release() {
  224. reader.release();
  225. }
  226. /**
  227. * Get the currently configured filter.
  228. *
  229. * @return the current filter. Never null as a filter is always needed.
  230. */
  231. public TreeFilter getFilter() {
  232. return filter;
  233. }
  234. /**
  235. * Set the tree entry filter for this walker.
  236. * <p>
  237. * Multiple filters may be combined by constructing an arbitrary tree of
  238. * <code>AndTreeFilter</code> or <code>OrTreeFilter</code> instances to
  239. * describe the boolean expression required by the application. Custom
  240. * filter implementations may also be constructed by applications.
  241. * <p>
  242. * Note that filters are not thread-safe and may not be shared by concurrent
  243. * TreeWalk instances. Every TreeWalk must be supplied its own unique
  244. * filter, unless the filter implementation specifically states it is (and
  245. * always will be) thread-safe. Callers may use {@link TreeFilter#clone()}
  246. * to create a unique filter tree for this TreeWalk instance.
  247. *
  248. * @param newFilter
  249. * the new filter. If null the special {@link TreeFilter#ALL}
  250. * filter will be used instead, as it matches every entry.
  251. * @see org.eclipse.jgit.treewalk.filter.AndTreeFilter
  252. * @see org.eclipse.jgit.treewalk.filter.OrTreeFilter
  253. */
  254. public void setFilter(final TreeFilter newFilter) {
  255. filter = newFilter != null ? newFilter : TreeFilter.ALL;
  256. }
  257. /**
  258. * Is this walker automatically entering into subtrees?
  259. * <p>
  260. * If the walker is recursive then the caller will not see a subtree node
  261. * and instead will only receive file nodes in all relevant subtrees.
  262. *
  263. * @return true if automatically entering subtrees is enabled.
  264. */
  265. public boolean isRecursive() {
  266. return recursive;
  267. }
  268. /**
  269. * Set the walker to enter (or not enter) subtrees automatically.
  270. * <p>
  271. * If recursive mode is enabled the walker will hide subtree nodes from the
  272. * calling application and will produce only file level nodes. If a tree
  273. * (directory) is deleted then all of the file level nodes will appear to be
  274. * deleted, recursively, through as many levels as necessary to account for
  275. * all entries.
  276. *
  277. * @param b
  278. * true to skip subtree nodes and only obtain files nodes.
  279. */
  280. public void setRecursive(final boolean b) {
  281. recursive = b;
  282. }
  283. /**
  284. * Does this walker return a tree entry after it exits the subtree?
  285. * <p>
  286. * If post order traversal is enabled then the walker will return a subtree
  287. * after it has returned the last entry within that subtree. This may cause
  288. * a subtree to be seen by the application twice if {@link #isRecursive()}
  289. * is false, as the application will see it once, call
  290. * {@link #enterSubtree()}, and then see it again as it leaves the subtree.
  291. * <p>
  292. * If an application does not enable {@link #isRecursive()} and it does not
  293. * call {@link #enterSubtree()} then the tree is returned only once as none
  294. * of the children were processed.
  295. *
  296. * @return true if subtrees are returned after entries within the subtree.
  297. */
  298. public boolean isPostOrderTraversal() {
  299. return postOrderTraversal;
  300. }
  301. /**
  302. * Set the walker to return trees after their children.
  303. *
  304. * @param b
  305. * true to get trees after their children.
  306. * @see #isPostOrderTraversal()
  307. */
  308. public void setPostOrderTraversal(final boolean b) {
  309. postOrderTraversal = b;
  310. }
  311. /** Reset this walker so new tree iterators can be added to it. */
  312. public void reset() {
  313. trees = new AbstractTreeIterator[0];
  314. advance = false;
  315. depth = 0;
  316. }
  317. /**
  318. * Reset this walker to run over a single existing tree.
  319. *
  320. * @param id
  321. * the tree we need to parse. The walker will execute over this
  322. * single tree if the reset is successful.
  323. * @throws MissingObjectException
  324. * the given tree object does not exist in this repository.
  325. * @throws IncorrectObjectTypeException
  326. * the given object id does not denote a tree, but instead names
  327. * some other non-tree type of object. Note that commits are not
  328. * trees, even if they are sometimes called a "tree-ish".
  329. * @throws CorruptObjectException
  330. * the object claimed to be a tree, but its contents did not
  331. * appear to be a tree. The repository may have data corruption.
  332. * @throws IOException
  333. * a loose object or pack file could not be read.
  334. */
  335. public void reset(final AnyObjectId id) throws MissingObjectException,
  336. IncorrectObjectTypeException, CorruptObjectException, IOException {
  337. if (trees.length == 1) {
  338. AbstractTreeIterator o = trees[0];
  339. while (o.parent != null)
  340. o = o.parent;
  341. if (o instanceof CanonicalTreeParser) {
  342. o.matches = null;
  343. o.matchShift = 0;
  344. ((CanonicalTreeParser) o).reset(reader, id);
  345. trees[0] = o;
  346. } else {
  347. trees[0] = parserFor(id);
  348. }
  349. } else {
  350. trees = new AbstractTreeIterator[] { parserFor(id) };
  351. }
  352. advance = false;
  353. depth = 0;
  354. }
  355. /**
  356. * Reset this walker to run over a set of existing trees.
  357. *
  358. * @param ids
  359. * the trees we need to parse. The walker will execute over this
  360. * many parallel trees if the reset is successful.
  361. * @throws MissingObjectException
  362. * the given tree object does not exist in this repository.
  363. * @throws IncorrectObjectTypeException
  364. * the given object id does not denote a tree, but instead names
  365. * some other non-tree type of object. Note that commits are not
  366. * trees, even if they are sometimes called a "tree-ish".
  367. * @throws CorruptObjectException
  368. * the object claimed to be a tree, but its contents did not
  369. * appear to be a tree. The repository may have data corruption.
  370. * @throws IOException
  371. * a loose object or pack file could not be read.
  372. */
  373. public void reset(final AnyObjectId[] ids) throws MissingObjectException,
  374. IncorrectObjectTypeException, CorruptObjectException, IOException {
  375. final int oldLen = trees.length;
  376. final int newLen = ids.length;
  377. final AbstractTreeIterator[] r = newLen == oldLen ? trees
  378. : new AbstractTreeIterator[newLen];
  379. for (int i = 0; i < newLen; i++) {
  380. AbstractTreeIterator o;
  381. if (i < oldLen) {
  382. o = trees[i];
  383. while (o.parent != null)
  384. o = o.parent;
  385. if (o instanceof CanonicalTreeParser && o.pathOffset == 0) {
  386. o.matches = null;
  387. o.matchShift = 0;
  388. ((CanonicalTreeParser) o).reset(reader, ids[i]);
  389. r[i] = o;
  390. continue;
  391. }
  392. }
  393. o = parserFor(ids[i]);
  394. r[i] = o;
  395. }
  396. trees = r;
  397. advance = false;
  398. depth = 0;
  399. }
  400. /**
  401. * Add an already existing tree object for walking.
  402. * <p>
  403. * The position of this tree is returned to the caller, in case the caller
  404. * has lost track of the order they added the trees into the walker.
  405. * <p>
  406. * The tree must have the same root as existing trees in the walk.
  407. *
  408. * @param id
  409. * identity of the tree object the caller wants walked.
  410. * @return position of this tree within the walker.
  411. * @throws MissingObjectException
  412. * the given tree object does not exist in this repository.
  413. * @throws IncorrectObjectTypeException
  414. * the given object id does not denote a tree, but instead names
  415. * some other non-tree type of object. Note that commits are not
  416. * trees, even if they are sometimes called a "tree-ish".
  417. * @throws CorruptObjectException
  418. * the object claimed to be a tree, but its contents did not
  419. * appear to be a tree. The repository may have data corruption.
  420. * @throws IOException
  421. * a loose object or pack file could not be read.
  422. */
  423. public int addTree(final AnyObjectId id) throws MissingObjectException,
  424. IncorrectObjectTypeException, CorruptObjectException, IOException {
  425. return addTree(parserFor(id));
  426. }
  427. /**
  428. * Add an already created tree iterator for walking.
  429. * <p>
  430. * The position of this tree is returned to the caller, in case the caller
  431. * has lost track of the order they added the trees into the walker.
  432. * <p>
  433. * The tree which the iterator operates on must have the same root as
  434. * existing trees in the walk.
  435. *
  436. * @param p
  437. * an iterator to walk over. The iterator should be new, with no
  438. * parent, and should still be positioned before the first entry.
  439. * The tree which the iterator operates on must have the same root
  440. * as other trees in the walk.
  441. *
  442. * @return position of this tree within the walker.
  443. * @throws CorruptObjectException
  444. * the iterator was unable to obtain its first entry, due to
  445. * possible data corruption within the backing data store.
  446. */
  447. public int addTree(final AbstractTreeIterator p)
  448. throws CorruptObjectException {
  449. final int n = trees.length;
  450. final AbstractTreeIterator[] newTrees = new AbstractTreeIterator[n + 1];
  451. System.arraycopy(trees, 0, newTrees, 0, n);
  452. newTrees[n] = p;
  453. p.matches = null;
  454. p.matchShift = 0;
  455. trees = newTrees;
  456. return n;
  457. }
  458. /**
  459. * Get the number of trees known to this walker.
  460. *
  461. * @return the total number of trees this walker is iterating over.
  462. */
  463. public int getTreeCount() {
  464. return trees.length;
  465. }
  466. /**
  467. * Advance this walker to the next relevant entry.
  468. *
  469. * @return true if there is an entry available; false if all entries have
  470. * been walked and the walk of this set of tree iterators is over.
  471. * @throws MissingObjectException
  472. * {@link #isRecursive()} was enabled, a subtree was found, but
  473. * the subtree object does not exist in this repository. The
  474. * repository may be missing objects.
  475. * @throws IncorrectObjectTypeException
  476. * {@link #isRecursive()} was enabled, a subtree was found, and
  477. * the subtree id does not denote a tree, but instead names some
  478. * other non-tree type of object. The repository may have data
  479. * corruption.
  480. * @throws CorruptObjectException
  481. * the contents of a tree did not appear to be a tree. The
  482. * repository may have data corruption.
  483. * @throws IOException
  484. * a loose object or pack file could not be read.
  485. */
  486. public boolean next() throws MissingObjectException,
  487. IncorrectObjectTypeException, CorruptObjectException, IOException {
  488. try {
  489. if (advance) {
  490. advance = false;
  491. postChildren = false;
  492. popEntriesEqual();
  493. }
  494. for (;;) {
  495. final AbstractTreeIterator t = min();
  496. if (t.eof()) {
  497. if (depth > 0) {
  498. exitSubtree();
  499. if (postOrderTraversal) {
  500. advance = true;
  501. postChildren = true;
  502. return true;
  503. }
  504. popEntriesEqual();
  505. continue;
  506. }
  507. return false;
  508. }
  509. currentHead = t;
  510. if (!filter.include(this)) {
  511. skipEntriesEqual();
  512. continue;
  513. }
  514. if (recursive && FileMode.TREE.equals(t.mode)) {
  515. enterSubtree();
  516. continue;
  517. }
  518. advance = true;
  519. return true;
  520. }
  521. } catch (StopWalkException stop) {
  522. for (final AbstractTreeIterator t : trees)
  523. t.stopWalk();
  524. return false;
  525. }
  526. }
  527. /**
  528. * Obtain the tree iterator for the current entry.
  529. * <p>
  530. * Entering into (or exiting out of) a subtree causes the current tree
  531. * iterator instance to be changed for the nth tree. This allows the tree
  532. * iterators to manage only one list of items, with the diving handled by
  533. * recursive trees.
  534. *
  535. * @param <T>
  536. * type of the tree iterator expected by the caller.
  537. * @param nth
  538. * tree to obtain the current iterator of.
  539. * @param clazz
  540. * type of the tree iterator expected by the caller.
  541. * @return r the current iterator of the requested type; null if the tree
  542. * has no entry to match the current path.
  543. */
  544. public <T extends AbstractTreeIterator> T getTree(final int nth,
  545. final Class<T> clazz) {
  546. final AbstractTreeIterator t = trees[nth];
  547. return t.matches == currentHead ? (T) t : null;
  548. }
  549. /**
  550. * Obtain the raw {@link FileMode} bits for the current entry.
  551. * <p>
  552. * Every added tree supplies mode bits, even if the tree does not contain
  553. * the current entry. In the latter case {@link FileMode#MISSING}'s mode
  554. * bits (0) are returned.
  555. *
  556. * @param nth
  557. * tree to obtain the mode bits from.
  558. * @return mode bits for the current entry of the nth tree.
  559. * @see FileMode#fromBits(int)
  560. */
  561. public int getRawMode(final int nth) {
  562. final AbstractTreeIterator t = trees[nth];
  563. return t.matches == currentHead ? t.mode : 0;
  564. }
  565. /**
  566. * Obtain the {@link FileMode} for the current entry.
  567. * <p>
  568. * Every added tree supplies a mode, even if the tree does not contain the
  569. * current entry. In the latter case {@link FileMode#MISSING} is returned.
  570. *
  571. * @param nth
  572. * tree to obtain the mode from.
  573. * @return mode for the current entry of the nth tree.
  574. */
  575. public FileMode getFileMode(final int nth) {
  576. return FileMode.fromBits(getRawMode(nth));
  577. }
  578. /**
  579. * Obtain the ObjectId for the current entry.
  580. * <p>
  581. * Using this method to compare ObjectId values between trees of this walker
  582. * is very inefficient. Applications should try to use
  583. * {@link #idEqual(int, int)} or {@link #getObjectId(MutableObjectId, int)}
  584. * whenever possible.
  585. * <p>
  586. * Every tree supplies an object id, even if the tree does not contain the
  587. * current entry. In the latter case {@link ObjectId#zeroId()} is returned.
  588. *
  589. * @param nth
  590. * tree to obtain the object identifier from.
  591. * @return object identifier for the current tree entry.
  592. * @see #getObjectId(MutableObjectId, int)
  593. * @see #idEqual(int, int)
  594. */
  595. public ObjectId getObjectId(final int nth) {
  596. final AbstractTreeIterator t = trees[nth];
  597. return t.matches == currentHead ? t.getEntryObjectId() : ObjectId
  598. .zeroId();
  599. }
  600. /**
  601. * Obtain the ObjectId for the current entry.
  602. * <p>
  603. * Every tree supplies an object id, even if the tree does not contain the
  604. * current entry. In the latter case {@link ObjectId#zeroId()} is supplied.
  605. * <p>
  606. * Applications should try to use {@link #idEqual(int, int)} when possible
  607. * as it avoids conversion overheads.
  608. *
  609. * @param out
  610. * buffer to copy the object id into.
  611. * @param nth
  612. * tree to obtain the object identifier from.
  613. * @see #idEqual(int, int)
  614. */
  615. public void getObjectId(final MutableObjectId out, final int nth) {
  616. final AbstractTreeIterator t = trees[nth];
  617. if (t.matches == currentHead)
  618. t.getEntryObjectId(out);
  619. else
  620. out.clear();
  621. }
  622. /**
  623. * Compare two tree's current ObjectId values for equality.
  624. *
  625. * @param nthA
  626. * first tree to compare the object id from.
  627. * @param nthB
  628. * second tree to compare the object id from.
  629. * @return result of
  630. * <code>getObjectId(nthA).equals(getObjectId(nthB))</code>.
  631. * @see #getObjectId(int)
  632. */
  633. public boolean idEqual(final int nthA, final int nthB) {
  634. final AbstractTreeIterator ch = currentHead;
  635. final AbstractTreeIterator a = trees[nthA];
  636. final AbstractTreeIterator b = trees[nthB];
  637. if (a.matches != ch && b.matches != ch) {
  638. // If neither tree matches the current path node then neither
  639. // tree has this entry. In such case the ObjectId is zero(),
  640. // and zero() is always equal to zero().
  641. //
  642. return true;
  643. }
  644. if (!a.hasId() || !b.hasId())
  645. return false;
  646. if (a.matches == ch && b.matches == ch)
  647. return a.idEqual(b);
  648. return false;
  649. }
  650. /**
  651. * Get the current entry's name within its parent tree.
  652. * <p>
  653. * This method is not very efficient and is primarily meant for debugging
  654. * and final output generation. Applications should try to avoid calling it,
  655. * and if invoked do so only once per interesting entry, where the name is
  656. * absolutely required for correct function.
  657. *
  658. * @return name of the current entry within the parent tree (or directory).
  659. * The name never includes a '/'.
  660. */
  661. public String getNameString() {
  662. final AbstractTreeIterator t = currentHead;
  663. final int off = t.pathOffset;
  664. final int end = t.pathLen;
  665. return RawParseUtils.decode(Constants.CHARSET, t.path, off, end);
  666. }
  667. /**
  668. * Get the current entry's complete path.
  669. * <p>
  670. * This method is not very efficient and is primarily meant for debugging
  671. * and final output generation. Applications should try to avoid calling it,
  672. * and if invoked do so only once per interesting entry, where the name is
  673. * absolutely required for correct function.
  674. *
  675. * @return complete path of the current entry, from the root of the
  676. * repository. If the current entry is in a subtree there will be at
  677. * least one '/' in the returned string.
  678. */
  679. public String getPathString() {
  680. return pathOf(currentHead);
  681. }
  682. /**
  683. * Get the current entry's complete path as a UTF-8 byte array.
  684. *
  685. * @return complete path of the current entry, from the root of the
  686. * repository. If the current entry is in a subtree there will be at
  687. * least one '/' in the returned string.
  688. */
  689. public byte[] getRawPath() {
  690. final AbstractTreeIterator t = currentHead;
  691. final int n = t.pathLen;
  692. final byte[] r = new byte[n];
  693. System.arraycopy(t.path, 0, r, 0, n);
  694. return r;
  695. }
  696. /**
  697. * Test if the supplied path matches the current entry's path.
  698. * <p>
  699. * This method tests that the supplied path is exactly equal to the current
  700. * entry, or is one of its parent directories. It is faster to use this
  701. * method then to use {@link #getPathString()} to first create a String
  702. * object, then test <code>startsWith</code> or some other type of string
  703. * match function.
  704. *
  705. * @param p
  706. * path buffer to test. Callers should ensure the path does not
  707. * end with '/' prior to invocation.
  708. * @param pLen
  709. * number of bytes from <code>buf</code> to test.
  710. * @return < 0 if p is before the current path; 0 if p matches the current
  711. * path; 1 if the current path is past p and p will never match
  712. * again on this tree walk.
  713. */
  714. public int isPathPrefix(final byte[] p, final int pLen) {
  715. final AbstractTreeIterator t = currentHead;
  716. final byte[] c = t.path;
  717. final int cLen = t.pathLen;
  718. int ci;
  719. for (ci = 0; ci < cLen && ci < pLen; ci++) {
  720. final int c_value = (c[ci] & 0xff) - (p[ci] & 0xff);
  721. if (c_value != 0)
  722. return c_value;
  723. }
  724. if (ci < cLen) {
  725. // Ran out of pattern but we still had current data.
  726. // If c[ci] == '/' then pattern matches the subtree.
  727. // Otherwise we cannot be certain so we return -1.
  728. //
  729. return c[ci] == '/' ? 0 : -1;
  730. }
  731. if (ci < pLen) {
  732. // Ran out of current, but we still have pattern data.
  733. // If p[ci] == '/' then pattern matches this subtree,
  734. // otherwise we cannot be certain so we return -1.
  735. //
  736. return p[ci] == '/' ? 0 : -1;
  737. }
  738. // Both strings are identical.
  739. //
  740. return 0;
  741. }
  742. /**
  743. * Test if the supplied path matches (being suffix of) the current entry's
  744. * path.
  745. * <p>
  746. * This method tests that the supplied path is exactly equal to the current
  747. * entry, or is relative to one of entry's parent directories. It is faster
  748. * to use this method then to use {@link #getPathString()} to first create
  749. * a String object, then test <code>endsWith</code> or some other type of
  750. * string match function.
  751. *
  752. * @param p
  753. * path buffer to test.
  754. * @param pLen
  755. * number of bytes from <code>buf</code> to test.
  756. * @return true if p is suffix of the current path;
  757. * false if otherwise
  758. */
  759. public boolean isPathSuffix(final byte[] p, final int pLen) {
  760. final AbstractTreeIterator t = currentHead;
  761. final byte[] c = t.path;
  762. final int cLen = t.pathLen;
  763. int ci;
  764. for (ci = 1; ci < cLen && ci < pLen; ci++) {
  765. if (c[cLen-ci] != p[pLen-ci])
  766. return false;
  767. }
  768. return true;
  769. }
  770. /**
  771. * Get the current subtree depth of this walker.
  772. *
  773. * @return the current subtree depth of this walker.
  774. */
  775. public int getDepth() {
  776. return depth;
  777. }
  778. /**
  779. * Is the current entry a subtree?
  780. * <p>
  781. * This method is faster then testing the raw mode bits of all trees to see
  782. * if any of them are a subtree. If at least one is a subtree then this
  783. * method will return true.
  784. *
  785. * @return true if {@link #enterSubtree()} will work on the current node.
  786. */
  787. public boolean isSubtree() {
  788. return FileMode.TREE.equals(currentHead.mode);
  789. }
  790. /**
  791. * Is the current entry a subtree returned after its children?
  792. *
  793. * @return true if the current node is a tree that has been returned after
  794. * its children were already processed.
  795. * @see #isPostOrderTraversal()
  796. */
  797. public boolean isPostChildren() {
  798. return postChildren && isSubtree();
  799. }
  800. /**
  801. * Enter into the current subtree.
  802. * <p>
  803. * If the current entry is a subtree this method arranges for its children
  804. * to be returned before the next sibling following the subtree is returned.
  805. *
  806. * @throws MissingObjectException
  807. * a subtree was found, but the subtree object does not exist in
  808. * this repository. The repository may be missing objects.
  809. * @throws IncorrectObjectTypeException
  810. * a subtree was found, and the subtree id does not denote a
  811. * tree, but instead names some other non-tree type of object.
  812. * The repository may have data corruption.
  813. * @throws CorruptObjectException
  814. * the contents of a tree did not appear to be a tree. The
  815. * repository may have data corruption.
  816. * @throws IOException
  817. * a loose object or pack file could not be read.
  818. */
  819. public void enterSubtree() throws MissingObjectException,
  820. IncorrectObjectTypeException, CorruptObjectException, IOException {
  821. final AbstractTreeIterator ch = currentHead;
  822. final AbstractTreeIterator[] tmp = new AbstractTreeIterator[trees.length];
  823. for (int i = 0; i < trees.length; i++) {
  824. final AbstractTreeIterator t = trees[i];
  825. final AbstractTreeIterator n;
  826. if (t.matches == ch && !t.eof() && FileMode.TREE.equals(t.mode))
  827. n = t.createSubtreeIterator(reader, idBuffer);
  828. else
  829. n = t.createEmptyTreeIterator();
  830. tmp[i] = n;
  831. }
  832. depth++;
  833. advance = false;
  834. System.arraycopy(tmp, 0, trees, 0, trees.length);
  835. }
  836. AbstractTreeIterator min() throws CorruptObjectException {
  837. int i = 0;
  838. AbstractTreeIterator minRef = trees[i];
  839. while (minRef.eof() && ++i < trees.length)
  840. minRef = trees[i];
  841. if (minRef.eof())
  842. return minRef;
  843. minRef.matches = minRef;
  844. while (++i < trees.length) {
  845. final AbstractTreeIterator t = trees[i];
  846. if (t.eof())
  847. continue;
  848. final int cmp = t.pathCompare(minRef);
  849. if (cmp < 0) {
  850. t.matches = t;
  851. minRef = t;
  852. } else if (cmp == 0) {
  853. t.matches = minRef;
  854. }
  855. }
  856. return minRef;
  857. }
  858. void popEntriesEqual() throws CorruptObjectException {
  859. final AbstractTreeIterator ch = currentHead;
  860. for (int i = 0; i < trees.length; i++) {
  861. final AbstractTreeIterator t = trees[i];
  862. if (t.matches == ch) {
  863. t.next(1);
  864. t.matches = null;
  865. }
  866. }
  867. }
  868. void skipEntriesEqual() throws CorruptObjectException {
  869. final AbstractTreeIterator ch = currentHead;
  870. for (int i = 0; i < trees.length; i++) {
  871. final AbstractTreeIterator t = trees[i];
  872. if (t.matches == ch) {
  873. t.skip();
  874. t.matches = null;
  875. }
  876. }
  877. }
  878. private void exitSubtree() {
  879. depth--;
  880. for (int i = 0; i < trees.length; i++)
  881. trees[i] = trees[i].parent;
  882. AbstractTreeIterator minRef = null;
  883. for (final AbstractTreeIterator t : trees) {
  884. if (t.matches != t)
  885. continue;
  886. if (minRef == null || t.pathCompare(minRef) < 0)
  887. minRef = t;
  888. }
  889. currentHead = minRef;
  890. }
  891. private CanonicalTreeParser parserFor(final AnyObjectId id)
  892. throws IncorrectObjectTypeException, IOException {
  893. final CanonicalTreeParser p = new CanonicalTreeParser();
  894. p.reset(reader, id);
  895. return p;
  896. }
  897. static String pathOf(final AbstractTreeIterator t) {
  898. return RawParseUtils.decode(Constants.CHARSET, t.path, 0, t.pathLen);
  899. }
  900. static String pathOf(final byte[] buf, int pos, int end) {
  901. return RawParseUtils.decode(Constants.CHARSET, buf, pos, end);
  902. }
  903. }