Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

DirCacheIterator.java 9.7KB

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