View Javadoc
1   /*
2    * Copyright (C) 2016, 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.internal.storage.reftree;
45  
46  import static org.eclipse.jgit.lib.Constants.OBJ_BLOB;
47  import static org.eclipse.jgit.lib.Constants.R_REFS;
48  import static org.eclipse.jgit.lib.Constants.encode;
49  import static org.eclipse.jgit.lib.FileMode.TYPE_GITLINK;
50  import static org.eclipse.jgit.lib.FileMode.TYPE_SYMLINK;
51  import static org.eclipse.jgit.lib.FileMode.TYPE_TREE;
52  import static org.eclipse.jgit.lib.Ref.Storage.NEW;
53  import static org.eclipse.jgit.lib.Ref.Storage.PACKED;
54  import static org.eclipse.jgit.lib.RefDatabase.MAX_SYMBOLIC_REF_DEPTH;
55  
56  import java.io.IOException;
57  
58  import org.eclipse.jgit.annotations.Nullable;
59  import org.eclipse.jgit.errors.IncorrectObjectTypeException;
60  import org.eclipse.jgit.lib.AnyObjectId;
61  import org.eclipse.jgit.lib.ObjectId;
62  import org.eclipse.jgit.lib.ObjectIdRef;
63  import org.eclipse.jgit.lib.ObjectReader;
64  import org.eclipse.jgit.lib.Ref;
65  import org.eclipse.jgit.lib.Repository;
66  import org.eclipse.jgit.lib.SymbolicRef;
67  import org.eclipse.jgit.revwalk.RevTree;
68  import org.eclipse.jgit.revwalk.RevWalk;
69  import org.eclipse.jgit.treewalk.AbstractTreeIterator;
70  import org.eclipse.jgit.treewalk.CanonicalTreeParser;
71  import org.eclipse.jgit.treewalk.TreeWalk;
72  import org.eclipse.jgit.util.Paths;
73  import org.eclipse.jgit.util.RawParseUtils;
74  import org.eclipse.jgit.util.RefList;
75  
76  /** A tree parser that extracts references from a {@link RefTree}. */
77  class Scanner {
78  	private static final int MAX_SYMLINK_BYTES = 10 << 10;
79  	private static final byte[] BINARY_R_REFS = encode(R_REFS);
80  	private static final byte[] REFS_DOT_DOT = encode("refs/.."); //$NON-NLS-1$
81  
82  	static class Result {
83  		final ObjectId refTreeId;
84  		final RefList<Ref> all;
85  		final RefList<Ref> sym;
86  
87  		Result(ObjectId id, RefList<Ref> all, RefList<Ref> sym) {
88  			this.refTreeId = id;
89  			this.all = all;
90  			this.sym = sym;
91  		}
92  	}
93  
94  	/**
95  	 * Scan a {@link RefTree} and parse entries into {@link Ref} instances.
96  	 *
97  	 * @param repo
98  	 *            source repository containing the commit and tree objects that
99  	 *            make up the RefTree.
100 	 * @param src
101 	 *            bootstrap reference such as {@code refs/txn/committed} to read
102 	 *            the reference tree tip from. The current ObjectId will be
103 	 *            included in {@link Result#refTreeId}.
104 	 * @param prefix
105 	 *            if non-empty a reference prefix to scan only a subdirectory.
106 	 *            For example {@code prefix = "refs/heads/"} will limit the scan
107 	 *            to only the {@code "heads"} directory of the RefTree, avoiding
108 	 *            other directories like {@code "tags"}. Empty string reads all
109 	 *            entries in the RefTree.
110 	 * @param recursive
111 	 *            if true recurse into subdirectories of the reference tree;
112 	 *            false to read only one level. Callers may use false during an
113 	 *            implementation of {@code exactRef(String)} where only one
114 	 *            reference is needed out of a specific subtree.
115 	 * @return sorted list of references after parsing.
116 	 * @throws IOException
117 	 *             tree cannot be accessed from the repository.
118 	 */
119 	static Result scanRefTree(Repository repo, @Nullable Ref src, String prefix,
120 			boolean recursive) throws IOException {
121 		RefList.Builder<Ref> all = new RefList.Builder<>();
122 		RefList.Builder<Ref> sym = new RefList.Builder<>();
123 
124 		ObjectId srcId;
125 		if (src != null && src.getObjectId() != null) {
126 			try (ObjectReader reader = repo.newObjectReader()) {
127 				srcId = src.getObjectId();
128 				scan(reader, srcId, prefix, recursive, all, sym);
129 			}
130 		} else {
131 			srcId = ObjectId.zeroId();
132 		}
133 
134 		RefList<Ref> aList = all.toRefList();
135 		for (int idx = 0; idx < sym.size();) {
136 			Ref s = sym.get(idx);
137 			Ref r = resolve(s, 0, aList);
138 			if (r != null) {
139 				sym.set(idx++, r);
140 			} else {
141 				// Remove broken symbolic reference, they don't exist.
142 				sym.remove(idx);
143 				int rm = aList.find(s.getName());
144 				if (0 <= rm) {
145 					aList = aList.remove(rm);
146 				}
147 			}
148 		}
149 		return new Result(srcId, aList, sym.toRefList());
150 	}
151 
152 	private static void scan(ObjectReader reader, AnyObjectId srcId,
153 			String prefix, boolean recursive,
154 			RefList.Builder<Ref> all, RefList.Builder<Ref> sym)
155 					throws IncorrectObjectTypeException, IOException {
156 		CanonicalTreeParser p = createParserAtPath(reader, srcId, prefix);
157 		if (p == null) {
158 			return;
159 		}
160 
161 		while (!p.eof()) {
162 			int mode = p.getEntryRawMode();
163 			if (mode == TYPE_TREE) {
164 				if (recursive) {
165 					p = p.createSubtreeIterator(reader);
166 				} else {
167 					p = p.next();
168 				}
169 				continue;
170 			}
171 
172 			if (!curElementHasPeelSuffix(p)) {
173 				Ref r = toRef(reader, mode, p);
174 				if (r != null) {
175 					all.add(r);
176 					if (r.isSymbolic()) {
177 						sym.add(r);
178 					}
179 				}
180 			} else if (mode == TYPE_GITLINK) {
181 				peel(all, p);
182 			}
183 			p = p.next();
184 		}
185 	}
186 
187 	private static CanonicalTreeParser createParserAtPath(ObjectReader reader,
188 			AnyObjectId srcId, String prefix) throws IOException {
189 		ObjectId root = toTree(reader, srcId);
190 		if (prefix.isEmpty()) {
191 			return new CanonicalTreeParser(BINARY_R_REFS, reader, root);
192 		}
193 
194 		String dir = RefTree.refPath(Paths.stripTrailingSeparator(prefix));
195 		TreeWalk tw = TreeWalk.forPath(reader, dir, root);
196 		if (tw == null || !tw.isSubtree()) {
197 			return null;
198 		}
199 
200 		ObjectId id = tw.getObjectId(0);
201 		return new CanonicalTreeParser(encode(prefix), reader, id);
202 	}
203 
204 	private static Ref resolve(Ref ref, int depth, RefList<Ref> refs)
205 			throws IOException {
206 		if (!ref.isSymbolic()) {
207 			return ref;
208 		} else if (MAX_SYMBOLIC_REF_DEPTH <= depth) {
209 			return null;
210 		}
211 
212 		Ref r = refs.get(ref.getTarget().getName());
213 		if (r == null) {
214 			return ref;
215 		}
216 
217 		Ref dst = resolve(r, depth + 1, refs);
218 		if (dst == null) {
219 			return null;
220 		}
221 		return new SymbolicRef(ref.getName(), dst);
222 	}
223 
224 	@SuppressWarnings("resource")
225 	private static RevTree toTree(ObjectReader reader, AnyObjectId id)
226 			throws IOException {
227 		return new RevWalk(reader).parseTree(id);
228 	}
229 
230 	private static boolean curElementHasPeelSuffix(AbstractTreeIterator itr) {
231 		int n = itr.getEntryPathLength();
232 		byte[] c = itr.getEntryPathBuffer();
233 		return n > 2 && c[n - 2] == ' ' && c[n - 1] == '^';
234 	}
235 
236 	private static void peel(RefList.Builder<Ref> all, CanonicalTreeParser p) {
237 		String name = refName(p, true);
238 		for (int idx = all.size() - 1; 0 <= idx; idx--) {
239 			Ref r = all.get(idx);
240 			int cmp = r.getName().compareTo(name);
241 			if (cmp == 0) {
242 				all.set(idx, new ObjectIdRef.PeeledTag(PACKED, r.getName(),
243 						r.getObjectId(), p.getEntryObjectId()));
244 				break;
245 			} else if (cmp < 0) {
246 				// Stray peeled name without matching base name; skip entry.
247 				break;
248 			}
249 		}
250 	}
251 
252 	private static Ref toRef(ObjectReader reader, int mode,
253 			CanonicalTreeParser p) throws IOException {
254 		if (mode == TYPE_GITLINK) {
255 			String name = refName(p, false);
256 			ObjectId id = p.getEntryObjectId();
257 			return new ObjectIdRef.PeeledNonTag(PACKED, name, id);
258 
259 		} else if (mode == TYPE_SYMLINK) {
260 			ObjectId id = p.getEntryObjectId();
261 			byte[] bin = reader.open(id, OBJ_BLOB)
262 					.getCachedBytes(MAX_SYMLINK_BYTES);
263 			String dst = RawParseUtils.decode(bin);
264 			Ref trg = new ObjectIdRef.Unpeeled(NEW, dst, null);
265 			String name = refName(p, false);
266 			return new SymbolicRef(name, trg);
267 		}
268 		return null;
269 	}
270 
271 	private static String refName(CanonicalTreeParser p, boolean peel) {
272 		byte[] buf = p.getEntryPathBuffer();
273 		int len = p.getEntryPathLength();
274 		if (peel) {
275 			len -= 2;
276 		}
277 		int ptr = 0;
278 		if (RawParseUtils.match(buf, ptr, REFS_DOT_DOT) > 0) {
279 			ptr = 7;
280 		}
281 		return RawParseUtils.decode(buf, ptr, len);
282 	}
283 
284 	private Scanner() {
285 	}
286 }