View Javadoc
1   /*
2    * Copyright (C) 2008-2010, Google Inc.
3    * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
4    * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org> and others
5    *
6    * This program and the accompanying materials are made available under the
7    * terms of the Eclipse Distribution License v. 1.0 which is available at
8    * https://www.eclipse.org/org/documents/edl-v10.php.
9    *
10   * SPDX-License-Identifier: BSD-3-Clause
11   */
12  
13  package org.eclipse.jgit.transport;
14  
15  import java.io.IOException;
16  import java.io.InputStream;
17  import java.io.OutputStream;
18  import java.text.MessageFormat;
19  import java.util.Collection;
20  import java.util.Collections;
21  import java.util.Date;
22  import java.util.Set;
23  
24  import org.eclipse.jgit.errors.PackProtocolException;
25  import org.eclipse.jgit.errors.TransportException;
26  import org.eclipse.jgit.internal.JGitText;
27  import org.eclipse.jgit.internal.storage.file.PackLock;
28  import org.eclipse.jgit.lib.AnyObjectId;
29  import org.eclipse.jgit.lib.Config;
30  import org.eclipse.jgit.lib.Constants;
31  import org.eclipse.jgit.lib.MutableObjectId;
32  import org.eclipse.jgit.lib.NullProgressMonitor;
33  import org.eclipse.jgit.lib.ObjectId;
34  import org.eclipse.jgit.lib.ObjectInserter;
35  import org.eclipse.jgit.lib.ProgressMonitor;
36  import org.eclipse.jgit.lib.Ref;
37  import org.eclipse.jgit.revwalk.RevCommit;
38  import org.eclipse.jgit.revwalk.RevCommitList;
39  import org.eclipse.jgit.revwalk.RevFlag;
40  import org.eclipse.jgit.revwalk.RevObject;
41  import org.eclipse.jgit.revwalk.RevSort;
42  import org.eclipse.jgit.revwalk.RevWalk;
43  import org.eclipse.jgit.revwalk.filter.CommitTimeRevFilter;
44  import org.eclipse.jgit.revwalk.filter.RevFilter;
45  import org.eclipse.jgit.transport.GitProtocolConstants.MultiAck;
46  import org.eclipse.jgit.transport.PacketLineIn.AckNackResult;
47  import org.eclipse.jgit.util.TemporaryBuffer;
48  
49  /**
50   * Fetch implementation using the native Git pack transfer service.
51   * <p>
52   * This is the canonical implementation for transferring objects from the remote
53   * repository to the local repository by talking to the 'git-upload-pack'
54   * service. Objects are packed on the remote side into a pack file and then sent
55   * down the pipe to us.
56   * <p>
57   * This connection requires only a bi-directional pipe or socket, and thus is
58   * easily wrapped up into a local process pipe, anonymous TCP socket, or a
59   * command executed through an SSH tunnel.
60   * <p>
61   * If {@link org.eclipse.jgit.transport.BasePackConnection#statelessRPC} is
62   * {@code true}, this connection can be tunneled over a request-response style
63   * RPC system like HTTP. The RPC call boundary is determined by this class
64   * switching from writing to the OutputStream to reading from the InputStream.
65   * <p>
66   * Concrete implementations should just call
67   * {@link #init(java.io.InputStream, java.io.OutputStream)} and
68   * {@link #readAdvertisedRefs()} methods in constructor or before any use. They
69   * should also handle resources releasing in {@link #close()} method if needed.
70   */
71  public abstract class BasePackFetchConnection extends BasePackConnection
72  		implements FetchConnection {
73  	/**
74  	 * Maximum number of 'have' lines to send before giving up.
75  	 * <p>
76  	 * During {@link #negotiate(ProgressMonitor)} we send at most this many
77  	 * commits to the remote peer as 'have' lines without an ACK response before
78  	 * we give up.
79  	 */
80  	private static final int MAX_HAVES = 256;
81  
82  	/**
83  	 * Amount of data the client sends before starting to read.
84  	 * <p>
85  	 * Any output stream given to the client must be able to buffer this many
86  	 * bytes before the client will stop writing and start reading from the
87  	 * input stream. If the output stream blocks before this many bytes are in
88  	 * the send queue, the system will deadlock.
89  	 */
90  	protected static final int MIN_CLIENT_BUFFER = 2 * 32 * 46 + 8;
91  
92  	/**
93  	 * Include tags if we are also including the referenced objects.
94  	 * @since 2.0
95  	 */
96  	public static final String OPTION_INCLUDE_TAG = GitProtocolConstants.OPTION_INCLUDE_TAG;
97  
98  	/**
99  	 * Multi-ACK support for improved negotiation.
100 	 * @since 2.0
101 	 */
102 	public static final String OPTION_MULTI_ACK = GitProtocolConstants.OPTION_MULTI_ACK;
103 
104 	/**
105 	 * Multi-ACK detailed support for improved negotiation.
106 	 * @since 2.0
107 	 */
108 	public static final String OPTION_MULTI_ACK_DETAILED = GitProtocolConstants.OPTION_MULTI_ACK_DETAILED;
109 
110 	/**
111 	 * The client supports packs with deltas but not their bases.
112 	 * @since 2.0
113 	 */
114 	public static final String OPTION_THIN_PACK = GitProtocolConstants.OPTION_THIN_PACK;
115 
116 	/**
117 	 * The client supports using the side-band for progress messages.
118 	 * @since 2.0
119 	 */
120 	public static final String OPTION_SIDE_BAND = GitProtocolConstants.OPTION_SIDE_BAND;
121 
122 	/**
123 	 * The client supports using the 64K side-band for progress messages.
124 	 * @since 2.0
125 	 */
126 	public static final String OPTION_SIDE_BAND_64K = GitProtocolConstants.OPTION_SIDE_BAND_64K;
127 
128 	/**
129 	 * The client supports packs with OFS deltas.
130 	 * @since 2.0
131 	 */
132 	public static final String OPTION_OFS_DELTA = GitProtocolConstants.OPTION_OFS_DELTA;
133 
134 	/**
135 	 * The client supports shallow fetches.
136 	 * @since 2.0
137 	 */
138 	public static final String OPTION_SHALLOW = GitProtocolConstants.OPTION_SHALLOW;
139 
140 	/**
141 	 * The client does not want progress messages and will ignore them.
142 	 * @since 2.0
143 	 */
144 	public static final String OPTION_NO_PROGRESS = GitProtocolConstants.OPTION_NO_PROGRESS;
145 
146 	/**
147 	 * The client supports receiving a pack before it has sent "done".
148 	 * @since 2.0
149 	 */
150 	public static final String OPTION_NO_DONE = GitProtocolConstants.OPTION_NO_DONE;
151 
152 	/**
153 	 * The client supports fetching objects at the tip of any ref, even if not
154 	 * advertised.
155 	 * @since 3.1
156 	 */
157 	public static final String OPTION_ALLOW_TIP_SHA1_IN_WANT = GitProtocolConstants.OPTION_ALLOW_TIP_SHA1_IN_WANT;
158 
159 	/**
160 	 * The client supports fetching objects that are reachable from a tip of a
161 	 * ref that is allowed to fetch.
162 	 * @since 4.1
163 	 */
164 	public static final String OPTION_ALLOW_REACHABLE_SHA1_IN_WANT = GitProtocolConstants.OPTION_ALLOW_REACHABLE_SHA1_IN_WANT;
165 
166 	/**
167 	 * The client specified a filter expression.
168 	 *
169 	 * @since 5.0
170 	 */
171 	public static final String OPTION_FILTER = GitProtocolConstants.OPTION_FILTER;
172 
173 	private final RevWalk walk;
174 
175 	/** All commits that are immediately reachable by a local ref. */
176 	private RevCommitList<RevCommit> reachableCommits;
177 
178 	/** Marks an object as having all its dependencies. */
179 	final RevFlag REACHABLE;
180 
181 	/** Marks a commit known to both sides of the connection. */
182 	final RevFlag COMMON;
183 
184 	/** Like {@link #COMMON} but means its also in {@link #pckState}. */
185 	private final RevFlag STATE;
186 
187 	/** Marks a commit listed in the advertised refs. */
188 	final RevFlag ADVERTISED;
189 
190 	private MultiAck multiAck = MultiAck.OFF;
191 
192 	private boolean thinPack;
193 
194 	private boolean sideband;
195 
196 	private boolean includeTags;
197 
198 	private boolean allowOfsDelta;
199 
200 	private boolean noDone;
201 
202 	private boolean noProgress;
203 
204 	private String lockMessage;
205 
206 	private PackLock packLock;
207 
208 	private int maxHaves;
209 
210 	/** RPC state, if {@link BasePackConnection#statelessRPC} is true. */
211 	private TemporaryBuffer.Heap state;
212 
213 	private PacketLineOut pckState;
214 
215 	/**
216 	 * Either FilterSpec.NO_FILTER for a filter that doesn't filter
217 	 * anything, or a filter that indicates what and what not to send to the
218 	 * server.
219 	 */
220 	private final FilterSpec filterSpec;
221 
222 	/**
223 	 * Create a new connection to fetch using the native git transport.
224 	 *
225 	 * @param packTransport
226 	 *            the transport.
227 	 */
228 	public BasePackFetchConnection(PackTransport packTransport) {
229 		super(packTransport);
230 
231 		if (local != null) {
232 			final FetchConfig cfg = getFetchConfig();
233 			allowOfsDelta = cfg.allowOfsDelta;
234 			maxHaves = cfg.maxHaves;
235 		} else {
236 			allowOfsDelta = true;
237 			maxHaves = Integer.MAX_VALUE;
238 		}
239 
240 		includeTags = transport.getTagOpt() != TagOpt.NO_TAGS;
241 		thinPack = transport.isFetchThin();
242 		filterSpec = transport.getFilterSpec();
243 
244 		if (local != null) {
245 			walk = new RevWalk(local);
246 			walk.setRetainBody(false);
247 			reachableCommits = new RevCommitList<>();
248 			REACHABLE = walk.newFlag("REACHABLE"); //$NON-NLS-1$
249 			COMMON = walk.newFlag("COMMON"); //$NON-NLS-1$
250 			STATE = walk.newFlag("STATE"); //$NON-NLS-1$
251 			ADVERTISED = walk.newFlag("ADVERTISED"); //$NON-NLS-1$
252 
253 			walk.carry(COMMON);
254 			walk.carry(REACHABLE);
255 			walk.carry(ADVERTISED);
256 		} else {
257 			walk = null;
258 			REACHABLE = null;
259 			COMMON = null;
260 			STATE = null;
261 			ADVERTISED = null;
262 		}
263 	}
264 
265 	static class FetchConfig {
266 		final boolean allowOfsDelta;
267 
268 		final int maxHaves;
269 
270 		FetchConfig(Config c) {
271 			allowOfsDelta = c.getBoolean("repack", "usedeltabaseoffset", true); //$NON-NLS-1$ //$NON-NLS-2$
272 			maxHaves = c.getInt("fetch", "maxhaves", Integer.MAX_VALUE); //$NON-NLS-1$ //$NON-NLS-2$
273 		}
274 
275 		FetchConfig(boolean allowOfsDelta, int maxHaves) {
276 			this.allowOfsDelta = allowOfsDelta;
277 			this.maxHaves = maxHaves;
278 		}
279 	}
280 
281 	/** {@inheritDoc} */
282 	@Override
283 	public final void fetch(final ProgressMonitor monitor,
284 			final Collection<Ref> want, final Set<ObjectId> have)
285 			throws TransportException {
286 		fetch(monitor, want, have, null);
287 	}
288 
289 	/** {@inheritDoc} */
290 	@Override
291 	public final void fetch(final ProgressMonitor monitor,
292 			final Collection<Ref> want, final Set<ObjectId> have,
293 			OutputStream outputStream) throws TransportException {
294 		markStartedOperation();
295 		doFetch(monitor, want, have, outputStream);
296 	}
297 
298 	/** {@inheritDoc} */
299 	@Override
300 	public boolean didFetchIncludeTags() {
301 		return false;
302 	}
303 
304 	/** {@inheritDoc} */
305 	@Override
306 	public boolean didFetchTestConnectivity() {
307 		return false;
308 	}
309 
310 	/** {@inheritDoc} */
311 	@Override
312 	public void setPackLockMessage(String message) {
313 		lockMessage = message;
314 	}
315 
316 	/** {@inheritDoc} */
317 	@Override
318 	public Collection<PackLock> getPackLocks() {
319 		if (packLock != null)
320 			return Collections.singleton(packLock);
321 		return Collections.<PackLock> emptyList();
322 	}
323 
324 	/**
325 	 * Execute common ancestor negotiation and fetch the objects.
326 	 *
327 	 * @param monitor
328 	 *            progress monitor to receive status updates. If the monitor is
329 	 *            the {@link org.eclipse.jgit.lib.NullProgressMonitor#INSTANCE}, then the no-progress
330 	 *            option enabled.
331 	 * @param want
332 	 *            the advertised remote references the caller wants to fetch.
333 	 * @param have
334 	 *            additional objects to assume that already exist locally. This
335 	 *            will be added to the set of objects reachable from the
336 	 *            destination repository's references.
337 	 * @param outputStream
338 	 *            ouputStream to write sideband messages to
339 	 * @throws org.eclipse.jgit.errors.TransportException
340 	 *             if any exception occurs.
341 	 * @since 3.0
342 	 */
343 	protected void doFetch(final ProgressMonitor monitor,
344 			final Collection<Ref> want, final Set<ObjectId> have,
345 			OutputStream outputStream) throws TransportException {
346 		try {
347 			noProgress = monitor == NullProgressMonitor.INSTANCE;
348 
349 			markRefsAdvertised();
350 			markReachable(have, maxTimeWanted(want));
351 
352 			if (statelessRPC) {
353 				state = new TemporaryBuffer.Heap(Integer.MAX_VALUE);
354 				pckState = new PacketLineOut(state);
355 			}
356 
357 			if (sendWants(want)) {
358 				negotiate(monitor);
359 
360 				walk.dispose();
361 				reachableCommits = null;
362 				state = null;
363 				pckState = null;
364 
365 				receivePack(monitor, outputStream);
366 			}
367 		} catch (CancelledException ce) {
368 			close();
369 			return; // Caller should test (or just know) this themselves.
370 		} catch (IOException | RuntimeException err) {
371 			close();
372 			throw new TransportException(err.getMessage(), err);
373 		}
374 	}
375 
376 	/** {@inheritDoc} */
377 	@Override
378 	public void close() {
379 		if (walk != null)
380 			walk.close();
381 		super.close();
382 	}
383 
384 	FetchConfig getFetchConfig() {
385 		return local.getConfig().get(FetchConfig::new);
386 	}
387 
388 	private int maxTimeWanted(Collection<Ref> wants) {
389 		int maxTime = 0;
390 		for (Ref r : wants) {
391 			try {
392 				final RevObject obj = walk.parseAny(r.getObjectId());
393 				if (obj instanceof RevCommit) {
394 					final int cTime = ((RevCommit) obj).getCommitTime();
395 					if (maxTime < cTime)
396 						maxTime = cTime;
397 				}
398 			} catch (IOException error) {
399 				// We don't have it, but we want to fetch (thus fixing error).
400 			}
401 		}
402 		return maxTime;
403 	}
404 
405 	private void markReachable(Set<ObjectId> have, int maxTime)
406 			throws IOException {
407 		for (Ref r : local.getRefDatabase().getRefs()) {
408 			ObjectId id = r.getPeeledObjectId();
409 			if (id == null)
410 				id = r.getObjectId();
411 			if (id == null)
412 				continue;
413 			parseReachable(id);
414 		}
415 
416 		for (ObjectId id : local.getAdditionalHaves())
417 			parseReachable(id);
418 
419 		for (ObjectId id : have)
420 			parseReachable(id);
421 
422 		if (maxTime > 0) {
423 			// Mark reachable commits until we reach maxTime. These may
424 			// wind up later matching up against things we want and we
425 			// can avoid asking for something we already happen to have.
426 			//
427 			final Date maxWhen = new Date(maxTime * 1000L);
428 			walk.sort(RevSort.COMMIT_TIME_DESC);
429 			walk.markStart(reachableCommits);
430 			walk.setRevFilter(CommitTimeRevFilter.after(maxWhen));
431 			for (;;) {
432 				final RevCommit c = walk.next();
433 				if (c == null)
434 					break;
435 				if (c.has(ADVERTISED) && !c.has(COMMON)) {
436 					// This is actually going to be a common commit, but
437 					// our peer doesn't know that fact yet.
438 					//
439 					c.add(COMMON);
440 					c.carry(COMMON);
441 					reachableCommits.add(c);
442 				}
443 			}
444 		}
445 	}
446 
447 	private void parseReachable(ObjectId id) {
448 		try {
449 			RevCommit o = walk.parseCommit(id);
450 			if (!o.has(REACHABLE)) {
451 				o.add(REACHABLE);
452 				reachableCommits.add(o);
453 			}
454 		} catch (IOException readError) {
455 			// If we cannot read the value of the ref skip it.
456 		}
457 	}
458 
459 	private boolean sendWants(Collection<Ref> want) throws IOException {
460 		final PacketLineOut p = statelessRPC ? pckState : pckOut;
461 		boolean first = true;
462 		for (Ref r : want) {
463 			ObjectId objectId = r.getObjectId();
464 			if (objectId == null) {
465 				continue;
466 			}
467 			try {
468 				if (walk.parseAny(objectId).has(REACHABLE)) {
469 					// We already have this object. Asking for it is
470 					// not a very good idea.
471 					//
472 					continue;
473 				}
474 			} catch (IOException err) {
475 				// Its OK, we don't have it, but we want to fix that
476 				// by fetching the object from the other side.
477 			}
478 
479 			final StringBuilder line = new StringBuilder(46);
480 			line.append("want "); //$NON-NLS-1$
481 			line.append(objectId.name());
482 			if (first) {
483 				line.append(enableCapabilities());
484 				first = false;
485 			}
486 			line.append('\n');
487 			p.writeString(line.toString());
488 		}
489 		if (first) {
490 			return false;
491 		}
492 		if (!filterSpec.isNoOp()) {
493 			p.writeString(filterSpec.filterLine());
494 		}
495 		p.end();
496 		outNeedsEnd = false;
497 		return true;
498 	}
499 
500 	private String enableCapabilities() throws TransportException {
501 		final StringBuilder line = new StringBuilder();
502 		if (noProgress)
503 			wantCapability(line, OPTION_NO_PROGRESS);
504 		if (includeTags)
505 			includeTags = wantCapability(line, OPTION_INCLUDE_TAG);
506 		if (allowOfsDelta)
507 			wantCapability(line, OPTION_OFS_DELTA);
508 
509 		if (wantCapability(line, OPTION_MULTI_ACK_DETAILED)) {
510 			multiAck = MultiAck.DETAILED;
511 			if (statelessRPC)
512 				noDone = wantCapability(line, OPTION_NO_DONE);
513 		} else if (wantCapability(line, OPTION_MULTI_ACK))
514 			multiAck = MultiAck.CONTINUE;
515 		else
516 			multiAck = MultiAck.OFF;
517 
518 		if (thinPack)
519 			thinPack = wantCapability(line, OPTION_THIN_PACK);
520 		if (wantCapability(line, OPTION_SIDE_BAND_64K))
521 			sideband = true;
522 		else if (wantCapability(line, OPTION_SIDE_BAND))
523 			sideband = true;
524 
525 		if (statelessRPC && multiAck != MultiAck.DETAILED) {
526 			// Our stateless RPC implementation relies upon the detailed
527 			// ACK status to tell us common objects for reuse in future
528 			// requests.  If its not enabled, we can't talk to the peer.
529 			//
530 			throw new PackProtocolException(uri, MessageFormat.format(
531 					JGitText.get().statelessRPCRequiresOptionToBeEnabled,
532 					OPTION_MULTI_ACK_DETAILED));
533 		}
534 
535 		if (!filterSpec.isNoOp() && !wantCapability(line, OPTION_FILTER)) {
536 			throw new PackProtocolException(uri,
537 					JGitText.get().filterRequiresCapability);
538 		}
539 
540 		addUserAgentCapability(line);
541 		return line.toString();
542 	}
543 
544 	private void negotiate(ProgressMonitor monitor) throws IOException,
545 			CancelledException {
546 		final MutableObjectId.html#MutableObjectId">MutableObjectId ackId = new MutableObjectId();
547 		int resultsPending = 0;
548 		int havesSent = 0;
549 		int havesSinceLastContinue = 0;
550 		boolean receivedContinue = false;
551 		boolean receivedAck = false;
552 		boolean receivedReady = false;
553 
554 		if (statelessRPC) {
555 			state.writeTo(out, null);
556 		}
557 
558 		negotiateBegin();
559 		SEND_HAVES: for (;;) {
560 			final RevCommit c = walk.next();
561 			if (c == null) {
562 				break SEND_HAVES;
563 			}
564 
565 			ObjectId o = c.getId();
566 			pckOut.writeString("have " + o.name() + "\n"); //$NON-NLS-1$ //$NON-NLS-2$
567 			havesSent++;
568 			havesSinceLastContinue++;
569 
570 			if ((31 & havesSent) != 0) {
571 				// We group the have lines into blocks of 32, each marked
572 				// with a flush (aka end). This one is within a block so
573 				// continue with another have line.
574 				//
575 				continue;
576 			}
577 
578 			if (monitor.isCancelled()) {
579 				throw new CancelledException();
580 			}
581 
582 			pckOut.end();
583 			resultsPending++; // Each end will cause a result to come back.
584 
585 			if (havesSent == 32 && !statelessRPC) {
586 				// On the first block we race ahead and try to send
587 				// more of the second block while waiting for the
588 				// remote to respond to our first block request.
589 				// This keeps us one block ahead of the peer.
590 				//
591 				continue;
592 			}
593 
594 			READ_RESULT: for (;;) {
595 				final AckNackResult anr = pckIn.readACK(ackId);
596 				switch (anr) {
597 				case NAK:
598 					// More have lines are necessary to compute the
599 					// pack on the remote side. Keep doing that.
600 					//
601 					resultsPending--;
602 					break READ_RESULT;
603 
604 				case ACK:
605 					// The remote side is happy and knows exactly what
606 					// to send us. There is no further negotiation and
607 					// we can break out immediately.
608 					//
609 					multiAck = MultiAck.OFF;
610 					resultsPending = 0;
611 					receivedAck = true;
612 					if (statelessRPC) {
613 						state.writeTo(out, null);
614 					}
615 					break SEND_HAVES;
616 
617 				case ACK_CONTINUE:
618 				case ACK_COMMON:
619 				case ACK_READY:
620 					// The server knows this commit (ackId). We don't
621 					// need to send any further along its ancestry, but
622 					// we need to continue to talk about other parts of
623 					// our local history.
624 					//
625 					markCommon(walk.parseAny(ackId), anr);
626 					receivedAck = true;
627 					receivedContinue = true;
628 					havesSinceLastContinue = 0;
629 					if (anr == AckNackResult.ACK_READY) {
630 						receivedReady = true;
631 					}
632 					break;
633 				}
634 
635 				if (monitor.isCancelled()) {
636 					throw new CancelledException();
637 				}
638 			}
639 
640 			if (noDone && receivedReady) {
641 				break SEND_HAVES;
642 			}
643 			if (statelessRPC) {
644 				state.writeTo(out, null);
645 			}
646 
647 			if ((receivedContinue && havesSinceLastContinue > MAX_HAVES)
648 					|| havesSent >= maxHaves) {
649 				// Our history must be really different from the remote's.
650 				// We just sent a whole slew of have lines, and it did not
651 				// recognize any of them. Avoid sending our entire history
652 				// to them by giving up early.
653 				//
654 				break SEND_HAVES;
655 			}
656 		}
657 
658 		// Tell the remote side we have run out of things to talk about.
659 		//
660 		if (monitor.isCancelled()) {
661 			throw new CancelledException();
662 		}
663 
664 		if (!receivedReady || !noDone) {
665 			// When statelessRPC is true we should always leave SEND_HAVES
666 			// loop above while in the middle of a request. This allows us
667 			// to just write done immediately.
668 			//
669 			pckOut.writeString("done\n"); //$NON-NLS-1$
670 			pckOut.flush();
671 		}
672 
673 		if (!receivedAck) {
674 			// Apparently if we have never received an ACK earlier
675 			// there is one more result expected from the done we
676 			// just sent to the remote.
677 			//
678 			multiAck = MultiAck.OFF;
679 			resultsPending++;
680 		}
681 
682 		READ_RESULT: while (resultsPending > 0 || multiAck != MultiAck.OFF) {
683 			final AckNackResult anr = pckIn.readACK(ackId);
684 			resultsPending--;
685 			switch (anr) {
686 			case NAK:
687 				// A NAK is a response to an end we queued earlier
688 				// we eat it and look for another ACK/NAK message.
689 				//
690 				break;
691 
692 			case ACK:
693 				// A solitary ACK at this point means the remote won't
694 				// speak anymore, but is going to send us a pack now.
695 				//
696 				break READ_RESULT;
697 
698 			case ACK_CONTINUE:
699 			case ACK_COMMON:
700 			case ACK_READY:
701 				// We will expect a normal ACK to break out of the loop.
702 				//
703 				multiAck = MultiAck.CONTINUE;
704 				break;
705 			}
706 
707 			if (monitor.isCancelled()) {
708 				throw new CancelledException();
709 			}
710 		}
711 	}
712 
713 	private void negotiateBegin() throws IOException {
714 		walk.resetRetain(REACHABLE, ADVERTISED);
715 		walk.markStart(reachableCommits);
716 		walk.sort(RevSort.COMMIT_TIME_DESC);
717 		walk.setRevFilter(new RevFilter() {
718 			@Override
719 			public RevFilter clone() {
720 				return this;
721 			}
722 
723 			@Override
724 			public boolean include(RevWalk walker, RevCommit c) {
725 				final boolean remoteKnowsIsCommon = c.has(COMMON);
726 				if (c.has(ADVERTISED)) {
727 					// Remote advertised this, and we have it, hence common.
728 					// Whether or not the remote knows that fact is tested
729 					// before we added the flag. If the remote doesn't know
730 					// we have to still send them this object.
731 					//
732 					c.add(COMMON);
733 				}
734 				return !remoteKnowsIsCommon;
735 			}
736 
737 			@Override
738 			public boolean requiresCommitBody() {
739 				return false;
740 			}
741 		});
742 	}
743 
744 	private void markRefsAdvertised() {
745 		for (Ref r : getRefs()) {
746 			markAdvertised(r.getObjectId());
747 			if (r.getPeeledObjectId() != null)
748 				markAdvertised(r.getPeeledObjectId());
749 		}
750 	}
751 
752 	private void markAdvertised(AnyObjectId id) {
753 		try {
754 			walk.parseAny(id).add(ADVERTISED);
755 		} catch (IOException readError) {
756 			// We probably just do not have this object locally.
757 		}
758 	}
759 
760 	private void markCommon(RevObject obj, AckNackResult anr)
761 			throws IOException {
762 		if (statelessRPC && anr == AckNackResult.ACK_COMMON && !obj.has(STATE)) {
763 			StringBuilder s;
764 
765 			s = new StringBuilder(6 + Constants.OBJECT_ID_STRING_LENGTH);
766 			s.append("have "); //$NON-NLS-1$
767 			s.append(obj.name());
768 			s.append('\n');
769 			pckState.writeString(s.toString());
770 			obj.add(STATE);
771 		}
772 		obj.add(COMMON);
773 		if (obj instanceof RevCommit)
774 			((RevCommit) obj).carry(COMMON);
775 	}
776 
777 	private void receivePack(final ProgressMonitor monitor,
778 			OutputStream outputStream) throws IOException {
779 		onReceivePack();
780 		InputStream input = in;
781 		if (sideband)
782 			input = new SideBandInputStream(input, monitor, getMessageWriter(),
783 					outputStream);
784 
785 		try (ObjectInserter ins = local.newObjectInserter()) {
786 			PackParser parser = ins.newPackParser(input);
787 			parser.setAllowThin(thinPack);
788 			parser.setObjectChecker(transport.getObjectChecker());
789 			parser.setLockMessage(lockMessage);
790 			packLock = parser.parse(monitor);
791 			ins.flush();
792 		}
793 	}
794 
795 	/**
796 	 * Notification event delivered just before the pack is received from the
797 	 * network. This event can be used by RPC such as {@link org.eclipse.jgit.transport.TransportHttp} to
798 	 * disable its request magic and ensure the pack stream is read correctly.
799 	 *
800 	 * @since 2.0
801 	 */
802 	protected void onReceivePack() {
803 		// By default do nothing for TCP based protocols.
804 	}
805 
806 	private static class CancelledException extends Exception {
807 		private static final long serialVersionUID = 1L;
808 	}
809 }