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

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