You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BasePackFetchConnection.java 24KB

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