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.

DirCacheIterator.java 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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.dircache;
  45. import java.io.IOException;
  46. import java.util.Arrays;
  47. import org.eclipse.jgit.errors.IncorrectObjectTypeException;
  48. import org.eclipse.jgit.lib.Constants;
  49. import org.eclipse.jgit.lib.FileMode;
  50. import org.eclipse.jgit.lib.ObjectReader;
  51. import org.eclipse.jgit.treewalk.AbstractTreeIterator;
  52. import org.eclipse.jgit.treewalk.EmptyTreeIterator;
  53. /**
  54. * Iterate a {@link DirCache} as part of a <code>TreeWalk</code>.
  55. * <p>
  56. * This is an iterator to adapt a loaded <code>DirCache</code> instance (such as
  57. * read from an existing <code>.git/index</code> file) to the tree structure
  58. * used by a <code>TreeWalk</code>, making it possible for applications to walk
  59. * over any combination of tree objects already in the object database, index
  60. * files, or working directories.
  61. *
  62. * @see org.eclipse.jgit.treewalk.TreeWalk
  63. */
  64. public class DirCacheIterator extends AbstractTreeIterator {
  65. /** The cache this iterator was created to walk. */
  66. protected final DirCache cache;
  67. /** The tree this iterator is walking. */
  68. private final DirCacheTree tree;
  69. /** First position in this tree. */
  70. private final int treeStart;
  71. /** Last position in this tree. */
  72. private final int treeEnd;
  73. /** Special buffer to hold the ObjectId of {@link #currentSubtree}. */
  74. private final byte[] subtreeId;
  75. /** Index of entry within {@link #cache}. */
  76. protected int ptr;
  77. /** Next subtree to consider within {@link #tree}. */
  78. private int nextSubtreePos;
  79. /** The current file entry from {@link #cache}. */
  80. protected DirCacheEntry currentEntry;
  81. /** The subtree containing {@link #currentEntry} if this is first entry. */
  82. protected DirCacheTree currentSubtree;
  83. /**
  84. * Create a new iterator for an already loaded DirCache instance.
  85. * <p>
  86. * The iterator implementation may copy part of the cache's data during
  87. * construction, so the cache must be read in prior to creating the
  88. * iterator.
  89. *
  90. * @param dc
  91. * the cache to walk. It must be already loaded into memory.
  92. */
  93. public DirCacheIterator(final DirCache dc) {
  94. cache = dc;
  95. tree = dc.getCacheTree(true);
  96. treeStart = 0;
  97. treeEnd = tree.getEntrySpan();
  98. subtreeId = new byte[Constants.OBJECT_ID_LENGTH];
  99. if (!eof())
  100. parseEntry();
  101. }
  102. DirCacheIterator(final DirCacheIterator p, final DirCacheTree dct) {
  103. super(p, p.path, p.pathLen + 1);
  104. cache = p.cache;
  105. tree = dct;
  106. treeStart = p.ptr;
  107. treeEnd = treeStart + tree.getEntrySpan();
  108. subtreeId = p.subtreeId;
  109. ptr = p.ptr;
  110. parseEntry();
  111. }
  112. @Override
  113. public AbstractTreeIterator createSubtreeIterator(final ObjectReader reader)
  114. throws IncorrectObjectTypeException, IOException {
  115. if (currentSubtree == null)
  116. throw new IncorrectObjectTypeException(getEntryObjectId(),
  117. Constants.TYPE_TREE);
  118. return new DirCacheIterator(this, currentSubtree);
  119. }
  120. @Override
  121. public EmptyTreeIterator createEmptyTreeIterator() {
  122. final byte[] n = new byte[Math.max(pathLen + 1, DEFAULT_PATH_SIZE)];
  123. System.arraycopy(path, 0, n, 0, pathLen);
  124. n[pathLen] = '/';
  125. return new EmptyTreeIterator(this, n, pathLen + 1);
  126. }
  127. @Override
  128. public byte[] idBuffer() {
  129. if (currentSubtree != null)
  130. return subtreeId;
  131. if (currentEntry != null)
  132. return currentEntry.idBuffer();
  133. return zeroid;
  134. }
  135. @Override
  136. public int idOffset() {
  137. if (currentSubtree != null)
  138. return 0;
  139. if (currentEntry != null)
  140. return currentEntry.idOffset();
  141. return 0;
  142. }
  143. @Override
  144. public void reset() {
  145. if (!first()) {
  146. ptr = treeStart;
  147. if (!eof())
  148. parseEntry();
  149. }
  150. }
  151. @Override
  152. public boolean first() {
  153. return ptr == treeStart;
  154. }
  155. @Override
  156. public boolean eof() {
  157. return ptr == treeEnd;
  158. }
  159. @Override
  160. public void next(int delta) {
  161. while (--delta >= 0) {
  162. if (currentSubtree != null)
  163. ptr += currentSubtree.getEntrySpan();
  164. else
  165. ptr++;
  166. if (eof())
  167. break;
  168. parseEntry();
  169. }
  170. }
  171. @Override
  172. public void back(int delta) {
  173. while (--delta >= 0) {
  174. if (currentSubtree != null)
  175. nextSubtreePos--;
  176. ptr--;
  177. parseEntry();
  178. if (currentSubtree != null)
  179. ptr -= currentSubtree.getEntrySpan() - 1;
  180. }
  181. }
  182. private void parseEntry() {
  183. currentEntry = cache.getEntry(ptr);
  184. final byte[] cep = currentEntry.path;
  185. if (nextSubtreePos != tree.getChildCount()) {
  186. final DirCacheTree s = tree.getChild(nextSubtreePos);
  187. if (s.contains(cep, pathOffset, cep.length)) {
  188. // The current position is the first file of this subtree.
  189. // Use the subtree instead as the current position.
  190. //
  191. currentSubtree = s;
  192. nextSubtreePos++;
  193. if (s.isValid())
  194. s.getObjectId().copyRawTo(subtreeId, 0);
  195. else
  196. Arrays.fill(subtreeId, (byte) 0);
  197. mode = FileMode.TREE.getBits();
  198. path = cep;
  199. pathLen = pathOffset + s.nameLength();
  200. return;
  201. }
  202. }
  203. // The current position is a file/symlink/gitlink so we
  204. // do not have a subtree located here.
  205. //
  206. mode = currentEntry.getRawMode();
  207. path = cep;
  208. pathLen = cep.length;
  209. currentSubtree = null;
  210. }
  211. /**
  212. * Get the DirCacheEntry for the current file.
  213. *
  214. * @return the current cache entry, if this iterator is positioned on a
  215. * non-tree.
  216. */
  217. public DirCacheEntry getDirCacheEntry() {
  218. return currentSubtree == null ? currentEntry : null;
  219. }
  220. }