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.

AbstractTreeIterator.java 24KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771
  1. /*
  2. * Copyright (C) 2008-2009, Google Inc.
  3. * Copyright (C) 2007, Robin Rosenberg <robin.rosenberg@dewire.com>
  4. * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
  5. * and other copyright owners as documented in the project's IP log.
  6. *
  7. * This program and the accompanying materials are made available
  8. * under the terms of the Eclipse Distribution License v1.0 which
  9. * accompanies this distribution, is reproduced below, and is
  10. * available at http://www.eclipse.org/org/documents/edl-v10.php
  11. *
  12. * All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or
  15. * without modification, are permitted provided that the following
  16. * conditions are met:
  17. *
  18. * - Redistributions of source code must retain the above copyright
  19. * notice, this list of conditions and the following disclaimer.
  20. *
  21. * - Redistributions in binary form must reproduce the above
  22. * copyright notice, this list of conditions and the following
  23. * disclaimer in the documentation and/or other materials provided
  24. * with the distribution.
  25. *
  26. * - Neither the name of the Eclipse Foundation, Inc. nor the
  27. * names of its contributors may be used to endorse or promote
  28. * products derived from this software without specific prior
  29. * written permission.
  30. *
  31. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  32. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  33. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  34. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  35. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  36. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  37. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  38. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  39. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  40. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  41. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  42. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  43. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  44. */
  45. package org.eclipse.jgit.treewalk;
  46. import static java.nio.charset.StandardCharsets.UTF_8;
  47. import java.io.IOException;
  48. import java.nio.ByteBuffer;
  49. import java.nio.CharBuffer;
  50. import org.eclipse.jgit.attributes.AttributesHandler;
  51. import org.eclipse.jgit.attributes.AttributesNode;
  52. import org.eclipse.jgit.errors.CorruptObjectException;
  53. import org.eclipse.jgit.errors.IncorrectObjectTypeException;
  54. import org.eclipse.jgit.lib.Constants;
  55. import org.eclipse.jgit.lib.FileMode;
  56. import org.eclipse.jgit.lib.MutableObjectId;
  57. import org.eclipse.jgit.lib.ObjectId;
  58. import org.eclipse.jgit.lib.ObjectReader;
  59. import org.eclipse.jgit.util.Paths;
  60. /**
  61. * Walks a Git tree (directory) in Git sort order.
  62. * <p>
  63. * A new iterator instance should be positioned on the first entry, or at eof.
  64. * Data for the first entry (if not at eof) should be available immediately.
  65. * <p>
  66. * Implementors must walk a tree in the Git sort order, which has the following
  67. * odd sorting:
  68. * <ol>
  69. * <li>A.c</li>
  70. * <li>A/c</li>
  71. * <li>A0c</li>
  72. * </ol>
  73. * <p>
  74. * In the second item, <code>A</code> is the name of a subtree and
  75. * <code>c</code> is a file within that subtree. The other two items are files
  76. * in the root level tree.
  77. *
  78. * @see CanonicalTreeParser
  79. */
  80. public abstract class AbstractTreeIterator {
  81. /** Default size for the {@link #path} buffer. */
  82. protected static final int DEFAULT_PATH_SIZE = 128;
  83. /** A dummy object id buffer that matches the zero ObjectId. */
  84. protected static final byte[] zeroid = new byte[Constants.OBJECT_ID_LENGTH];
  85. /**
  86. * Iterator for the parent tree; null if we are the root iterator.
  87. * <p>
  88. * Used by {@link TreeWalk} and {@link AttributesHandler}
  89. *
  90. * @since 4.3
  91. */
  92. public final AbstractTreeIterator parent;
  93. /** The iterator this current entry is path equal to. */
  94. AbstractTreeIterator matches;
  95. /**
  96. * Parsed rules of .gitattributes file if it exists.
  97. *
  98. * @since 4.2
  99. */
  100. protected AttributesNode attributesNode;
  101. /**
  102. * Number of entries we moved forward to force a D/F conflict match.
  103. *
  104. * @see NameConflictTreeWalk
  105. */
  106. int matchShift;
  107. /**
  108. * Mode bits for the current entry.
  109. * <p>
  110. * A numerical value from FileMode is usually faster for an iterator to
  111. * obtain from its data source so this is the preferred representation.
  112. *
  113. * @see org.eclipse.jgit.lib.FileMode
  114. */
  115. protected int mode;
  116. /**
  117. * Path buffer for the current entry.
  118. * <p>
  119. * This buffer is pre-allocated at the start of walking and is shared from
  120. * parent iterators down into their subtree iterators. The sharing allows
  121. * the current entry to always be a full path from the root, while each
  122. * subtree only needs to populate the part that is under their control.
  123. */
  124. protected byte[] path;
  125. /**
  126. * Position within {@link #path} this iterator starts writing at.
  127. * <p>
  128. * This is the first offset in {@link #path} that this iterator must
  129. * populate during {@link #next}. At the root level (when {@link #parent}
  130. * is null) this is 0. For a subtree iterator the index before this position
  131. * should have the value '/'.
  132. */
  133. protected final int pathOffset;
  134. /**
  135. * Total length of the current entry's complete path from the root.
  136. * <p>
  137. * This is the number of bytes within {@link #path} that pertain to the
  138. * current entry. Values at this index through the end of the array are
  139. * garbage and may be randomly populated from prior entries.
  140. */
  141. protected int pathLen;
  142. /**
  143. * Create a new iterator with no parent.
  144. */
  145. protected AbstractTreeIterator() {
  146. parent = null;
  147. path = new byte[DEFAULT_PATH_SIZE];
  148. pathOffset = 0;
  149. }
  150. /**
  151. * Create a new iterator with no parent and a prefix.
  152. * <p>
  153. * The prefix path supplied is inserted in front of all paths generated by
  154. * this iterator. It is intended to be used when an iterator is being
  155. * created for a subsection of an overall repository and needs to be
  156. * combined with other iterators that are created to run over the entire
  157. * repository namespace.
  158. *
  159. * @param prefix
  160. * position of this iterator in the repository tree. The value
  161. * may be null or the empty string to indicate the prefix is the
  162. * root of the repository. A trailing slash ('/') is
  163. * automatically appended if the prefix does not end in '/'.
  164. */
  165. protected AbstractTreeIterator(String prefix) {
  166. parent = null;
  167. if (prefix != null && prefix.length() > 0) {
  168. final ByteBuffer b;
  169. b = UTF_8.encode(CharBuffer.wrap(prefix));
  170. pathLen = b.limit();
  171. path = new byte[Math.max(DEFAULT_PATH_SIZE, pathLen + 1)];
  172. b.get(path, 0, pathLen);
  173. if (path[pathLen - 1] != '/')
  174. path[pathLen++] = '/';
  175. pathOffset = pathLen;
  176. } else {
  177. path = new byte[DEFAULT_PATH_SIZE];
  178. pathOffset = 0;
  179. }
  180. }
  181. /**
  182. * Create a new iterator with no parent and a prefix.
  183. * <p>
  184. * The prefix path supplied is inserted in front of all paths generated by
  185. * this iterator. It is intended to be used when an iterator is being
  186. * created for a subsection of an overall repository and needs to be
  187. * combined with other iterators that are created to run over the entire
  188. * repository namespace.
  189. *
  190. * @param prefix
  191. * position of this iterator in the repository tree. The value
  192. * may be null or the empty array to indicate the prefix is the
  193. * root of the repository. A trailing slash ('/') is
  194. * automatically appended if the prefix does not end in '/'.
  195. */
  196. protected AbstractTreeIterator(byte[] prefix) {
  197. parent = null;
  198. if (prefix != null && prefix.length > 0) {
  199. pathLen = prefix.length;
  200. path = new byte[Math.max(DEFAULT_PATH_SIZE, pathLen + 1)];
  201. System.arraycopy(prefix, 0, path, 0, pathLen);
  202. if (path[pathLen - 1] != '/')
  203. path[pathLen++] = '/';
  204. pathOffset = pathLen;
  205. } else {
  206. path = new byte[DEFAULT_PATH_SIZE];
  207. pathOffset = 0;
  208. }
  209. }
  210. /**
  211. * Create an iterator for a subtree of an existing iterator.
  212. *
  213. * @param p
  214. * parent tree iterator.
  215. */
  216. protected AbstractTreeIterator(AbstractTreeIterator p) {
  217. parent = p;
  218. path = p.path;
  219. pathOffset = p.pathLen + 1;
  220. if (pathOffset > path.length) {
  221. growPath(p.pathLen);
  222. }
  223. path[pathOffset - 1] = '/';
  224. }
  225. /**
  226. * Create an iterator for a subtree of an existing iterator.
  227. * <p>
  228. * The caller is responsible for setting up the path of the child iterator.
  229. *
  230. * @param p
  231. * parent tree iterator.
  232. * @param childPath
  233. * path array to be used by the child iterator. This path must
  234. * contain the path from the top of the walk to the first child
  235. * and must end with a '/'.
  236. * @param childPathOffset
  237. * position within <code>childPath</code> where the child can
  238. * insert its data. The value at
  239. * <code>childPath[childPathOffset-1]</code> must be '/'.
  240. */
  241. protected AbstractTreeIterator(final AbstractTreeIterator p,
  242. final byte[] childPath, final int childPathOffset) {
  243. parent = p;
  244. path = childPath;
  245. pathOffset = childPathOffset;
  246. }
  247. /**
  248. * Grow the path buffer larger.
  249. *
  250. * @param len
  251. * number of live bytes in the path buffer. This many bytes will
  252. * be moved into the larger buffer.
  253. */
  254. protected void growPath(int len) {
  255. setPathCapacity(path.length << 1, len);
  256. }
  257. /**
  258. * Ensure that path is capable to hold at least {@code capacity} bytes
  259. *
  260. * @param capacity
  261. * the amount of bytes to hold
  262. * @param len
  263. * the amount of live bytes in path buffer
  264. */
  265. protected void ensurePathCapacity(int capacity, int len) {
  266. if (path.length >= capacity)
  267. return;
  268. final byte[] o = path;
  269. int current = o.length;
  270. int newCapacity = current;
  271. while (newCapacity < capacity && newCapacity > 0)
  272. newCapacity <<= 1;
  273. setPathCapacity(newCapacity, len);
  274. }
  275. /**
  276. * Set path buffer capacity to the specified size
  277. *
  278. * @param capacity
  279. * the new size
  280. * @param len
  281. * the amount of bytes to copy
  282. */
  283. private void setPathCapacity(int capacity, int len) {
  284. final byte[] o = path;
  285. final byte[] n = new byte[capacity];
  286. System.arraycopy(o, 0, n, 0, len);
  287. for (AbstractTreeIterator p = this; p != null && p.path == o; p = p.parent)
  288. p.path = n;
  289. }
  290. /**
  291. * Compare the path of this current entry to another iterator's entry.
  292. *
  293. * @param p
  294. * the other iterator to compare the path against.
  295. * @return -1 if this entry sorts first; 0 if the entries are equal; 1 if
  296. * p's entry sorts first.
  297. */
  298. public int pathCompare(AbstractTreeIterator p) {
  299. return pathCompare(p, p.mode);
  300. }
  301. int pathCompare(AbstractTreeIterator p, int pMode) {
  302. // Its common when we are a subtree for both parents to match;
  303. // when this happens everything in path[0..cPos] is known to
  304. // be equal and does not require evaluation again.
  305. //
  306. int cPos = alreadyMatch(this, p);
  307. return pathCompare(p.path, cPos, p.pathLen, pMode, cPos);
  308. }
  309. /**
  310. * Seek the iterator on a file, if present.
  311. *
  312. * @param name
  313. * file name to find (will not find a directory).
  314. * @return true if the file exists in this tree; false otherwise.
  315. * @throws org.eclipse.jgit.errors.CorruptObjectException
  316. * tree is invalid.
  317. * @since 4.2
  318. */
  319. public boolean findFile(String name) throws CorruptObjectException {
  320. return findFile(Constants.encode(name));
  321. }
  322. /**
  323. * Seek the iterator on a file, if present.
  324. *
  325. * @param name
  326. * file name to find (will not find a directory).
  327. * @return true if the file exists in this tree; false otherwise.
  328. * @throws org.eclipse.jgit.errors.CorruptObjectException
  329. * tree is invalid.
  330. * @since 4.2
  331. */
  332. public boolean findFile(byte[] name) throws CorruptObjectException {
  333. for (; !eof(); next(1)) {
  334. int cmp = pathCompare(name, 0, name.length, 0, pathOffset);
  335. if (cmp == 0) {
  336. return true;
  337. } else if (cmp > 0) {
  338. return false;
  339. }
  340. }
  341. return false;
  342. }
  343. /**
  344. * Compare the path of this current entry to a raw buffer.
  345. *
  346. * @param buf
  347. * the raw path buffer.
  348. * @param pos
  349. * position to start reading the raw buffer.
  350. * @param end
  351. * one past the end of the raw buffer (length is end - pos).
  352. * @param pathMode
  353. * the mode of the path.
  354. * @return -1 if this entry sorts first; 0 if the entries are equal; 1 if
  355. * p's entry sorts first.
  356. */
  357. public int pathCompare(byte[] buf, int pos, int end, int pathMode) {
  358. return pathCompare(buf, pos, end, pathMode, 0);
  359. }
  360. private int pathCompare(byte[] b, int bPos, int bEnd, int bMode, int aPos) {
  361. return Paths.compare(
  362. path, aPos, pathLen, mode,
  363. b, bPos, bEnd, bMode);
  364. }
  365. private static int alreadyMatch(AbstractTreeIterator a,
  366. AbstractTreeIterator b) {
  367. for (;;) {
  368. final AbstractTreeIterator ap = a.parent;
  369. final AbstractTreeIterator bp = b.parent;
  370. if (ap == null || bp == null)
  371. return 0;
  372. if (ap.matches == bp.matches)
  373. return a.pathOffset;
  374. a = ap;
  375. b = bp;
  376. }
  377. }
  378. /**
  379. * Check if the current entry of both iterators has the same id.
  380. * <p>
  381. * This method is faster than {@link #getEntryObjectId()} as it does not
  382. * require copying the bytes out of the buffers. A direct {@link #idBuffer}
  383. * compare operation is performed.
  384. *
  385. * @param otherIterator
  386. * the other iterator to test against.
  387. * @return true if both iterators have the same object id; false otherwise.
  388. */
  389. public boolean idEqual(AbstractTreeIterator otherIterator) {
  390. return ObjectId.equals(idBuffer(), idOffset(),
  391. otherIterator.idBuffer(), otherIterator.idOffset());
  392. }
  393. /**
  394. * Whether the entry has a valid ObjectId.
  395. *
  396. * @return {@code true} if the entry has a valid ObjectId.
  397. */
  398. public abstract boolean hasId();
  399. /**
  400. * Get the object id of the current entry.
  401. *
  402. * @return an object id for the current entry.
  403. */
  404. public ObjectId getEntryObjectId() {
  405. return ObjectId.fromRaw(idBuffer(), idOffset());
  406. }
  407. /**
  408. * Obtain the ObjectId for the current entry.
  409. *
  410. * @param out
  411. * buffer to copy the object id into.
  412. */
  413. public void getEntryObjectId(MutableObjectId out) {
  414. out.fromRaw(idBuffer(), idOffset());
  415. }
  416. /**
  417. * Get the file mode of the current entry.
  418. *
  419. * @return the file mode of the current entry.
  420. */
  421. public FileMode getEntryFileMode() {
  422. return FileMode.fromBits(mode);
  423. }
  424. /**
  425. * Get the file mode of the current entry as bits.
  426. *
  427. * @return the file mode of the current entry as bits.
  428. */
  429. public int getEntryRawMode() {
  430. return mode;
  431. }
  432. /**
  433. * Get path of the current entry, as a string.
  434. *
  435. * @return path of the current entry, as a string.
  436. */
  437. public String getEntryPathString() {
  438. return TreeWalk.pathOf(this);
  439. }
  440. /**
  441. * Get the current entry path buffer.
  442. * <p>
  443. * Note that the returned byte[] has to be used together with
  444. * {@link #getEntryPathLength()} (only use bytes up to this length).
  445. *
  446. * @return the internal buffer holding the current path.
  447. */
  448. public byte[] getEntryPathBuffer() {
  449. return path;
  450. }
  451. /**
  452. * Get length of the path in {@link #getEntryPathBuffer()}.
  453. *
  454. * @return length of the path in {@link #getEntryPathBuffer()}.
  455. */
  456. public int getEntryPathLength() {
  457. return pathLen;
  458. }
  459. /**
  460. * Get the current entry's path hash code.
  461. * <p>
  462. * This method computes a hash code on the fly for this path, the hash is
  463. * suitable to cluster objects that may have similar paths together.
  464. *
  465. * @return path hash code; any integer may be returned.
  466. */
  467. public int getEntryPathHashCode() {
  468. int hash = 0;
  469. for (int i = Math.max(0, pathLen - 16); i < pathLen; i++) {
  470. byte c = path[i];
  471. if (c != ' ')
  472. hash = (hash >>> 2) + (c << 24);
  473. }
  474. return hash;
  475. }
  476. /**
  477. * Get the byte array buffer object IDs must be copied out of.
  478. * <p>
  479. * The id buffer contains the bytes necessary to construct an ObjectId for
  480. * the current entry of this iterator. The buffer can be the same buffer for
  481. * all entries, or it can be a unique buffer per-entry. Implementations are
  482. * encouraged to expose their private buffer whenever possible to reduce
  483. * garbage generation and copying costs.
  484. *
  485. * @return byte array the implementation stores object IDs within.
  486. * @see #getEntryObjectId()
  487. */
  488. public abstract byte[] idBuffer();
  489. /**
  490. * Get the position within {@link #idBuffer()} of this entry's ObjectId.
  491. *
  492. * @return offset into the array returned by {@link #idBuffer()} where the
  493. * ObjectId must be copied out of.
  494. */
  495. public abstract int idOffset();
  496. /**
  497. * Create a new iterator for the current entry's subtree.
  498. * <p>
  499. * The parent reference of the iterator must be <code>this</code>,
  500. * otherwise the caller would not be able to exit out of the subtree
  501. * iterator correctly and return to continue walking <code>this</code>.
  502. *
  503. * @param reader
  504. * reader to load the tree data from.
  505. * @return a new parser that walks over the current subtree.
  506. * @throws org.eclipse.jgit.errors.IncorrectObjectTypeException
  507. * the current entry is not actually a tree and cannot be parsed
  508. * as though it were a tree.
  509. * @throws java.io.IOException
  510. * a loose object or pack file could not be read.
  511. */
  512. public abstract AbstractTreeIterator createSubtreeIterator(
  513. ObjectReader reader) throws IncorrectObjectTypeException,
  514. IOException;
  515. /**
  516. * Create a new iterator as though the current entry were a subtree.
  517. *
  518. * @return a new empty tree iterator.
  519. */
  520. public EmptyTreeIterator createEmptyTreeIterator() {
  521. return new EmptyTreeIterator(this);
  522. }
  523. /**
  524. * Create a new iterator for the current entry's subtree.
  525. * <p>
  526. * The parent reference of the iterator must be <code>this</code>, otherwise
  527. * the caller would not be able to exit out of the subtree iterator
  528. * correctly and return to continue walking <code>this</code>.
  529. *
  530. * @param reader
  531. * reader to load the tree data from.
  532. * @param idBuffer
  533. * temporary ObjectId buffer for use by this method.
  534. * @return a new parser that walks over the current subtree.
  535. * @throws org.eclipse.jgit.errors.IncorrectObjectTypeException
  536. * the current entry is not actually a tree and cannot be parsed
  537. * as though it were a tree.
  538. * @throws java.io.IOException
  539. * a loose object or pack file could not be read.
  540. */
  541. public AbstractTreeIterator createSubtreeIterator(
  542. final ObjectReader reader, final MutableObjectId idBuffer)
  543. throws IncorrectObjectTypeException, IOException {
  544. return createSubtreeIterator(reader);
  545. }
  546. /**
  547. * Position this iterator on the first entry.
  548. *
  549. * The default implementation of this method uses {@code back(1)} until
  550. * {@code first()} is true. This is most likely not the most efficient
  551. * method of repositioning the iterator to its first entry, so subclasses
  552. * are strongly encouraged to override the method.
  553. *
  554. * @throws org.eclipse.jgit.errors.CorruptObjectException
  555. * the tree is invalid.
  556. */
  557. public void reset() throws CorruptObjectException {
  558. while (!first())
  559. back(1);
  560. }
  561. /**
  562. * Is this tree iterator positioned on its first entry?
  563. * <p>
  564. * An iterator is positioned on the first entry if <code>back(1)</code>
  565. * would be an invalid request as there is no entry before the current one.
  566. * <p>
  567. * An empty iterator (one with no entries) will be
  568. * <code>first() &amp;&amp; eof()</code>.
  569. *
  570. * @return true if the iterator is positioned on the first entry.
  571. */
  572. public abstract boolean first();
  573. /**
  574. * Is this tree iterator at its EOF point (no more entries)?
  575. * <p>
  576. * An iterator is at EOF if there is no current entry.
  577. *
  578. * @return true if we have walked all entries and have none left.
  579. */
  580. public abstract boolean eof();
  581. /**
  582. * Move to next entry, populating this iterator with the entry data.
  583. * <p>
  584. * The delta indicates how many moves forward should occur. The most common
  585. * delta is 1 to move to the next entry.
  586. * <p>
  587. * Implementations must populate the following members:
  588. * <ul>
  589. * <li>{@link #mode}</li>
  590. * <li>{@link #path} (from {@link #pathOffset} to {@link #pathLen})</li>
  591. * <li>{@link #pathLen}</li>
  592. * </ul>
  593. * as well as any implementation dependent information necessary to
  594. * accurately return data from {@link #idBuffer()} and {@link #idOffset()}
  595. * when demanded.
  596. *
  597. * @param delta
  598. * number of entries to move the iterator by. Must be a positive,
  599. * non-zero integer.
  600. * @throws org.eclipse.jgit.errors.CorruptObjectException
  601. * the tree is invalid.
  602. */
  603. public abstract void next(int delta) throws CorruptObjectException;
  604. /**
  605. * Move to prior entry, populating this iterator with the entry data.
  606. * <p>
  607. * The delta indicates how many moves backward should occur.The most common
  608. * delta is 1 to move to the prior entry.
  609. * <p>
  610. * Implementations must populate the following members:
  611. * <ul>
  612. * <li>{@link #mode}</li>
  613. * <li>{@link #path} (from {@link #pathOffset} to {@link #pathLen})</li>
  614. * <li>{@link #pathLen}</li>
  615. * </ul>
  616. * as well as any implementation dependent information necessary to
  617. * accurately return data from {@link #idBuffer()} and {@link #idOffset()}
  618. * when demanded.
  619. *
  620. * @param delta
  621. * number of entries to move the iterator by. Must be a positive,
  622. * non-zero integer.
  623. * @throws org.eclipse.jgit.errors.CorruptObjectException
  624. * the tree is invalid.
  625. */
  626. public abstract void back(int delta) throws CorruptObjectException;
  627. /**
  628. * Advance to the next tree entry, populating this iterator with its data.
  629. * <p>
  630. * This method behaves like <code>seek(1)</code> but is called by
  631. * {@link org.eclipse.jgit.treewalk.TreeWalk} only if a
  632. * {@link org.eclipse.jgit.treewalk.filter.TreeFilter} was used and ruled
  633. * out the current entry from the results. In such cases this tree iterator
  634. * may perform special behavior.
  635. *
  636. * @throws org.eclipse.jgit.errors.CorruptObjectException
  637. * the tree is invalid.
  638. */
  639. public void skip() throws CorruptObjectException {
  640. next(1);
  641. }
  642. /**
  643. * Indicates to the iterator that no more entries will be read.
  644. * <p>
  645. * This is only invoked by TreeWalk when the iteration is aborted early due
  646. * to a {@link org.eclipse.jgit.errors.StopWalkException} being thrown from
  647. * within a TreeFilter.
  648. */
  649. public void stopWalk() {
  650. // Do nothing by default. Most iterators do not care.
  651. }
  652. /**
  653. * Whether the iterator implements {@link #stopWalk()}.
  654. *
  655. * @return {@code true} if the iterator implements {@link #stopWalk()}.
  656. * @since 4.2
  657. */
  658. protected boolean needsStopWalk() {
  659. return false;
  660. }
  661. /**
  662. * Get the length of the name component of the path for the current entry.
  663. *
  664. * @return the length of the name component of the path for the current
  665. * entry.
  666. */
  667. public int getNameLength() {
  668. return pathLen - pathOffset;
  669. }
  670. /**
  671. * JGit internal API for use by
  672. * {@link org.eclipse.jgit.dircache.DirCacheCheckout}
  673. *
  674. * @return start of name component part within {@link #getEntryPathBuffer()}
  675. * @since 2.0
  676. */
  677. public int getNameOffset() {
  678. return pathOffset;
  679. }
  680. /**
  681. * Get the name component of the current entry path into the provided
  682. * buffer.
  683. *
  684. * @param buffer
  685. * the buffer to get the name into, it is assumed that buffer can
  686. * hold the name
  687. * @param offset
  688. * the offset of the name in the buffer
  689. * @see #getNameLength()
  690. */
  691. public void getName(byte[] buffer, int offset) {
  692. System.arraycopy(path, pathOffset, buffer, offset, pathLen - pathOffset);
  693. }
  694. /** {@inheritDoc} */
  695. @SuppressWarnings("nls")
  696. @Override
  697. public String toString() {
  698. return getClass().getSimpleName() + "[" + getEntryPathString() + "]"; //$NON-NLS-1$
  699. }
  700. /**
  701. * Whether or not this Iterator is iterating through the working tree.
  702. *
  703. * @return whether or not this Iterator is iterating through the working
  704. * tree
  705. * @since 4.3
  706. */
  707. public boolean isWorkTree() {
  708. return false;
  709. }
  710. }