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

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