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
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
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
= {};
65 /** First offset within {@link #raw} of the prior entry. */
68 /** First offset within {@link #raw} of the current entry's data. */
71 /** Offset one past the current entry (first byte of next entry). */
74 /** Create a new parser. */
75 public CanonicalTreeParser() {
80 * Create a new parser for a tree appearing in a subset of a repository.
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 '/'.
88 * reader to load the tree data from.
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.
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
,
104 reset(reader
, treeId
);
107 private CanonicalTreeParser(final CanonicalTreeParser p
) {
112 * Reset this parser to walk through the given tree data.
115 * the raw tree content.
117 public void reset(final byte[] treeData
) {
128 * Reset this parser to walk through the given tree.
131 * reader to use during repository access.
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
,
147 CanonicalTreeParser p
= this;
148 while (p
.parent
!= null)
149 p
= (CanonicalTreeParser
) p
.parent
;
154 /** @return this iterator, or its parent, if the tree is at eof. */
155 public CanonicalTreeParser
next() {
156 CanonicalTreeParser p
= this;
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
;
164 p
= (CanonicalTreeParser
) p
.parent
;
168 p
.prevPtr
= p
.currPtr
;
169 p
.currPtr
= p
.nextPtr
;
176 * Reset this parser to walk through the given tree.
179 * reader to use during repository access.
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());
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.
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.
216 * reader to load the tree data from.
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
)
226 final CanonicalTreeParser p
= new CanonicalTreeParser(this);
231 public CanonicalTreeParser
createSubtreeIterator(final ObjectReader reader
)
232 throws IncorrectObjectTypeException
, IOException
{
233 return createSubtreeIterator(reader
, new MutableObjectId());
237 public byte[] idBuffer() {
242 public int idOffset() {
243 return nextPtr
- Constants
.OBJECT_ID_LENGTH
;
247 public boolean first() {
251 public boolean eof() {
252 return currPtr
== raw
.length
;
256 public void next(int delta
) {
258 // Moving forward one is the most common case.
267 // Fast skip over records, then parse the last one.
269 final int end
= raw
.length
;
271 while (--delta
> 0 && ptr
!= end
) {
273 while (raw
[ptr
] != 0)
275 ptr
+= Constants
.OBJECT_ID_LENGTH
+ 1;
278 throw new ArrayIndexOutOfBoundsException(delta
);
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.
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);
306 while (ptr
!= currPtr
) {
307 System
.arraycopy(trace
, 1, trace
, 0, delta
);
309 while (raw
[ptr
] != 0)
311 ptr
+= Constants
.OBJECT_ID_LENGTH
+ 1;
314 throw new ArrayIndexOutOfBoundsException(delta
);
320 private void parseEntry() {
340 } catch (ArrayIndexOutOfBoundsException e
) {
346 nextPtr
= ptr
+ Constants
.OBJECT_ID_LENGTH
;