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.1KB

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