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 	private final RevWalk walk;
197 
198 	/** All commits that are immediately reachable by a local ref. */
199 	private RevCommitList<RevCommit> reachableCommits;
200 
201 	/** Marks an object as having all its dependencies. */
202 	final RevFlag REACHABLE;
203 
204 	/** Marks a commit known to both sides of the connection. */
205 	final RevFlag COMMON;
206 
207 	/** Like {@link #COMMON} but means its also in {@link #pckState}. */
208 	private final RevFlag STATE;
209 
210 	/** Marks a commit listed in the advertised refs. */
211 	final RevFlag ADVERTISED;
212 
213 	private MultiAck multiAck = MultiAck.OFF;
214 
215 	private boolean thinPack;
216 
217 	private boolean sideband;
218 
219 	private boolean includeTags;
220 
221 	private boolean allowOfsDelta;
222 
223 	private boolean noDone;
224 
225 	private boolean noProgress;
226 
227 	private String lockMessage;
228 
229 	private PackLock packLock;
230 
231 	/** RPC state, if {@link BasePackConnection#statelessRPC} is true. */
232 	private TemporaryBuffer.Heap state;
233 
234 	private PacketLineOut pckState;
235 
236 	/**
237 	 * Create a new connection to fetch using the native git transport.
238 	 *
239 	 * @param packTransport
240 	 *            the transport.
241 	 */
242 	public BasePackFetchConnection(final PackTransport packTransport) {
243 		super(packTransport);
244 
245 		if (local != null) {
246 			final FetchConfig cfg = local.getConfig().get(FetchConfig.KEY);
247 			allowOfsDelta = cfg.allowOfsDelta;
248 		} else {
249 			allowOfsDelta = true;
250 		}
251 		includeTags = transport.getTagOpt() != TagOpt.NO_TAGS;
252 		thinPack = transport.isFetchThin();
253 
254 		if (local != null) {
255 			walk = new RevWalk(local);
256 			reachableCommits = new RevCommitList<RevCommit>();
257 			REACHABLE = walk.newFlag("REACHABLE"); //$NON-NLS-1$
258 			COMMON = walk.newFlag("COMMON"); //$NON-NLS-1$
259 			STATE = walk.newFlag("STATE"); //$NON-NLS-1$
260 			ADVERTISED = walk.newFlag("ADVERTISED"); //$NON-NLS-1$
261 
262 			walk.carry(COMMON);
263 			walk.carry(REACHABLE);
264 			walk.carry(ADVERTISED);
265 		} else {
266 			walk = null;
267 			REACHABLE = null;
268 			COMMON = null;
269 			STATE = null;
270 			ADVERTISED = null;
271 		}
272 	}
273 
274 	private static class FetchConfig {
275 		static final SectionParser<FetchConfig> KEY = new SectionParser<FetchConfig>() {
276 			public FetchConfig parse(final Config cfg) {
277 				return new FetchConfig(cfg);
278 			}
279 		};
280 
281 		final boolean allowOfsDelta;
282 
283 		FetchConfig(final Config c) {
284 			allowOfsDelta = c.getBoolean("repack", "usedeltabaseoffset", true); //$NON-NLS-1$ //$NON-NLS-2$
285 		}
286 	}
287 
288 	public final void fetch(final ProgressMonitor monitor,
289 			final Collection<Ref> want, final Set<ObjectId> have)
290 			throws TransportException {
291 		fetch(monitor, want, have, null);
292 	}
293 
294 	/**
295 	 * @since 3.0
296 	 */
297 	public final void fetch(final ProgressMonitor monitor,
298 			final Collection<Ref> want, final Set<ObjectId> have,
299 			OutputStream outputStream) throws TransportException {
300 		markStartedOperation();
301 		doFetch(monitor, want, have, outputStream);
302 	}
303 
304 	public boolean didFetchIncludeTags() {
305 		return false;
306 	}
307 
308 	public boolean didFetchTestConnectivity() {
309 		return false;
310 	}
311 
312 	public void setPackLockMessage(final String message) {
313 		lockMessage = message;
314 	}
315 
316 	public Collection<PackLock> getPackLocks() {
317 		if (packLock != null)
318 			return Collections.singleton(packLock);
319 		return Collections.<PackLock> emptyList();
320 	}
321 
322 	/**
323 	 * Execute common ancestor negotiation and fetch the objects.
324 	 *
325 	 * @param monitor
326 	 *            progress monitor to receive status updates. If the monitor is
327 	 *            the {@link NullProgressMonitor#INSTANCE}, then the no-progress
328 	 *            option enabled.
329 	 * @param want
330 	 *            the advertised remote references the caller wants to fetch.
331 	 * @param have
332 	 *            additional objects to assume that already exist locally. This
333 	 *            will be added to the set of objects reachable from the
334 	 *            destination repository's references.
335 	 * @param outputStream
336 	 *            ouputStream to write sideband messages to
337 	 * @throws TransportException
338 	 *             if any exception occurs.
339 	 * @since 3.0
340 	 */
341 	protected void doFetch(final ProgressMonitor monitor,
342 			final Collection<Ref> want, final Set<ObjectId> have,
343 			OutputStream outputStream) throws TransportException {
344 		try {
345 			noProgress = monitor == NullProgressMonitor.INSTANCE;
346 
347 			markRefsAdvertised();
348 			markReachable(have, maxTimeWanted(want));
349 
350 			if (statelessRPC) {
351 				state = new TemporaryBuffer.Heap(Integer.MAX_VALUE);
352 				pckState = new PacketLineOut(state);
353 			}
354 
355 			if (sendWants(want)) {
356 				negotiate(monitor);
357 
358 				walk.dispose();
359 				reachableCommits = null;
360 				state = null;
361 				pckState = null;
362 
363 				receivePack(monitor, outputStream);
364 			}
365 		} catch (CancelledException ce) {
366 			close();
367 			return; // Caller should test (or just know) this themselves.
368 		} catch (IOException err) {
369 			close();
370 			throw new TransportException(err.getMessage(), err);
371 		} catch (RuntimeException err) {
372 			close();
373 			throw new TransportException(err.getMessage(), err);
374 		}
375 	}
376 
377 	@Override
378 	public void close() {
379 		if (walk != null)
380 			walk.close();
381 		super.close();
382 	}
383 
384 	private int maxTimeWanted(final Collection<Ref> wants) {
385 		int maxTime = 0;
386 		for (final Ref r : wants) {
387 			try {
388 				final RevObject obj = walk.parseAny(r.getObjectId());
389 				if (obj instanceof RevCommit) {
390 					final int cTime = ((RevCommit) obj).getCommitTime();
391 					if (maxTime < cTime)
392 						maxTime = cTime;
393 				}
394 			} catch (IOException error) {
395 				// We don't have it, but we want to fetch (thus fixing error).
396 			}
397 		}
398 		return maxTime;
399 	}
400 
401 	private void markReachable(final Set<ObjectId> have, final int maxTime)
402 			throws IOException {
403 		Map<String, Ref> refs = local.getRefDatabase().getRefs(ALL);
404 		for (final Ref r : refs.values()) {
405 			ObjectId id = r.getPeeledObjectId();
406 			if (id == null)
407 				id = r.getObjectId();
408 			if (id == null)
409 				continue;
410 			parseReachable(id);
411 		}
412 
413 		for (ObjectId id : local.getAdditionalHaves())
414 			parseReachable(id);
415 
416 		for (ObjectId id : have)
417 			parseReachable(id);
418 
419 		if (maxTime > 0) {
420 			// Mark reachable commits until we reach maxTime. These may
421 			// wind up later matching up against things we want and we
422 			// can avoid asking for something we already happen to have.
423 			//
424 			final Date maxWhen = new Date(maxTime * 1000L);
425 			walk.sort(RevSort.COMMIT_TIME_DESC);
426 			walk.markStart(reachableCommits);
427 			walk.setRevFilter(CommitTimeRevFilter.after(maxWhen));
428 			for (;;) {
429 				final RevCommit c = walk.next();
430 				if (c == null)
431 					break;
432 				if (c.has(ADVERTISED) && !c.has(COMMON)) {
433 					// This is actually going to be a common commit, but
434 					// our peer doesn't know that fact yet.
435 					//
436 					c.add(COMMON);
437 					c.carry(COMMON);
438 					reachableCommits.add(c);
439 				}
440 			}
441 		}
442 	}
443 
444 	private void parseReachable(ObjectId id) {
445 		try {
446 			RevCommit o = walk.parseCommit(id);
447 			if (!o.has(REACHABLE)) {
448 				o.add(REACHABLE);
449 				reachableCommits.add(o);
450 			}
451 		} catch (IOException readError) {
452 			// If we cannot read the value of the ref skip it.
453 		}
454 	}
455 
456 	private boolean sendWants(final Collection<Ref> want) throws IOException {
457 		final PacketLineOut p = statelessRPC ? pckState : pckOut;
458 		boolean first = true;
459 		for (final Ref r : want) {
460 			try {
461 				if (walk.parseAny(r.getObjectId()).has(REACHABLE)) {
462 					// We already have this object. Asking for it is
463 					// not a very good idea.
464 					//
465 					continue;
466 				}
467 			} catch (IOException err) {
468 				// Its OK, we don't have it, but we want to fix that
469 				// by fetching the object from the other side.
470 			}
471 
472 			final StringBuilder line = new StringBuilder(46);
473 			line.append("want "); //$NON-NLS-1$
474 			line.append(r.getObjectId().name());
475 			if (first) {
476 				line.append(enableCapabilities());
477 				first = false;
478 			}
479 			line.append('\n');
480 			p.writeString(line.toString());
481 		}
482 		if (first)
483 			return false;
484 		p.end();
485 		outNeedsEnd = false;
486 		return true;
487 	}
488 
489 	private String enableCapabilities() throws TransportException {
490 		final StringBuilder line = new StringBuilder();
491 		if (noProgress)
492 			wantCapability(line, OPTION_NO_PROGRESS);
493 		if (includeTags)
494 			includeTags = wantCapability(line, OPTION_INCLUDE_TAG);
495 		if (allowOfsDelta)
496 			wantCapability(line, OPTION_OFS_DELTA);
497 
498 		if (wantCapability(line, OPTION_MULTI_ACK_DETAILED)) {
499 			multiAck = MultiAck.DETAILED;
500 			if (statelessRPC)
501 				noDone = wantCapability(line, OPTION_NO_DONE);
502 		} else if (wantCapability(line, OPTION_MULTI_ACK))
503 			multiAck = MultiAck.CONTINUE;
504 		else
505 			multiAck = MultiAck.OFF;
506 
507 		if (thinPack)
508 			thinPack = wantCapability(line, OPTION_THIN_PACK);
509 		if (wantCapability(line, OPTION_SIDE_BAND_64K))
510 			sideband = true;
511 		else if (wantCapability(line, OPTION_SIDE_BAND))
512 			sideband = true;
513 
514 		if (statelessRPC && multiAck != MultiAck.DETAILED) {
515 			// Our stateless RPC implementation relies upon the detailed
516 			// ACK status to tell us common objects for reuse in future
517 			// requests.  If its not enabled, we can't talk to the peer.
518 			//
519 			throw new PackProtocolException(uri, MessageFormat.format(
520 					JGitText.get().statelessRPCRequiresOptionToBeEnabled,
521 					OPTION_MULTI_ACK_DETAILED));
522 		}
523 
524 		addUserAgentCapability(line);
525 		return line.toString();
526 	}
527 
528 	private void negotiate(final ProgressMonitor monitor) throws IOException,
529 			CancelledException {
530 		final MutableObjectId ackId = new MutableObjectId();
531 		int resultsPending = 0;
532 		int havesSent = 0;
533 		int havesSinceLastContinue = 0;
534 		boolean receivedContinue = false;
535 		boolean receivedAck = false;
536 		boolean receivedReady = false;
537 
538 		if (statelessRPC)
539 			state.writeTo(out, null);
540 
541 		negotiateBegin();
542 		SEND_HAVES: for (;;) {
543 			final RevCommit c = walk.next();
544 			if (c == null)
545 				break SEND_HAVES;
546 
547 			pckOut.writeString("have " + c.getId().name() + "\n"); //$NON-NLS-1$ //$NON-NLS-2$
548 			havesSent++;
549 			havesSinceLastContinue++;
550 
551 			if ((31 & havesSent) != 0) {
552 				// We group the have lines into blocks of 32, each marked
553 				// with a flush (aka end). This one is within a block so
554 				// continue with another have line.
555 				//
556 				continue;
557 			}
558 
559 			if (monitor.isCancelled())
560 				throw new CancelledException();
561 
562 			pckOut.end();
563 			resultsPending++; // Each end will cause a result to come back.
564 
565 			if (havesSent == 32 && !statelessRPC) {
566 				// On the first block we race ahead and try to send
567 				// more of the second block while waiting for the
568 				// remote to respond to our first block request.
569 				// This keeps us one block ahead of the peer.
570 				//
571 				continue;
572 			}
573 
574 			READ_RESULT: for (;;) {
575 				final AckNackResult anr = pckIn.readACK(ackId);
576 				switch (anr) {
577 				case NAK:
578 					// More have lines are necessary to compute the
579 					// pack on the remote side. Keep doing that.
580 					//
581 					resultsPending--;
582 					break READ_RESULT;
583 
584 				case ACK:
585 					// The remote side is happy and knows exactly what
586 					// to send us. There is no further negotiation and
587 					// we can break out immediately.
588 					//
589 					multiAck = MultiAck.OFF;
590 					resultsPending = 0;
591 					receivedAck = true;
592 					if (statelessRPC)
593 						state.writeTo(out, null);
594 					break SEND_HAVES;
595 
596 				case ACK_CONTINUE:
597 				case ACK_COMMON:
598 				case ACK_READY:
599 					// The server knows this commit (ackId). We don't
600 					// need to send any further along its ancestry, but
601 					// we need to continue to talk about other parts of
602 					// our local history.
603 					//
604 					markCommon(walk.parseAny(ackId), anr);
605 					receivedAck = true;
606 					receivedContinue = true;
607 					havesSinceLastContinue = 0;
608 					if (anr == AckNackResult.ACK_READY)
609 						receivedReady = true;
610 					break;
611 				}
612 
613 				if (monitor.isCancelled())
614 					throw new CancelledException();
615 			}
616 
617 			if (noDone & receivedReady)
618 				break SEND_HAVES;
619 			if (statelessRPC)
620 				state.writeTo(out, null);
621 
622 			if (receivedContinue && havesSinceLastContinue > MAX_HAVES) {
623 				// Our history must be really different from the remote's.
624 				// We just sent a whole slew of have lines, and it did not
625 				// recognize any of them. Avoid sending our entire history
626 				// to them by giving up early.
627 				//
628 				break SEND_HAVES;
629 			}
630 		}
631 
632 		// Tell the remote side we have run out of things to talk about.
633 		//
634 		if (monitor.isCancelled())
635 			throw new CancelledException();
636 
637 		if (!receivedReady || !noDone) {
638 			// When statelessRPC is true we should always leave SEND_HAVES
639 			// loop above while in the middle of a request. This allows us
640 			// to just write done immediately.
641 			//
642 			pckOut.writeString("done\n"); //$NON-NLS-1$
643 			pckOut.flush();
644 		}
645 
646 		if (!receivedAck) {
647 			// Apparently if we have never received an ACK earlier
648 			// there is one more result expected from the done we
649 			// just sent to the remote.
650 			//
651 			multiAck = MultiAck.OFF;
652 			resultsPending++;
653 		}
654 
655 		READ_RESULT: while (resultsPending > 0 || multiAck != MultiAck.OFF) {
656 			final AckNackResult anr = pckIn.readACK(ackId);
657 			resultsPending--;
658 			switch (anr) {
659 			case NAK:
660 				// A NAK is a response to an end we queued earlier
661 				// we eat it and look for another ACK/NAK message.
662 				//
663 				break;
664 
665 			case ACK:
666 				// A solitary ACK at this point means the remote won't
667 				// speak anymore, but is going to send us a pack now.
668 				//
669 				break READ_RESULT;
670 
671 			case ACK_CONTINUE:
672 			case ACK_COMMON:
673 			case ACK_READY:
674 				// We will expect a normal ACK to break out of the loop.
675 				//
676 				multiAck = MultiAck.CONTINUE;
677 				break;
678 			}
679 
680 			if (monitor.isCancelled())
681 				throw new CancelledException();
682 		}
683 	}
684 
685 	private void negotiateBegin() throws IOException {
686 		walk.resetRetain(REACHABLE, ADVERTISED);
687 		walk.markStart(reachableCommits);
688 		walk.sort(RevSort.COMMIT_TIME_DESC);
689 		walk.setRevFilter(new RevFilter() {
690 			@Override
691 			public RevFilter clone() {
692 				return this;
693 			}
694 
695 			@Override
696 			public boolean include(final RevWalk walker, final RevCommit c) {
697 				final boolean remoteKnowsIsCommon = c.has(COMMON);
698 				if (c.has(ADVERTISED)) {
699 					// Remote advertised this, and we have it, hence common.
700 					// Whether or not the remote knows that fact is tested
701 					// before we added the flag. If the remote doesn't know
702 					// we have to still send them this object.
703 					//
704 					c.add(COMMON);
705 				}
706 				return !remoteKnowsIsCommon;
707 			}
708 
709 			@Override
710 			public boolean requiresCommitBody() {
711 				return false;
712 			}
713 		});
714 	}
715 
716 	private void markRefsAdvertised() {
717 		for (final Ref r : getRefs()) {
718 			markAdvertised(r.getObjectId());
719 			if (r.getPeeledObjectId() != null)
720 				markAdvertised(r.getPeeledObjectId());
721 		}
722 	}
723 
724 	private void markAdvertised(final AnyObjectId id) {
725 		try {
726 			walk.parseAny(id).add(ADVERTISED);
727 		} catch (IOException readError) {
728 			// We probably just do not have this object locally.
729 		}
730 	}
731 
732 	private void markCommon(final RevObject obj, final AckNackResult anr)
733 			throws IOException {
734 		if (statelessRPC && anr == AckNackResult.ACK_COMMON && !obj.has(STATE)) {
735 			StringBuilder s;
736 
737 			s = new StringBuilder(6 + Constants.OBJECT_ID_STRING_LENGTH);
738 			s.append("have "); //$NON-NLS-1$
739 			s.append(obj.name());
740 			s.append('\n');
741 			pckState.writeString(s.toString());
742 			obj.add(STATE);
743 		}
744 		obj.add(COMMON);
745 		if (obj instanceof RevCommit)
746 			((RevCommit) obj).carry(COMMON);
747 	}
748 
749 	private void receivePack(final ProgressMonitor monitor,
750 			OutputStream outputStream) throws IOException {
751 		onReceivePack();
752 		InputStream input = in;
753 		if (sideband)
754 			input = new SideBandInputStream(input, monitor, getMessageWriter(),
755 					outputStream);
756 
757 		try (ObjectInserter ins = local.newObjectInserter()) {
758 			PackParser parser = ins.newPackParser(input);
759 			parser.setAllowThin(thinPack);
760 			parser.setObjectChecker(transport.getObjectChecker());
761 			parser.setLockMessage(lockMessage);
762 			packLock = parser.parse(monitor);
763 			ins.flush();
764 		}
765 	}
766 
767 	/**
768 	 * Notification event delivered just before the pack is received from the
769 	 * network. This event can be used by RPC such as {@link TransportHttp} to
770 	 * disable its request magic and ensure the pack stream is read correctly.
771 	 *
772 	 * @since 2.0
773 	 */
774 	protected void onReceivePack() {
775 		// By default do nothing for TCP based protocols.
776 	}
777 
778 	private static class CancelledException extends Exception {
779 		private static final long serialVersionUID = 1L;
780 	}
781 }