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