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

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