Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

ReceivePack.java 32KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104
  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 static org.eclipse.jgit.transport.BasePackPushConnection.CAPABILITY_DELETE_REFS;
  45. import static org.eclipse.jgit.transport.BasePackPushConnection.CAPABILITY_OFS_DELTA;
  46. import static org.eclipse.jgit.transport.BasePackPushConnection.CAPABILITY_REPORT_STATUS;
  47. import static org.eclipse.jgit.transport.BasePackPushConnection.CAPABILITY_SIDE_BAND_64K;
  48. import static org.eclipse.jgit.transport.SideBandOutputStream.CH_DATA;
  49. import static org.eclipse.jgit.transport.SideBandOutputStream.CH_PROGRESS;
  50. import static org.eclipse.jgit.transport.SideBandOutputStream.MAX_BUF;
  51. import java.io.EOFException;
  52. import java.io.IOException;
  53. import java.io.InputStream;
  54. import java.io.OutputStream;
  55. import java.io.OutputStreamWriter;
  56. import java.io.Writer;
  57. import java.util.ArrayList;
  58. import java.util.Collections;
  59. import java.util.HashSet;
  60. import java.util.List;
  61. import java.util.Map;
  62. import java.util.Set;
  63. import org.eclipse.jgit.errors.MissingObjectException;
  64. import org.eclipse.jgit.errors.PackProtocolException;
  65. import org.eclipse.jgit.lib.Config;
  66. import org.eclipse.jgit.lib.Constants;
  67. import org.eclipse.jgit.lib.NullProgressMonitor;
  68. import org.eclipse.jgit.lib.ObjectId;
  69. import org.eclipse.jgit.lib.ObjectIdSubclassMap;
  70. import org.eclipse.jgit.lib.PackLock;
  71. import org.eclipse.jgit.lib.PersonIdent;
  72. import org.eclipse.jgit.lib.Ref;
  73. import org.eclipse.jgit.lib.RefUpdate;
  74. import org.eclipse.jgit.lib.Repository;
  75. import org.eclipse.jgit.lib.Config.SectionParser;
  76. import org.eclipse.jgit.revwalk.ObjectWalk;
  77. import org.eclipse.jgit.revwalk.RevBlob;
  78. import org.eclipse.jgit.revwalk.RevCommit;
  79. import org.eclipse.jgit.revwalk.RevFlag;
  80. import org.eclipse.jgit.revwalk.RevObject;
  81. import org.eclipse.jgit.revwalk.RevTag;
  82. import org.eclipse.jgit.revwalk.RevTree;
  83. import org.eclipse.jgit.revwalk.RevWalk;
  84. import org.eclipse.jgit.transport.ReceiveCommand.Result;
  85. import org.eclipse.jgit.transport.RefAdvertiser.PacketLineOutRefAdvertiser;
  86. import org.eclipse.jgit.util.io.InterruptTimer;
  87. import org.eclipse.jgit.util.io.TimeoutInputStream;
  88. import org.eclipse.jgit.util.io.TimeoutOutputStream;
  89. /**
  90. * Implements the server side of a push connection, receiving objects.
  91. */
  92. public class ReceivePack {
  93. /** Database we write the stored objects into. */
  94. private final Repository db;
  95. /** Revision traversal support over {@link #db}. */
  96. private final RevWalk walk;
  97. /**
  98. * Is the client connection a bi-directional socket or pipe?
  99. * <p>
  100. * If true, this class assumes it can perform multiple read and write cycles
  101. * with the client over the input and output streams. This matches the
  102. * functionality available with a standard TCP/IP connection, or a local
  103. * operating system or in-memory pipe.
  104. * <p>
  105. * If false, this class runs in a read everything then output results mode,
  106. * making it suitable for single round-trip systems RPCs such as HTTP.
  107. */
  108. private boolean biDirectionalPipe = true;
  109. /** Should an incoming transfer validate objects? */
  110. private boolean checkReceivedObjects;
  111. /** Should an incoming transfer permit create requests? */
  112. private boolean allowCreates;
  113. /** Should an incoming transfer permit delete requests? */
  114. private boolean allowDeletes;
  115. /** Should an incoming transfer permit non-fast-forward requests? */
  116. private boolean allowNonFastForwards;
  117. private boolean allowOfsDelta;
  118. /** Identity to record action as within the reflog. */
  119. private PersonIdent refLogIdent;
  120. /** Filter used while advertising the refs to the client. */
  121. private RefFilter refFilter;
  122. /** Hook to validate the update commands before execution. */
  123. private PreReceiveHook preReceive;
  124. /** Hook to report on the commands after execution. */
  125. private PostReceiveHook postReceive;
  126. /** Timeout in seconds to wait for client interaction. */
  127. private int timeout;
  128. /** Timer to manage {@link #timeout}. */
  129. private InterruptTimer timer;
  130. private TimeoutInputStream timeoutIn;
  131. private InputStream rawIn;
  132. private OutputStream rawOut;
  133. private PacketLineIn pckIn;
  134. private PacketLineOut pckOut;
  135. private Writer msgs;
  136. private IndexPack ip;
  137. /** The refs we advertised as existing at the start of the connection. */
  138. private Map<String, Ref> refs;
  139. /** Capabilities requested by the client. */
  140. private Set<String> enabledCapablities;
  141. /** Commands to execute, as received by the client. */
  142. private List<ReceiveCommand> commands;
  143. /** Error to display instead of advertising the references. */
  144. private StringBuilder advertiseError;
  145. /** An exception caught while unpacking and fsck'ing the objects. */
  146. private Throwable unpackError;
  147. /** If {@link BasePackPushConnection#CAPABILITY_REPORT_STATUS} is enabled. */
  148. private boolean reportStatus;
  149. /** If {@link BasePackPushConnection#CAPABILITY_SIDE_BAND_64K} is enabled. */
  150. private boolean sideBand;
  151. /** Lock around the received pack file, while updating refs. */
  152. private PackLock packLock;
  153. private boolean ensureObjectsProvidedVisible;
  154. /**
  155. * Create a new pack receive for an open repository.
  156. *
  157. * @param into
  158. * the destination repository.
  159. */
  160. public ReceivePack(final Repository into) {
  161. db = into;
  162. walk = new RevWalk(db);
  163. final ReceiveConfig cfg = db.getConfig().get(ReceiveConfig.KEY);
  164. checkReceivedObjects = cfg.checkReceivedObjects;
  165. allowCreates = cfg.allowCreates;
  166. allowDeletes = cfg.allowDeletes;
  167. allowNonFastForwards = cfg.allowNonFastForwards;
  168. allowOfsDelta = cfg.allowOfsDelta;
  169. refFilter = RefFilter.DEFAULT;
  170. preReceive = PreReceiveHook.NULL;
  171. postReceive = PostReceiveHook.NULL;
  172. }
  173. private static class ReceiveConfig {
  174. static final SectionParser<ReceiveConfig> KEY = new SectionParser<ReceiveConfig>() {
  175. public ReceiveConfig parse(final Config cfg) {
  176. return new ReceiveConfig(cfg);
  177. }
  178. };
  179. final boolean checkReceivedObjects;
  180. final boolean allowCreates;
  181. final boolean allowDeletes;
  182. final boolean allowNonFastForwards;
  183. final boolean allowOfsDelta;
  184. ReceiveConfig(final Config config) {
  185. checkReceivedObjects = config.getBoolean("receive", "fsckobjects",
  186. false);
  187. allowCreates = true;
  188. allowDeletes = !config.getBoolean("receive", "denydeletes", false);
  189. allowNonFastForwards = !config.getBoolean("receive",
  190. "denynonfastforwards", false);
  191. allowOfsDelta = config.getBoolean("repack", "usedeltabaseoffset",
  192. true);
  193. }
  194. }
  195. /** @return the repository this receive completes into. */
  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. return refs;
  206. }
  207. /**
  208. * Configure this receive pack instance to ensure that the provided
  209. * objects are visible to the user.
  210. * <p>
  211. * By default, a receive pack assumes that its user will only provide
  212. * references to objects that it can see. Setting this flag to {@code true}
  213. * will add an additional check that verifies that the objects that were
  214. * provided are reachable by a tree or a commit that the user can see.
  215. * <p>
  216. * This option is useful when the code doesn't trust the client not to
  217. * provide a forged SHA-1 reference to an object in an attempt to access
  218. * parts of the DAG that they aren't allowed to see, via the configured
  219. * {@link RefFilter}.
  220. *
  221. * @param b {@code true} to enable the additional check.
  222. */
  223. public void setEnsureProvidedObjectsVisible(boolean b) {
  224. this.ensureObjectsProvidedVisible = b;
  225. }
  226. /**
  227. * @return true if this class expects a bi-directional pipe opened between
  228. * the client and itself. The default is true.
  229. */
  230. public boolean isBiDirectionalPipe() {
  231. return biDirectionalPipe;
  232. }
  233. /**
  234. * @param twoWay
  235. * if true, this class will assume the socket is a fully
  236. * bidirectional pipe between the two peers and takes advantage
  237. * of that by first transmitting the known refs, then waiting to
  238. * read commands. If false, this class assumes it must read the
  239. * commands before writing output and does not perform the
  240. * initial advertising.
  241. */
  242. public void setBiDirectionalPipe(final boolean twoWay) {
  243. biDirectionalPipe = twoWay;
  244. }
  245. /**
  246. * @return true if this instance will verify received objects are formatted
  247. * correctly. Validating objects requires more CPU time on this side
  248. * of the connection.
  249. */
  250. public boolean isCheckReceivedObjects() {
  251. return checkReceivedObjects;
  252. }
  253. /**
  254. * @param check
  255. * true to enable checking received objects; false to assume all
  256. * received objects are valid.
  257. */
  258. public void setCheckReceivedObjects(final boolean check) {
  259. checkReceivedObjects = check;
  260. }
  261. /** @return true if the client can request refs to be created. */
  262. public boolean isAllowCreates() {
  263. return allowCreates;
  264. }
  265. /**
  266. * @param canCreate
  267. * true to permit create ref commands to be processed.
  268. */
  269. public void setAllowCreates(final boolean canCreate) {
  270. allowCreates = canCreate;
  271. }
  272. /** @return true if the client can request refs to be deleted. */
  273. public boolean isAllowDeletes() {
  274. return allowDeletes;
  275. }
  276. /**
  277. * @param canDelete
  278. * true to permit delete ref commands to be processed.
  279. */
  280. public void setAllowDeletes(final boolean canDelete) {
  281. allowDeletes = canDelete;
  282. }
  283. /**
  284. * @return true if the client can request non-fast-forward updates of a ref,
  285. * possibly making objects unreachable.
  286. */
  287. public boolean isAllowNonFastForwards() {
  288. return allowNonFastForwards;
  289. }
  290. /**
  291. * @param canRewind
  292. * true to permit the client to ask for non-fast-forward updates
  293. * of an existing ref.
  294. */
  295. public void setAllowNonFastForwards(final boolean canRewind) {
  296. allowNonFastForwards = canRewind;
  297. }
  298. /** @return identity of the user making the changes in the reflog. */
  299. public PersonIdent getRefLogIdent() {
  300. return refLogIdent;
  301. }
  302. /**
  303. * Set the identity of the user appearing in the affected reflogs.
  304. * <p>
  305. * The timestamp portion of the identity is ignored. A new identity with the
  306. * current timestamp will be created automatically when the updates occur
  307. * and the log records are written.
  308. *
  309. * @param pi
  310. * identity of the user. If null the identity will be
  311. * automatically determined based on the repository
  312. * configuration.
  313. */
  314. public void setRefLogIdent(final PersonIdent pi) {
  315. refLogIdent = pi;
  316. }
  317. /** @return the filter used while advertising the refs to the client */
  318. public RefFilter getRefFilter() {
  319. return refFilter;
  320. }
  321. /**
  322. * Set the filter used while advertising the refs to the client.
  323. * <p>
  324. * Only refs allowed by this filter will be shown to the client.
  325. * Clients may still attempt to create or update a reference hidden
  326. * by the configured {@link RefFilter}. These attempts should be
  327. * rejected by a matching {@link PreReceiveHook}.
  328. *
  329. * @param refFilter
  330. * the filter; may be null to show all refs.
  331. */
  332. public void setRefFilter(final RefFilter refFilter) {
  333. this.refFilter = refFilter != null ? refFilter : RefFilter.DEFAULT;
  334. }
  335. /** @return get the hook invoked before updates occur. */
  336. public PreReceiveHook getPreReceiveHook() {
  337. return preReceive;
  338. }
  339. /**
  340. * Set the hook which is invoked prior to commands being executed.
  341. * <p>
  342. * Only valid commands (those which have no obvious errors according to the
  343. * received input and this instance's configuration) are passed into the
  344. * hook. The hook may mark a command with a result of any value other than
  345. * {@link Result#NOT_ATTEMPTED} to block its execution.
  346. * <p>
  347. * The hook may be called with an empty command collection if the current
  348. * set is completely invalid.
  349. *
  350. * @param h
  351. * the hook instance; may be null to disable the hook.
  352. */
  353. public void setPreReceiveHook(final PreReceiveHook h) {
  354. preReceive = h != null ? h : PreReceiveHook.NULL;
  355. }
  356. /** @return get the hook invoked after updates occur. */
  357. public PostReceiveHook getPostReceiveHook() {
  358. return postReceive;
  359. }
  360. /**
  361. * Set the hook which is invoked after commands are executed.
  362. * <p>
  363. * Only successful commands (type is {@link Result#OK}) are passed into the
  364. * hook. The hook may be called with an empty command collection if the
  365. * current set all resulted in an error.
  366. *
  367. * @param h
  368. * the hook instance; may be null to disable the hook.
  369. */
  370. public void setPostReceiveHook(final PostReceiveHook h) {
  371. postReceive = h != null ? h : PostReceiveHook.NULL;
  372. }
  373. /** @return timeout (in seconds) before aborting an IO operation. */
  374. public int getTimeout() {
  375. return timeout;
  376. }
  377. /**
  378. * Set the timeout before willing to abort an IO call.
  379. *
  380. * @param seconds
  381. * number of seconds to wait (with no data transfer occurring)
  382. * before aborting an IO read or write operation with the
  383. * connected client.
  384. */
  385. public void setTimeout(final int seconds) {
  386. timeout = seconds;
  387. }
  388. /** @return all of the command received by the current request. */
  389. public List<ReceiveCommand> getAllCommands() {
  390. return Collections.unmodifiableList(commands);
  391. }
  392. /**
  393. * Send an error message to the client.
  394. * <p>
  395. * If any error messages are sent before the references are advertised to
  396. * the client, the errors will be sent instead of the advertisement and the
  397. * receive operation will be aborted. All clients should receive and display
  398. * such early stage errors.
  399. * <p>
  400. * If the reference advertisements have already been sent, messages are sent
  401. * in a side channel. If the client doesn't support receiving messages, the
  402. * message will be discarded, with no other indication to the caller or to
  403. * the client.
  404. * <p>
  405. * {@link PreReceiveHook}s should always try to use
  406. * {@link ReceiveCommand#setResult(Result, String)} with a result status of
  407. * {@link Result#REJECTED_OTHER_REASON} to indicate any reasons for
  408. * rejecting an update. Messages attached to a command are much more likely
  409. * to be returned to the client.
  410. *
  411. * @param what
  412. * string describing the problem identified by the hook. The
  413. * string must not end with an LF, and must not contain an LF.
  414. */
  415. public void sendError(final String what) {
  416. if (refs == null) {
  417. if (advertiseError == null)
  418. advertiseError = new StringBuilder();
  419. advertiseError.append(what).append('\n');
  420. } else {
  421. try {
  422. if (msgs != null)
  423. msgs.write("error: " + what + "\n");
  424. } catch (IOException e) {
  425. // Ignore write failures.
  426. }
  427. }
  428. }
  429. /**
  430. * Send a message to the client, if it supports receiving them.
  431. * <p>
  432. * If the client doesn't support receiving messages, the message will be
  433. * discarded, with no other indication to the caller or to the client.
  434. *
  435. * @param what
  436. * string describing the problem identified by the hook. The
  437. * string must not end with an LF, and must not contain an LF.
  438. */
  439. public void sendMessage(final String what) {
  440. try {
  441. if (msgs != null)
  442. msgs.write(what + "\n");
  443. } catch (IOException e) {
  444. // Ignore write failures.
  445. }
  446. }
  447. /**
  448. * Execute the receive task on the socket.
  449. *
  450. * @param input
  451. * raw input to read client commands and pack data from. Caller
  452. * must ensure the input is buffered, otherwise read performance
  453. * may suffer.
  454. * @param output
  455. * response back to the Git network client. Caller must ensure
  456. * the output is buffered, otherwise write performance may
  457. * suffer.
  458. * @param messages
  459. * secondary "notice" channel to send additional messages out
  460. * through. When run over SSH this should be tied back to the
  461. * standard error channel of the command execution. For most
  462. * other network connections this should be null.
  463. * @throws IOException
  464. */
  465. public void receive(final InputStream input, final OutputStream output,
  466. final OutputStream messages) throws IOException {
  467. try {
  468. rawIn = input;
  469. rawOut = output;
  470. if (timeout > 0) {
  471. final Thread caller = Thread.currentThread();
  472. timer = new InterruptTimer(caller.getName() + "-Timer");
  473. timeoutIn = new TimeoutInputStream(rawIn, timer);
  474. TimeoutOutputStream o = new TimeoutOutputStream(rawOut, timer);
  475. timeoutIn.setTimeout(timeout * 1000);
  476. o.setTimeout(timeout * 1000);
  477. rawIn = timeoutIn;
  478. rawOut = o;
  479. }
  480. pckIn = new PacketLineIn(rawIn);
  481. pckOut = new PacketLineOut(rawOut);
  482. if (messages != null)
  483. msgs = new OutputStreamWriter(messages, Constants.CHARSET);
  484. enabledCapablities = new HashSet<String>();
  485. commands = new ArrayList<ReceiveCommand>();
  486. service();
  487. } finally {
  488. try {
  489. if (pckOut != null)
  490. pckOut.flush();
  491. if (msgs != null)
  492. msgs.flush();
  493. if (sideBand) {
  494. // If we are using side band, we need to send a final
  495. // flush-pkt to tell the remote peer the side band is
  496. // complete and it should stop decoding. We need to
  497. // use the original output stream as rawOut is now the
  498. // side band data channel.
  499. //
  500. new PacketLineOut(output).end();
  501. }
  502. } finally {
  503. unlockPack();
  504. timeoutIn = null;
  505. rawIn = null;
  506. rawOut = null;
  507. pckIn = null;
  508. pckOut = null;
  509. msgs = null;
  510. refs = null;
  511. enabledCapablities = null;
  512. commands = null;
  513. if (timer != null) {
  514. try {
  515. timer.terminate();
  516. } finally {
  517. timer = null;
  518. }
  519. }
  520. }
  521. }
  522. }
  523. private void service() throws IOException {
  524. if (biDirectionalPipe)
  525. sendAdvertisedRefs(new PacketLineOutRefAdvertiser(pckOut));
  526. else
  527. refs = refFilter.filter(db.getAllRefs());
  528. if (advertiseError != null)
  529. return;
  530. recvCommands();
  531. if (!commands.isEmpty()) {
  532. enableCapabilities();
  533. if (needPack()) {
  534. try {
  535. receivePack();
  536. if (isCheckReceivedObjects())
  537. checkConnectivity();
  538. ip = null;
  539. unpackError = null;
  540. } catch (IOException err) {
  541. unpackError = err;
  542. } catch (RuntimeException err) {
  543. unpackError = err;
  544. } catch (Error err) {
  545. unpackError = err;
  546. }
  547. }
  548. if (unpackError == null) {
  549. validateCommands();
  550. executeCommands();
  551. }
  552. unlockPack();
  553. if (reportStatus) {
  554. sendStatusReport(true, new Reporter() {
  555. void sendString(final String s) throws IOException {
  556. pckOut.writeString(s + "\n");
  557. }
  558. });
  559. pckOut.end();
  560. } else if (msgs != null) {
  561. sendStatusReport(false, new Reporter() {
  562. void sendString(final String s) throws IOException {
  563. msgs.write(s + "\n");
  564. }
  565. });
  566. }
  567. postReceive.onPostReceive(this, filterCommands(Result.OK));
  568. }
  569. }
  570. private void unlockPack() {
  571. if (packLock != null) {
  572. packLock.unlock();
  573. packLock = null;
  574. }
  575. }
  576. /**
  577. * Generate an advertisement of available refs and capabilities.
  578. *
  579. * @param adv
  580. * the advertisement formatter.
  581. * @throws IOException
  582. * the formatter failed to write an advertisement.
  583. */
  584. public void sendAdvertisedRefs(final RefAdvertiser adv) throws IOException {
  585. if (advertiseError != null) {
  586. adv.writeOne("ERR " + advertiseError);
  587. return;
  588. }
  589. final RevFlag advertised = walk.newFlag("ADVERTISED");
  590. adv.init(walk, advertised);
  591. adv.advertiseCapability(CAPABILITY_SIDE_BAND_64K);
  592. adv.advertiseCapability(CAPABILITY_DELETE_REFS);
  593. adv.advertiseCapability(CAPABILITY_REPORT_STATUS);
  594. if (allowOfsDelta)
  595. adv.advertiseCapability(CAPABILITY_OFS_DELTA);
  596. refs = refFilter.filter(db.getAllRefs());
  597. final Ref head = refs.remove(Constants.HEAD);
  598. adv.send(refs);
  599. if (head != null && !head.isSymbolic())
  600. adv.advertiseHave(head.getObjectId());
  601. adv.includeAdditionalHaves();
  602. if (adv.isEmpty())
  603. adv.advertiseId(ObjectId.zeroId(), "capabilities^{}");
  604. adv.end();
  605. }
  606. private void recvCommands() throws IOException {
  607. for (;;) {
  608. String line;
  609. try {
  610. line = pckIn.readStringRaw();
  611. } catch (EOFException eof) {
  612. if (commands.isEmpty())
  613. return;
  614. throw eof;
  615. }
  616. if (line == PacketLineIn.END)
  617. break;
  618. if (commands.isEmpty()) {
  619. final int nul = line.indexOf('\0');
  620. if (nul >= 0) {
  621. for (String c : line.substring(nul + 1).split(" "))
  622. enabledCapablities.add(c);
  623. line = line.substring(0, nul);
  624. }
  625. }
  626. if (line.length() < 83) {
  627. final String m = "error: invalid protocol: wanted 'old new ref'";
  628. sendError(m);
  629. throw new PackProtocolException(m);
  630. }
  631. final ObjectId oldId = ObjectId.fromString(line.substring(0, 40));
  632. final ObjectId newId = ObjectId.fromString(line.substring(41, 81));
  633. final String name = line.substring(82);
  634. final ReceiveCommand cmd = new ReceiveCommand(oldId, newId, name);
  635. if (name.equals(Constants.HEAD)) {
  636. cmd.setResult(Result.REJECTED_CURRENT_BRANCH);
  637. } else {
  638. cmd.setRef(refs.get(cmd.getRefName()));
  639. }
  640. commands.add(cmd);
  641. }
  642. }
  643. private void enableCapabilities() {
  644. reportStatus = enabledCapablities.contains(CAPABILITY_REPORT_STATUS);
  645. sideBand = enabledCapablities.contains(CAPABILITY_SIDE_BAND_64K);
  646. if (sideBand) {
  647. OutputStream out = rawOut;
  648. rawOut = new SideBandOutputStream(CH_DATA, MAX_BUF, out);
  649. pckOut = new PacketLineOut(rawOut);
  650. msgs = new OutputStreamWriter(new SideBandOutputStream(CH_PROGRESS,
  651. MAX_BUF, out), Constants.CHARSET);
  652. }
  653. }
  654. private boolean needPack() {
  655. for (final ReceiveCommand cmd : commands) {
  656. if (cmd.getType() != ReceiveCommand.Type.DELETE)
  657. return true;
  658. }
  659. return false;
  660. }
  661. private void receivePack() throws IOException {
  662. // It might take the client a while to pack the objects it needs
  663. // to send to us. We should increase our timeout so we don't
  664. // abort while the client is computing.
  665. //
  666. if (timeoutIn != null)
  667. timeoutIn.setTimeout(10 * timeout * 1000);
  668. ip = IndexPack.create(db, rawIn);
  669. ip.setFixThin(true);
  670. ip.setNeedNewObjectIds(ensureObjectsProvidedVisible);
  671. ip.setNeedBaseObjectIds(ensureObjectsProvidedVisible);
  672. ip.setObjectChecking(isCheckReceivedObjects());
  673. ip.index(NullProgressMonitor.INSTANCE);
  674. String lockMsg = "jgit receive-pack";
  675. if (getRefLogIdent() != null)
  676. lockMsg += " from " + getRefLogIdent().toExternalString();
  677. packLock = ip.renameAndOpenPack(lockMsg);
  678. if (timeoutIn != null)
  679. timeoutIn.setTimeout(timeout * 1000);
  680. }
  681. private void checkConnectivity() throws IOException {
  682. ObjectIdSubclassMap<ObjectId> baseObjects = null;
  683. ObjectIdSubclassMap<ObjectId> providedObjects = null;
  684. if (ensureObjectsProvidedVisible) {
  685. baseObjects = ip.getBaseObjectIds();
  686. providedObjects = ip.getNewObjectIds();
  687. }
  688. ip = null;
  689. final ObjectWalk ow = new ObjectWalk(db);
  690. for (final ReceiveCommand cmd : commands) {
  691. if (cmd.getResult() != Result.NOT_ATTEMPTED)
  692. continue;
  693. if (cmd.getType() == ReceiveCommand.Type.DELETE)
  694. continue;
  695. ow.markStart(ow.parseAny(cmd.getNewId()));
  696. }
  697. for (final Ref ref : refs.values()) {
  698. RevObject o = ow.parseAny(ref.getObjectId());
  699. ow.markUninteresting(o);
  700. if (ensureObjectsProvidedVisible && !baseObjects.isEmpty()) {
  701. while (o instanceof RevTag)
  702. o = ((RevTag) o).getObject();
  703. if (o instanceof RevCommit)
  704. o = ((RevCommit) o).getTree();
  705. if (o instanceof RevTree)
  706. ow.markUninteresting(o);
  707. }
  708. }
  709. if (ensureObjectsProvidedVisible) {
  710. for (ObjectId id : baseObjects) {
  711. RevObject b = ow.lookupAny(id, Constants.OBJ_BLOB);
  712. if (!b.has(RevFlag.UNINTERESTING))
  713. throw new MissingObjectException(b, b.getType());
  714. }
  715. }
  716. RevCommit c;
  717. while ((c = ow.next()) != null) {
  718. if (ensureObjectsProvidedVisible && !providedObjects.contains(c))
  719. throw new MissingObjectException(c, Constants.TYPE_COMMIT);
  720. }
  721. RevObject o;
  722. while ((o = ow.nextObject()) != null) {
  723. if (o instanceof RevBlob && !db.hasObject(o))
  724. throw new MissingObjectException(o, Constants.TYPE_BLOB);
  725. if (ensureObjectsProvidedVisible && !providedObjects.contains(o))
  726. throw new MissingObjectException(o, o.getType());
  727. }
  728. }
  729. private void validateCommands() {
  730. for (final ReceiveCommand cmd : commands) {
  731. final Ref ref = cmd.getRef();
  732. if (cmd.getResult() != Result.NOT_ATTEMPTED)
  733. continue;
  734. if (cmd.getType() == ReceiveCommand.Type.DELETE
  735. && !isAllowDeletes()) {
  736. // Deletes are not supported on this repository.
  737. //
  738. cmd.setResult(Result.REJECTED_NODELETE);
  739. continue;
  740. }
  741. if (cmd.getType() == ReceiveCommand.Type.CREATE) {
  742. if (!isAllowCreates()) {
  743. cmd.setResult(Result.REJECTED_NOCREATE);
  744. continue;
  745. }
  746. if (ref != null && !isAllowNonFastForwards()) {
  747. // Creation over an existing ref is certainly not going
  748. // to be a fast-forward update. We can reject it early.
  749. //
  750. cmd.setResult(Result.REJECTED_NONFASTFORWARD);
  751. continue;
  752. }
  753. if (ref != null) {
  754. // A well behaved client shouldn't have sent us a
  755. // create command for a ref we advertised to it.
  756. //
  757. cmd.setResult(Result.REJECTED_OTHER_REASON, "ref exists");
  758. continue;
  759. }
  760. }
  761. if (cmd.getType() == ReceiveCommand.Type.DELETE && ref != null
  762. && !ObjectId.zeroId().equals(cmd.getOldId())
  763. && !ref.getObjectId().equals(cmd.getOldId())) {
  764. // Delete commands can be sent with the old id matching our
  765. // advertised value, *OR* with the old id being 0{40}. Any
  766. // other requested old id is invalid.
  767. //
  768. cmd.setResult(Result.REJECTED_OTHER_REASON,
  769. "invalid old id sent");
  770. continue;
  771. }
  772. if (cmd.getType() == ReceiveCommand.Type.UPDATE) {
  773. if (ref == null) {
  774. // The ref must have been advertised in order to be updated.
  775. //
  776. cmd.setResult(Result.REJECTED_OTHER_REASON, "no such ref");
  777. continue;
  778. }
  779. if (!ref.getObjectId().equals(cmd.getOldId())) {
  780. // A properly functioning client will send the same
  781. // object id we advertised.
  782. //
  783. cmd.setResult(Result.REJECTED_OTHER_REASON,
  784. "invalid old id sent");
  785. continue;
  786. }
  787. // Is this possibly a non-fast-forward style update?
  788. //
  789. RevObject oldObj, newObj;
  790. try {
  791. oldObj = walk.parseAny(cmd.getOldId());
  792. } catch (IOException e) {
  793. cmd.setResult(Result.REJECTED_MISSING_OBJECT, cmd
  794. .getOldId().name());
  795. continue;
  796. }
  797. try {
  798. newObj = walk.parseAny(cmd.getNewId());
  799. } catch (IOException e) {
  800. cmd.setResult(Result.REJECTED_MISSING_OBJECT, cmd
  801. .getNewId().name());
  802. continue;
  803. }
  804. if (oldObj instanceof RevCommit && newObj instanceof RevCommit) {
  805. try {
  806. if (!walk.isMergedInto((RevCommit) oldObj,
  807. (RevCommit) newObj)) {
  808. cmd
  809. .setType(ReceiveCommand.Type.UPDATE_NONFASTFORWARD);
  810. }
  811. } catch (MissingObjectException e) {
  812. cmd.setResult(Result.REJECTED_MISSING_OBJECT, e
  813. .getMessage());
  814. } catch (IOException e) {
  815. cmd.setResult(Result.REJECTED_OTHER_REASON);
  816. }
  817. } else {
  818. cmd.setType(ReceiveCommand.Type.UPDATE_NONFASTFORWARD);
  819. }
  820. }
  821. if (!cmd.getRefName().startsWith(Constants.R_REFS)
  822. || !Repository.isValidRefName(cmd.getRefName())) {
  823. cmd.setResult(Result.REJECTED_OTHER_REASON, "funny refname");
  824. }
  825. }
  826. }
  827. private void executeCommands() {
  828. preReceive.onPreReceive(this, filterCommands(Result.NOT_ATTEMPTED));
  829. for (final ReceiveCommand cmd : filterCommands(Result.NOT_ATTEMPTED))
  830. execute(cmd);
  831. }
  832. private void execute(final ReceiveCommand cmd) {
  833. try {
  834. final RefUpdate ru = db.updateRef(cmd.getRefName());
  835. ru.setRefLogIdent(getRefLogIdent());
  836. switch (cmd.getType()) {
  837. case DELETE:
  838. if (!ObjectId.zeroId().equals(cmd.getOldId())) {
  839. // We can only do a CAS style delete if the client
  840. // didn't bork its delete request by sending the
  841. // wrong zero id rather than the advertised one.
  842. //
  843. ru.setExpectedOldObjectId(cmd.getOldId());
  844. }
  845. ru.setForceUpdate(true);
  846. status(cmd, ru.delete(walk));
  847. break;
  848. case CREATE:
  849. case UPDATE:
  850. case UPDATE_NONFASTFORWARD:
  851. ru.setForceUpdate(isAllowNonFastForwards());
  852. ru.setExpectedOldObjectId(cmd.getOldId());
  853. ru.setNewObjectId(cmd.getNewId());
  854. ru.setRefLogMessage("push", true);
  855. status(cmd, ru.update(walk));
  856. break;
  857. }
  858. } catch (IOException err) {
  859. cmd.setResult(Result.REJECTED_OTHER_REASON, "lock error: "
  860. + err.getMessage());
  861. }
  862. }
  863. private void status(final ReceiveCommand cmd, final RefUpdate.Result result) {
  864. switch (result) {
  865. case NOT_ATTEMPTED:
  866. cmd.setResult(Result.NOT_ATTEMPTED);
  867. break;
  868. case LOCK_FAILURE:
  869. case IO_FAILURE:
  870. cmd.setResult(Result.LOCK_FAILURE);
  871. break;
  872. case NO_CHANGE:
  873. case NEW:
  874. case FORCED:
  875. case FAST_FORWARD:
  876. cmd.setResult(Result.OK);
  877. break;
  878. case REJECTED:
  879. cmd.setResult(Result.REJECTED_NONFASTFORWARD);
  880. break;
  881. case REJECTED_CURRENT_BRANCH:
  882. cmd.setResult(Result.REJECTED_CURRENT_BRANCH);
  883. break;
  884. default:
  885. cmd.setResult(Result.REJECTED_OTHER_REASON, result.name());
  886. break;
  887. }
  888. }
  889. private List<ReceiveCommand> filterCommands(final Result want) {
  890. final List<ReceiveCommand> r = new ArrayList<ReceiveCommand>(commands
  891. .size());
  892. for (final ReceiveCommand cmd : commands) {
  893. if (cmd.getResult() == want)
  894. r.add(cmd);
  895. }
  896. return r;
  897. }
  898. private void sendStatusReport(final boolean forClient, final Reporter out)
  899. throws IOException {
  900. if (unpackError != null) {
  901. out.sendString("unpack error " + unpackError.getMessage());
  902. if (forClient) {
  903. for (final ReceiveCommand cmd : commands) {
  904. out.sendString("ng " + cmd.getRefName()
  905. + " n/a (unpacker error)");
  906. }
  907. }
  908. return;
  909. }
  910. if (forClient)
  911. out.sendString("unpack ok");
  912. for (final ReceiveCommand cmd : commands) {
  913. if (cmd.getResult() == Result.OK) {
  914. if (forClient)
  915. out.sendString("ok " + cmd.getRefName());
  916. continue;
  917. }
  918. final StringBuilder r = new StringBuilder();
  919. r.append("ng ");
  920. r.append(cmd.getRefName());
  921. r.append(" ");
  922. switch (cmd.getResult()) {
  923. case NOT_ATTEMPTED:
  924. r.append("server bug; ref not processed");
  925. break;
  926. case REJECTED_NOCREATE:
  927. r.append("creation prohibited");
  928. break;
  929. case REJECTED_NODELETE:
  930. r.append("deletion prohibited");
  931. break;
  932. case REJECTED_NONFASTFORWARD:
  933. r.append("non-fast forward");
  934. break;
  935. case REJECTED_CURRENT_BRANCH:
  936. r.append("branch is currently checked out");
  937. break;
  938. case REJECTED_MISSING_OBJECT:
  939. if (cmd.getMessage() == null)
  940. r.append("missing object(s)");
  941. else if (cmd.getMessage().length() == Constants.OBJECT_ID_STRING_LENGTH)
  942. r.append("object " + cmd.getMessage() + " missing");
  943. else
  944. r.append(cmd.getMessage());
  945. break;
  946. case REJECTED_OTHER_REASON:
  947. if (cmd.getMessage() == null)
  948. r.append("unspecified reason");
  949. else
  950. r.append(cmd.getMessage());
  951. break;
  952. case LOCK_FAILURE:
  953. r.append("failed to lock");
  954. break;
  955. case OK:
  956. // We shouldn't have reached this case (see 'ok' case above).
  957. continue;
  958. }
  959. out.sendString(r.toString());
  960. }
  961. }
  962. static abstract class Reporter {
  963. abstract void sendString(String s) throws IOException;
  964. }
  965. }