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.

FetchV2Request.java 8.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /*
  2. * Copyright (C) 2018, Google LLC.
  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 static java.util.Objects.requireNonNull;
  45. import java.util.ArrayList;
  46. import java.util.Collections;
  47. import java.util.HashSet;
  48. import java.util.List;
  49. import java.util.Set;
  50. import org.eclipse.jgit.annotations.NonNull;
  51. import org.eclipse.jgit.annotations.Nullable;
  52. import org.eclipse.jgit.lib.ObjectId;
  53. /**
  54. * Fetch request from git protocol v2.
  55. *
  56. * <p>
  57. * This is used as an input to {@link ProtocolV2Hook}.
  58. *
  59. * @since 5.1
  60. */
  61. public final class FetchV2Request extends FetchRequest {
  62. private final List<ObjectId> peerHas;
  63. private final List<String> wantedRefs;
  64. private final boolean doneReceived;
  65. @NonNull
  66. private final List<String> serverOptions;
  67. FetchV2Request(@NonNull List<ObjectId> peerHas,
  68. @NonNull List<String> wantedRefs,
  69. @NonNull Set<ObjectId> wantIds,
  70. @NonNull Set<ObjectId> clientShallowCommits, int deepenSince,
  71. @NonNull List<String> deepenNotRefs, int depth,
  72. @NonNull FilterSpec filterSpec,
  73. boolean doneReceived, @NonNull Set<String> clientCapabilities,
  74. @Nullable String agent, @NonNull List<String> serverOptions) {
  75. super(wantIds, depth, clientShallowCommits, filterSpec,
  76. clientCapabilities, deepenSince,
  77. deepenNotRefs, agent);
  78. this.peerHas = requireNonNull(peerHas);
  79. this.wantedRefs = requireNonNull(wantedRefs);
  80. this.doneReceived = doneReceived;
  81. this.serverOptions = requireNonNull(serverOptions);
  82. }
  83. /**
  84. * @return object ids received in the "have" lines
  85. */
  86. @NonNull
  87. List<ObjectId> getPeerHas() {
  88. return peerHas;
  89. }
  90. /**
  91. * @return list of references received in "want-ref" lines
  92. *
  93. * @since 5.4
  94. */
  95. @NonNull
  96. public List<String> getWantedRefs() {
  97. return wantedRefs;
  98. }
  99. /**
  100. * @return true if the request had a "done" line
  101. */
  102. boolean wasDoneReceived() {
  103. return doneReceived;
  104. }
  105. /**
  106. * Options received in server-option lines. The caller can choose to act on
  107. * these in an application-specific way
  108. *
  109. * @return Immutable list of server options received in the request
  110. *
  111. * @since 5.2
  112. */
  113. @NonNull
  114. public List<String> getServerOptions() {
  115. return serverOptions;
  116. }
  117. /** @return A builder of {@link FetchV2Request}. */
  118. static Builder builder() {
  119. return new Builder();
  120. }
  121. /** A builder for {@link FetchV2Request}. */
  122. static final class Builder {
  123. final List<ObjectId> peerHas = new ArrayList<>();
  124. final List<String> wantedRefs = new ArrayList<>();
  125. final Set<ObjectId> wantIds = new HashSet<>();
  126. final Set<ObjectId> clientShallowCommits = new HashSet<>();
  127. final List<String> deepenNotRefs = new ArrayList<>();
  128. final Set<String> clientCapabilities = new HashSet<>();
  129. int depth;
  130. int deepenSince;
  131. FilterSpec filterSpec = FilterSpec.NO_FILTER;
  132. boolean doneReceived;
  133. @Nullable
  134. String agent;
  135. final List<String> serverOptions = new ArrayList<>();
  136. private Builder() {
  137. }
  138. /**
  139. * @param objectId
  140. * object id received in a "have" line
  141. * @return this builder
  142. */
  143. Builder addPeerHas(ObjectId objectId) {
  144. peerHas.add(objectId);
  145. return this;
  146. }
  147. /**
  148. * Ref received in "want-ref" line and the object-id it refers to
  149. *
  150. * @param refName
  151. * reference name
  152. * @return this builder
  153. */
  154. Builder addWantedRef(String refName) {
  155. wantedRefs.add(refName);
  156. return this;
  157. }
  158. /**
  159. * @param clientCapability
  160. * capability line sent by the client
  161. * @return this builder
  162. */
  163. Builder addClientCapability(String clientCapability) {
  164. clientCapabilities.add(clientCapability);
  165. return this;
  166. }
  167. /**
  168. * @param wantId
  169. * object id received in a "want" line
  170. * @return this builder
  171. */
  172. Builder addWantId(ObjectId wantId) {
  173. wantIds.add(wantId);
  174. return this;
  175. }
  176. /**
  177. * @param shallowOid
  178. * object id received in a "shallow" line
  179. * @return this builder
  180. */
  181. Builder addClientShallowCommit(ObjectId shallowOid) {
  182. clientShallowCommits.add(shallowOid);
  183. return this;
  184. }
  185. /**
  186. * @param d
  187. * Depth received in a "deepen" line
  188. * @return this builder
  189. */
  190. Builder setDepth(int d) {
  191. depth = d;
  192. return this;
  193. }
  194. /**
  195. * @return depth set in the request (via a "deepen" line). Defaulting to
  196. * 0 if not set.
  197. */
  198. int getDepth() {
  199. return depth;
  200. }
  201. /**
  202. * @return true if there has been at least one "deepen not" line in the
  203. * request so far
  204. */
  205. boolean hasDeepenNotRefs() {
  206. return !deepenNotRefs.isEmpty();
  207. }
  208. /**
  209. * @param deepenNotRef
  210. * reference received in a "deepen not" line
  211. * @return this builder
  212. */
  213. Builder addDeepenNotRef(String deepenNotRef) {
  214. deepenNotRefs.add(deepenNotRef);
  215. return this;
  216. }
  217. /**
  218. * @param value
  219. * Unix timestamp received in a "deepen since" line
  220. * @return this builder
  221. */
  222. Builder setDeepenSince(int value) {
  223. deepenSince = value;
  224. return this;
  225. }
  226. /**
  227. * @return shallow since value, sent before in a "deepen since" line. 0
  228. * by default.
  229. */
  230. int getDeepenSince() {
  231. return deepenSince;
  232. }
  233. /**
  234. * @param filter
  235. * spec set in a "filter" line
  236. * @return this builder
  237. */
  238. Builder setFilterSpec(@NonNull FilterSpec filter) {
  239. filterSpec = requireNonNull(filter);
  240. return this;
  241. }
  242. /**
  243. * Mark that the "done" line has been received.
  244. *
  245. * @return this builder
  246. */
  247. Builder setDoneReceived() {
  248. doneReceived = true;
  249. return this;
  250. }
  251. /**
  252. * Value of an agent line received after the command and before the
  253. * arguments. E.g. "agent=a.b.c/1.0" should set "a.b.c/1.0".
  254. *
  255. * @param agentValue
  256. * the client-supplied agent capability, without the leading
  257. * "agent="
  258. * @return this builder
  259. */
  260. Builder setAgent(@Nullable String agentValue) {
  261. agent = agentValue;
  262. return this;
  263. }
  264. /**
  265. * Records an application-specific option supplied in a server-option
  266. * line, for later retrieval with
  267. * {@link FetchV2Request#getServerOptions}.
  268. *
  269. * @param value
  270. * the client-supplied server-option capability, without
  271. * leading "server-option=".
  272. * @return this builder
  273. */
  274. Builder addServerOption(@NonNull String value) {
  275. serverOptions.add(value);
  276. return this;
  277. }
  278. /**
  279. * @return Initialized fetch request
  280. */
  281. FetchV2Request build() {
  282. return new FetchV2Request(peerHas, wantedRefs, wantIds,
  283. clientShallowCommits, deepenSince, deepenNotRefs,
  284. depth, filterSpec, doneReceived, clientCapabilities,
  285. agent, Collections.unmodifiableList(serverOptions));
  286. }
  287. }
  288. }