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