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

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