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.

CanonicalTreeParser.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. /*
  2. * Copyright (C) 2008-2010, 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.treewalk;
  45. import static org.eclipse.jgit.lib.Constants.DOT_GIT_ATTRIBUTES;
  46. import static org.eclipse.jgit.lib.Constants.OBJECT_ID_LENGTH;
  47. import static org.eclipse.jgit.lib.Constants.OBJ_BLOB;
  48. import static org.eclipse.jgit.lib.Constants.OBJ_TREE;
  49. import static org.eclipse.jgit.lib.Constants.TYPE_TREE;
  50. import static org.eclipse.jgit.lib.Constants.encode;
  51. import java.io.IOException;
  52. import java.io.InputStream;
  53. import java.util.Arrays;
  54. import java.util.Collections;
  55. import org.eclipse.jgit.attributes.AttributesNode;
  56. import org.eclipse.jgit.attributes.AttributesRule;
  57. import org.eclipse.jgit.errors.IncorrectObjectTypeException;
  58. import org.eclipse.jgit.errors.MissingObjectException;
  59. import org.eclipse.jgit.lib.AnyObjectId;
  60. import org.eclipse.jgit.lib.FileMode;
  61. import org.eclipse.jgit.lib.MutableObjectId;
  62. import org.eclipse.jgit.lib.ObjectId;
  63. import org.eclipse.jgit.lib.ObjectReader;
  64. /**
  65. * Parses raw Git trees from the canonical semi-text/semi-binary format.
  66. */
  67. public class CanonicalTreeParser extends AbstractTreeIterator {
  68. private static final byte[] EMPTY = {};
  69. private static final byte[] ATTRS = encode(DOT_GIT_ATTRIBUTES);
  70. private byte[] raw;
  71. /** First offset within {@link #raw} of the prior entry. */
  72. private int prevPtr;
  73. /** First offset within {@link #raw} of the current entry's data. */
  74. private int currPtr;
  75. /** Offset one past the current entry (first byte of next entry). */
  76. private int nextPtr;
  77. /**
  78. * Create a new parser.
  79. */
  80. public CanonicalTreeParser() {
  81. reset(EMPTY);
  82. }
  83. /**
  84. * Create a new parser for a tree appearing in a subset of a repository.
  85. *
  86. * @param prefix
  87. * position of this iterator in the repository tree. The value
  88. * may be null or the empty array to indicate the prefix is the
  89. * root of the repository. A trailing slash ('/') is
  90. * automatically appended if the prefix does not end in '/'.
  91. * @param reader
  92. * reader to load the tree data from.
  93. * @param treeId
  94. * identity of the tree being parsed; used only in exception
  95. * messages if data corruption is found.
  96. * @throws MissingObjectException
  97. * the object supplied is not available from the repository.
  98. * @throws org.eclipse.jgit.errors.IncorrectObjectTypeException
  99. * the object supplied as an argument is not actually a tree and
  100. * cannot be parsed as though it were a tree.
  101. * @throws java.io.IOException
  102. * a loose object or pack file could not be read.
  103. */
  104. public CanonicalTreeParser(final byte[] prefix, final ObjectReader reader,
  105. final AnyObjectId treeId) throws IncorrectObjectTypeException,
  106. IOException {
  107. super(prefix);
  108. reset(reader, treeId);
  109. }
  110. private CanonicalTreeParser(CanonicalTreeParser p) {
  111. super(p);
  112. }
  113. /**
  114. * Get the parent of this tree parser.
  115. *
  116. * @return the parent of this tree parser.
  117. * @deprecated internal use only
  118. */
  119. @Deprecated
  120. public CanonicalTreeParser getParent() {
  121. return (CanonicalTreeParser) parent;
  122. }
  123. /**
  124. * Reset this parser to walk through the given tree data.
  125. *
  126. * @param treeData
  127. * the raw tree content.
  128. */
  129. public void reset(byte[] treeData) {
  130. attributesNode = null;
  131. raw = treeData;
  132. prevPtr = -1;
  133. currPtr = 0;
  134. if (eof())
  135. nextPtr = 0;
  136. else
  137. parseEntry();
  138. }
  139. /**
  140. * Reset this parser to walk through the given tree.
  141. *
  142. * @param reader
  143. * reader to use during repository access.
  144. * @param id
  145. * identity of the tree being parsed; used only in exception
  146. * messages if data corruption is found.
  147. * @return the root level parser.
  148. * @throws MissingObjectException
  149. * the object supplied is not available from the repository.
  150. * @throws org.eclipse.jgit.errors.IncorrectObjectTypeException
  151. * the object supplied as an argument is not actually a tree and
  152. * cannot be parsed as though it were a tree.
  153. * @throws java.io.IOException
  154. * a loose object or pack file could not be read.
  155. */
  156. public CanonicalTreeParser resetRoot(final ObjectReader reader,
  157. final AnyObjectId id) throws IncorrectObjectTypeException,
  158. IOException {
  159. CanonicalTreeParser p = this;
  160. while (p.parent != null)
  161. p = (CanonicalTreeParser) p.parent;
  162. p.reset(reader, id);
  163. return p;
  164. }
  165. /**
  166. * Get this iterator, or its parent, if the tree is at eof.
  167. *
  168. * @return this iterator, or its parent, if the tree is at eof.
  169. */
  170. public CanonicalTreeParser next() {
  171. CanonicalTreeParser p = this;
  172. for (;;) {
  173. if (p.nextPtr == p.raw.length) {
  174. // This parser has reached EOF, return to the parent.
  175. if (p.parent == null) {
  176. p.currPtr = p.nextPtr;
  177. return p;
  178. }
  179. p = (CanonicalTreeParser) p.parent;
  180. continue;
  181. }
  182. p.prevPtr = p.currPtr;
  183. p.currPtr = p.nextPtr;
  184. p.parseEntry();
  185. return p;
  186. }
  187. }
  188. /**
  189. * Reset this parser to walk through the given tree.
  190. *
  191. * @param reader
  192. * reader to use during repository access.
  193. * @param id
  194. * identity of the tree being parsed; used only in exception
  195. * messages if data corruption is found.
  196. * @throws MissingObjectException
  197. * the object supplied is not available from the repository.
  198. * @throws org.eclipse.jgit.errors.IncorrectObjectTypeException
  199. * the object supplied as an argument is not actually a tree and
  200. * cannot be parsed as though it were a tree.
  201. * @throws java.io.IOException
  202. * a loose object or pack file could not be read.
  203. */
  204. public void reset(ObjectReader reader, AnyObjectId id)
  205. throws IncorrectObjectTypeException, IOException {
  206. reset(reader.open(id, OBJ_TREE).getCachedBytes());
  207. }
  208. /** {@inheritDoc} */
  209. @Override
  210. public CanonicalTreeParser createSubtreeIterator(final ObjectReader reader,
  211. final MutableObjectId idBuffer)
  212. throws IncorrectObjectTypeException, IOException {
  213. idBuffer.fromRaw(idBuffer(), idOffset());
  214. if (!FileMode.TREE.equals(mode)) {
  215. final ObjectId me = idBuffer.toObjectId();
  216. throw new IncorrectObjectTypeException(me, TYPE_TREE);
  217. }
  218. return createSubtreeIterator0(reader, idBuffer);
  219. }
  220. /**
  221. * Back door to quickly create a subtree iterator for any subtree.
  222. * <p>
  223. * Don't use this unless you are ObjectWalk. The method is meant to be
  224. * called only once the current entry has been identified as a tree and its
  225. * identity has been converted into an ObjectId.
  226. *
  227. * @param reader
  228. * reader to load the tree data from.
  229. * @param id
  230. * ObjectId of the tree to open.
  231. * @return a new parser that walks over the current subtree.
  232. * @throws java.io.IOException
  233. * a loose object or pack file could not be read.
  234. */
  235. public final CanonicalTreeParser createSubtreeIterator0(
  236. final ObjectReader reader, final AnyObjectId id)
  237. throws IOException {
  238. final CanonicalTreeParser p = new CanonicalTreeParser(this);
  239. p.reset(reader, id);
  240. return p;
  241. }
  242. /** {@inheritDoc} */
  243. @Override
  244. public CanonicalTreeParser createSubtreeIterator(ObjectReader reader)
  245. throws IncorrectObjectTypeException, IOException {
  246. return createSubtreeIterator(reader, new MutableObjectId());
  247. }
  248. /** {@inheritDoc} */
  249. @Override
  250. public boolean hasId() {
  251. return true;
  252. }
  253. /** {@inheritDoc} */
  254. @Override
  255. public byte[] idBuffer() {
  256. return raw;
  257. }
  258. /** {@inheritDoc} */
  259. @Override
  260. public int idOffset() {
  261. return nextPtr - OBJECT_ID_LENGTH;
  262. }
  263. /** {@inheritDoc} */
  264. @Override
  265. public void reset() {
  266. if (!first())
  267. reset(raw);
  268. }
  269. /** {@inheritDoc} */
  270. @Override
  271. public boolean first() {
  272. return currPtr == 0;
  273. }
  274. /** {@inheritDoc} */
  275. @Override
  276. public boolean eof() {
  277. return currPtr == raw.length;
  278. }
  279. /** {@inheritDoc} */
  280. @Override
  281. public void next(int delta) {
  282. if (delta == 1) {
  283. // Moving forward one is the most common case.
  284. //
  285. prevPtr = currPtr;
  286. currPtr = nextPtr;
  287. if (!eof())
  288. parseEntry();
  289. return;
  290. }
  291. // Fast skip over records, then parse the last one.
  292. //
  293. final int end = raw.length;
  294. int ptr = nextPtr;
  295. while (--delta > 0 && ptr != end) {
  296. prevPtr = ptr;
  297. while (raw[ptr] != 0)
  298. ptr++;
  299. ptr += OBJECT_ID_LENGTH + 1;
  300. }
  301. if (delta != 0)
  302. throw new ArrayIndexOutOfBoundsException(delta);
  303. currPtr = ptr;
  304. if (!eof())
  305. parseEntry();
  306. }
  307. /** {@inheritDoc} */
  308. @Override
  309. public void back(int delta) {
  310. if (delta == 1 && 0 <= prevPtr) {
  311. // Moving back one is common in NameTreeWalk, as the average tree
  312. // won't have D/F type conflicts to study.
  313. //
  314. currPtr = prevPtr;
  315. prevPtr = -1;
  316. if (!eof())
  317. parseEntry();
  318. return;
  319. } else if (delta <= 0)
  320. throw new ArrayIndexOutOfBoundsException(delta);
  321. // Fast skip through the records, from the beginning of the tree.
  322. // There is no reliable way to read the tree backwards, so we must
  323. // parse all over again from the beginning. We hold the last "delta"
  324. // positions in a buffer, so we can find the correct position later.
  325. //
  326. final int[] trace = new int[delta + 1];
  327. Arrays.fill(trace, -1);
  328. int ptr = 0;
  329. while (ptr != currPtr) {
  330. System.arraycopy(trace, 1, trace, 0, delta);
  331. trace[delta] = ptr;
  332. while (raw[ptr] != 0)
  333. ptr++;
  334. ptr += OBJECT_ID_LENGTH + 1;
  335. }
  336. if (trace[1] == -1)
  337. throw new ArrayIndexOutOfBoundsException(delta);
  338. prevPtr = trace[0];
  339. currPtr = trace[1];
  340. parseEntry();
  341. }
  342. private void parseEntry() {
  343. int ptr = currPtr;
  344. byte c = raw[ptr++];
  345. int tmp = c - '0';
  346. for (;;) {
  347. c = raw[ptr++];
  348. if (' ' == c)
  349. break;
  350. tmp <<= 3;
  351. tmp += c - '0';
  352. }
  353. mode = tmp;
  354. tmp = pathOffset;
  355. for (;; tmp++) {
  356. c = raw[ptr++];
  357. if (c == 0) {
  358. break;
  359. }
  360. if (tmp >= path.length) {
  361. growPath(tmp);
  362. }
  363. path[tmp] = c;
  364. }
  365. pathLen = tmp;
  366. nextPtr = ptr + OBJECT_ID_LENGTH;
  367. }
  368. /**
  369. * Retrieve the {@link org.eclipse.jgit.attributes.AttributesNode} for the
  370. * current entry.
  371. *
  372. * @param reader
  373. * {@link org.eclipse.jgit.lib.ObjectReader} used to parse the
  374. * .gitattributes entry.
  375. * @return {@link org.eclipse.jgit.attributes.AttributesNode} for the
  376. * current entry.
  377. * @throws java.io.IOException
  378. * @since 4.2
  379. */
  380. public AttributesNode getEntryAttributesNode(ObjectReader reader)
  381. throws IOException {
  382. if (attributesNode == null) {
  383. attributesNode = findAttributes(reader);
  384. }
  385. return attributesNode.getRules().isEmpty() ? null : attributesNode;
  386. }
  387. private AttributesNode findAttributes(ObjectReader reader)
  388. throws IOException {
  389. CanonicalTreeParser itr = new CanonicalTreeParser();
  390. itr.reset(raw);
  391. if (itr.findFile(ATTRS)) {
  392. return loadAttributes(reader, itr.getEntryObjectId());
  393. }
  394. return noAttributes();
  395. }
  396. private static AttributesNode loadAttributes(ObjectReader reader,
  397. AnyObjectId id) throws IOException {
  398. AttributesNode r = new AttributesNode();
  399. try (InputStream in = reader.open(id, OBJ_BLOB).openStream()) {
  400. r.parse(in);
  401. }
  402. return r.getRules().isEmpty() ? noAttributes() : r;
  403. }
  404. private static AttributesNode noAttributes() {
  405. return new AttributesNode(Collections.<AttributesRule> emptyList());
  406. }
  407. }