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.5KB

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