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 			try {
468 				if (walk.parseAny(r.getObjectId()).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(r.getObjectId().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 		p.end();
492 		outNeedsEnd = false;
493 		return true;
494 	}
495 
496 	private String enableCapabilities() throws TransportException {
497 		final StringBuilder line = new StringBuilder();
498 		if (noProgress)
499 			wantCapability(line, OPTION_NO_PROGRESS);
500 		if (includeTags)
501 			includeTags = wantCapability(line, OPTION_INCLUDE_TAG);
502 		if (allowOfsDelta)
503 			wantCapability(line, OPTION_OFS_DELTA);
504 
505 		if (wantCapability(line, OPTION_MULTI_ACK_DETAILED)) {
506 			multiAck = MultiAck.DETAILED;
507 			if (statelessRPC)
508 				noDone = wantCapability(line, OPTION_NO_DONE);
509 		} else if (wantCapability(line, OPTION_MULTI_ACK))
510 			multiAck = MultiAck.CONTINUE;
511 		else
512 			multiAck = MultiAck.OFF;
513 
514 		if (thinPack)
515 			thinPack = wantCapability(line, OPTION_THIN_PACK);
516 		if (wantCapability(line, OPTION_SIDE_BAND_64K))
517 			sideband = true;
518 		else if (wantCapability(line, OPTION_SIDE_BAND))
519 			sideband = true;
520 
521 		if (statelessRPC && multiAck != MultiAck.DETAILED) {
522 			// Our stateless RPC implementation relies upon the detailed
523 			// ACK status to tell us common objects for reuse in future
524 			// requests.  If its not enabled, we can't talk to the peer.
525 			//
526 			throw new PackProtocolException(uri, MessageFormat.format(
527 					JGitText.get().statelessRPCRequiresOptionToBeEnabled,
528 					OPTION_MULTI_ACK_DETAILED));
529 		}
530 
531 		addUserAgentCapability(line);
532 		return line.toString();
533 	}
534 
535 	private void negotiate(final ProgressMonitor monitor) throws IOException,
536 			CancelledException {
537 		final MutableObjectId ackId = new MutableObjectId();
538 		int resultsPending = 0;
539 		int havesSent = 0;
540 		int havesSinceLastContinue = 0;
541 		boolean receivedContinue = false;
542 		boolean receivedAck = false;
543 		boolean receivedReady = false;
544 
545 		if (statelessRPC)
546 			state.writeTo(out, null);
547 
548 		negotiateBegin();
549 		SEND_HAVES: for (;;) {
550 			final RevCommit c = walk.next();
551 			if (c == null)
552 				break SEND_HAVES;
553 
554 			pckOut.writeString("have " + c.getId().name() + "\n"); //$NON-NLS-1$ //$NON-NLS-2$
555 			havesSent++;
556 			havesSinceLastContinue++;
557 
558 			if ((31 & havesSent) != 0) {
559 				// We group the have lines into blocks of 32, each marked
560 				// with a flush (aka end). This one is within a block so
561 				// continue with another have line.
562 				//
563 				continue;
564 			}
565 
566 			if (monitor.isCancelled())
567 				throw new CancelledException();
568 
569 			pckOut.end();
570 			resultsPending++; // Each end will cause a result to come back.
571 
572 			if (havesSent == 32 && !statelessRPC) {
573 				// On the first block we race ahead and try to send
574 				// more of the second block while waiting for the
575 				// remote to respond to our first block request.
576 				// This keeps us one block ahead of the peer.
577 				//
578 				continue;
579 			}
580 
581 			READ_RESULT: for (;;) {
582 				final AckNackResult anr = pckIn.readACK(ackId);
583 				switch (anr) {
584 				case NAK:
585 					// More have lines are necessary to compute the
586 					// pack on the remote side. Keep doing that.
587 					//
588 					resultsPending--;
589 					break READ_RESULT;
590 
591 				case ACK:
592 					// The remote side is happy and knows exactly what
593 					// to send us. There is no further negotiation and
594 					// we can break out immediately.
595 					//
596 					multiAck = MultiAck.OFF;
597 					resultsPending = 0;
598 					receivedAck = true;
599 					if (statelessRPC)
600 						state.writeTo(out, null);
601 					break SEND_HAVES;
602 
603 				case ACK_CONTINUE:
604 				case ACK_COMMON:
605 				case ACK_READY:
606 					// The server knows this commit (ackId). We don't
607 					// need to send any further along its ancestry, but
608 					// we need to continue to talk about other parts of
609 					// our local history.
610 					//
611 					markCommon(walk.parseAny(ackId), anr);
612 					receivedAck = true;
613 					receivedContinue = true;
614 					havesSinceLastContinue = 0;
615 					if (anr == AckNackResult.ACK_READY)
616 						receivedReady = true;
617 					break;
618 				}
619 
620 				if (monitor.isCancelled())
621 					throw new CancelledException();
622 			}
623 
624 			if (noDone & receivedReady)
625 				break SEND_HAVES;
626 			if (statelessRPC)
627 				state.writeTo(out, null);
628 
629 			if (receivedContinue && havesSinceLastContinue > MAX_HAVES) {
630 				// Our history must be really different from the remote's.
631 				// We just sent a whole slew of have lines, and it did not
632 				// recognize any of them. Avoid sending our entire history
633 				// to them by giving up early.
634 				//
635 				break SEND_HAVES;
636 			}
637 		}
638 
639 		// Tell the remote side we have run out of things to talk about.
640 		//
641 		if (monitor.isCancelled())
642 			throw new CancelledException();
643 
644 		if (!receivedReady || !noDone) {
645 			// When statelessRPC is true we should always leave SEND_HAVES
646 			// loop above while in the middle of a request. This allows us
647 			// to just write done immediately.
648 			//
649 			pckOut.writeString("done\n"); //$NON-NLS-1$
650 			pckOut.flush();
651 		}
652 
653 		if (!receivedAck) {
654 			// Apparently if we have never received an ACK earlier
655 			// there is one more result expected from the done we
656 			// just sent to the remote.
657 			//
658 			multiAck = MultiAck.OFF;
659 			resultsPending++;
660 		}
661 
662 		READ_RESULT: while (resultsPending > 0 || multiAck != MultiAck.OFF) {
663 			final AckNackResult anr = pckIn.readACK(ackId);
664 			resultsPending--;
665 			switch (anr) {
666 			case NAK:
667 				// A NAK is a response to an end we queued earlier
668 				// we eat it and look for another ACK/NAK message.
669 				//
670 				break;
671 
672 			case ACK:
673 				// A solitary ACK at this point means the remote won't
674 				// speak anymore, but is going to send us a pack now.
675 				//
676 				break READ_RESULT;
677 
678 			case ACK_CONTINUE:
679 			case ACK_COMMON:
680 			case ACK_READY:
681 				// We will expect a normal ACK to break out of the loop.
682 				//
683 				multiAck = MultiAck.CONTINUE;
684 				break;
685 			}
686 
687 			if (monitor.isCancelled())
688 				throw new CancelledException();
689 		}
690 	}
691 
692 	private void negotiateBegin() throws IOException {
693 		walk.resetRetain(REACHABLE, ADVERTISED);
694 		walk.markStart(reachableCommits);
695 		walk.sort(RevSort.COMMIT_TIME_DESC);
696 		walk.setRevFilter(new RevFilter() {
697 			@Override
698 			public RevFilter clone() {
699 				return this;
700 			}
701 
702 			@Override
703 			public boolean include(final RevWalk walker, final RevCommit c) {
704 				final boolean remoteKnowsIsCommon = c.has(COMMON);
705 				if (c.has(ADVERTISED)) {
706 					// Remote advertised this, and we have it, hence common.
707 					// Whether or not the remote knows that fact is tested
708 					// before we added the flag. If the remote doesn't know
709 					// we have to still send them this object.
710 					//
711 					c.add(COMMON);
712 				}
713 				return !remoteKnowsIsCommon;
714 			}
715 
716 			@Override
717 			public boolean requiresCommitBody() {
718 				return false;
719 			}
720 		});
721 	}
722 
723 	private void markRefsAdvertised() {
724 		for (final Ref r : getRefs()) {
725 			markAdvertised(r.getObjectId());
726 			if (r.getPeeledObjectId() != null)
727 				markAdvertised(r.getPeeledObjectId());
728 		}
729 	}
730 
731 	private void markAdvertised(final AnyObjectId id) {
732 		try {
733 			walk.parseAny(id).add(ADVERTISED);
734 		} catch (IOException readError) {
735 			// We probably just do not have this object locally.
736 		}
737 	}
738 
739 	private void markCommon(final RevObject obj, final AckNackResult anr)
740 			throws IOException {
741 		if (statelessRPC && anr == AckNackResult.ACK_COMMON && !obj.has(STATE)) {
742 			StringBuilder s;
743 
744 			s = new StringBuilder(6 + Constants.OBJECT_ID_STRING_LENGTH);
745 			s.append("have "); //$NON-NLS-1$
746 			s.append(obj.name());
747 			s.append('\n');
748 			pckState.writeString(s.toString());
749 			obj.add(STATE);
750 		}
751 		obj.add(COMMON);
752 		if (obj instanceof RevCommit)
753 			((RevCommit) obj).carry(COMMON);
754 	}
755 
756 	private void receivePack(final ProgressMonitor monitor,
757 			OutputStream outputStream) throws IOException {
758 		onReceivePack();
759 		InputStream input = in;
760 		if (sideband)
761 			input = new SideBandInputStream(input, monitor, getMessageWriter(),
762 					outputStream);
763 
764 		try (ObjectInserter ins = local.newObjectInserter()) {
765 			PackParser parser = ins.newPackParser(input);
766 			parser.setAllowThin(thinPack);
767 			parser.setObjectChecker(transport.getObjectChecker());
768 			parser.setLockMessage(lockMessage);
769 			packLock = parser.parse(monitor);
770 			ins.flush();
771 		}
772 	}
773 
774 	/**
775 	 * Notification event delivered just before the pack is received from the
776 	 * network. This event can be used by RPC such as {@link TransportHttp} to
777 	 * disable its request magic and ensure the pack stream is read correctly.
778 	 *
779 	 * @since 2.0
780 	 */
781 	protected void onReceivePack() {
782 		// By default do nothing for TCP based protocols.
783 	}
784 
785 	private static class CancelledException extends Exception {
786 		private static final long serialVersionUID = 1L;
787 	}
788 }