Merge changes I39bfefee,I47795987,I70d120fb,I58cc5e01,I96bee7b9
[jgit.git] / org.eclipse.jgit / src / org / eclipse / jgit / treewalk / CanonicalTreeParser.java
blob8e4094a05528aa750def981e1de79c682cc7e171
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.
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
11 * All rights reserved.
13 * Redistribution and use in source and binary forms, with or
14 * without modification, are permitted provided that the following
15 * conditions are met:
17 * - Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
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.
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.
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.
45 package org.eclipse.jgit.treewalk;
47 import java.io.IOException;
48 import java.util.Arrays;
50 import org.eclipse.jgit.errors.IncorrectObjectTypeException;
51 import org.eclipse.jgit.errors.MissingObjectException;
52 import org.eclipse.jgit.lib.AnyObjectId;
53 import org.eclipse.jgit.lib.Constants;
54 import org.eclipse.jgit.lib.FileMode;
55 import org.eclipse.jgit.lib.MutableObjectId;
56 import org.eclipse.jgit.lib.ObjectId;
57 import org.eclipse.jgit.lib.ObjectReader;
59 /** Parses raw Git trees from the canonical semi-text/semi-binary format. */
60 public class CanonicalTreeParser extends AbstractTreeIterator {
61 private static final byte[] EMPTY = {};
63 private byte[] raw;
65 /** First offset within {@link #raw} of the prior entry. */
66 private int prevPtr;
68 /** First offset within {@link #raw} of the current entry's data. */
69 private int currPtr;
71 /** Offset one past the current entry (first byte of next entry). */
72 private int nextPtr;
74 /** Create a new parser. */
75 public CanonicalTreeParser() {
76 reset(EMPTY);
79 /**
80 * Create a new parser for a tree appearing in a subset of a repository.
82 * @param prefix
83 * position of this iterator in the repository tree. The value
84 * may be null or the empty array to indicate the prefix is the
85 * root of the repository. A trailing slash ('/') is
86 * automatically appended if the prefix does not end in '/'.
87 * @param reader
88 * reader to load the tree data from.
89 * @param treeId
90 * identity of the tree being parsed; used only in exception
91 * messages if data corruption is found.
92 * @throws MissingObjectException
93 * the object supplied is not available from the repository.
94 * @throws IncorrectObjectTypeException
95 * the object supplied as an argument is not actually a tree and
96 * cannot be parsed as though it were a tree.
97 * @throws IOException
98 * a loose object or pack file could not be read.
100 public CanonicalTreeParser(final byte[] prefix, final ObjectReader reader,
101 final AnyObjectId treeId) throws IncorrectObjectTypeException,
102 IOException {
103 super(prefix);
104 reset(reader, treeId);
107 private CanonicalTreeParser(final CanonicalTreeParser p) {
108 super(p);
112 * Reset this parser to walk through the given tree data.
114 * @param treeData
115 * the raw tree content.
117 public void reset(final byte[] treeData) {
118 raw = treeData;
119 prevPtr = -1;
120 currPtr = 0;
121 if (eof())
122 nextPtr = 0;
123 else
124 parseEntry();
128 * Reset this parser to walk through the given tree.
130 * @param reader
131 * reader to use during repository access.
132 * @param id
133 * identity of the tree being parsed; used only in exception
134 * messages if data corruption is found.
135 * @return the root level parser.
136 * @throws MissingObjectException
137 * the object supplied is not available from the repository.
138 * @throws IncorrectObjectTypeException
139 * the object supplied as an argument is not actually a tree and
140 * cannot be parsed as though it were a tree.
141 * @throws IOException
142 * a loose object or pack file could not be read.
144 public CanonicalTreeParser resetRoot(final ObjectReader reader,
145 final AnyObjectId id) throws IncorrectObjectTypeException,
146 IOException {
147 CanonicalTreeParser p = this;
148 while (p.parent != null)
149 p = (CanonicalTreeParser) p.parent;
150 p.reset(reader, id);
151 return p;
154 /** @return this iterator, or its parent, if the tree is at eof. */
155 public CanonicalTreeParser next() {
156 CanonicalTreeParser p = this;
157 for (;;) {
158 if (p.nextPtr == p.raw.length) {
159 // This parser has reached EOF, return to the parent.
160 if (p.parent == null) {
161 p.currPtr = p.nextPtr;
162 return p;
164 p = (CanonicalTreeParser) p.parent;
165 continue;
168 p.prevPtr = p.currPtr;
169 p.currPtr = p.nextPtr;
170 p.parseEntry();
171 return p;
176 * Reset this parser to walk through the given tree.
178 * @param reader
179 * reader to use during repository access.
180 * @param id
181 * identity of the tree being parsed; used only in exception
182 * messages if data corruption is found.
183 * @throws MissingObjectException
184 * the object supplied is not available from the repository.
185 * @throws IncorrectObjectTypeException
186 * the object supplied as an argument is not actually a tree and
187 * cannot be parsed as though it were a tree.
188 * @throws IOException
189 * a loose object or pack file could not be read.
191 public void reset(final ObjectReader reader, final AnyObjectId id)
192 throws IncorrectObjectTypeException, IOException {
193 reset(reader.open(id, Constants.OBJ_TREE).getCachedBytes());
196 @Override
197 public CanonicalTreeParser createSubtreeIterator(final ObjectReader reader,
198 final MutableObjectId idBuffer)
199 throws IncorrectObjectTypeException, IOException {
200 idBuffer.fromRaw(idBuffer(), idOffset());
201 if (!FileMode.TREE.equals(mode)) {
202 final ObjectId me = idBuffer.toObjectId();
203 throw new IncorrectObjectTypeException(me, Constants.TYPE_TREE);
205 return createSubtreeIterator0(reader, idBuffer);
209 * Back door to quickly create a subtree iterator for any subtree.
210 * <p>
211 * Don't use this unless you are ObjectWalk. The method is meant to be
212 * called only once the current entry has been identified as a tree and its
213 * identity has been converted into an ObjectId.
215 * @param reader
216 * reader to load the tree data from.
217 * @param id
218 * ObjectId of the tree to open.
219 * @return a new parser that walks over the current subtree.
220 * @throws IOException
221 * a loose object or pack file could not be read.
223 public final CanonicalTreeParser createSubtreeIterator0(
224 final ObjectReader reader, final AnyObjectId id)
225 throws IOException {
226 final CanonicalTreeParser p = new CanonicalTreeParser(this);
227 p.reset(reader, id);
228 return p;
231 public CanonicalTreeParser createSubtreeIterator(final ObjectReader reader)
232 throws IncorrectObjectTypeException, IOException {
233 return createSubtreeIterator(reader, new MutableObjectId());
236 @Override
237 public byte[] idBuffer() {
238 return raw;
241 @Override
242 public int idOffset() {
243 return nextPtr - Constants.OBJECT_ID_LENGTH;
246 @Override
247 public boolean first() {
248 return currPtr == 0;
251 public boolean eof() {
252 return currPtr == raw.length;
255 @Override
256 public void next(int delta) {
257 if (delta == 1) {
258 // Moving forward one is the most common case.
260 prevPtr = currPtr;
261 currPtr = nextPtr;
262 if (!eof())
263 parseEntry();
264 return;
267 // Fast skip over records, then parse the last one.
269 final int end = raw.length;
270 int ptr = nextPtr;
271 while (--delta > 0 && ptr != end) {
272 prevPtr = ptr;
273 while (raw[ptr] != 0)
274 ptr++;
275 ptr += Constants.OBJECT_ID_LENGTH + 1;
277 if (delta != 0)
278 throw new ArrayIndexOutOfBoundsException(delta);
279 currPtr = ptr;
280 if (!eof())
281 parseEntry();
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.
290 currPtr = prevPtr;
291 prevPtr = -1;
292 if (!eof())
293 parseEntry();
294 return;
295 } else if (delta <= 0)
296 throw new ArrayIndexOutOfBoundsException(delta);
298 // Fast skip through the records, from the beginning of the tree.
299 // There is no reliable way to read the tree backwards, so we must
300 // parse all over again from the beginning. We hold the last "delta"
301 // positions in a buffer, so we can find the correct position later.
303 final int[] trace = new int[delta + 1];
304 Arrays.fill(trace, -1);
305 int ptr = 0;
306 while (ptr != currPtr) {
307 System.arraycopy(trace, 1, trace, 0, delta);
308 trace[delta] = ptr;
309 while (raw[ptr] != 0)
310 ptr++;
311 ptr += Constants.OBJECT_ID_LENGTH + 1;
313 if (trace[1] == -1)
314 throw new ArrayIndexOutOfBoundsException(delta);
315 prevPtr = trace[0];
316 currPtr = trace[1];
317 parseEntry();
320 private void parseEntry() {
321 int ptr = currPtr;
322 byte c = raw[ptr++];
323 int tmp = c - '0';
324 for (;;) {
325 c = raw[ptr++];
326 if (' ' == c)
327 break;
328 tmp <<= 3;
329 tmp += c - '0';
331 mode = tmp;
333 tmp = pathOffset;
334 for (;; tmp++) {
335 c = raw[ptr++];
336 if (c == 0)
337 break;
338 try {
339 path[tmp] = c;
340 } catch (ArrayIndexOutOfBoundsException e) {
341 growPath(tmp);
342 path[tmp] = c;
345 pathLen = tmp;
346 nextPtr = ptr + Constants.OBJECT_ID_LENGTH;