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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117
  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.Collections;
  51. import java.util.HashSet;
  52. import java.util.List;
  53. import java.util.Map;
  54. import java.util.Set;
  55. import org.eclipse.jgit.JGitText;
  56. import org.eclipse.jgit.errors.CorruptObjectException;
  57. import org.eclipse.jgit.errors.IncorrectObjectTypeException;
  58. import org.eclipse.jgit.errors.MissingObjectException;
  59. import org.eclipse.jgit.errors.PackProtocolException;
  60. import org.eclipse.jgit.lib.Constants;
  61. import org.eclipse.jgit.lib.NullProgressMonitor;
  62. import org.eclipse.jgit.lib.ObjectId;
  63. import org.eclipse.jgit.lib.ProgressMonitor;
  64. import org.eclipse.jgit.lib.Ref;
  65. import org.eclipse.jgit.lib.Repository;
  66. import org.eclipse.jgit.revwalk.AsyncRevObjectQueue;
  67. import org.eclipse.jgit.revwalk.DepthWalk;
  68. import org.eclipse.jgit.revwalk.ObjectWalk;
  69. import org.eclipse.jgit.revwalk.RevCommit;
  70. import org.eclipse.jgit.revwalk.RevFlag;
  71. import org.eclipse.jgit.revwalk.RevFlagSet;
  72. import org.eclipse.jgit.revwalk.RevObject;
  73. import org.eclipse.jgit.revwalk.RevTag;
  74. import org.eclipse.jgit.revwalk.RevWalk;
  75. import org.eclipse.jgit.revwalk.filter.CommitTimeRevFilter;
  76. import org.eclipse.jgit.storage.pack.PackConfig;
  77. import org.eclipse.jgit.storage.pack.PackWriter;
  78. import org.eclipse.jgit.transport.BasePackFetchConnection.MultiAck;
  79. import org.eclipse.jgit.transport.RefAdvertiser.PacketLineOutRefAdvertiser;
  80. import org.eclipse.jgit.util.io.InterruptTimer;
  81. import org.eclipse.jgit.util.io.TimeoutInputStream;
  82. import org.eclipse.jgit.util.io.TimeoutOutputStream;
  83. /**
  84. * Implements the server side of a fetch connection, transmitting objects.
  85. */
  86. public class UploadPack {
  87. static final String OPTION_INCLUDE_TAG = BasePackFetchConnection.OPTION_INCLUDE_TAG;
  88. static final String OPTION_MULTI_ACK = BasePackFetchConnection.OPTION_MULTI_ACK;
  89. static final String OPTION_MULTI_ACK_DETAILED = BasePackFetchConnection.OPTION_MULTI_ACK_DETAILED;
  90. static final String OPTION_THIN_PACK = BasePackFetchConnection.OPTION_THIN_PACK;
  91. static final String OPTION_SIDE_BAND = BasePackFetchConnection.OPTION_SIDE_BAND;
  92. static final String OPTION_SIDE_BAND_64K = BasePackFetchConnection.OPTION_SIDE_BAND_64K;
  93. static final String OPTION_OFS_DELTA = BasePackFetchConnection.OPTION_OFS_DELTA;
  94. static final String OPTION_NO_PROGRESS = BasePackFetchConnection.OPTION_NO_PROGRESS;
  95. static final String OPTION_NO_DONE = BasePackFetchConnection.OPTION_NO_DONE;
  96. static final String OPTION_SHALLOW = BasePackFetchConnection.OPTION_SHALLOW;
  97. /** Policy the server uses to validate client requests */
  98. public static enum RequestPolicy {
  99. /** Client may only ask for objects the server advertised a reference for. */
  100. ADVERTISED,
  101. /** Client may ask for any commit reachable from a reference. */
  102. REACHABLE_COMMIT,
  103. /** Client may ask for any SHA-1 in the repository. */
  104. ANY;
  105. }
  106. /** Database we read the objects from. */
  107. private final Repository db;
  108. /** Revision traversal support over {@link #db}. */
  109. private final RevWalk walk;
  110. /** Configuration to pass into the PackWriter. */
  111. private PackConfig packConfig;
  112. /** Timeout in seconds to wait for client interaction. */
  113. private int timeout;
  114. /**
  115. * Is the client connection a bi-directional socket or pipe?
  116. * <p>
  117. * If true, this class assumes it can perform multiple read and write cycles
  118. * with the client over the input and output streams. This matches the
  119. * functionality available with a standard TCP/IP connection, or a local
  120. * operating system or in-memory pipe.
  121. * <p>
  122. * If false, this class runs in a read everything then output results mode,
  123. * making it suitable for single round-trip systems RPCs such as HTTP.
  124. */
  125. private boolean biDirectionalPipe = true;
  126. /** Timer to manage {@link #timeout}. */
  127. private InterruptTimer timer;
  128. private InputStream rawIn;
  129. private OutputStream rawOut;
  130. private PacketLineIn pckIn;
  131. private PacketLineOut pckOut;
  132. /** The refs we advertised as existing at the start of the connection. */
  133. private Map<String, Ref> refs;
  134. /** Filter used while advertising the refs to the client. */
  135. private RefFilter refFilter;
  136. /** Hook handling the various upload phases. */
  137. private PreUploadHook preUploadHook = PreUploadHook.NULL;
  138. /** Capabilities requested by the client. */
  139. private final Set<String> options = new HashSet<String>();
  140. /** Raw ObjectIds the client has asked for, before validating them. */
  141. private final Set<ObjectId> wantIds = new HashSet<ObjectId>();
  142. /** Objects the client wants to obtain. */
  143. private final Set<RevObject> wantAll = new HashSet<RevObject>();
  144. /** Objects on both sides, these don't have to be sent. */
  145. private final Set<RevObject> commonBase = new HashSet<RevObject>();
  146. /** Shallow commits the client already has. */
  147. private final Set<ObjectId> clientShallowCommits = new HashSet<ObjectId>();
  148. /** Shallow commits on the client which are now becoming unshallow */
  149. private final List<ObjectId> unshallowCommits = new ArrayList<ObjectId>();
  150. /** Desired depth from the client on a shallow request. */
  151. private int depth;
  152. /** Commit time of the oldest common commit, in seconds. */
  153. private int oldestTime;
  154. /** null if {@link #commonBase} should be examined again. */
  155. private Boolean okToGiveUp;
  156. private boolean sentReady;
  157. /** Objects we sent in our advertisement list, clients can ask for these. */
  158. private Set<ObjectId> advertised;
  159. /** Marked on objects the client has asked us to give them. */
  160. private final RevFlag WANT;
  161. /** Marked on objects both we and the client have. */
  162. private final RevFlag PEER_HAS;
  163. /** Marked on objects in {@link #commonBase}. */
  164. private final RevFlag COMMON;
  165. /** Objects where we found a path from the want list to a common base. */
  166. private final RevFlag SATISFIED;
  167. private final RevFlagSet SAVE;
  168. private RequestPolicy requestPolicy = RequestPolicy.ADVERTISED;
  169. private MultiAck multiAck = MultiAck.OFF;
  170. private boolean noDone;
  171. private PackWriter.Statistics statistics;
  172. private UploadPackLogger logger;
  173. /**
  174. * Create a new pack upload for an open repository.
  175. *
  176. * @param copyFrom
  177. * the source repository.
  178. */
  179. public UploadPack(final Repository copyFrom) {
  180. db = copyFrom;
  181. walk = new RevWalk(db);
  182. walk.setRetainBody(false);
  183. WANT = walk.newFlag("WANT");
  184. PEER_HAS = walk.newFlag("PEER_HAS");
  185. COMMON = walk.newFlag("COMMON");
  186. SATISFIED = walk.newFlag("SATISFIED");
  187. walk.carry(PEER_HAS);
  188. SAVE = new RevFlagSet();
  189. SAVE.add(WANT);
  190. SAVE.add(PEER_HAS);
  191. SAVE.add(COMMON);
  192. SAVE.add(SATISFIED);
  193. refFilter = RefFilter.DEFAULT;
  194. }
  195. /** @return the repository this upload is reading from. */
  196. public final Repository getRepository() {
  197. return db;
  198. }
  199. /** @return the RevWalk instance used by this connection. */
  200. public final RevWalk getRevWalk() {
  201. return walk;
  202. }
  203. /** @return all refs which were advertised to the client. */
  204. public final Map<String, Ref> getAdvertisedRefs() {
  205. if (refs == null)
  206. setAdvertisedRefs(db.getAllRefs());
  207. return refs;
  208. }
  209. /**
  210. * @param allRefs
  211. * explicit set of references to claim as advertised by this
  212. * UploadPack instance. This overrides any references that
  213. * may exist in the source repository. The map is passed
  214. * to the configured {@link #getRefFilter()}.
  215. */
  216. public void setAdvertisedRefs(Map<String, Ref> allRefs) {
  217. refs = refFilter.filter(allRefs);
  218. }
  219. /** @return timeout (in seconds) before aborting an IO operation. */
  220. public int getTimeout() {
  221. return timeout;
  222. }
  223. /**
  224. * Set the timeout before willing to abort an IO call.
  225. *
  226. * @param seconds
  227. * number of seconds to wait (with no data transfer occurring)
  228. * before aborting an IO read or write operation with the
  229. * connected client.
  230. */
  231. public void setTimeout(final int seconds) {
  232. timeout = seconds;
  233. }
  234. /**
  235. * @return true if this class expects a bi-directional pipe opened between
  236. * the client and itself. The default is true.
  237. */
  238. public boolean isBiDirectionalPipe() {
  239. return biDirectionalPipe;
  240. }
  241. /**
  242. * @param twoWay
  243. * if true, this class will assume the socket is a fully
  244. * bidirectional pipe between the two peers and takes advantage
  245. * of that by first transmitting the known refs, then waiting to
  246. * read commands. If false, this class assumes it must read the
  247. * commands before writing output and does not perform the
  248. * initial advertising.
  249. */
  250. public void setBiDirectionalPipe(final boolean twoWay) {
  251. biDirectionalPipe = twoWay;
  252. if (!biDirectionalPipe && requestPolicy == RequestPolicy.ADVERTISED)
  253. requestPolicy = RequestPolicy.REACHABLE_COMMIT;
  254. }
  255. /** @return policy used by the service to validate client requests. */
  256. public RequestPolicy getRequestPolicy() {
  257. return requestPolicy;
  258. }
  259. /**
  260. * @param policy
  261. * the policy used to enforce validation of a client's want list.
  262. * By default the policy is {@link RequestPolicy#ADVERTISED},
  263. * which is the Git default requiring clients to only ask for an
  264. * object that a reference directly points to. This may be relaxed
  265. * to {@link RequestPolicy#REACHABLE_COMMIT} when callers
  266. * have {@link #setBiDirectionalPipe(boolean)} set to false.
  267. */
  268. public void setRequestPolicy(RequestPolicy policy) {
  269. requestPolicy = policy != null ? policy : RequestPolicy.ADVERTISED;
  270. }
  271. /** @return the filter used while advertising the refs to the client */
  272. public RefFilter getRefFilter() {
  273. return refFilter;
  274. }
  275. /**
  276. * Set the filter used while advertising the refs to the client.
  277. * <p>
  278. * Only refs allowed by this filter will be sent to the client. This can
  279. * be used by a server to restrict the list of references the client can
  280. * obtain through clone or fetch, effectively limiting the access to only
  281. * certain refs.
  282. *
  283. * @param refFilter
  284. * the filter; may be null to show all refs.
  285. */
  286. public void setRefFilter(final RefFilter refFilter) {
  287. this.refFilter = refFilter != null ? refFilter : RefFilter.DEFAULT;
  288. }
  289. /** @return the configured upload hook. */
  290. public PreUploadHook getPreUploadHook() {
  291. return preUploadHook;
  292. }
  293. /**
  294. * Set the hook that controls how this instance will behave.
  295. *
  296. * @param hook
  297. * the hook; if null no special actions are taken.
  298. */
  299. public void setPreUploadHook(PreUploadHook hook) {
  300. preUploadHook = hook != null ? hook : PreUploadHook.NULL;
  301. }
  302. /**
  303. * Set the configuration used by the pack generator.
  304. *
  305. * @param pc
  306. * configuration controlling packing parameters. If null the
  307. * source repository's settings will be used.
  308. */
  309. public void setPackConfig(PackConfig pc) {
  310. this.packConfig = pc;
  311. }
  312. /**
  313. * Set the logger.
  314. *
  315. * @param logger
  316. * the logger instance. If null, no logging occurs.
  317. */
  318. public void setLogger(UploadPackLogger logger) {
  319. this.logger = logger;
  320. }
  321. /**
  322. * Execute the upload task on the socket.
  323. *
  324. * @param input
  325. * raw input to read client commands from. Caller must ensure the
  326. * input is buffered, otherwise read performance may suffer.
  327. * @param output
  328. * response back to the Git network client, to write the pack
  329. * data onto. Caller must ensure the output is buffered,
  330. * otherwise write performance may suffer.
  331. * @param messages
  332. * secondary "notice" channel to send additional messages out
  333. * through. When run over SSH this should be tied back to the
  334. * standard error channel of the command execution. For most
  335. * other network connections this should be null.
  336. * @throws IOException
  337. */
  338. public void upload(final InputStream input, final OutputStream output,
  339. final OutputStream messages) throws IOException {
  340. try {
  341. rawIn = input;
  342. rawOut = output;
  343. if (timeout > 0) {
  344. final Thread caller = Thread.currentThread();
  345. timer = new InterruptTimer(caller.getName() + "-Timer");
  346. TimeoutInputStream i = new TimeoutInputStream(rawIn, timer);
  347. TimeoutOutputStream o = new TimeoutOutputStream(rawOut, timer);
  348. i.setTimeout(timeout * 1000);
  349. o.setTimeout(timeout * 1000);
  350. rawIn = i;
  351. rawOut = o;
  352. }
  353. pckIn = new PacketLineIn(rawIn);
  354. pckOut = new PacketLineOut(rawOut);
  355. service();
  356. } finally {
  357. walk.release();
  358. if (timer != null) {
  359. try {
  360. timer.terminate();
  361. } finally {
  362. timer = null;
  363. }
  364. }
  365. }
  366. }
  367. /**
  368. * Get the PackWriter's statistics if a pack was sent to the client.
  369. *
  370. * @return statistics about pack output, if a pack was sent. Null if no pack
  371. * was sent, such as during the negotation phase of a smart HTTP
  372. * connection, or if the client was already up-to-date.
  373. */
  374. public PackWriter.Statistics getPackStatistics() {
  375. return statistics;
  376. }
  377. private void service() throws IOException {
  378. if (biDirectionalPipe)
  379. sendAdvertisedRefs(new PacketLineOutRefAdvertiser(pckOut));
  380. else if (requestPolicy == RequestPolicy.ANY)
  381. advertised = Collections.emptySet();
  382. else {
  383. advertised = new HashSet<ObjectId>();
  384. for (Ref ref : getAdvertisedRefs().values()) {
  385. if (ref.getObjectId() != null)
  386. advertised.add(ref.getObjectId());
  387. }
  388. }
  389. boolean sendPack;
  390. try {
  391. recvWants();
  392. if (wantIds.isEmpty()) {
  393. preUploadHook.onBeginNegotiateRound(this, wantIds, 0);
  394. preUploadHook.onEndNegotiateRound(this, wantIds, 0, 0, false);
  395. return;
  396. }
  397. if (options.contains(OPTION_MULTI_ACK_DETAILED)) {
  398. multiAck = MultiAck.DETAILED;
  399. noDone = options.contains(OPTION_NO_DONE);
  400. } else if (options.contains(OPTION_MULTI_ACK))
  401. multiAck = MultiAck.CONTINUE;
  402. else
  403. multiAck = MultiAck.OFF;
  404. if (depth != 0)
  405. processShallow();
  406. sendPack = negotiate();
  407. } catch (PackProtocolException err) {
  408. reportErrorDuringNegotiate(err.getMessage());
  409. throw err;
  410. } catch (UploadPackMayNotContinueException err) {
  411. if (!err.isOutput() && err.getMessage() != null) {
  412. try {
  413. pckOut.writeString("ERR " + err.getMessage() + "\n");
  414. err.setOutput();
  415. } catch (Throwable err2) {
  416. // Ignore this secondary failure (and not mark output).
  417. }
  418. }
  419. throw err;
  420. } catch (IOException err) {
  421. reportErrorDuringNegotiate(JGitText.get().internalServerError);
  422. throw err;
  423. } catch (RuntimeException err) {
  424. reportErrorDuringNegotiate(JGitText.get().internalServerError);
  425. throw err;
  426. } catch (Error err) {
  427. reportErrorDuringNegotiate(JGitText.get().internalServerError);
  428. throw err;
  429. }
  430. if (sendPack)
  431. sendPack();
  432. }
  433. private void reportErrorDuringNegotiate(String msg) {
  434. try {
  435. pckOut.writeString("ERR " + msg + "\n");
  436. } catch (Throwable err) {
  437. // Ignore this secondary failure.
  438. }
  439. }
  440. private void processShallow() throws IOException {
  441. DepthWalk.RevWalk depthWalk =
  442. new DepthWalk.RevWalk(walk.getObjectReader(), depth);
  443. // Find all the commits which will be shallow
  444. for (ObjectId o : wantIds) {
  445. try {
  446. depthWalk.markRoot(depthWalk.parseCommit(o));
  447. } catch (IncorrectObjectTypeException notCommit) {
  448. // Ignore non-commits in this loop.
  449. }
  450. }
  451. RevCommit o;
  452. while ((o = depthWalk.next()) != null) {
  453. DepthWalk.Commit c = (DepthWalk.Commit) o;
  454. // Commits at the boundary which aren't already shallow in
  455. // the client need to be marked as such
  456. if (c.getDepth() == depth && !clientShallowCommits.contains(c))
  457. pckOut.writeString("shallow " + o.name());
  458. // Commits not on the boundary which are shallow in the client
  459. // need to become unshallowed
  460. if (c.getDepth() < depth && clientShallowCommits.contains(c)) {
  461. unshallowCommits.add(c.copy());
  462. pckOut.writeString("unshallow " + c.name());
  463. }
  464. }
  465. pckOut.end();
  466. }
  467. /**
  468. * Generate an advertisement of available refs and capabilities.
  469. *
  470. * @param adv
  471. * the advertisement formatter.
  472. * @throws IOException
  473. * the formatter failed to write an advertisement.
  474. * @throws UploadPackMayNotContinueException
  475. * the hook denied advertisement.
  476. */
  477. public void sendAdvertisedRefs(final RefAdvertiser adv) throws IOException,
  478. UploadPackMayNotContinueException {
  479. try {
  480. preUploadHook.onPreAdvertiseRefs(this);
  481. } catch (UploadPackMayNotContinueException fail) {
  482. if (fail.getMessage() != null) {
  483. adv.writeOne("ERR " + fail.getMessage());
  484. fail.setOutput();
  485. }
  486. throw fail;
  487. }
  488. adv.init(db);
  489. adv.advertiseCapability(OPTION_INCLUDE_TAG);
  490. adv.advertiseCapability(OPTION_MULTI_ACK_DETAILED);
  491. adv.advertiseCapability(OPTION_MULTI_ACK);
  492. adv.advertiseCapability(OPTION_OFS_DELTA);
  493. adv.advertiseCapability(OPTION_SIDE_BAND);
  494. adv.advertiseCapability(OPTION_SIDE_BAND_64K);
  495. adv.advertiseCapability(OPTION_THIN_PACK);
  496. adv.advertiseCapability(OPTION_NO_PROGRESS);
  497. adv.advertiseCapability(OPTION_SHALLOW);
  498. if (!biDirectionalPipe)
  499. adv.advertiseCapability(OPTION_NO_DONE);
  500. adv.setDerefTags(true);
  501. advertised = adv.send(getAdvertisedRefs());
  502. adv.end();
  503. }
  504. private void recvWants() throws IOException {
  505. boolean isFirst = true;
  506. for (;;) {
  507. String line;
  508. try {
  509. line = pckIn.readString();
  510. } catch (EOFException eof) {
  511. if (isFirst)
  512. break;
  513. throw eof;
  514. }
  515. if (line == PacketLineIn.END)
  516. break;
  517. if (line.startsWith("deepen ")) {
  518. depth = Integer.parseInt(line.substring(7));
  519. continue;
  520. }
  521. if (line.startsWith("shallow ")) {
  522. clientShallowCommits.add(ObjectId.fromString(line.substring(8)));
  523. continue;
  524. }
  525. if (!line.startsWith("want ") || line.length() < 45)
  526. throw new PackProtocolException(MessageFormat.format(JGitText.get().expectedGot, "want", line));
  527. if (isFirst && line.length() > 45) {
  528. String opt = line.substring(45);
  529. if (opt.startsWith(" "))
  530. opt = opt.substring(1);
  531. for (String c : opt.split(" "))
  532. options.add(c);
  533. line = line.substring(0, 45);
  534. }
  535. wantIds.add(ObjectId.fromString(line.substring(5)));
  536. isFirst = false;
  537. }
  538. }
  539. private boolean negotiate() throws IOException {
  540. okToGiveUp = Boolean.FALSE;
  541. ObjectId last = ObjectId.zeroId();
  542. List<ObjectId> peerHas = new ArrayList<ObjectId>(64);
  543. for (;;) {
  544. String line;
  545. try {
  546. line = pckIn.readString();
  547. } catch (EOFException eof) {
  548. // EOF on stateless RPC (aka smart HTTP) and non-shallow request
  549. // means the client asked for the updated shallow/unshallow data,
  550. // disconnected, and will try another request with actual want/have.
  551. // Don't report the EOF here, its a bug in the protocol that the client
  552. // just disconnects without sending an END.
  553. if (!biDirectionalPipe && depth > 0)
  554. return false;
  555. throw eof;
  556. }
  557. if (line == PacketLineIn.END) {
  558. last = processHaveLines(peerHas, last);
  559. if (commonBase.isEmpty() || multiAck != MultiAck.OFF)
  560. pckOut.writeString("NAK\n");
  561. if (noDone && sentReady) {
  562. pckOut.writeString("ACK " + last.name() + "\n");
  563. return true;
  564. }
  565. if (!biDirectionalPipe)
  566. return false;
  567. pckOut.flush();
  568. } else if (line.startsWith("have ") && line.length() == 45) {
  569. peerHas.add(ObjectId.fromString(line.substring(5)));
  570. } else if (line.equals("done")) {
  571. last = processHaveLines(peerHas, last);
  572. if (commonBase.isEmpty())
  573. pckOut.writeString("NAK\n");
  574. else if (multiAck != MultiAck.OFF)
  575. pckOut.writeString("ACK " + last.name() + "\n");
  576. return true;
  577. } else {
  578. throw new PackProtocolException(MessageFormat.format(JGitText.get().expectedGot, "have", line));
  579. }
  580. }
  581. }
  582. private ObjectId processHaveLines(List<ObjectId> peerHas, ObjectId last)
  583. throws IOException {
  584. preUploadHook.onBeginNegotiateRound(this, wantIds, peerHas.size());
  585. if (peerHas.isEmpty())
  586. return last;
  587. List<ObjectId> toParse = peerHas;
  588. HashSet<ObjectId> peerHasSet = null;
  589. boolean needMissing = false;
  590. sentReady = false;
  591. if (wantAll.isEmpty() && !wantIds.isEmpty()) {
  592. // We have not yet parsed the want list. Parse it now.
  593. peerHasSet = new HashSet<ObjectId>(peerHas);
  594. int cnt = wantIds.size() + peerHasSet.size();
  595. toParse = new ArrayList<ObjectId>(cnt);
  596. toParse.addAll(wantIds);
  597. toParse.addAll(peerHasSet);
  598. needMissing = true;
  599. }
  600. Set<RevObject> notAdvertisedWants = null;
  601. int haveCnt = 0;
  602. AsyncRevObjectQueue q = walk.parseAny(toParse, needMissing);
  603. try {
  604. for (;;) {
  605. RevObject obj;
  606. try {
  607. obj = q.next();
  608. } catch (MissingObjectException notFound) {
  609. ObjectId id = notFound.getObjectId();
  610. if (wantIds.contains(id)) {
  611. String msg = MessageFormat.format(
  612. JGitText.get().wantNotValid, id.name());
  613. throw new PackProtocolException(msg, notFound);
  614. }
  615. continue;
  616. }
  617. if (obj == null)
  618. break;
  619. // If the object is still found in wantIds, the want
  620. // list wasn't parsed earlier, and was done in this batch.
  621. //
  622. if (wantIds.remove(obj)) {
  623. if (!advertised.contains(obj) && requestPolicy != RequestPolicy.ANY) {
  624. if (notAdvertisedWants == null)
  625. notAdvertisedWants = new HashSet<RevObject>();
  626. notAdvertisedWants.add(obj);
  627. }
  628. if (!obj.has(WANT)) {
  629. obj.add(WANT);
  630. wantAll.add(obj);
  631. }
  632. if (!(obj instanceof RevCommit))
  633. obj.add(SATISFIED);
  634. if (obj instanceof RevTag) {
  635. RevObject target = walk.peel(obj);
  636. if (target instanceof RevCommit) {
  637. if (!target.has(WANT)) {
  638. target.add(WANT);
  639. wantAll.add(target);
  640. }
  641. }
  642. }
  643. if (!peerHasSet.contains(obj))
  644. continue;
  645. }
  646. last = obj;
  647. haveCnt++;
  648. if (obj instanceof RevCommit) {
  649. RevCommit c = (RevCommit) obj;
  650. if (oldestTime == 0 || c.getCommitTime() < oldestTime)
  651. oldestTime = c.getCommitTime();
  652. }
  653. if (obj.has(PEER_HAS))
  654. continue;
  655. obj.add(PEER_HAS);
  656. if (obj instanceof RevCommit)
  657. ((RevCommit) obj).carry(PEER_HAS);
  658. addCommonBase(obj);
  659. // If both sides have the same object; let the client know.
  660. //
  661. switch (multiAck) {
  662. case OFF:
  663. if (commonBase.size() == 1)
  664. pckOut.writeString("ACK " + obj.name() + "\n");
  665. break;
  666. case CONTINUE:
  667. pckOut.writeString("ACK " + obj.name() + " continue\n");
  668. break;
  669. case DETAILED:
  670. pckOut.writeString("ACK " + obj.name() + " common\n");
  671. break;
  672. }
  673. }
  674. } finally {
  675. q.release();
  676. }
  677. // If the client asked for non advertised object, check our policy.
  678. if (notAdvertisedWants != null && !notAdvertisedWants.isEmpty()) {
  679. switch (requestPolicy) {
  680. case ADVERTISED:
  681. default:
  682. throw new PackProtocolException(MessageFormat.format(
  683. JGitText.get().wantNotValid,
  684. notAdvertisedWants.iterator().next().name()));
  685. case REACHABLE_COMMIT:
  686. checkNotAdvertisedWants(notAdvertisedWants);
  687. break;
  688. case ANY:
  689. // Allow whatever was asked for.
  690. break;
  691. }
  692. }
  693. int missCnt = peerHas.size() - haveCnt;
  694. // If we don't have one of the objects but we're also willing to
  695. // create a pack at this point, let the client know so it stops
  696. // telling us about its history.
  697. //
  698. boolean didOkToGiveUp = false;
  699. if (0 < missCnt) {
  700. for (int i = peerHas.size() - 1; i >= 0; i--) {
  701. ObjectId id = peerHas.get(i);
  702. if (walk.lookupOrNull(id) == null) {
  703. didOkToGiveUp = true;
  704. if (okToGiveUp()) {
  705. switch (multiAck) {
  706. case OFF:
  707. break;
  708. case CONTINUE:
  709. pckOut.writeString("ACK " + id.name() + " continue\n");
  710. break;
  711. case DETAILED:
  712. pckOut.writeString("ACK " + id.name() + " ready\n");
  713. sentReady = true;
  714. break;
  715. }
  716. }
  717. break;
  718. }
  719. }
  720. }
  721. if (multiAck == MultiAck.DETAILED && !didOkToGiveUp && okToGiveUp()) {
  722. ObjectId id = peerHas.get(peerHas.size() - 1);
  723. sentReady = true;
  724. pckOut.writeString("ACK " + id.name() + " ready\n");
  725. sentReady = true;
  726. }
  727. preUploadHook.onEndNegotiateRound(this, wantAll, haveCnt, missCnt, sentReady);
  728. peerHas.clear();
  729. return last;
  730. }
  731. private void checkNotAdvertisedWants(Set<RevObject> notAdvertisedWants)
  732. throws MissingObjectException, IncorrectObjectTypeException, IOException {
  733. // Walk the requested commits back to the advertised commits.
  734. // If any commit exists, a branch was deleted or rewound and
  735. // the repository owner no longer exports that requested item.
  736. // If the requested commit is merged into an advertised branch
  737. // it will be marked UNINTERESTING and no commits return.
  738. for (RevObject o : notAdvertisedWants) {
  739. if (!(o instanceof RevCommit)) {
  740. throw new PackProtocolException(MessageFormat.format(
  741. JGitText.get().wantNotValid,
  742. notAdvertisedWants.iterator().next().name()));
  743. }
  744. walk.markStart((RevCommit) o);
  745. }
  746. for (ObjectId id : advertised) {
  747. try {
  748. walk.markUninteresting(walk.parseCommit(id));
  749. } catch (IncorrectObjectTypeException notCommit) {
  750. continue;
  751. }
  752. }
  753. RevCommit bad = walk.next();
  754. if (bad != null) {
  755. throw new PackProtocolException(MessageFormat.format(
  756. JGitText.get().wantNotValid,
  757. bad.name()));
  758. }
  759. walk.reset();
  760. }
  761. private void addCommonBase(final RevObject o) {
  762. if (!o.has(COMMON)) {
  763. o.add(COMMON);
  764. commonBase.add(o);
  765. okToGiveUp = null;
  766. }
  767. }
  768. private boolean okToGiveUp() throws PackProtocolException {
  769. if (okToGiveUp == null)
  770. okToGiveUp = Boolean.valueOf(okToGiveUpImp());
  771. return okToGiveUp.booleanValue();
  772. }
  773. private boolean okToGiveUpImp() throws PackProtocolException {
  774. if (commonBase.isEmpty())
  775. return false;
  776. try {
  777. for (RevObject obj : wantAll) {
  778. if (!wantSatisfied(obj))
  779. return false;
  780. }
  781. return true;
  782. } catch (IOException e) {
  783. throw new PackProtocolException(JGitText.get().internalRevisionError, e);
  784. }
  785. }
  786. private boolean wantSatisfied(final RevObject want) throws IOException {
  787. if (want.has(SATISFIED))
  788. return true;
  789. walk.resetRetain(SAVE);
  790. walk.markStart((RevCommit) want);
  791. if (oldestTime != 0)
  792. walk.setRevFilter(CommitTimeRevFilter.after(oldestTime * 1000L));
  793. for (;;) {
  794. final RevCommit c = walk.next();
  795. if (c == null)
  796. break;
  797. if (c.has(PEER_HAS)) {
  798. addCommonBase(c);
  799. want.add(SATISFIED);
  800. return true;
  801. }
  802. }
  803. return false;
  804. }
  805. private void sendPack() throws IOException {
  806. final boolean sideband = options.contains(OPTION_SIDE_BAND)
  807. || options.contains(OPTION_SIDE_BAND_64K);
  808. if (!biDirectionalPipe) {
  809. // Ensure the request was fully consumed. Any remaining input must
  810. // be a protocol error. If we aren't at EOF the implementation is broken.
  811. int eof = rawIn.read();
  812. if (0 <= eof)
  813. throw new CorruptObjectException(MessageFormat.format(
  814. JGitText.get().expectedEOFReceived,
  815. "\\x" + Integer.toHexString(eof)));
  816. }
  817. if (sideband) {
  818. try {
  819. sendPack(true);
  820. } catch (UploadPackMayNotContinueException noPack) {
  821. // This was already reported on (below).
  822. throw noPack;
  823. } catch (IOException err) {
  824. if (reportInternalServerErrorOverSideband())
  825. throw new UploadPackInternalServerErrorException(err);
  826. else
  827. throw err;
  828. } catch (RuntimeException err) {
  829. if (reportInternalServerErrorOverSideband())
  830. throw new UploadPackInternalServerErrorException(err);
  831. else
  832. throw err;
  833. } catch (Error err) {
  834. if (reportInternalServerErrorOverSideband())
  835. throw new UploadPackInternalServerErrorException(err);
  836. else
  837. throw err;
  838. }
  839. } else {
  840. sendPack(false);
  841. }
  842. }
  843. private boolean reportInternalServerErrorOverSideband() {
  844. try {
  845. SideBandOutputStream err = new SideBandOutputStream(
  846. SideBandOutputStream.CH_ERROR,
  847. SideBandOutputStream.SMALL_BUF,
  848. rawOut);
  849. err.write(Constants.encode(JGitText.get().internalServerError));
  850. err.flush();
  851. return true;
  852. } catch (Throwable cannotReport) {
  853. // Ignore the reason. This is a secondary failure.
  854. return false;
  855. }
  856. }
  857. private void sendPack(final boolean sideband) throws IOException {
  858. ProgressMonitor pm = NullProgressMonitor.INSTANCE;
  859. OutputStream packOut = rawOut;
  860. SideBandOutputStream msgOut = null;
  861. if (sideband) {
  862. int bufsz = SideBandOutputStream.SMALL_BUF;
  863. if (options.contains(OPTION_SIDE_BAND_64K))
  864. bufsz = SideBandOutputStream.MAX_BUF;
  865. packOut = new SideBandOutputStream(SideBandOutputStream.CH_DATA,
  866. bufsz, rawOut);
  867. if (!options.contains(OPTION_NO_PROGRESS)) {
  868. msgOut = new SideBandOutputStream(
  869. SideBandOutputStream.CH_PROGRESS, bufsz, rawOut);
  870. pm = new SideBandProgressMonitor(msgOut);
  871. }
  872. }
  873. try {
  874. if (wantAll.isEmpty()) {
  875. preUploadHook.onSendPack(this, wantIds, commonBase);
  876. } else {
  877. preUploadHook.onSendPack(this, wantAll, commonBase);
  878. }
  879. } catch (UploadPackMayNotContinueException noPack) {
  880. if (sideband && noPack.getMessage() != null) {
  881. noPack.setOutput();
  882. SideBandOutputStream err = new SideBandOutputStream(
  883. SideBandOutputStream.CH_ERROR,
  884. SideBandOutputStream.SMALL_BUF, rawOut);
  885. err.write(Constants.encode(noPack.getMessage()));
  886. err.flush();
  887. }
  888. throw noPack;
  889. }
  890. PackConfig cfg = packConfig;
  891. if (cfg == null)
  892. cfg = new PackConfig(db);
  893. final PackWriter pw = new PackWriter(cfg, walk.getObjectReader());
  894. try {
  895. pw.setUseCachedPacks(true);
  896. pw.setReuseDeltaCommits(true);
  897. pw.setDeltaBaseAsOffset(options.contains(OPTION_OFS_DELTA));
  898. pw.setThin(options.contains(OPTION_THIN_PACK));
  899. pw.setReuseValidatingObjects(false);
  900. if (commonBase.isEmpty() && refs != null) {
  901. Set<ObjectId> tagTargets = new HashSet<ObjectId>();
  902. for (Ref ref : refs.values()) {
  903. if (ref.getPeeledObjectId() != null)
  904. tagTargets.add(ref.getPeeledObjectId());
  905. else if (ref.getObjectId() == null)
  906. continue;
  907. else if (ref.getName().startsWith(Constants.R_HEADS))
  908. tagTargets.add(ref.getObjectId());
  909. }
  910. pw.setTagTargets(tagTargets);
  911. }
  912. if (depth > 0)
  913. pw.setShallowPack(depth, unshallowCommits);
  914. RevWalk rw = walk;
  915. if (wantAll.isEmpty()) {
  916. pw.preparePack(pm, wantIds, commonBase);
  917. } else {
  918. walk.reset();
  919. ObjectWalk ow = walk.toObjectWalkWithSameObjects();
  920. pw.preparePack(pm, ow, wantAll, commonBase);
  921. rw = ow;
  922. }
  923. if (options.contains(OPTION_INCLUDE_TAG) && refs != null) {
  924. for (Ref ref : refs.values()) {
  925. ObjectId objectId = ref.getObjectId();
  926. // If the object was already requested, skip it.
  927. if (wantAll.isEmpty()) {
  928. if (wantIds.contains(objectId))
  929. continue;
  930. } else {
  931. RevObject obj = rw.lookupOrNull(objectId);
  932. if (obj != null && obj.has(WANT))
  933. continue;
  934. }
  935. if (!ref.isPeeled())
  936. ref = db.peel(ref);
  937. ObjectId peeledId = ref.getPeeledObjectId();
  938. if (peeledId == null)
  939. continue;
  940. objectId = ref.getObjectId();
  941. if (pw.willInclude(peeledId) && !pw.willInclude(objectId))
  942. pw.addObject(rw.parseAny(objectId));
  943. }
  944. }
  945. pw.writePack(pm, NullProgressMonitor.INSTANCE, packOut);
  946. statistics = pw.getStatistics();
  947. if (msgOut != null) {
  948. String msg = pw.getStatistics().getMessage() + '\n';
  949. msgOut.write(Constants.encode(msg));
  950. msgOut.flush();
  951. }
  952. } finally {
  953. pw.release();
  954. }
  955. if (sideband)
  956. pckOut.end();
  957. if (logger != null && statistics != null)
  958. logger.onPackStatistics(statistics);
  959. }
  960. }