Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. * </ul>
  120. *
  121. * @param name
  122. * the name of a single protocol capability supported by the
  123. * caller. The set of capabilities are sent to the client in the
  124. * advertisement, allowing the client to later selectively enable
  125. * features it recognizes.
  126. */
  127. public void advertiseCapability(String name) {
  128. capablities.add(name);
  129. }
  130. /**
  131. * Format an advertisement for the supplied refs.
  132. *
  133. * @param refs
  134. * zero or more refs to format for the client. The collection is
  135. * sorted before display if necessary, and therefore may appear
  136. * in any order.
  137. * @return set of ObjectIds that were advertised to the client.
  138. * @throws IOException
  139. * the underlying output stream failed to write out an
  140. * advertisement record.
  141. */
  142. public Set<ObjectId> send(Map<String, Ref> refs) throws IOException {
  143. for (Ref ref : getSortedRefs(refs)) {
  144. if (ref.getObjectId() == null)
  145. continue;
  146. advertiseAny(ref.getObjectId(), ref.getName());
  147. if (!derefTags)
  148. continue;
  149. if (!ref.isPeeled()) {
  150. if (repository == null)
  151. continue;
  152. ref = repository.peel(ref);
  153. }
  154. if (ref.getPeeledObjectId() != null)
  155. advertiseAny(ref.getPeeledObjectId(), ref.getName() + "^{}");
  156. }
  157. return sent;
  158. }
  159. private Iterable<Ref> getSortedRefs(Map<String, Ref> all) {
  160. if (all instanceof RefMap
  161. || (all instanceof SortedMap && ((SortedMap) all).comparator() == null))
  162. return all.values();
  163. return RefComparator.sort(all.values());
  164. }
  165. /**
  166. * Advertise one object is available using the magic {@code .have}.
  167. * <p>
  168. * The magic {@code .have} advertisement is not available for fetching by a
  169. * client, but can be used by a client when considering a delta base
  170. * candidate before transferring data in a push. Within the record created
  171. * by this method the ref name is simply the invalid string {@code .have}.
  172. *
  173. * @param id
  174. * identity of the object that is assumed to exist.
  175. * @throws IOException
  176. * the underlying output stream failed to write out an
  177. * advertisement record.
  178. */
  179. public void advertiseHave(AnyObjectId id) throws IOException {
  180. advertiseAnyOnce(id, ".have");
  181. }
  182. /** @return true if no advertisements have been sent yet. */
  183. public boolean isEmpty() {
  184. return first;
  185. }
  186. private void advertiseAnyOnce(AnyObjectId obj, final String refName)
  187. throws IOException {
  188. if (!sent.contains(obj))
  189. advertiseAny(obj, refName);
  190. }
  191. private void advertiseAny(AnyObjectId obj, final String refName)
  192. throws IOException {
  193. sent.add(obj.toObjectId());
  194. advertiseId(obj, refName);
  195. }
  196. /**
  197. * Advertise one object under a specific name.
  198. * <p>
  199. * If the advertised object is a tag, this method does not advertise the
  200. * peeled version of it.
  201. *
  202. * @param id
  203. * the object to advertise.
  204. * @param refName
  205. * name of the reference to advertise the object as, can be any
  206. * string not including the NUL byte.
  207. * @throws IOException
  208. * the underlying output stream failed to write out an
  209. * advertisement record.
  210. */
  211. public void advertiseId(final AnyObjectId id, final String refName)
  212. throws IOException {
  213. tmpLine.setLength(0);
  214. id.copyTo(tmpId, tmpLine);
  215. tmpLine.append(' ');
  216. tmpLine.append(refName);
  217. if (first) {
  218. first = false;
  219. if (!capablities.isEmpty()) {
  220. tmpLine.append('\0');
  221. for (final String capName : capablities) {
  222. tmpLine.append(' ');
  223. tmpLine.append(capName);
  224. }
  225. tmpLine.append(' ');
  226. }
  227. }
  228. tmpLine.append('\n');
  229. writeOne(tmpLine);
  230. }
  231. /**
  232. * Write a single advertisement line.
  233. *
  234. * @param line
  235. * the advertisement line to be written. The line always ends
  236. * with LF. Never null or the empty string.
  237. * @throws IOException
  238. * the underlying output stream failed to write out an
  239. * advertisement record.
  240. */
  241. protected abstract void writeOne(CharSequence line) throws IOException;
  242. /**
  243. * Mark the end of the advertisements.
  244. *
  245. * @throws IOException
  246. * the underlying output stream failed to write out an
  247. * advertisement record.
  248. */
  249. protected abstract void end() throws IOException;
  250. }