View Javadoc
1   /*
2    * Copyright (C) 2010, Google Inc.
3    * and other copyright owners as documented in the project's IP log.
4    *
5    * This program and the accompanying materials are made available
6    * under the terms of the Eclipse Distribution License v1.0 which
7    * accompanies this distribution, is reproduced below, and is
8    * available at http://www.eclipse.org/org/documents/edl-v10.php
9    *
10   * All rights reserved.
11   *
12   * Redistribution and use in source and binary forms, with or
13   * without modification, are permitted provided that the following
14   * conditions are met:
15   *
16   * - Redistributions of source code must retain the above copyright
17   *   notice, this list of conditions and the following disclaimer.
18   *
19   * - Redistributions in binary form must reproduce the above
20   *   copyright notice, this list of conditions and the following
21   *   disclaimer in the documentation and/or other materials provided
22   *   with the distribution.
23   *
24   * - Neither the name of the Eclipse Foundation, Inc. nor the
25   *   names of its contributors may be used to endorse or promote
26   *   products derived from this software without specific prior
27   *   written permission.
28   *
29   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42   */
43  
44  package org.eclipse.jgit.notes;
45  
46  import static org.eclipse.jgit.lib.Constants.OBJECT_ID_STRING_LENGTH;
47  import static org.eclipse.jgit.lib.Constants.encodeASCII;
48  import static org.eclipse.jgit.lib.FileMode.TREE;
49  import static org.eclipse.jgit.util.RawParseUtils.parseHexInt4;
50  
51  import java.io.IOException;
52  
53  import org.eclipse.jgit.errors.IncorrectObjectTypeException;
54  import org.eclipse.jgit.lib.AbbreviatedObjectId;
55  import org.eclipse.jgit.lib.FileMode;
56  import org.eclipse.jgit.lib.MutableObjectId;
57  import org.eclipse.jgit.lib.ObjectId;
58  import org.eclipse.jgit.lib.ObjectReader;
59  import org.eclipse.jgit.treewalk.CanonicalTreeParser;
60  
61  /** Custom tree parser to select note bucket type and load it. */
62  final class NoteParser extends CanonicalTreeParser {
63  	/**
64  	 * Parse a tree object into a {@link NoteBucket} instance.
65  	 *
66  	 * The type of note tree is automatically detected by examining the items
67  	 * within the tree, and allocating the proper storage type based on the
68  	 * first note-like entry encountered. Since the method parses by guessing
69  	 * the type on the first element, malformed note trees can be read as the
70  	 * wrong type of tree.
71  	 *
72  	 * This method is not recursive, it parses the one tree given to it and
73  	 * returns the bucket. If there are subtrees for note storage, they are
74  	 * setup as lazy pointers that will be resolved at a later time.
75  	 *
76  	 * @param prefix
77  	 *            common hex digits that all notes within this tree share. The
78  	 *            root tree has {@code prefix.length() == 0}, the first-level
79  	 *            subtrees should be {@code prefix.length()==2}, etc.
80  	 * @param treeId
81  	 *            the tree to read from the repository.
82  	 * @param reader
83  	 *            reader to access the tree object.
84  	 * @return bucket to holding the notes of the specified tree.
85  	 * @throws IOException
86  	 *             {@code treeId} cannot be accessed.
87  	 */
88  	static InMemoryNoteBucket parse(AbbreviatedObjectId prefix,
89  			final ObjectId treeId, final ObjectReader reader)
90  			throws IOException {
91  		return new NoteParser(prefix, reader, treeId).parse();
92  	}
93  
94  	private final int prefixLen;
95  
96  	private final int pathPadding;
97  
98  	private NonNoteEntry firstNonNote;
99  
100 	private NonNoteEntry lastNonNote;
101 
102 	private NoteParser(AbbreviatedObjectId prefix, ObjectReader r, ObjectId t)
103 			throws IncorrectObjectTypeException, IOException {
104 		super(encodeASCII(prefix.name()), r, t);
105 		prefixLen = prefix.length();
106 
107 		// Our path buffer has a '/' that we don't want after the prefix.
108 		// Drop it by shifting the path down one position.
109 		pathPadding = 0 < prefixLen ? 1 : 0;
110 		if (0 < pathPadding)
111 			System.arraycopy(path, 0, path, pathPadding, prefixLen);
112 	}
113 
114 	private InMemoryNoteBucket parse() {
115 		InMemoryNoteBucket r = parseTree();
116 		r.nonNotes = firstNonNote;
117 		return r;
118 	}
119 
120 	private InMemoryNoteBucket parseTree() {
121 		for (; !eof(); next(1)) {
122 			if (pathLen == pathPadding + OBJECT_ID_STRING_LENGTH && isHex())
123 				return parseLeafTree();
124 
125 			else if (getNameLength() == 2 && isHex() && isTree())
126 				return parseFanoutTree();
127 
128 			else
129 				storeNonNote();
130 		}
131 
132 		// If we cannot determine the style used, assume its a leaf.
133 		return new LeafBucket(prefixLen);
134 	}
135 
136 	private LeafBucket parseLeafTree() {
137 		final LeafBucketket.html#LeafBucket">LeafBucket leaf = new LeafBucket(prefixLen);
138 		final MutableObjectId.html#MutableObjectId">MutableObjectId idBuf = new MutableObjectId();
139 
140 		for (; !eof(); next(1)) {
141 			if (parseObjectId(idBuf))
142 				leaf.parseOneEntry(idBuf, getEntryObjectId());
143 			else
144 				storeNonNote();
145 		}
146 
147 		return leaf;
148 	}
149 
150 	private boolean parseObjectId(MutableObjectId id) {
151 		if (pathLen == pathPadding + OBJECT_ID_STRING_LENGTH) {
152 			try {
153 				id.fromString(path, pathPadding);
154 				return true;
155 			} catch (ArrayIndexOutOfBoundsException notHex) {
156 				return false;
157 			}
158 		}
159 		return false;
160 	}
161 
162 	private FanoutBucket parseFanoutTree() {
163 		final FanoutBuckett.html#FanoutBucket">FanoutBucket fanout = new FanoutBucket(prefixLen);
164 
165 		for (; !eof(); next(1)) {
166 			final int cell = parseFanoutCell();
167 			if (0 <= cell)
168 				fanout.setBucket(cell, getEntryObjectId());
169 			else
170 				storeNonNote();
171 		}
172 
173 		return fanout;
174 	}
175 
176 	private int parseFanoutCell() {
177 		if (getNameLength() == 2 && isTree()) {
178 			try {
179 				return (parseHexInt4(path[pathOffset + 0]) << 4)
180 						| parseHexInt4(path[pathOffset + 1]);
181 			} catch (ArrayIndexOutOfBoundsException notHex) {
182 				return -1;
183 			}
184 		} else {
185 			return -1;
186 		}
187 	}
188 
189 	private void storeNonNote() {
190 		ObjectId id = getEntryObjectId();
191 		FileMode fileMode = getEntryFileMode();
192 
193 		byte[] name = new byte[getNameLength()];
194 		getName(name, 0);
195 
196 		NonNoteEntry ent = new NonNoteEntry(name, fileMode, id);
197 		if (firstNonNote == null)
198 			firstNonNote = ent;
199 		if (lastNonNote != null)
200 			lastNonNote.next = ent;
201 		lastNonNote = ent;
202 	}
203 
204 	private boolean isTree() {
205 		return TREE.equals(mode);
206 	}
207 
208 	private boolean isHex() {
209 		try {
210 			for (int i = pathOffset; i < pathLen; i++)
211 				parseHexInt4(path[i]);
212 			return true;
213 		} catch (ArrayIndexOutOfBoundsException fail) {
214 			return false;
215 		}
216 	}
217 }