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

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