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