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

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