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.

RefAdvertiser.java 10.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /*
  2. * Copyright (C) 2008-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. package org.eclipse.jgit.transport;
  44. import java.io.IOException;
  45. import java.util.LinkedHashSet;
  46. import java.util.Map;
  47. import java.util.Set;
  48. import java.util.SortedMap;
  49. import org.eclipse.jgit.lib.AnyObjectId;
  50. import org.eclipse.jgit.lib.Constants;
  51. import org.eclipse.jgit.lib.ObjectId;
  52. import org.eclipse.jgit.lib.Ref;
  53. import org.eclipse.jgit.lib.RefComparator;
  54. import org.eclipse.jgit.lib.Repository;
  55. import org.eclipse.jgit.revwalk.RevFlag;
  56. import org.eclipse.jgit.revwalk.RevObject;
  57. import org.eclipse.jgit.revwalk.RevTag;
  58. import org.eclipse.jgit.revwalk.RevWalk;
  59. import org.eclipse.jgit.util.RefMap;
  60. /** Support for the start of {@link UploadPack} and {@link ReceivePack}. */
  61. public abstract class RefAdvertiser {
  62. /** Advertiser which frames lines in a {@link PacketLineOut} format. */
  63. public static class PacketLineOutRefAdvertiser extends RefAdvertiser {
  64. private final PacketLineOut pckOut;
  65. /**
  66. * Create a new advertiser for the supplied stream.
  67. *
  68. * @param out
  69. * the output stream.
  70. */
  71. public PacketLineOutRefAdvertiser(PacketLineOut out) {
  72. pckOut = out;
  73. }
  74. @Override
  75. protected void writeOne(final CharSequence line) throws IOException {
  76. pckOut.writeString(line.toString());
  77. }
  78. @Override
  79. protected void end() throws IOException {
  80. pckOut.end();
  81. }
  82. }
  83. private RevWalk walk;
  84. private RevFlag ADVERTISED;
  85. private final StringBuilder tmpLine = new StringBuilder(100);
  86. private final char[] tmpId = new char[Constants.OBJECT_ID_STRING_LENGTH];
  87. private final Set<String> capablities = new LinkedHashSet<String>();
  88. private boolean derefTags;
  89. private boolean first = true;
  90. /**
  91. * Initialize a new advertisement formatter.
  92. *
  93. * @param protoWalk
  94. * the RevWalk used to parse objects that are advertised.
  95. * @param advertisedFlag
  96. * flag marked on any advertised objects parsed out of the
  97. * {@code protoWalk}'s object pool, permitting the caller to
  98. * later quickly determine if an object was advertised (or not).
  99. */
  100. public void init(final RevWalk protoWalk, final RevFlag advertisedFlag) {
  101. walk = protoWalk;
  102. ADVERTISED = advertisedFlag;
  103. }
  104. /**
  105. * Toggle tag peeling.
  106. * <p>
  107. * <p>
  108. * This method must be invoked prior to any of the following:
  109. * <ul>
  110. * <li>{@link #send(Map)}
  111. * <li>{@link #advertiseHave(AnyObjectId)}
  112. * <li>{@link #includeAdditionalHaves(Repository)}
  113. * </ul>
  114. *
  115. * @param deref
  116. * true to show the dereferenced value of a tag as the special
  117. * ref <code>$tag^{}</code> ; false to omit it from the output.
  118. */
  119. public void setDerefTags(final boolean deref) {
  120. derefTags = deref;
  121. }
  122. /**
  123. * Add one protocol capability to the initial advertisement.
  124. * <p>
  125. * This method must be invoked prior to any of the following:
  126. * <ul>
  127. * <li>{@link #send(Map)}
  128. * <li>{@link #advertiseHave(AnyObjectId)}
  129. * <li>{@link #includeAdditionalHaves(Repository)}
  130. * </ul>
  131. *
  132. * @param name
  133. * the name of a single protocol capability supported by the
  134. * caller. The set of capabilities are sent to the client in the
  135. * advertisement, allowing the client to later selectively enable
  136. * features it recognizes.
  137. */
  138. public void advertiseCapability(String name) {
  139. capablities.add(name);
  140. }
  141. /**
  142. * Format an advertisement for the supplied refs.
  143. *
  144. * @param refs
  145. * zero or more refs to format for the client. The collection is
  146. * sorted before display if necessary, and therefore may appear
  147. * in any order.
  148. * @throws IOException
  149. * the underlying output stream failed to write out an
  150. * advertisement record.
  151. */
  152. public void send(final Map<String, Ref> refs) throws IOException {
  153. for (final Ref r : getSortedRefs(refs)) {
  154. final RevObject obj = parseAnyOrNull(r.getObjectId());
  155. if (obj != null) {
  156. advertiseAny(obj, r.getName());
  157. if (derefTags && obj instanceof RevTag)
  158. advertiseTag((RevTag) obj, r.getName() + "^{}");
  159. }
  160. }
  161. }
  162. private Iterable<Ref> getSortedRefs(Map<String, Ref> all) {
  163. if (all instanceof RefMap
  164. || (all instanceof SortedMap && ((SortedMap) all).comparator() == null))
  165. return all.values();
  166. return RefComparator.sort(all.values());
  167. }
  168. /**
  169. * Advertise one object is available using the magic {@code .have}.
  170. * <p>
  171. * The magic {@code .have} advertisement is not available for fetching by a
  172. * client, but can be used by a client when considering a delta base
  173. * candidate before transferring data in a push. Within the record created
  174. * by this method the ref name is simply the invalid string {@code .have}.
  175. *
  176. * @param id
  177. * identity of the object that is assumed to exist.
  178. * @throws IOException
  179. * the underlying output stream failed to write out an
  180. * advertisement record.
  181. */
  182. public void advertiseHave(AnyObjectId id) throws IOException {
  183. RevObject obj = parseAnyOrNull(id);
  184. if (obj != null) {
  185. advertiseAnyOnce(obj, ".have");
  186. if (obj instanceof RevTag)
  187. advertiseAnyOnce(((RevTag) obj).getObject(), ".have");
  188. }
  189. }
  190. /**
  191. * Include references of alternate repositories as {@code .have} lines.
  192. *
  193. * @param src
  194. * repository to get the additional reachable objects from.
  195. * @throws IOException
  196. * the underlying output stream failed to write out an
  197. * advertisement record.
  198. */
  199. public void includeAdditionalHaves(Repository src) throws IOException {
  200. for (ObjectId id : src.getAdditionalHaves())
  201. advertiseHave(id);
  202. }
  203. /** @return true if no advertisements have been sent yet. */
  204. public boolean isEmpty() {
  205. return first;
  206. }
  207. private RevObject parseAnyOrNull(final AnyObjectId id) {
  208. if (id == null)
  209. return null;
  210. try {
  211. return walk.parseAny(id);
  212. } catch (IOException e) {
  213. return null;
  214. }
  215. }
  216. private void advertiseAnyOnce(final RevObject obj, final String refName)
  217. throws IOException {
  218. if (!obj.has(ADVERTISED))
  219. advertiseAny(obj, refName);
  220. }
  221. private void advertiseAny(final RevObject obj, final String refName)
  222. throws IOException {
  223. obj.add(ADVERTISED);
  224. advertiseId(obj, refName);
  225. }
  226. private void advertiseTag(final RevTag tag, final String refName)
  227. throws IOException {
  228. RevObject o = tag;
  229. do {
  230. // Fully unwrap here so later on we have these already parsed.
  231. final RevObject target = ((RevTag) o).getObject();
  232. try {
  233. walk.parseHeaders(target);
  234. } catch (IOException err) {
  235. return;
  236. }
  237. target.add(ADVERTISED);
  238. o = target;
  239. } while (o instanceof RevTag);
  240. advertiseAny(tag.getObject(), refName);
  241. }
  242. /**
  243. * Advertise one object under a specific name.
  244. * <p>
  245. * If the advertised object is a tag, this method does not advertise the
  246. * peeled version of it.
  247. *
  248. * @param id
  249. * the object to advertise.
  250. * @param refName
  251. * name of the reference to advertise the object as, can be any
  252. * string not including the NUL byte.
  253. * @throws IOException
  254. * the underlying output stream failed to write out an
  255. * advertisement record.
  256. */
  257. public void advertiseId(final AnyObjectId id, final String refName)
  258. throws IOException {
  259. tmpLine.setLength(0);
  260. id.copyTo(tmpId, tmpLine);
  261. tmpLine.append(' ');
  262. tmpLine.append(refName);
  263. if (first) {
  264. first = false;
  265. if (!capablities.isEmpty()) {
  266. tmpLine.append('\0');
  267. for (final String capName : capablities) {
  268. tmpLine.append(' ');
  269. tmpLine.append(capName);
  270. }
  271. tmpLine.append(' ');
  272. }
  273. }
  274. tmpLine.append('\n');
  275. writeOne(tmpLine);
  276. }
  277. /**
  278. * Write a single advertisement line.
  279. *
  280. * @param line
  281. * the advertisement line to be written. The line always ends
  282. * with LF. Never null or the empty string.
  283. * @throws IOException
  284. * the underlying output stream failed to write out an
  285. * advertisement record.
  286. */
  287. protected abstract void writeOne(CharSequence line) throws IOException;
  288. /**
  289. * Mark the end of the advertisements.
  290. *
  291. * @throws IOException
  292. * the underlying output stream failed to write out an
  293. * advertisement record.
  294. */
  295. protected abstract void end() throws IOException;
  296. }