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