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 10KB

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