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.

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