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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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.io.InputStream;
  47. import java.util.Collections;
  48. import org.eclipse.jgit.attributes.AttributesNode;
  49. import org.eclipse.jgit.attributes.AttributesRule;
  50. import org.eclipse.jgit.errors.IncorrectObjectTypeException;
  51. import org.eclipse.jgit.lib.Constants;
  52. import org.eclipse.jgit.lib.FileMode;
  53. import org.eclipse.jgit.lib.ObjectId;
  54. import org.eclipse.jgit.lib.ObjectLoader;
  55. import org.eclipse.jgit.lib.ObjectReader;
  56. import org.eclipse.jgit.treewalk.AbstractTreeIterator;
  57. import org.eclipse.jgit.treewalk.EmptyTreeIterator;
  58. import org.eclipse.jgit.util.RawParseUtils;
  59. /**
  60. * Iterate a {@link DirCache} as part of a <code>TreeWalk</code>.
  61. * <p>
  62. * This is an iterator to adapt a loaded <code>DirCache</code> instance (such as
  63. * read from an existing <code>.git/index</code> file) to the tree structure
  64. * used by a <code>TreeWalk</code>, making it possible for applications to walk
  65. * over any combination of tree objects already in the object database, index
  66. * files, or working directories.
  67. *
  68. * @see org.eclipse.jgit.treewalk.TreeWalk
  69. */
  70. public class DirCacheIterator extends AbstractTreeIterator {
  71. /** Byte array holding ".gitattributes" string */
  72. private static final byte[] DOT_GIT_ATTRIBUTES_BYTES = Constants.DOT_GIT_ATTRIBUTES
  73. .getBytes();
  74. /** The cache this iterator was created to walk. */
  75. protected final DirCache cache;
  76. /** The tree this iterator is walking. */
  77. private final DirCacheTree tree;
  78. /** First position in this tree. */
  79. private final int treeStart;
  80. /** Last position in this tree. */
  81. private final int treeEnd;
  82. /** Special buffer to hold the ObjectId of {@link #currentSubtree}. */
  83. private final byte[] subtreeId;
  84. /** Index of entry within {@link #cache}. */
  85. protected int ptr;
  86. /** Next subtree to consider within {@link #tree}. */
  87. private int nextSubtreePos;
  88. /** The current file entry from {@link #cache}. */
  89. protected DirCacheEntry currentEntry;
  90. /** The subtree containing {@link #currentEntry} if this is first entry. */
  91. protected DirCacheTree currentSubtree;
  92. /** Holds an {@link AttributesNode} for the current entry */
  93. private AttributesNode attributesNode;
  94. /**
  95. * Create a new iterator for an already loaded DirCache instance.
  96. * <p>
  97. * The iterator implementation may copy part of the cache's data during
  98. * construction, so the cache must be read in prior to creating the
  99. * iterator.
  100. *
  101. * @param dc
  102. * the cache to walk. It must be already loaded into memory.
  103. */
  104. public DirCacheIterator(final DirCache dc) {
  105. cache = dc;
  106. tree = dc.getCacheTree(true);
  107. treeStart = 0;
  108. treeEnd = tree.getEntrySpan();
  109. subtreeId = new byte[Constants.OBJECT_ID_LENGTH];
  110. if (!eof())
  111. parseEntry();
  112. }
  113. DirCacheIterator(final DirCacheIterator p, final DirCacheTree dct) {
  114. super(p, p.path, p.pathLen + 1);
  115. cache = p.cache;
  116. tree = dct;
  117. treeStart = p.ptr;
  118. treeEnd = treeStart + tree.getEntrySpan();
  119. subtreeId = p.subtreeId;
  120. ptr = p.ptr;
  121. parseEntry();
  122. }
  123. @Override
  124. public AbstractTreeIterator createSubtreeIterator(final ObjectReader reader)
  125. throws IncorrectObjectTypeException, IOException {
  126. if (currentSubtree == null)
  127. throw new IncorrectObjectTypeException(getEntryObjectId(),
  128. Constants.TYPE_TREE);
  129. return new DirCacheIterator(this, currentSubtree);
  130. }
  131. @Override
  132. public EmptyTreeIterator createEmptyTreeIterator() {
  133. final byte[] n = new byte[Math.max(pathLen + 1, DEFAULT_PATH_SIZE)];
  134. System.arraycopy(path, 0, n, 0, pathLen);
  135. n[pathLen] = '/';
  136. return new EmptyTreeIterator(this, n, pathLen + 1);
  137. }
  138. @Override
  139. public boolean hasId() {
  140. if (currentSubtree != null)
  141. return currentSubtree.isValid();
  142. return currentEntry != null;
  143. }
  144. @Override
  145. public byte[] idBuffer() {
  146. if (currentSubtree != null)
  147. return currentSubtree.isValid() ? subtreeId : zeroid;
  148. if (currentEntry != null)
  149. return currentEntry.idBuffer();
  150. return zeroid;
  151. }
  152. @Override
  153. public int idOffset() {
  154. if (currentSubtree != null)
  155. return 0;
  156. if (currentEntry != null)
  157. return currentEntry.idOffset();
  158. return 0;
  159. }
  160. @Override
  161. public void reset() {
  162. if (!first()) {
  163. ptr = treeStart;
  164. nextSubtreePos = 0;
  165. currentEntry = null;
  166. currentSubtree = null;
  167. if (!eof())
  168. parseEntry();
  169. }
  170. }
  171. @Override
  172. public boolean first() {
  173. return ptr == treeStart;
  174. }
  175. @Override
  176. public boolean eof() {
  177. return ptr == treeEnd;
  178. }
  179. @Override
  180. public void next(int delta) {
  181. while (--delta >= 0) {
  182. if (currentSubtree != null)
  183. ptr += currentSubtree.getEntrySpan();
  184. else
  185. ptr++;
  186. if (eof())
  187. break;
  188. parseEntry();
  189. }
  190. }
  191. @Override
  192. public void back(int delta) {
  193. while (--delta >= 0) {
  194. if (currentSubtree != null)
  195. nextSubtreePos--;
  196. ptr--;
  197. parseEntry(false);
  198. if (currentSubtree != null)
  199. ptr -= currentSubtree.getEntrySpan() - 1;
  200. }
  201. }
  202. private void parseEntry() {
  203. parseEntry(true);
  204. }
  205. private void parseEntry(boolean forward) {
  206. currentEntry = cache.getEntry(ptr);
  207. final byte[] cep = currentEntry.path;
  208. if (!forward) {
  209. if (nextSubtreePos > 0) {
  210. final DirCacheTree p = tree.getChild(nextSubtreePos - 1);
  211. if (p.contains(cep, pathOffset, cep.length)) {
  212. nextSubtreePos--;
  213. currentSubtree = p;
  214. }
  215. }
  216. }
  217. if (nextSubtreePos != tree.getChildCount()) {
  218. final DirCacheTree s = tree.getChild(nextSubtreePos);
  219. if (s.contains(cep, pathOffset, cep.length)) {
  220. // The current position is the first file of this subtree.
  221. // Use the subtree instead as the current position.
  222. //
  223. currentSubtree = s;
  224. nextSubtreePos++;
  225. if (s.isValid())
  226. s.getObjectId().copyRawTo(subtreeId, 0);
  227. mode = FileMode.TREE.getBits();
  228. path = cep;
  229. pathLen = pathOffset + s.nameLength();
  230. return;
  231. }
  232. }
  233. // The current position is a file/symlink/gitlink so we
  234. // do not have a subtree located here.
  235. //
  236. mode = currentEntry.getRawMode();
  237. path = cep;
  238. pathLen = cep.length;
  239. currentSubtree = null;
  240. // Checks if this entry is a .gitattributes file
  241. if (RawParseUtils.match(path, pathOffset, DOT_GIT_ATTRIBUTES_BYTES) == path.length)
  242. attributesNode = new LazyLoadingAttributesNode(
  243. currentEntry.getObjectId());
  244. }
  245. /**
  246. * Get the DirCacheEntry for the current file.
  247. *
  248. * @return the current cache entry, if this iterator is positioned on a
  249. * non-tree.
  250. */
  251. public DirCacheEntry getDirCacheEntry() {
  252. return currentSubtree == null ? currentEntry : null;
  253. }
  254. /**
  255. * Retrieves the {@link AttributesNode} for the current entry.
  256. *
  257. * @param reader
  258. * {@link ObjectReader} used to parse the .gitattributes entry.
  259. * @return {@link AttributesNode} for the current entry.
  260. * @throws IOException
  261. * @since 3.7
  262. */
  263. public AttributesNode getEntryAttributesNode(ObjectReader reader)
  264. throws IOException {
  265. if (attributesNode instanceof LazyLoadingAttributesNode)
  266. attributesNode = ((LazyLoadingAttributesNode) attributesNode)
  267. .load(reader);
  268. return attributesNode;
  269. }
  270. /**
  271. * {@link AttributesNode} implementation that provides lazy loading
  272. * facilities.
  273. */
  274. private static class LazyLoadingAttributesNode extends AttributesNode {
  275. final ObjectId objectId;
  276. LazyLoadingAttributesNode(ObjectId objectId) {
  277. super(Collections.<AttributesRule> emptyList());
  278. this.objectId = objectId;
  279. }
  280. AttributesNode load(ObjectReader reader) throws IOException {
  281. AttributesNode r = new AttributesNode();
  282. ObjectLoader loader = reader.open(objectId);
  283. if (loader != null) {
  284. InputStream in = loader.openStream();
  285. try {
  286. r.parse(in);
  287. } finally {
  288. in.close();
  289. }
  290. }
  291. return r.getRules().isEmpty() ? null : r;
  292. }
  293. }
  294. }