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