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

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