You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Scanner.java 9.3KB

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