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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  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 | RuntimeException err) {
  346. close();
  347. throw new TransportException(err.getMessage(), err);
  348. }
  349. }
  350. /** {@inheritDoc} */
  351. @Override
  352. public void close() {
  353. if (walk != null)
  354. walk.close();
  355. super.close();
  356. }
  357. FetchConfig getFetchConfig() {
  358. return local.getConfig().get(FetchConfig::new);
  359. }
  360. private int maxTimeWanted(Collection<Ref> wants) {
  361. int maxTime = 0;
  362. for (Ref r : wants) {
  363. try {
  364. final RevObject obj = walk.parseAny(r.getObjectId());
  365. if (obj instanceof RevCommit) {
  366. final int cTime = ((RevCommit) obj).getCommitTime();
  367. if (maxTime < cTime)
  368. maxTime = cTime;
  369. }
  370. } catch (IOException error) {
  371. // We don't have it, but we want to fetch (thus fixing error).
  372. }
  373. }
  374. return maxTime;
  375. }
  376. private void markReachable(Set<ObjectId> have, int maxTime)
  377. throws IOException {
  378. for (Ref r : local.getRefDatabase().getRefs()) {
  379. ObjectId id = r.getPeeledObjectId();
  380. if (id == null)
  381. id = r.getObjectId();
  382. if (id == null)
  383. continue;
  384. parseReachable(id);
  385. }
  386. for (ObjectId id : local.getAdditionalHaves())
  387. parseReachable(id);
  388. for (ObjectId id : have)
  389. parseReachable(id);
  390. if (maxTime > 0) {
  391. // Mark reachable commits until we reach maxTime. These may
  392. // wind up later matching up against things we want and we
  393. // can avoid asking for something we already happen to have.
  394. //
  395. final Date maxWhen = new Date(maxTime * 1000L);
  396. walk.sort(RevSort.COMMIT_TIME_DESC);
  397. walk.markStart(reachableCommits);
  398. walk.setRevFilter(CommitTimeRevFilter.after(maxWhen));
  399. for (;;) {
  400. final RevCommit c = walk.next();
  401. if (c == null)
  402. break;
  403. if (c.has(ADVERTISED) && !c.has(COMMON)) {
  404. // This is actually going to be a common commit, but
  405. // our peer doesn't know that fact yet.
  406. //
  407. c.add(COMMON);
  408. c.carry(COMMON);
  409. reachableCommits.add(c);
  410. }
  411. }
  412. }
  413. }
  414. private void parseReachable(ObjectId id) {
  415. try {
  416. RevCommit o = walk.parseCommit(id);
  417. if (!o.has(REACHABLE)) {
  418. o.add(REACHABLE);
  419. reachableCommits.add(o);
  420. }
  421. } catch (IOException readError) {
  422. // If we cannot read the value of the ref skip it.
  423. }
  424. }
  425. private boolean sendWants(Collection<Ref> want) throws IOException {
  426. final PacketLineOut p = statelessRPC ? pckState : pckOut;
  427. boolean first = true;
  428. for (Ref r : want) {
  429. ObjectId objectId = r.getObjectId();
  430. if (objectId == null) {
  431. continue;
  432. }
  433. try {
  434. if (walk.parseAny(objectId).has(REACHABLE)) {
  435. // We already have this object. Asking for it is
  436. // not a very good idea.
  437. //
  438. continue;
  439. }
  440. } catch (IOException err) {
  441. // Its OK, we don't have it, but we want to fix that
  442. // by fetching the object from the other side.
  443. }
  444. final StringBuilder line = new StringBuilder(46);
  445. line.append("want "); //$NON-NLS-1$
  446. line.append(objectId.name());
  447. if (first) {
  448. line.append(enableCapabilities());
  449. first = false;
  450. }
  451. line.append('\n');
  452. p.writeString(line.toString());
  453. }
  454. if (first) {
  455. return false;
  456. }
  457. if (!filterSpec.isNoOp()) {
  458. p.writeString(filterSpec.filterLine());
  459. }
  460. p.end();
  461. outNeedsEnd = false;
  462. return true;
  463. }
  464. private String enableCapabilities() throws TransportException {
  465. final StringBuilder line = new StringBuilder();
  466. if (noProgress)
  467. wantCapability(line, OPTION_NO_PROGRESS);
  468. if (includeTags)
  469. includeTags = wantCapability(line, OPTION_INCLUDE_TAG);
  470. if (allowOfsDelta)
  471. wantCapability(line, OPTION_OFS_DELTA);
  472. if (wantCapability(line, OPTION_MULTI_ACK_DETAILED)) {
  473. multiAck = MultiAck.DETAILED;
  474. if (statelessRPC)
  475. noDone = wantCapability(line, OPTION_NO_DONE);
  476. } else if (wantCapability(line, OPTION_MULTI_ACK))
  477. multiAck = MultiAck.CONTINUE;
  478. else
  479. multiAck = MultiAck.OFF;
  480. if (thinPack)
  481. thinPack = wantCapability(line, OPTION_THIN_PACK);
  482. if (wantCapability(line, OPTION_SIDE_BAND_64K))
  483. sideband = true;
  484. else if (wantCapability(line, OPTION_SIDE_BAND))
  485. sideband = true;
  486. if (statelessRPC && multiAck != MultiAck.DETAILED) {
  487. // Our stateless RPC implementation relies upon the detailed
  488. // ACK status to tell us common objects for reuse in future
  489. // requests. If its not enabled, we can't talk to the peer.
  490. //
  491. throw new PackProtocolException(uri, MessageFormat.format(
  492. JGitText.get().statelessRPCRequiresOptionToBeEnabled,
  493. OPTION_MULTI_ACK_DETAILED));
  494. }
  495. if (!filterSpec.isNoOp() && !wantCapability(line, OPTION_FILTER)) {
  496. throw new PackProtocolException(uri,
  497. JGitText.get().filterRequiresCapability);
  498. }
  499. addUserAgentCapability(line);
  500. return line.toString();
  501. }
  502. private void negotiate(ProgressMonitor monitor) throws IOException,
  503. CancelledException {
  504. final MutableObjectId ackId = new MutableObjectId();
  505. int resultsPending = 0;
  506. int havesSent = 0;
  507. int havesSinceLastContinue = 0;
  508. boolean receivedContinue = false;
  509. boolean receivedAck = false;
  510. boolean receivedReady = false;
  511. if (statelessRPC) {
  512. state.writeTo(out, null);
  513. }
  514. negotiateBegin();
  515. SEND_HAVES: for (;;) {
  516. final RevCommit c = walk.next();
  517. if (c == null) {
  518. break SEND_HAVES;
  519. }
  520. ObjectId o = c.getId();
  521. pckOut.writeString("have " + o.name() + "\n"); //$NON-NLS-1$ //$NON-NLS-2$
  522. havesSent++;
  523. havesSinceLastContinue++;
  524. if ((31 & havesSent) != 0) {
  525. // We group the have lines into blocks of 32, each marked
  526. // with a flush (aka end). This one is within a block so
  527. // continue with another have line.
  528. //
  529. continue;
  530. }
  531. if (monitor.isCancelled()) {
  532. throw new CancelledException();
  533. }
  534. pckOut.end();
  535. resultsPending++; // Each end will cause a result to come back.
  536. if (havesSent == 32 && !statelessRPC) {
  537. // On the first block we race ahead and try to send
  538. // more of the second block while waiting for the
  539. // remote to respond to our first block request.
  540. // This keeps us one block ahead of the peer.
  541. //
  542. continue;
  543. }
  544. READ_RESULT: for (;;) {
  545. final AckNackResult anr = pckIn.readACK(ackId);
  546. switch (anr) {
  547. case NAK:
  548. // More have lines are necessary to compute the
  549. // pack on the remote side. Keep doing that.
  550. //
  551. resultsPending--;
  552. break READ_RESULT;
  553. case ACK:
  554. // The remote side is happy and knows exactly what
  555. // to send us. There is no further negotiation and
  556. // we can break out immediately.
  557. //
  558. multiAck = MultiAck.OFF;
  559. resultsPending = 0;
  560. receivedAck = true;
  561. if (statelessRPC) {
  562. state.writeTo(out, null);
  563. }
  564. break SEND_HAVES;
  565. case ACK_CONTINUE:
  566. case ACK_COMMON:
  567. case ACK_READY:
  568. // The server knows this commit (ackId). We don't
  569. // need to send any further along its ancestry, but
  570. // we need to continue to talk about other parts of
  571. // our local history.
  572. //
  573. markCommon(walk.parseAny(ackId), anr);
  574. receivedAck = true;
  575. receivedContinue = true;
  576. havesSinceLastContinue = 0;
  577. if (anr == AckNackResult.ACK_READY) {
  578. receivedReady = true;
  579. }
  580. break;
  581. }
  582. if (monitor.isCancelled()) {
  583. throw new CancelledException();
  584. }
  585. }
  586. if (noDone & receivedReady) {
  587. break SEND_HAVES;
  588. }
  589. if (statelessRPC) {
  590. state.writeTo(out, null);
  591. }
  592. if (receivedContinue && havesSinceLastContinue > MAX_HAVES
  593. || havesSent >= maxHaves) {
  594. // Our history must be really different from the remote's.
  595. // We just sent a whole slew of have lines, and it did not
  596. // recognize any of them. Avoid sending our entire history
  597. // to them by giving up early.
  598. //
  599. break SEND_HAVES;
  600. }
  601. }
  602. // Tell the remote side we have run out of things to talk about.
  603. //
  604. if (monitor.isCancelled()) {
  605. throw new CancelledException();
  606. }
  607. if (!receivedReady || !noDone) {
  608. // When statelessRPC is true we should always leave SEND_HAVES
  609. // loop above while in the middle of a request. This allows us
  610. // to just write done immediately.
  611. //
  612. pckOut.writeString("done\n"); //$NON-NLS-1$
  613. pckOut.flush();
  614. }
  615. if (!receivedAck) {
  616. // Apparently if we have never received an ACK earlier
  617. // there is one more result expected from the done we
  618. // just sent to the remote.
  619. //
  620. multiAck = MultiAck.OFF;
  621. resultsPending++;
  622. }
  623. READ_RESULT: while (resultsPending > 0 || multiAck != MultiAck.OFF) {
  624. final AckNackResult anr = pckIn.readACK(ackId);
  625. resultsPending--;
  626. switch (anr) {
  627. case NAK:
  628. // A NAK is a response to an end we queued earlier
  629. // we eat it and look for another ACK/NAK message.
  630. //
  631. break;
  632. case ACK:
  633. // A solitary ACK at this point means the remote won't
  634. // speak anymore, but is going to send us a pack now.
  635. //
  636. break READ_RESULT;
  637. case ACK_CONTINUE:
  638. case ACK_COMMON:
  639. case ACK_READY:
  640. // We will expect a normal ACK to break out of the loop.
  641. //
  642. multiAck = MultiAck.CONTINUE;
  643. break;
  644. }
  645. if (monitor.isCancelled()) {
  646. throw new CancelledException();
  647. }
  648. }
  649. }
  650. private void negotiateBegin() throws IOException {
  651. walk.resetRetain(REACHABLE, ADVERTISED);
  652. walk.markStart(reachableCommits);
  653. walk.sort(RevSort.COMMIT_TIME_DESC);
  654. walk.setRevFilter(new RevFilter() {
  655. @Override
  656. public RevFilter clone() {
  657. return this;
  658. }
  659. @Override
  660. public boolean include(RevWalk walker, RevCommit c) {
  661. final boolean remoteKnowsIsCommon = c.has(COMMON);
  662. if (c.has(ADVERTISED)) {
  663. // Remote advertised this, and we have it, hence common.
  664. // Whether or not the remote knows that fact is tested
  665. // before we added the flag. If the remote doesn't know
  666. // we have to still send them this object.
  667. //
  668. c.add(COMMON);
  669. }
  670. return !remoteKnowsIsCommon;
  671. }
  672. @Override
  673. public boolean requiresCommitBody() {
  674. return false;
  675. }
  676. });
  677. }
  678. private void markRefsAdvertised() {
  679. for (Ref r : getRefs()) {
  680. markAdvertised(r.getObjectId());
  681. if (r.getPeeledObjectId() != null)
  682. markAdvertised(r.getPeeledObjectId());
  683. }
  684. }
  685. private void markAdvertised(AnyObjectId id) {
  686. try {
  687. walk.parseAny(id).add(ADVERTISED);
  688. } catch (IOException readError) {
  689. // We probably just do not have this object locally.
  690. }
  691. }
  692. private void markCommon(RevObject obj, AckNackResult anr)
  693. throws IOException {
  694. if (statelessRPC && anr == AckNackResult.ACK_COMMON && !obj.has(STATE)) {
  695. StringBuilder s;
  696. s = new StringBuilder(6 + Constants.OBJECT_ID_STRING_LENGTH);
  697. s.append("have "); //$NON-NLS-1$
  698. s.append(obj.name());
  699. s.append('\n');
  700. pckState.writeString(s.toString());
  701. obj.add(STATE);
  702. }
  703. obj.add(COMMON);
  704. if (obj instanceof RevCommit)
  705. ((RevCommit) obj).carry(COMMON);
  706. }
  707. private void receivePack(final ProgressMonitor monitor,
  708. OutputStream outputStream) throws IOException {
  709. onReceivePack();
  710. InputStream input = in;
  711. if (sideband)
  712. input = new SideBandInputStream(input, monitor, getMessageWriter(),
  713. outputStream);
  714. try (ObjectInserter ins = local.newObjectInserter()) {
  715. PackParser parser = ins.newPackParser(input);
  716. parser.setAllowThin(thinPack);
  717. parser.setObjectChecker(transport.getObjectChecker());
  718. parser.setLockMessage(lockMessage);
  719. packLock = parser.parse(monitor);
  720. ins.flush();
  721. }
  722. }
  723. /**
  724. * Notification event delivered just before the pack is received from the
  725. * network. This event can be used by RPC such as {@link org.eclipse.jgit.transport.TransportHttp} to
  726. * disable its request magic and ensure the pack stream is read correctly.
  727. *
  728. * @since 2.0
  729. */
  730. protected void onReceivePack() {
  731. // By default do nothing for TCP based protocols.
  732. }
  733. private static class CancelledException extends Exception {
  734. private static final long serialVersionUID = 1L;
  735. }
  736. }