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.

UploadPack.java 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
  1. /*
  2. * Copyright (C) 2008-2010, Google Inc.
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.transport;
  44. import java.io.EOFException;
  45. import java.io.IOException;
  46. import java.io.InputStream;
  47. import java.io.OutputStream;
  48. import java.text.MessageFormat;
  49. import java.util.ArrayList;
  50. import java.util.HashSet;
  51. import java.util.List;
  52. import java.util.Map;
  53. import java.util.Set;
  54. import org.eclipse.jgit.JGitText;
  55. import org.eclipse.jgit.errors.MissingObjectException;
  56. import org.eclipse.jgit.errors.PackProtocolException;
  57. import org.eclipse.jgit.lib.Constants;
  58. import org.eclipse.jgit.lib.NullProgressMonitor;
  59. import org.eclipse.jgit.lib.ObjectId;
  60. import org.eclipse.jgit.lib.ProgressMonitor;
  61. import org.eclipse.jgit.lib.Ref;
  62. import org.eclipse.jgit.lib.Repository;
  63. import org.eclipse.jgit.revwalk.AsyncRevObjectQueue;
  64. import org.eclipse.jgit.revwalk.ObjectWalk;
  65. import org.eclipse.jgit.revwalk.RevCommit;
  66. import org.eclipse.jgit.revwalk.RevFlag;
  67. import org.eclipse.jgit.revwalk.RevFlagSet;
  68. import org.eclipse.jgit.revwalk.RevObject;
  69. import org.eclipse.jgit.revwalk.RevTag;
  70. import org.eclipse.jgit.revwalk.RevWalk;
  71. import org.eclipse.jgit.revwalk.filter.CommitTimeRevFilter;
  72. import org.eclipse.jgit.storage.pack.PackConfig;
  73. import org.eclipse.jgit.storage.pack.PackWriter;
  74. import org.eclipse.jgit.transport.BasePackFetchConnection.MultiAck;
  75. import org.eclipse.jgit.transport.RefAdvertiser.PacketLineOutRefAdvertiser;
  76. import org.eclipse.jgit.util.io.InterruptTimer;
  77. import org.eclipse.jgit.util.io.TimeoutInputStream;
  78. import org.eclipse.jgit.util.io.TimeoutOutputStream;
  79. /**
  80. * Implements the server side of a fetch connection, transmitting objects.
  81. */
  82. public class UploadPack {
  83. static final String OPTION_INCLUDE_TAG = BasePackFetchConnection.OPTION_INCLUDE_TAG;
  84. static final String OPTION_MULTI_ACK = BasePackFetchConnection.OPTION_MULTI_ACK;
  85. static final String OPTION_MULTI_ACK_DETAILED = BasePackFetchConnection.OPTION_MULTI_ACK_DETAILED;
  86. static final String OPTION_THIN_PACK = BasePackFetchConnection.OPTION_THIN_PACK;
  87. static final String OPTION_SIDE_BAND = BasePackFetchConnection.OPTION_SIDE_BAND;
  88. static final String OPTION_SIDE_BAND_64K = BasePackFetchConnection.OPTION_SIDE_BAND_64K;
  89. static final String OPTION_OFS_DELTA = BasePackFetchConnection.OPTION_OFS_DELTA;
  90. static final String OPTION_NO_PROGRESS = BasePackFetchConnection.OPTION_NO_PROGRESS;
  91. /** Database we read the objects from. */
  92. private final Repository db;
  93. /** Revision traversal support over {@link #db}. */
  94. private final RevWalk walk;
  95. /** Configuration to pass into the PackWriter. */
  96. private PackConfig packConfig;
  97. /** Timeout in seconds to wait for client interaction. */
  98. private int timeout;
  99. /**
  100. * Is the client connection a bi-directional socket or pipe?
  101. * <p>
  102. * If true, this class assumes it can perform multiple read and write cycles
  103. * with the client over the input and output streams. This matches the
  104. * functionality available with a standard TCP/IP connection, or a local
  105. * operating system or in-memory pipe.
  106. * <p>
  107. * If false, this class runs in a read everything then output results mode,
  108. * making it suitable for single round-trip systems RPCs such as HTTP.
  109. */
  110. private boolean biDirectionalPipe = true;
  111. /** Timer to manage {@link #timeout}. */
  112. private InterruptTimer timer;
  113. private InputStream rawIn;
  114. private OutputStream rawOut;
  115. private PacketLineIn pckIn;
  116. private PacketLineOut pckOut;
  117. /** The refs we advertised as existing at the start of the connection. */
  118. private Map<String, Ref> refs;
  119. /** Filter used while advertising the refs to the client. */
  120. private RefFilter refFilter;
  121. /** Capabilities requested by the client. */
  122. private final Set<String> options = new HashSet<String>();
  123. /** Raw ObjectIds the client has asked for, before validating them. */
  124. private final Set<ObjectId> wantIds = new HashSet<ObjectId>();
  125. /** Objects the client wants to obtain. */
  126. private final List<RevObject> wantAll = new ArrayList<RevObject>();
  127. /** Objects on both sides, these don't have to be sent. */
  128. private final List<RevObject> commonBase = new ArrayList<RevObject>();
  129. /** Commit time of the oldest common commit, in seconds. */
  130. private int oldestTime;
  131. /** null if {@link #commonBase} should be examined again. */
  132. private Boolean okToGiveUp;
  133. /** Objects we sent in our advertisement list, clients can ask for these. */
  134. private Set<ObjectId> advertised;
  135. /** Marked on objects the client has asked us to give them. */
  136. private final RevFlag WANT;
  137. /** Marked on objects both we and the client have. */
  138. private final RevFlag PEER_HAS;
  139. /** Marked on objects in {@link #commonBase}. */
  140. private final RevFlag COMMON;
  141. /** Objects where we found a path from the want list to a common base. */
  142. private final RevFlag SATISFIED;
  143. private final RevFlagSet SAVE;
  144. private MultiAck multiAck = MultiAck.OFF;
  145. private PackWriter.Statistics statistics;
  146. private UploadPackLogger logger;
  147. /**
  148. * Create a new pack upload for an open repository.
  149. *
  150. * @param copyFrom
  151. * the source repository.
  152. */
  153. public UploadPack(final Repository copyFrom) {
  154. db = copyFrom;
  155. walk = new RevWalk(db);
  156. walk.setRetainBody(false);
  157. WANT = walk.newFlag("WANT");
  158. PEER_HAS = walk.newFlag("PEER_HAS");
  159. COMMON = walk.newFlag("COMMON");
  160. SATISFIED = walk.newFlag("SATISFIED");
  161. walk.carry(PEER_HAS);
  162. SAVE = new RevFlagSet();
  163. SAVE.add(WANT);
  164. SAVE.add(PEER_HAS);
  165. SAVE.add(COMMON);
  166. SAVE.add(SATISFIED);
  167. refFilter = RefFilter.DEFAULT;
  168. }
  169. /** @return the repository this upload is reading from. */
  170. public final Repository getRepository() {
  171. return db;
  172. }
  173. /** @return the RevWalk instance used by this connection. */
  174. public final RevWalk getRevWalk() {
  175. return walk;
  176. }
  177. /** @return all refs which were advertised to the client. */
  178. public final Map<String, Ref> getAdvertisedRefs() {
  179. return refs;
  180. }
  181. /** @return timeout (in seconds) before aborting an IO operation. */
  182. public int getTimeout() {
  183. return timeout;
  184. }
  185. /**
  186. * Set the timeout before willing to abort an IO call.
  187. *
  188. * @param seconds
  189. * number of seconds to wait (with no data transfer occurring)
  190. * before aborting an IO read or write operation with the
  191. * connected client.
  192. */
  193. public void setTimeout(final int seconds) {
  194. timeout = seconds;
  195. }
  196. /**
  197. * @return true if this class expects a bi-directional pipe opened between
  198. * the client and itself. The default is true.
  199. */
  200. public boolean isBiDirectionalPipe() {
  201. return biDirectionalPipe;
  202. }
  203. /**
  204. * @param twoWay
  205. * if true, this class will assume the socket is a fully
  206. * bidirectional pipe between the two peers and takes advantage
  207. * of that by first transmitting the known refs, then waiting to
  208. * read commands. If false, this class assumes it must read the
  209. * commands before writing output and does not perform the
  210. * initial advertising.
  211. */
  212. public void setBiDirectionalPipe(final boolean twoWay) {
  213. biDirectionalPipe = twoWay;
  214. }
  215. /** @return the filter used while advertising the refs to the client */
  216. public RefFilter getRefFilter() {
  217. return refFilter;
  218. }
  219. /**
  220. * Set the filter used while advertising the refs to the client.
  221. * <p>
  222. * Only refs allowed by this filter will be sent to the client. This can
  223. * be used by a server to restrict the list of references the client can
  224. * obtain through clone or fetch, effectively limiting the access to only
  225. * certain refs.
  226. *
  227. * @param refFilter
  228. * the filter; may be null to show all refs.
  229. */
  230. public void setRefFilter(final RefFilter refFilter) {
  231. this.refFilter = refFilter != null ? refFilter : RefFilter.DEFAULT;
  232. }
  233. /**
  234. * Set the configuration used by the pack generator.
  235. *
  236. * @param pc
  237. * configuration controlling packing parameters. If null the
  238. * source repository's settings will be used.
  239. */
  240. public void setPackConfig(PackConfig pc) {
  241. this.packConfig = pc;
  242. }
  243. /**
  244. * Set the logger.
  245. *
  246. * @param logger
  247. * the logger instance. If null, no logging occurs.
  248. */
  249. public void setLogger(UploadPackLogger logger) {
  250. this.logger = logger;
  251. }
  252. /**
  253. * Execute the upload task on the socket.
  254. *
  255. * @param input
  256. * raw input to read client commands from. Caller must ensure the
  257. * input is buffered, otherwise read performance may suffer.
  258. * @param output
  259. * response back to the Git network client, to write the pack
  260. * data onto. Caller must ensure the output is buffered,
  261. * otherwise write performance may suffer.
  262. * @param messages
  263. * secondary "notice" channel to send additional messages out
  264. * through. When run over SSH this should be tied back to the
  265. * standard error channel of the command execution. For most
  266. * other network connections this should be null.
  267. * @throws IOException
  268. */
  269. public void upload(final InputStream input, final OutputStream output,
  270. final OutputStream messages) throws IOException {
  271. try {
  272. rawIn = input;
  273. rawOut = output;
  274. if (timeout > 0) {
  275. final Thread caller = Thread.currentThread();
  276. timer = new InterruptTimer(caller.getName() + "-Timer");
  277. TimeoutInputStream i = new TimeoutInputStream(rawIn, timer);
  278. TimeoutOutputStream o = new TimeoutOutputStream(rawOut, timer);
  279. i.setTimeout(timeout * 1000);
  280. o.setTimeout(timeout * 1000);
  281. rawIn = i;
  282. rawOut = o;
  283. }
  284. pckIn = new PacketLineIn(rawIn);
  285. pckOut = new PacketLineOut(rawOut);
  286. service();
  287. } finally {
  288. walk.release();
  289. if (timer != null) {
  290. try {
  291. timer.terminate();
  292. } finally {
  293. timer = null;
  294. }
  295. }
  296. }
  297. }
  298. /**
  299. * Get the PackWriter's statistics if a pack was sent to the client.
  300. *
  301. * @return statistics about pack output, if a pack was sent. Null if no pack
  302. * was sent, such as during the negotation phase of a smart HTTP
  303. * connection, or if the client was already up-to-date.
  304. */
  305. public PackWriter.Statistics getPackStatistics() {
  306. return statistics;
  307. }
  308. private void service() throws IOException {
  309. if (biDirectionalPipe)
  310. sendAdvertisedRefs(new PacketLineOutRefAdvertiser(pckOut));
  311. else {
  312. advertised = new HashSet<ObjectId>();
  313. refs = refFilter.filter(db.getAllRefs());
  314. for (Ref ref : refs.values()) {
  315. if (ref.getObjectId() != null)
  316. advertised.add(ref.getObjectId());
  317. }
  318. }
  319. recvWants();
  320. if (wantIds.isEmpty())
  321. return;
  322. if (options.contains(OPTION_MULTI_ACK_DETAILED))
  323. multiAck = MultiAck.DETAILED;
  324. else if (options.contains(OPTION_MULTI_ACK))
  325. multiAck = MultiAck.CONTINUE;
  326. else
  327. multiAck = MultiAck.OFF;
  328. if (negotiate())
  329. sendPack();
  330. }
  331. /**
  332. * Generate an advertisement of available refs and capabilities.
  333. *
  334. * @param adv
  335. * the advertisement formatter.
  336. * @throws IOException
  337. * the formatter failed to write an advertisement.
  338. */
  339. public void sendAdvertisedRefs(final RefAdvertiser adv) throws IOException {
  340. adv.init(db);
  341. adv.advertiseCapability(OPTION_INCLUDE_TAG);
  342. adv.advertiseCapability(OPTION_MULTI_ACK_DETAILED);
  343. adv.advertiseCapability(OPTION_MULTI_ACK);
  344. adv.advertiseCapability(OPTION_OFS_DELTA);
  345. adv.advertiseCapability(OPTION_SIDE_BAND);
  346. adv.advertiseCapability(OPTION_SIDE_BAND_64K);
  347. adv.advertiseCapability(OPTION_THIN_PACK);
  348. adv.advertiseCapability(OPTION_NO_PROGRESS);
  349. adv.setDerefTags(true);
  350. refs = refFilter.filter(db.getAllRefs());
  351. advertised = adv.send(refs);
  352. adv.end();
  353. }
  354. private void recvWants() throws IOException {
  355. boolean isFirst = true;
  356. for (;;) {
  357. String line;
  358. try {
  359. line = pckIn.readString();
  360. } catch (EOFException eof) {
  361. if (isFirst)
  362. break;
  363. throw eof;
  364. }
  365. if (line == PacketLineIn.END)
  366. break;
  367. if (!line.startsWith("want ") || line.length() < 45)
  368. throw new PackProtocolException(MessageFormat.format(JGitText.get().expectedGot, "want", line));
  369. if (isFirst && line.length() > 45) {
  370. String opt = line.substring(45);
  371. if (opt.startsWith(" "))
  372. opt = opt.substring(1);
  373. for (String c : opt.split(" "))
  374. options.add(c);
  375. line = line.substring(0, 45);
  376. }
  377. wantIds.add(ObjectId.fromString(line.substring(5)));
  378. isFirst = false;
  379. }
  380. }
  381. private boolean negotiate() throws IOException {
  382. okToGiveUp = Boolean.FALSE;
  383. ObjectId last = ObjectId.zeroId();
  384. List<ObjectId> peerHas = new ArrayList<ObjectId>(64);
  385. for (;;) {
  386. String line;
  387. try {
  388. line = pckIn.readString();
  389. } catch (EOFException eof) {
  390. throw eof;
  391. }
  392. if (line == PacketLineIn.END) {
  393. last = processHaveLines(peerHas, last);
  394. if (commonBase.isEmpty() || multiAck != MultiAck.OFF)
  395. pckOut.writeString("NAK\n");
  396. if (!biDirectionalPipe)
  397. return false;
  398. pckOut.flush();
  399. } else if (line.startsWith("have ") && line.length() == 45) {
  400. peerHas.add(ObjectId.fromString(line.substring(5)));
  401. } else if (line.equals("done")) {
  402. last = processHaveLines(peerHas, last);
  403. if (commonBase.isEmpty())
  404. pckOut.writeString("NAK\n");
  405. else if (multiAck != MultiAck.OFF)
  406. pckOut.writeString("ACK " + last.name() + "\n");
  407. return true;
  408. } else {
  409. throw new PackProtocolException(MessageFormat.format(JGitText.get().expectedGot, "have", line));
  410. }
  411. }
  412. }
  413. private ObjectId processHaveLines(List<ObjectId> peerHas, ObjectId last)
  414. throws IOException {
  415. if (peerHas.isEmpty())
  416. return last;
  417. List<ObjectId> toParse = peerHas;
  418. HashSet<ObjectId> peerHasSet = null;
  419. boolean needMissing = false;
  420. if (wantAll.isEmpty() && !wantIds.isEmpty()) {
  421. // We have not yet parsed the want list. Parse it now.
  422. peerHasSet = new HashSet<ObjectId>(peerHas);
  423. int cnt = wantIds.size() + peerHasSet.size();
  424. toParse = new ArrayList<ObjectId>(cnt);
  425. toParse.addAll(wantIds);
  426. toParse.addAll(peerHasSet);
  427. needMissing = true;
  428. }
  429. AsyncRevObjectQueue q = walk.parseAny(toParse, needMissing);
  430. try {
  431. for (;;) {
  432. RevObject obj;
  433. try {
  434. obj = q.next();
  435. } catch (MissingObjectException notFound) {
  436. if (wantIds.contains(notFound.getObjectId())) {
  437. throw new PackProtocolException(
  438. MessageFormat.format(JGitText.get().notValid,
  439. notFound.getMessage()), notFound);
  440. }
  441. continue;
  442. }
  443. if (obj == null)
  444. break;
  445. // If the object is still found in wantIds, the want
  446. // list wasn't parsed earlier, and was done in this batch.
  447. //
  448. if (wantIds.remove(obj)) {
  449. if (!advertised.contains(obj)) {
  450. throw new PackProtocolException(MessageFormat.format(
  451. JGitText.get().notValid, obj.name()));
  452. }
  453. if (!obj.has(WANT)) {
  454. obj.add(WANT);
  455. wantAll.add(obj);
  456. }
  457. if (!(obj instanceof RevCommit))
  458. obj.add(SATISFIED);
  459. if (obj instanceof RevTag) {
  460. RevObject target = walk.peel(obj);
  461. if (target instanceof RevCommit) {
  462. if (!target.has(WANT)) {
  463. target.add(WANT);
  464. wantAll.add(target);
  465. }
  466. }
  467. }
  468. if (!peerHasSet.contains(obj))
  469. continue;
  470. }
  471. last = obj;
  472. if (obj instanceof RevCommit) {
  473. RevCommit c = (RevCommit) obj;
  474. if (oldestTime == 0 || c.getCommitTime() < oldestTime)
  475. oldestTime = c.getCommitTime();
  476. }
  477. if (obj.has(PEER_HAS))
  478. continue;
  479. obj.add(PEER_HAS);
  480. if (obj instanceof RevCommit)
  481. ((RevCommit) obj).carry(PEER_HAS);
  482. addCommonBase(obj);
  483. // If both sides have the same object; let the client know.
  484. //
  485. switch (multiAck) {
  486. case OFF:
  487. if (commonBase.size() == 1)
  488. pckOut.writeString("ACK " + obj.name() + "\n");
  489. break;
  490. case CONTINUE:
  491. pckOut.writeString("ACK " + obj.name() + " continue\n");
  492. break;
  493. case DETAILED:
  494. pckOut.writeString("ACK " + obj.name() + " common\n");
  495. break;
  496. }
  497. }
  498. } finally {
  499. q.release();
  500. }
  501. // If we don't have one of the objects but we're also willing to
  502. // create a pack at this point, let the client know so it stops
  503. // telling us about its history.
  504. //
  505. for (int i = peerHas.size() - 1; i >= 0; i--) {
  506. ObjectId id = peerHas.get(i);
  507. if (walk.lookupOrNull(id) == null) {
  508. if (okToGiveUp()) {
  509. switch (multiAck) {
  510. case OFF:
  511. break;
  512. case CONTINUE:
  513. pckOut.writeString("ACK " + id.name() + " continue\n");
  514. break;
  515. case DETAILED:
  516. pckOut.writeString("ACK " + id.name() + " ready\n");
  517. break;
  518. }
  519. }
  520. break;
  521. }
  522. }
  523. peerHas.clear();
  524. return last;
  525. }
  526. private void addCommonBase(final RevObject o) {
  527. if (!o.has(COMMON)) {
  528. o.add(COMMON);
  529. commonBase.add(o);
  530. okToGiveUp = null;
  531. }
  532. }
  533. private boolean okToGiveUp() throws PackProtocolException {
  534. if (okToGiveUp == null)
  535. okToGiveUp = Boolean.valueOf(okToGiveUpImp());
  536. return okToGiveUp.booleanValue();
  537. }
  538. private boolean okToGiveUpImp() throws PackProtocolException {
  539. if (commonBase.isEmpty())
  540. return false;
  541. try {
  542. for (RevObject obj : wantAll) {
  543. if (!wantSatisfied(obj))
  544. return false;
  545. }
  546. return true;
  547. } catch (IOException e) {
  548. throw new PackProtocolException(JGitText.get().internalRevisionError, e);
  549. }
  550. }
  551. private boolean wantSatisfied(final RevObject want) throws IOException {
  552. if (want.has(SATISFIED))
  553. return true;
  554. walk.resetRetain(SAVE);
  555. walk.markStart((RevCommit) want);
  556. if (oldestTime != 0)
  557. walk.setRevFilter(CommitTimeRevFilter.after(oldestTime * 1000L));
  558. for (;;) {
  559. final RevCommit c = walk.next();
  560. if (c == null)
  561. break;
  562. if (c.has(PEER_HAS)) {
  563. addCommonBase(c);
  564. want.add(SATISFIED);
  565. return true;
  566. }
  567. }
  568. return false;
  569. }
  570. private void sendPack() throws IOException {
  571. final boolean sideband = options.contains(OPTION_SIDE_BAND)
  572. || options.contains(OPTION_SIDE_BAND_64K);
  573. ProgressMonitor pm = NullProgressMonitor.INSTANCE;
  574. OutputStream packOut = rawOut;
  575. SideBandOutputStream msgOut = null;
  576. if (sideband) {
  577. int bufsz = SideBandOutputStream.SMALL_BUF;
  578. if (options.contains(OPTION_SIDE_BAND_64K))
  579. bufsz = SideBandOutputStream.MAX_BUF;
  580. packOut = new SideBandOutputStream(SideBandOutputStream.CH_DATA,
  581. bufsz, rawOut);
  582. if (!options.contains(OPTION_NO_PROGRESS)) {
  583. msgOut = new SideBandOutputStream(
  584. SideBandOutputStream.CH_PROGRESS, bufsz, rawOut);
  585. pm = new SideBandProgressMonitor(msgOut);
  586. }
  587. }
  588. PackConfig cfg = packConfig;
  589. if (cfg == null)
  590. cfg = new PackConfig(db);
  591. final PackWriter pw = new PackWriter(cfg, walk.getObjectReader());
  592. try {
  593. pw.setUseCachedPacks(true);
  594. pw.setReuseDeltaCommits(true);
  595. pw.setDeltaBaseAsOffset(options.contains(OPTION_OFS_DELTA));
  596. pw.setThin(options.contains(OPTION_THIN_PACK));
  597. pw.setReuseValidatingObjects(false);
  598. if (commonBase.isEmpty()) {
  599. Set<ObjectId> tagTargets = new HashSet<ObjectId>();
  600. for (Ref ref : refs.values()) {
  601. if (ref.getPeeledObjectId() != null)
  602. tagTargets.add(ref.getPeeledObjectId());
  603. else if (ref.getObjectId() == null)
  604. continue;
  605. else if (ref.getName().startsWith(Constants.R_HEADS))
  606. tagTargets.add(ref.getObjectId());
  607. }
  608. pw.setTagTargets(tagTargets);
  609. }
  610. RevWalk rw = walk;
  611. if (wantAll.isEmpty()) {
  612. pw.preparePack(pm, wantIds, commonBase);
  613. } else {
  614. walk.reset();
  615. ObjectWalk ow = walk.toObjectWalkWithSameObjects();
  616. pw.preparePack(pm, ow, wantAll, commonBase);
  617. rw = ow;
  618. }
  619. if (options.contains(OPTION_INCLUDE_TAG)) {
  620. for (Ref ref : refs.values()) {
  621. ObjectId objectId = ref.getObjectId();
  622. // If the object was already requested, skip it.
  623. if (wantAll.isEmpty()) {
  624. if (wantIds.contains(objectId))
  625. continue;
  626. } else {
  627. RevObject obj = rw.lookupOrNull(objectId);
  628. if (obj != null && obj.has(WANT))
  629. continue;
  630. }
  631. if (!ref.isPeeled())
  632. ref = db.peel(ref);
  633. ObjectId peeledId = ref.getPeeledObjectId();
  634. if (peeledId == null)
  635. continue;
  636. objectId = ref.getObjectId();
  637. if (pw.willInclude(peeledId) && !pw.willInclude(objectId))
  638. pw.addObject(rw.parseAny(objectId));
  639. }
  640. }
  641. pw.writePack(pm, NullProgressMonitor.INSTANCE, packOut);
  642. statistics = pw.getStatistics();
  643. if (msgOut != null) {
  644. String msg = pw.getStatistics().getMessage() + '\n';
  645. msgOut.write(Constants.encode(msg));
  646. msgOut.flush();
  647. }
  648. } finally {
  649. pw.release();
  650. }
  651. if (sideband)
  652. pckOut.end();
  653. if (logger != null && statistics != null)
  654. logger.onPackStatistics(statistics);
  655. }
  656. }