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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  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.transport.GitProtocolConstants.MultiAck;
  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.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. private final RevWalk walk;
  179. /** All commits that are immediately reachable by a local ref. */
  180. private RevCommitList<RevCommit> reachableCommits;
  181. /** Marks an object as having all its dependencies. */
  182. final RevFlag REACHABLE;
  183. /** Marks a commit known to both sides of the connection. */
  184. final RevFlag COMMON;
  185. /** Like {@link #COMMON} but means its also in {@link #pckState}. */
  186. private final RevFlag STATE;
  187. /** Marks a commit listed in the advertised refs. */
  188. final RevFlag ADVERTISED;
  189. private MultiAck multiAck = MultiAck.OFF;
  190. private boolean thinPack;
  191. private boolean sideband;
  192. private boolean includeTags;
  193. private boolean allowOfsDelta;
  194. private boolean noDone;
  195. private boolean noProgress;
  196. private String lockMessage;
  197. private PackLock packLock;
  198. /** RPC state, if {@link BasePackConnection#statelessRPC} is true. */
  199. private TemporaryBuffer.Heap state;
  200. private PacketLineOut pckState;
  201. /**
  202. * Create a new connection to fetch using the native git transport.
  203. *
  204. * @param packTransport
  205. * the transport.
  206. */
  207. public BasePackFetchConnection(final PackTransport packTransport) {
  208. super(packTransport);
  209. if (local != null) {
  210. final FetchConfig cfg = local.getConfig().get(FetchConfig.KEY);
  211. allowOfsDelta = cfg.allowOfsDelta;
  212. } else {
  213. allowOfsDelta = true;
  214. }
  215. includeTags = transport.getTagOpt() != TagOpt.NO_TAGS;
  216. thinPack = transport.isFetchThin();
  217. if (local != null) {
  218. walk = new RevWalk(local);
  219. reachableCommits = new RevCommitList<RevCommit>();
  220. REACHABLE = walk.newFlag("REACHABLE"); //$NON-NLS-1$
  221. COMMON = walk.newFlag("COMMON"); //$NON-NLS-1$
  222. STATE = walk.newFlag("STATE"); //$NON-NLS-1$
  223. ADVERTISED = walk.newFlag("ADVERTISED"); //$NON-NLS-1$
  224. walk.carry(COMMON);
  225. walk.carry(REACHABLE);
  226. walk.carry(ADVERTISED);
  227. } else {
  228. walk = null;
  229. REACHABLE = null;
  230. COMMON = null;
  231. STATE = null;
  232. ADVERTISED = null;
  233. }
  234. }
  235. private static class FetchConfig {
  236. static final SectionParser<FetchConfig> KEY = new SectionParser<FetchConfig>() {
  237. public FetchConfig parse(final Config cfg) {
  238. return new FetchConfig(cfg);
  239. }
  240. };
  241. final boolean allowOfsDelta;
  242. FetchConfig(final Config c) {
  243. allowOfsDelta = c.getBoolean("repack", "usedeltabaseoffset", true); //$NON-NLS-1$ //$NON-NLS-2$
  244. }
  245. }
  246. public final void fetch(final ProgressMonitor monitor,
  247. final Collection<Ref> want, final Set<ObjectId> have)
  248. throws TransportException {
  249. fetch(monitor, want, have, null);
  250. }
  251. /**
  252. * @since 3.0
  253. */
  254. public final void fetch(final ProgressMonitor monitor,
  255. final Collection<Ref> want, final Set<ObjectId> have,
  256. OutputStream outputStream) throws TransportException {
  257. markStartedOperation();
  258. doFetch(monitor, want, have, outputStream);
  259. }
  260. public boolean didFetchIncludeTags() {
  261. return false;
  262. }
  263. public boolean didFetchTestConnectivity() {
  264. return false;
  265. }
  266. public void setPackLockMessage(final String message) {
  267. lockMessage = message;
  268. }
  269. public Collection<PackLock> getPackLocks() {
  270. if (packLock != null)
  271. return Collections.singleton(packLock);
  272. return Collections.<PackLock> emptyList();
  273. }
  274. /**
  275. * Execute common ancestor negotiation and fetch the objects.
  276. *
  277. * @param monitor
  278. * progress monitor to receive status updates. If the monitor is
  279. * the {@link NullProgressMonitor#INSTANCE}, then the no-progress
  280. * option enabled.
  281. * @param want
  282. * the advertised remote references the caller wants to fetch.
  283. * @param have
  284. * additional objects to assume that already exist locally. This
  285. * will be added to the set of objects reachable from the
  286. * destination repository's references.
  287. * @param outputStream
  288. * ouputStream to write sideband messages to
  289. * @throws TransportException
  290. * if any exception occurs.
  291. * @since 3.0
  292. */
  293. protected void doFetch(final ProgressMonitor monitor,
  294. final Collection<Ref> want, final Set<ObjectId> have,
  295. OutputStream outputStream) throws TransportException {
  296. try {
  297. noProgress = monitor == NullProgressMonitor.INSTANCE;
  298. markRefsAdvertised();
  299. markReachable(have, maxTimeWanted(want));
  300. if (statelessRPC) {
  301. state = new TemporaryBuffer.Heap(Integer.MAX_VALUE);
  302. pckState = new PacketLineOut(state);
  303. }
  304. if (sendWants(want)) {
  305. negotiate(monitor);
  306. walk.dispose();
  307. reachableCommits = null;
  308. state = null;
  309. pckState = null;
  310. receivePack(monitor, outputStream);
  311. }
  312. } catch (CancelledException ce) {
  313. close();
  314. return; // Caller should test (or just know) this themselves.
  315. } catch (IOException err) {
  316. close();
  317. throw new TransportException(err.getMessage(), err);
  318. } catch (RuntimeException err) {
  319. close();
  320. throw new TransportException(err.getMessage(), err);
  321. }
  322. }
  323. @Override
  324. public void close() {
  325. if (walk != null)
  326. walk.release();
  327. super.close();
  328. }
  329. private int maxTimeWanted(final Collection<Ref> wants) {
  330. int maxTime = 0;
  331. for (final Ref r : wants) {
  332. try {
  333. final RevObject obj = walk.parseAny(r.getObjectId());
  334. if (obj instanceof RevCommit) {
  335. final int cTime = ((RevCommit) obj).getCommitTime();
  336. if (maxTime < cTime)
  337. maxTime = cTime;
  338. }
  339. } catch (IOException error) {
  340. // We don't have it, but we want to fetch (thus fixing error).
  341. }
  342. }
  343. return maxTime;
  344. }
  345. private void markReachable(final Set<ObjectId> have, final int maxTime)
  346. throws IOException {
  347. Map<String, Ref> refs = local.getRefDatabase().getRefs(ALL);
  348. for (final Ref r : refs.values()) {
  349. ObjectId id = r.getPeeledObjectId();
  350. if (id == null)
  351. id = r.getObjectId();
  352. if (id == null)
  353. continue;
  354. parseReachable(id);
  355. }
  356. for (ObjectId id : local.getAdditionalHaves())
  357. parseReachable(id);
  358. for (ObjectId id : have)
  359. parseReachable(id);
  360. if (maxTime > 0) {
  361. // Mark reachable commits until we reach maxTime. These may
  362. // wind up later matching up against things we want and we
  363. // can avoid asking for something we already happen to have.
  364. //
  365. final Date maxWhen = new Date(maxTime * 1000L);
  366. walk.sort(RevSort.COMMIT_TIME_DESC);
  367. walk.markStart(reachableCommits);
  368. walk.setRevFilter(CommitTimeRevFilter.after(maxWhen));
  369. for (;;) {
  370. final RevCommit c = walk.next();
  371. if (c == null)
  372. break;
  373. if (c.has(ADVERTISED) && !c.has(COMMON)) {
  374. // This is actually going to be a common commit, but
  375. // our peer doesn't know that fact yet.
  376. //
  377. c.add(COMMON);
  378. c.carry(COMMON);
  379. reachableCommits.add(c);
  380. }
  381. }
  382. }
  383. }
  384. private void parseReachable(ObjectId id) {
  385. try {
  386. RevCommit o = walk.parseCommit(id);
  387. if (!o.has(REACHABLE)) {
  388. o.add(REACHABLE);
  389. reachableCommits.add(o);
  390. }
  391. } catch (IOException readError) {
  392. // If we cannot read the value of the ref skip it.
  393. }
  394. }
  395. private boolean sendWants(final Collection<Ref> want) throws IOException {
  396. final PacketLineOut p = statelessRPC ? pckState : pckOut;
  397. boolean first = true;
  398. for (final Ref r : want) {
  399. try {
  400. if (walk.parseAny(r.getObjectId()).has(REACHABLE)) {
  401. // We already have this object. Asking for it is
  402. // not a very good idea.
  403. //
  404. continue;
  405. }
  406. } catch (IOException err) {
  407. // Its OK, we don't have it, but we want to fix that
  408. // by fetching the object from the other side.
  409. }
  410. final StringBuilder line = new StringBuilder(46);
  411. line.append("want "); //$NON-NLS-1$
  412. line.append(r.getObjectId().name());
  413. if (first) {
  414. line.append(enableCapabilities());
  415. first = false;
  416. }
  417. line.append('\n');
  418. p.writeString(line.toString());
  419. }
  420. if (first)
  421. return false;
  422. p.end();
  423. outNeedsEnd = false;
  424. return true;
  425. }
  426. private String enableCapabilities() throws TransportException {
  427. final StringBuilder line = new StringBuilder();
  428. if (noProgress)
  429. wantCapability(line, OPTION_NO_PROGRESS);
  430. if (includeTags)
  431. includeTags = wantCapability(line, OPTION_INCLUDE_TAG);
  432. if (allowOfsDelta)
  433. wantCapability(line, OPTION_OFS_DELTA);
  434. if (wantCapability(line, OPTION_MULTI_ACK_DETAILED)) {
  435. multiAck = MultiAck.DETAILED;
  436. if (statelessRPC)
  437. noDone = wantCapability(line, OPTION_NO_DONE);
  438. } else if (wantCapability(line, OPTION_MULTI_ACK))
  439. multiAck = MultiAck.CONTINUE;
  440. else
  441. multiAck = MultiAck.OFF;
  442. if (thinPack)
  443. thinPack = wantCapability(line, OPTION_THIN_PACK);
  444. if (wantCapability(line, OPTION_SIDE_BAND_64K))
  445. sideband = true;
  446. else if (wantCapability(line, OPTION_SIDE_BAND))
  447. sideband = true;
  448. if (statelessRPC && multiAck != MultiAck.DETAILED) {
  449. // Our stateless RPC implementation relies upon the detailed
  450. // ACK status to tell us common objects for reuse in future
  451. // requests. If its not enabled, we can't talk to the peer.
  452. //
  453. throw new PackProtocolException(uri, MessageFormat.format(
  454. JGitText.get().statelessRPCRequiresOptionToBeEnabled,
  455. OPTION_MULTI_ACK_DETAILED));
  456. }
  457. return line.toString();
  458. }
  459. private void negotiate(final ProgressMonitor monitor) throws IOException,
  460. CancelledException {
  461. final MutableObjectId ackId = new MutableObjectId();
  462. int resultsPending = 0;
  463. int havesSent = 0;
  464. int havesSinceLastContinue = 0;
  465. boolean receivedContinue = false;
  466. boolean receivedAck = false;
  467. boolean receivedReady = false;
  468. if (statelessRPC)
  469. state.writeTo(out, null);
  470. negotiateBegin();
  471. SEND_HAVES: for (;;) {
  472. final RevCommit c = walk.next();
  473. if (c == null)
  474. break SEND_HAVES;
  475. pckOut.writeString("have " + c.getId().name() + "\n"); //$NON-NLS-1$ //$NON-NLS-2$
  476. havesSent++;
  477. havesSinceLastContinue++;
  478. if ((31 & havesSent) != 0) {
  479. // We group the have lines into blocks of 32, each marked
  480. // with a flush (aka end). This one is within a block so
  481. // continue with another have line.
  482. //
  483. continue;
  484. }
  485. if (monitor.isCancelled())
  486. throw new CancelledException();
  487. pckOut.end();
  488. resultsPending++; // Each end will cause a result to come back.
  489. if (havesSent == 32 && !statelessRPC) {
  490. // On the first block we race ahead and try to send
  491. // more of the second block while waiting for the
  492. // remote to respond to our first block request.
  493. // This keeps us one block ahead of the peer.
  494. //
  495. continue;
  496. }
  497. READ_RESULT: for (;;) {
  498. final AckNackResult anr = pckIn.readACK(ackId);
  499. switch (anr) {
  500. case NAK:
  501. // More have lines are necessary to compute the
  502. // pack on the remote side. Keep doing that.
  503. //
  504. resultsPending--;
  505. break READ_RESULT;
  506. case ACK:
  507. // The remote side is happy and knows exactly what
  508. // to send us. There is no further negotiation and
  509. // we can break out immediately.
  510. //
  511. multiAck = MultiAck.OFF;
  512. resultsPending = 0;
  513. receivedAck = true;
  514. if (statelessRPC)
  515. state.writeTo(out, null);
  516. break SEND_HAVES;
  517. case ACK_CONTINUE:
  518. case ACK_COMMON:
  519. case ACK_READY:
  520. // The server knows this commit (ackId). We don't
  521. // need to send any further along its ancestry, but
  522. // we need to continue to talk about other parts of
  523. // our local history.
  524. //
  525. markCommon(walk.parseAny(ackId), anr);
  526. receivedAck = true;
  527. receivedContinue = true;
  528. havesSinceLastContinue = 0;
  529. if (anr == AckNackResult.ACK_READY)
  530. receivedReady = true;
  531. break;
  532. }
  533. if (monitor.isCancelled())
  534. throw new CancelledException();
  535. }
  536. if (noDone & receivedReady)
  537. break SEND_HAVES;
  538. if (statelessRPC)
  539. state.writeTo(out, null);
  540. if (receivedContinue && havesSinceLastContinue > MAX_HAVES) {
  541. // Our history must be really different from the remote's.
  542. // We just sent a whole slew of have lines, and it did not
  543. // recognize any of them. Avoid sending our entire history
  544. // to them by giving up early.
  545. //
  546. break SEND_HAVES;
  547. }
  548. }
  549. // Tell the remote side we have run out of things to talk about.
  550. //
  551. if (monitor.isCancelled())
  552. throw new CancelledException();
  553. if (!receivedReady || !noDone) {
  554. // When statelessRPC is true we should always leave SEND_HAVES
  555. // loop above while in the middle of a request. This allows us
  556. // to just write done immediately.
  557. //
  558. pckOut.writeString("done\n"); //$NON-NLS-1$
  559. pckOut.flush();
  560. }
  561. if (!receivedAck) {
  562. // Apparently if we have never received an ACK earlier
  563. // there is one more result expected from the done we
  564. // just sent to the remote.
  565. //
  566. multiAck = MultiAck.OFF;
  567. resultsPending++;
  568. }
  569. READ_RESULT: while (resultsPending > 0 || multiAck != MultiAck.OFF) {
  570. final AckNackResult anr = pckIn.readACK(ackId);
  571. resultsPending--;
  572. switch (anr) {
  573. case NAK:
  574. // A NAK is a response to an end we queued earlier
  575. // we eat it and look for another ACK/NAK message.
  576. //
  577. break;
  578. case ACK:
  579. // A solitary ACK at this point means the remote won't
  580. // speak anymore, but is going to send us a pack now.
  581. //
  582. break READ_RESULT;
  583. case ACK_CONTINUE:
  584. case ACK_COMMON:
  585. case ACK_READY:
  586. // We will expect a normal ACK to break out of the loop.
  587. //
  588. multiAck = MultiAck.CONTINUE;
  589. break;
  590. }
  591. if (monitor.isCancelled())
  592. throw new CancelledException();
  593. }
  594. }
  595. private void negotiateBegin() throws IOException {
  596. walk.resetRetain(REACHABLE, ADVERTISED);
  597. walk.markStart(reachableCommits);
  598. walk.sort(RevSort.COMMIT_TIME_DESC);
  599. walk.setRevFilter(new RevFilter() {
  600. @Override
  601. public RevFilter clone() {
  602. return this;
  603. }
  604. @Override
  605. public boolean include(final RevWalk walker, final RevCommit c) {
  606. final boolean remoteKnowsIsCommon = c.has(COMMON);
  607. if (c.has(ADVERTISED)) {
  608. // Remote advertised this, and we have it, hence common.
  609. // Whether or not the remote knows that fact is tested
  610. // before we added the flag. If the remote doesn't know
  611. // we have to still send them this object.
  612. //
  613. c.add(COMMON);
  614. }
  615. return !remoteKnowsIsCommon;
  616. }
  617. @Override
  618. public boolean requiresCommitBody() {
  619. return false;
  620. }
  621. });
  622. }
  623. private void markRefsAdvertised() {
  624. for (final Ref r : getRefs()) {
  625. markAdvertised(r.getObjectId());
  626. if (r.getPeeledObjectId() != null)
  627. markAdvertised(r.getPeeledObjectId());
  628. }
  629. }
  630. private void markAdvertised(final AnyObjectId id) {
  631. try {
  632. walk.parseAny(id).add(ADVERTISED);
  633. } catch (IOException readError) {
  634. // We probably just do not have this object locally.
  635. }
  636. }
  637. private void markCommon(final RevObject obj, final AckNackResult anr)
  638. throws IOException {
  639. if (statelessRPC && anr == AckNackResult.ACK_COMMON && !obj.has(STATE)) {
  640. StringBuilder s;
  641. s = new StringBuilder(6 + Constants.OBJECT_ID_STRING_LENGTH);
  642. s.append("have "); //$NON-NLS-1$
  643. s.append(obj.name());
  644. s.append('\n');
  645. pckState.writeString(s.toString());
  646. obj.add(STATE);
  647. }
  648. obj.add(COMMON);
  649. if (obj instanceof RevCommit)
  650. ((RevCommit) obj).carry(COMMON);
  651. }
  652. private void receivePack(final ProgressMonitor monitor,
  653. OutputStream outputStream) throws IOException {
  654. onReceivePack();
  655. InputStream input = in;
  656. if (sideband)
  657. input = new SideBandInputStream(input, monitor, getMessageWriter(),
  658. outputStream);
  659. ObjectInserter ins = local.newObjectInserter();
  660. try {
  661. PackParser parser = ins.newPackParser(input);
  662. parser.setAllowThin(thinPack);
  663. parser.setObjectChecking(transport.isCheckFetchedObjects());
  664. parser.setLockMessage(lockMessage);
  665. packLock = parser.parse(monitor);
  666. ins.flush();
  667. } finally {
  668. ins.release();
  669. }
  670. }
  671. /**
  672. * Notification event delivered just before the pack is received from the
  673. * network. This event can be used by RPC such as {@link TransportHttp} to
  674. * disable its request magic and ensure the pack stream is read correctly.
  675. *
  676. * @since 2.0
  677. */
  678. protected void onReceivePack() {
  679. // By default do nothing for TCP based protocols.
  680. }
  681. private static class CancelledException extends Exception {
  682. private static final long serialVersionUID = 1L;
  683. }
  684. }