View Javadoc
1   /*
2    * Copyright (C) 2008-2009, 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  
45  package org.eclipse.jgit.dircache;
46  
47  import static org.eclipse.jgit.lib.FileMode.TREE;
48  import static org.eclipse.jgit.lib.TreeFormatter.entrySize;
49  
50  import java.io.IOException;
51  import java.io.OutputStream;
52  import java.nio.ByteBuffer;
53  import java.util.Arrays;
54  import java.util.Comparator;
55  
56  import org.eclipse.jgit.errors.UnmergedPathException;
57  import org.eclipse.jgit.lib.Constants;
58  import org.eclipse.jgit.lib.ObjectId;
59  import org.eclipse.jgit.lib.ObjectInserter;
60  import org.eclipse.jgit.lib.TreeFormatter;
61  import org.eclipse.jgit.util.MutableInteger;
62  import org.eclipse.jgit.util.RawParseUtils;
63  
64  /**
65   * Single tree record from the 'TREE' {@link org.eclipse.jgit.dircache.DirCache}
66   * extension.
67   * <p>
68   * A valid cache tree record contains the object id of a tree object and the
69   * total number of {@link org.eclipse.jgit.dircache.DirCacheEntry} instances
70   * (counted recursively) from the DirCache contained within the tree. This
71   * information facilitates faster traversal of the index and quicker generation
72   * of tree objects prior to creating a new commit.
73   * <p>
74   * An invalid cache tree record indicates a known subtree whose file entries
75   * have changed in ways that cause the tree to no longer have a known object id.
76   * Invalid cache tree records must be revalidated prior to use.
77   */
78  public class DirCacheTree {
79  	private static final byte[] NO_NAME = {};
80  
81  	private static final DirCacheTree[] NO_CHILDREN = {};
82  
83  	private static final Comparator<DirCacheTree> TREE_CMP = new Comparator<DirCacheTree>() {
84  		@Override
85  		public int compare(DirCacheTree o1, DirCacheTree o2) {
86  			final byte[] a = o1.encodedName;
87  			final byte[] b = o2.encodedName;
88  			final int aLen = a.length;
89  			final int bLen = b.length;
90  			int cPos;
91  			for (cPos = 0; cPos < aLen && cPos < bLen; cPos++) {
92  				final int cmp = (a[cPos] & 0xff) - (b[cPos] & 0xff);
93  				if (cmp != 0)
94  					return cmp;
95  			}
96  			if (aLen == bLen)
97  				return 0;
98  			if (aLen < bLen)
99  				return '/' - (b[cPos] & 0xff);
100 			return (a[cPos] & 0xff) - '/';
101 		}
102 	};
103 
104 	/** Tree this tree resides in; null if we are the root. */
105 	private DirCacheTree parent;
106 
107 	/** Name of this tree within its parent. */
108 	byte[] encodedName;
109 
110 	/** Number of {@link DirCacheEntry} records that belong to this tree. */
111 	private int entrySpan;
112 
113 	/** Unique SHA-1 of this tree; null if invalid. */
114 	private ObjectId id;
115 
116 	/** Child trees, if any, sorted by {@link #encodedName}. */
117 	private DirCacheTree[] children;
118 
119 	/** Number of valid children in {@link #children}. */
120 	private int childCnt;
121 
122 	DirCacheTree() {
123 		encodedName = NO_NAME;
124 		children = NO_CHILDREN;
125 		childCnt = 0;
126 		entrySpan = -1;
127 	}
128 
129 	private DirCacheTree(final DirCacheTree myParent, final byte[] path,
130 			final int pathOff, final int pathLen) {
131 		parent = myParent;
132 		encodedName = new byte[pathLen];
133 		System.arraycopy(path, pathOff, encodedName, 0, pathLen);
134 		children = NO_CHILDREN;
135 		childCnt = 0;
136 		entrySpan = -1;
137 	}
138 
139 	DirCacheTree(final byte[] in, final MutableInteger off,
140 			final DirCacheTree myParent) {
141 		parent = myParent;
142 
143 		int ptr = RawParseUtils.next(in, off.value, '\0');
144 		final int nameLen = ptr - off.value - 1;
145 		if (nameLen > 0) {
146 			encodedName = new byte[nameLen];
147 			System.arraycopy(in, off.value, encodedName, 0, nameLen);
148 		} else
149 			encodedName = NO_NAME;
150 
151 		entrySpan = RawParseUtils.parseBase10(in, ptr, off);
152 		final int subcnt = RawParseUtils.parseBase10(in, off.value, off);
153 		off.value = RawParseUtils.next(in, off.value, '\n');
154 
155 		if (entrySpan >= 0) {
156 			// Valid trees have a positive entry count and an id of a
157 			// tree object that should exist in the object database.
158 			//
159 			id = ObjectId.fromRaw(in, off.value);
160 			off.value += Constants.OBJECT_ID_LENGTH;
161 		}
162 
163 		if (subcnt > 0) {
164 			boolean alreadySorted = true;
165 			children = new DirCacheTree[subcnt];
166 			for (int i = 0; i < subcnt; i++) {
167 				children[i] = new DirCacheTree(in, off, this);
168 
169 				// C Git's ordering differs from our own; it prefers to
170 				// sort by length first. This sometimes produces a sort
171 				// we do not desire. On the other hand it may have been
172 				// created by us, and be sorted the way we want.
173 				//
174 				if (alreadySorted && i > 0
175 						&& TREE_CMP.compare(children[i - 1], children[i]) > 0)
176 					alreadySorted = false;
177 			}
178 			if (!alreadySorted)
179 				Arrays.sort(children, 0, subcnt, TREE_CMP);
180 		} else {
181 			// Leaf level trees have no children, only (file) entries.
182 			//
183 			children = NO_CHILDREN;
184 		}
185 		childCnt = subcnt;
186 	}
187 
188 	void write(byte[] tmp, OutputStream os) throws IOException {
189 		int ptr = tmp.length;
190 		tmp[--ptr] = '\n';
191 		ptr = RawParseUtils.formatBase10(tmp, ptr, childCnt);
192 		tmp[--ptr] = ' ';
193 		ptr = RawParseUtils.formatBase10(tmp, ptr, isValid() ? entrySpan : -1);
194 		tmp[--ptr] = 0;
195 
196 		os.write(encodedName);
197 		os.write(tmp, ptr, tmp.length - ptr);
198 		if (isValid()) {
199 			id.copyRawTo(tmp, 0);
200 			os.write(tmp, 0, Constants.OBJECT_ID_LENGTH);
201 		}
202 		for (int i = 0; i < childCnt; i++)
203 			children[i].write(tmp, os);
204 	}
205 
206 	/**
207 	 * Determine if this cache is currently valid.
208 	 * <p>
209 	 * A valid cache tree knows how many
210 	 * {@link org.eclipse.jgit.dircache.DirCacheEntry} instances from the parent
211 	 * {@link org.eclipse.jgit.dircache.DirCache} reside within this tree
212 	 * (recursively enumerated). It also knows the object id of the tree, as the
213 	 * tree should be readily available from the repository's object database.
214 	 *
215 	 * @return true if this tree is knows key details about itself; false if the
216 	 *         tree needs to be regenerated.
217 	 */
218 	public boolean isValid() {
219 		return id != null;
220 	}
221 
222 	/**
223 	 * Get the number of entries this tree spans within the DirCache.
224 	 * <p>
225 	 * If this tree is not valid (see {@link #isValid()}) this method's return
226 	 * value is always strictly negative (less than 0) but is otherwise an
227 	 * undefined result.
228 	 *
229 	 * @return total number of entries (recursively) contained within this tree.
230 	 */
231 	public int getEntrySpan() {
232 		return entrySpan;
233 	}
234 
235 	/**
236 	 * Get the number of cached subtrees contained within this tree.
237 	 *
238 	 * @return number of child trees available through this tree.
239 	 */
240 	public int getChildCount() {
241 		return childCnt;
242 	}
243 
244 	/**
245 	 * Get the i-th child cache tree.
246 	 *
247 	 * @param i
248 	 *            index of the child to obtain.
249 	 * @return the child tree.
250 	 */
251 	public DirCacheTree getChild(int i) {
252 		return children[i];
253 	}
254 
255 	/**
256 	 * Get the tree's ObjectId.
257 	 * <p>
258 	 * If {@link #isValid()} returns false this method will return null.
259 	 *
260 	 * @return ObjectId of this tree or null.
261 	 * @since 4.3
262 	 */
263 	public ObjectId getObjectId() {
264 		return id;
265 	}
266 
267 	/**
268 	 * Get the tree's name within its parent.
269 	 * <p>
270 	 * This method is not very efficient and is primarily meant for debugging
271 	 * and final output generation. Applications should try to avoid calling it,
272 	 * and if invoked do so only once per interesting entry, where the name is
273 	 * absolutely required for correct function.
274 	 *
275 	 * @return name of the tree. This does not contain any '/' characters.
276 	 */
277 	public String getNameString() {
278 		final ByteBuffer bb = ByteBuffer.wrap(encodedName);
279 		return Constants.CHARSET.decode(bb).toString();
280 	}
281 
282 	/**
283 	 * Get the tree's path within the repository.
284 	 * <p>
285 	 * This method is not very efficient and is primarily meant for debugging
286 	 * and final output generation. Applications should try to avoid calling it,
287 	 * and if invoked do so only once per interesting entry, where the name is
288 	 * absolutely required for correct function.
289 	 *
290 	 * @return path of the tree, relative to the repository root. If this is not
291 	 *         the root tree the path ends with '/'. The root tree's path string
292 	 *         is the empty string ("").
293 	 */
294 	public String getPathString() {
295 		final StringBuilder r = new StringBuilder();
296 		appendName(r);
297 		return r.toString();
298 	}
299 
300 	/**
301 	 * Write (if necessary) this tree to the object store.
302 	 *
303 	 * @param cache
304 	 *            the complete cache from DirCache.
305 	 * @param cIdx
306 	 *            first position of <code>cache</code> that is a member of this
307 	 *            tree. The path of <code>cache[cacheIdx].path</code> for the
308 	 *            range <code>[0,pathOff-1)</code> matches the complete path of
309 	 *            this tree, from the root of the repository.
310 	 * @param pathOffset
311 	 *            number of bytes of <code>cache[cacheIdx].path</code> that
312 	 *            matches this tree's path. The value at array position
313 	 *            <code>cache[cacheIdx].path[pathOff-1]</code> is always '/' if
314 	 *            <code>pathOff</code> is > 0.
315 	 * @param ow
316 	 *            the writer to use when serializing to the store.
317 	 * @return identity of this tree.
318 	 * @throws UnmergedPathException
319 	 *             one or more paths contain higher-order stages (stage > 0),
320 	 *             which cannot be stored in a tree object.
321 	 * @throws IOException
322 	 *             an unexpected error occurred writing to the object store.
323 	 */
324 	ObjectId writeTree(final DirCacheEntry[] cache, int cIdx,
325 			final int pathOffset, final ObjectInserter ow)
326 			throws UnmergedPathException, IOException {
327 		if (id == null) {
328 			final int endIdx = cIdx + entrySpan;
329 			final TreeFormatter fmt = new TreeFormatter(computeSize(cache,
330 					cIdx, pathOffset, ow));
331 			int childIdx = 0;
332 			int entryIdx = cIdx;
333 
334 			while (entryIdx < endIdx) {
335 				final DirCacheEntry e = cache[entryIdx];
336 				final byte[] ep = e.path;
337 				if (childIdx < childCnt) {
338 					final DirCacheTree st = children[childIdx];
339 					if (st.contains(ep, pathOffset, ep.length)) {
340 						fmt.append(st.encodedName, TREE, st.id);
341 						entryIdx += st.entrySpan;
342 						childIdx++;
343 						continue;
344 					}
345 				}
346 
347 				fmt.append(ep, pathOffset, ep.length - pathOffset, e
348 						.getFileMode(), e.idBuffer(), e.idOffset());
349 				entryIdx++;
350 			}
351 
352 			id = ow.insert(fmt);
353 		}
354 		return id;
355 	}
356 
357 	private int computeSize(final DirCacheEntry[] cache, int cIdx,
358 			final int pathOffset, final ObjectInserter ow)
359 			throws UnmergedPathException, IOException {
360 		final int endIdx = cIdx + entrySpan;
361 		int childIdx = 0;
362 		int entryIdx = cIdx;
363 		int size = 0;
364 
365 		while (entryIdx < endIdx) {
366 			final DirCacheEntry e = cache[entryIdx];
367 			if (e.getStage() != 0)
368 				throw new UnmergedPathException(e);
369 
370 			final byte[] ep = e.path;
371 			if (childIdx < childCnt) {
372 				final DirCacheTree st = children[childIdx];
373 				if (st.contains(ep, pathOffset, ep.length)) {
374 					final int stOffset = pathOffset + st.nameLength() + 1;
375 					st.writeTree(cache, entryIdx, stOffset, ow);
376 
377 					size += entrySize(TREE, st.nameLength());
378 
379 					entryIdx += st.entrySpan;
380 					childIdx++;
381 					continue;
382 				}
383 			}
384 
385 			size += entrySize(e.getFileMode(), ep.length - pathOffset);
386 			entryIdx++;
387 		}
388 
389 		return size;
390 	}
391 
392 	private void appendName(StringBuilder r) {
393 		if (parent != null) {
394 			parent.appendName(r);
395 			r.append(getNameString());
396 			r.append('/');
397 		} else if (nameLength() > 0) {
398 			r.append(getNameString());
399 			r.append('/');
400 		}
401 	}
402 
403 	final int nameLength() {
404 		return encodedName.length;
405 	}
406 
407 	final boolean contains(byte[] a, int aOff, int aLen) {
408 		final byte[] e = encodedName;
409 		final int eLen = e.length;
410 		for (int eOff = 0; eOff < eLen && aOff < aLen; eOff++, aOff++)
411 			if (e[eOff] != a[aOff])
412 				return false;
413 		if (aOff >= aLen)
414 			return false;
415 		return a[aOff] == '/';
416 	}
417 
418 	/**
419 	 * Update (if necessary) this tree's entrySpan.
420 	 *
421 	 * @param cache
422 	 *            the complete cache from DirCache.
423 	 * @param cCnt
424 	 *            number of entries in <code>cache</code> that are valid for
425 	 *            iteration.
426 	 * @param cIdx
427 	 *            first position of <code>cache</code> that is a member of this
428 	 *            tree. The path of <code>cache[cacheIdx].path</code> for the
429 	 *            range <code>[0,pathOff-1)</code> matches the complete path of
430 	 *            this tree, from the root of the repository.
431 	 * @param pathOff
432 	 *            number of bytes of <code>cache[cacheIdx].path</code> that
433 	 *            matches this tree's path. The value at array position
434 	 *            <code>cache[cacheIdx].path[pathOff-1]</code> is always '/' if
435 	 *            <code>pathOff</code> is > 0.
436 	 */
437 	void validate(final DirCacheEntry[] cache, final int cCnt, int cIdx,
438 			final int pathOff) {
439 		if (entrySpan >= 0 && cIdx + entrySpan <= cCnt) {
440 			// If we are valid, our children are also valid.
441 			// We have no need to validate them.
442 			//
443 			return;
444 		}
445 
446 		entrySpan = 0;
447 		if (cCnt == 0) {
448 			// Special case of an empty index, and we are the root tree.
449 			//
450 			return;
451 		}
452 
453 		final byte[] firstPath = cache[cIdx].path;
454 		int stIdx = 0;
455 		while (cIdx < cCnt) {
456 			final byte[] currPath = cache[cIdx].path;
457 			if (pathOff > 0 && !peq(firstPath, currPath, pathOff)) {
458 				// The current entry is no longer in this tree. Our
459 				// span is updated and the remainder goes elsewhere.
460 				//
461 				break;
462 			}
463 
464 			DirCacheTree st = stIdx < childCnt ? children[stIdx] : null;
465 			final int cc = namecmp(currPath, pathOff, st);
466 			if (cc > 0) {
467 				// This subtree is now empty.
468 				//
469 				removeChild(stIdx);
470 				continue;
471 			}
472 
473 			if (cc < 0) {
474 				final int p = slash(currPath, pathOff);
475 				if (p < 0) {
476 					// The entry has no '/' and thus is directly in this
477 					// tree. Count it as one of our own.
478 					//
479 					cIdx++;
480 					entrySpan++;
481 					continue;
482 				}
483 
484 				// Build a new subtree for this entry.
485 				//
486 				st = new DirCacheTree(this, currPath, pathOff, p - pathOff);
487 				insertChild(stIdx, st);
488 			}
489 
490 			// The entry is contained in this subtree.
491 			//
492 			assert(st != null);
493 			st.validate(cache, cCnt, cIdx, pathOff + st.nameLength() + 1);
494 			cIdx += st.entrySpan;
495 			entrySpan += st.entrySpan;
496 			stIdx++;
497 		}
498 
499 		// None of our remaining children can be in this tree
500 		// as the current cache entry is after our own name.
501 		//
502 		while (stIdx < childCnt)
503 			removeChild(childCnt - 1);
504 	}
505 
506 	private void insertChild(int stIdx, DirCacheTree st) {
507 		final DirCacheTree[] c = children;
508 		if (childCnt + 1 <= c.length) {
509 			if (stIdx < childCnt)
510 				System.arraycopy(c, stIdx, c, stIdx + 1, childCnt - stIdx);
511 			c[stIdx] = st;
512 			childCnt++;
513 			return;
514 		}
515 
516 		final int n = c.length;
517 		final DirCacheTree[] a = new DirCacheTree[n + 1];
518 		if (stIdx > 0)
519 			System.arraycopy(c, 0, a, 0, stIdx);
520 		a[stIdx] = st;
521 		if (stIdx < n)
522 			System.arraycopy(c, stIdx, a, stIdx + 1, n - stIdx);
523 		children = a;
524 		childCnt++;
525 	}
526 
527 	private void removeChild(int stIdx) {
528 		final int n = --childCnt;
529 		if (stIdx < n)
530 			System.arraycopy(children, stIdx + 1, children, stIdx, n - stIdx);
531 		children[n] = null;
532 	}
533 
534 	static boolean peq(byte[] a, byte[] b, int aLen) {
535 		if (b.length < aLen)
536 			return false;
537 		for (aLen--; aLen >= 0; aLen--)
538 			if (a[aLen] != b[aLen])
539 				return false;
540 		return true;
541 	}
542 
543 	private static int namecmp(byte[] a, int aPos, DirCacheTree ct) {
544 		if (ct == null)
545 			return -1;
546 		final byte[] b = ct.encodedName;
547 		final int aLen = a.length;
548 		final int bLen = b.length;
549 		int bPos = 0;
550 		for (; aPos < aLen && bPos < bLen; aPos++, bPos++) {
551 			final int cmp = (a[aPos] & 0xff) - (b[bPos] & 0xff);
552 			if (cmp != 0)
553 				return cmp;
554 		}
555 		if (bPos == bLen)
556 			return a[aPos] == '/' ? 0 : -1;
557 		return aLen - bLen;
558 	}
559 
560 	private static int slash(byte[] a, int aPos) {
561 		final int aLen = a.length;
562 		for (; aPos < aLen; aPos++)
563 			if (a[aPos] == '/')
564 				return aPos;
565 		return -1;
566 	}
567 
568 	/** {@inheritDoc} */
569 	@Override
570 	public String toString() {
571 		return getNameString();
572 	}
573 }