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 java.util.ArrayList;
46  import java.util.HashSet;
47  import java.util.List;
48  import java.util.Set;
49  
50  import org.eclipse.jgit.annotations.NonNull;
51  import org.eclipse.jgit.lib.ObjectId;
52  
53  /**
54   * fetch protocol v2 request.
55   *
56   * <p>
57   * This is used as an input to {@link ProtocolV2Hook}.
58   *
59   * @since 5.1
60   */
61  public final class FetchV2Request {
62  	private final List<ObjectId> peerHas;
63  
64  	private final List<String> wantedRefs;
65  
66  	private final Set<ObjectId> wantsIds;
67  
68  	private final Set<ObjectId> clientShallowCommits;
69  
70  	private final int deepenSince;
71  
72  	private final List<String> deepenNotRefs;
73  
74  	private final int depth;
75  
76  	private final long filterBlobLimit;
77  
78  	private final Set<String> options;
79  
80  	private final boolean doneReceived;
81  
82  	private FetchV2Request(List<ObjectId> peerHas,
83  			List<String> wantedRefs, Set<ObjectId> wantsIds,
84  			Set<ObjectId> clientShallowCommits, int deepenSince,
85  			List<String> deepenNotRefs, int depth, long filterBlobLimit,
86  			boolean doneReceived, Set<String> options) {
87  		this.peerHas = peerHas;
88  		this.wantedRefs = wantedRefs;
89  		this.wantsIds = wantsIds;
90  		this.clientShallowCommits = clientShallowCommits;
91  		this.deepenSince = deepenSince;
92  		this.deepenNotRefs = deepenNotRefs;
93  		this.depth = depth;
94  		this.filterBlobLimit = filterBlobLimit;
95  		this.doneReceived = doneReceived;
96  		this.options = options;
97  	}
98  
99  	/**
100 	 * @return object ids in the "have" lines of the request
101 	 */
102 	@NonNull
103 	List<ObjectId> getPeerHas() {
104 		return this.peerHas;
105 	}
106 
107 	/**
108 	 * @return list of references in the "want-ref" lines of the request
109 	 */
110 	@NonNull
111 	List<String> getWantedRefs() {
112 		return wantedRefs;
113 	}
114 
115 	/**
116 	 * @return object ids in the "want" (but not "want-ref") lines of the request
117 	 */
118 	@NonNull
119 	Set<ObjectId> getWantsIds() {
120 		return wantsIds;
121 	}
122 
123 	/**
124 	 * Shallow commits the client already has.
125 	 *
126 	 * These are sent by the client in "shallow" request lines.
127 	 *
128 	 * @return set of commits the client has declared as shallow.
129 	 */
130 	@NonNull
131 	Set<ObjectId> getClientShallowCommits() {
132 		return clientShallowCommits;
133 	}
134 
135 	/**
136 	 * The value in a "deepen-since" line in the request, indicating the
137 	 * timestamp where to stop fetching/cloning.
138 	 *
139 	 * @return timestamp in seconds since the epoch, where to stop the shallow
140 	 *         fetch/clone. Defaults to 0 if not set in the request.
141 	 */
142 	int getDeepenSince() {
143 		return deepenSince;
144 	}
145 
146 	/**
147 	 * @return the refs in "deepen-not" lines in the request.
148 	 */
149 	@NonNull
150 	List<String> getDeepenNotRefs() {
151 		return deepenNotRefs;
152 	}
153 
154 	/**
155 	 * @return the depth set in a "deepen" line. 0 by default.
156 	 */
157 	int getDepth() {
158 		return depth;
159 	}
160 
161 	/**
162 	 * @return the blob limit set in a "filter" line (-1 if not set)
163 	 */
164 	long getFilterBlobLimit() {
165 		return filterBlobLimit;
166 	}
167 
168 	/**
169 	 * @return true if the request had a "done" line
170 	 */
171 	boolean wasDoneReceived() {
172 		return doneReceived;
173 	}
174 
175 	/**
176 	 * Options that tune the expected response from the server, like
177 	 * "thin-pack", "no-progress" or "ofs-delta"
178 	 *
179 	 * These are options listed and well-defined in the git protocol
180 	 * specification
181 	 *
182 	 * @return options found in the request lines
183 	 */
184 	@NonNull
185 	Set<String> getOptions() {
186 		return options;
187 	}
188 
189 	/** @return A builder of {@link FetchV2Request}. */
190 	static Builder builder() {
191 		return new Builder();
192 	}
193 
194 
195 	/** A builder for {@link FetchV2Request}. */
196 	static final class Builder {
197 		List<ObjectId> peerHas = new ArrayList<>();
198 
199 		List<String> wantedRefs = new ArrayList<>();
200 
201 		Set<ObjectId> wantsIds = new HashSet<>();
202 
203 		Set<ObjectId> clientShallowCommits = new HashSet<>();
204 
205 		List<String> deepenNotRefs = new ArrayList<>();
206 
207 		Set<String> options = new HashSet<>();
208 
209 		int depth;
210 
211 		int deepenSince;
212 
213 		long filterBlobLimit = -1;
214 
215 		boolean doneReceived;
216 
217 		private Builder() {
218 		}
219 
220 		/**
221 		 * @param objectId
222 		 *            from a "have" line in a fetch request
223 		 * @return the builder
224 		 */
225 		Builder addPeerHas(ObjectId objectId) {
226 			peerHas.add(objectId);
227 			return this;
228 		}
229 
230 		/**
231 		 * From a "want-ref" line in a fetch request
232 		 *
233 		 * @param refName
234 		 *            reference name
235 		 * @return the builder
236 		 */
237 		Builder addWantedRef(String refName) {
238 			wantedRefs.add(refName);
239 			return this;
240 		}
241 
242 		/**
243 		 * @param option
244 		 *            fetch request lines acting as options
245 		 * @return the builder
246 		 */
247 		Builder addOption(String option) {
248 			options.add(option);
249 			return this;
250 		}
251 
252 		/**
253 		 * @param objectId
254 		 *            from a "want" line in a fetch request
255 		 * @return the builder
256 		 */
257 		Builder addWantsId(ObjectId objectId) {
258 			wantsIds.add(objectId);
259 			return this;
260 		}
261 
262 		/**
263 		 * @param shallowOid
264 		 *            from a "shallow" line in the fetch request
265 		 * @return the builder
266 		 */
267 		Builder addClientShallowCommit(ObjectId shallowOid) {
268 			this.clientShallowCommits.add(shallowOid);
269 			return this;
270 		}
271 
272 		/**
273 		 * @param d
274 		 *            from a "deepen" line in the fetch request
275 		 * @return the builder
276 		 */
277 		Builder setDepth(int d) {
278 			this.depth = d;
279 			return this;
280 		}
281 
282 		/**
283 		 * @return depth set in the request (via a "deepen" line). Defaulting to
284 		 *         0 if not set.
285 		 */
286 		int getDepth() {
287 			return this.depth;
288 		}
289 
290 		/**
291 		 * @return if there has been any "deepen not" line in the request
292 		 */
293 		boolean hasDeepenNotRefs() {
294 			return !deepenNotRefs.isEmpty();
295 		}
296 
297 		/**
298 		 * @param deepenNotRef reference in a "deepen not" line
299 		 * @return the builder
300 		 */
301 		Builder addDeepenNotRef(String deepenNotRef) {
302 			this.deepenNotRefs.add(deepenNotRef);
303 			return this;
304 		}
305 
306 		/**
307 		 * @param value
308 		 *            Unix timestamp received in a "deepen since" line
309 		 * @return the builder
310 		 */
311 		Builder setDeepenSince(int value) {
312 			this.deepenSince = value;
313 			return this;
314 		}
315 
316 		/**
317 		 * @return shallow since value, sent before in a "deepen since" line. 0
318 		 *         by default.
319 		 */
320 		int getDeepenSince() {
321 			return this.deepenSince;
322 		}
323 
324 		/**
325 		 * @param filterBlobLimit
326 		 *            set in a "filter" line
327 		 * @return the builder
328 		 */
329 		Builder setFilterBlobLimit(long filterBlobLimit) {
330 			this.filterBlobLimit = filterBlobLimit;
331 			return this;
332 		}
333 
334 		/**
335 		 * Mark that the "done" line has been received.
336 		 *
337 		 * @return the builder
338 		 */
339 		Builder setDoneReceived() {
340 			this.doneReceived = true;
341 			return this;
342 		}
343 		/**
344 		 * @return Initialized fetch request
345 		 */
346 		FetchV2Request build() {
347 			return new FetchV2Request(peerHas, wantedRefs, wantsIds,
348 					clientShallowCommits, deepenSince, deepenNotRefs,
349 					depth, filterBlobLimit, doneReceived, options);
350 		}
351 	}
352 }