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 23KB

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