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.

ReceivePack.java 32KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106
  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. final Set<ObjectId> baseObjects;
  683. if (ensureObjectsProvidedVisible)
  684. baseObjects = ip.getBaseObjectIds();
  685. else
  686. baseObjects = Collections.emptySet();
  687. final ObjectWalk ow = new ObjectWalk(db);
  688. for (final ReceiveCommand cmd : commands) {
  689. if (cmd.getResult() != Result.NOT_ATTEMPTED)
  690. continue;
  691. if (cmd.getType() == ReceiveCommand.Type.DELETE)
  692. continue;
  693. ow.markStart(ow.parseAny(cmd.getNewId()));
  694. }
  695. for (final Ref ref : refs.values()) {
  696. RevObject o = ow.parseAny(ref.getObjectId());
  697. ow.markUninteresting(o);
  698. if (ensureObjectsProvidedVisible && !baseObjects.isEmpty()) {
  699. while (o instanceof RevTag)
  700. o = ((RevTag) o).getObject();
  701. if (o instanceof RevCommit)
  702. o = ((RevCommit) o).getTree();
  703. if (o instanceof RevTree)
  704. ow.markUninteresting(o);
  705. }
  706. }
  707. ObjectIdSubclassMap<ObjectId> provided =
  708. new ObjectIdSubclassMap<ObjectId>();
  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. for (ObjectId id : ip.getNewObjectIds())
  716. provided.add(id);
  717. }
  718. RevCommit c;
  719. while ((c = ow.next()) != null) {
  720. if (ensureObjectsProvidedVisible && !provided.contains(c))
  721. throw new MissingObjectException(c, Constants.TYPE_COMMIT);
  722. }
  723. RevObject o;
  724. while ((o = ow.nextObject()) != null) {
  725. if (o instanceof RevBlob && !db.hasObject(o))
  726. throw new MissingObjectException(o, Constants.TYPE_BLOB);
  727. if (ensureObjectsProvidedVisible && !provided.contains(o))
  728. throw new MissingObjectException(o, Constants.TYPE_BLOB);
  729. }
  730. }
  731. private void validateCommands() {
  732. for (final ReceiveCommand cmd : commands) {
  733. final Ref ref = cmd.getRef();
  734. if (cmd.getResult() != Result.NOT_ATTEMPTED)
  735. continue;
  736. if (cmd.getType() == ReceiveCommand.Type.DELETE
  737. && !isAllowDeletes()) {
  738. // Deletes are not supported on this repository.
  739. //
  740. cmd.setResult(Result.REJECTED_NODELETE);
  741. continue;
  742. }
  743. if (cmd.getType() == ReceiveCommand.Type.CREATE) {
  744. if (!isAllowCreates()) {
  745. cmd.setResult(Result.REJECTED_NOCREATE);
  746. continue;
  747. }
  748. if (ref != null && !isAllowNonFastForwards()) {
  749. // Creation over an existing ref is certainly not going
  750. // to be a fast-forward update. We can reject it early.
  751. //
  752. cmd.setResult(Result.REJECTED_NONFASTFORWARD);
  753. continue;
  754. }
  755. if (ref != null) {
  756. // A well behaved client shouldn't have sent us a
  757. // create command for a ref we advertised to it.
  758. //
  759. cmd.setResult(Result.REJECTED_OTHER_REASON, "ref exists");
  760. continue;
  761. }
  762. }
  763. if (cmd.getType() == ReceiveCommand.Type.DELETE && ref != null
  764. && !ObjectId.zeroId().equals(cmd.getOldId())
  765. && !ref.getObjectId().equals(cmd.getOldId())) {
  766. // Delete commands can be sent with the old id matching our
  767. // advertised value, *OR* with the old id being 0{40}. Any
  768. // other requested old id is invalid.
  769. //
  770. cmd.setResult(Result.REJECTED_OTHER_REASON,
  771. "invalid old id sent");
  772. continue;
  773. }
  774. if (cmd.getType() == ReceiveCommand.Type.UPDATE) {
  775. if (ref == null) {
  776. // The ref must have been advertised in order to be updated.
  777. //
  778. cmd.setResult(Result.REJECTED_OTHER_REASON, "no such ref");
  779. continue;
  780. }
  781. if (!ref.getObjectId().equals(cmd.getOldId())) {
  782. // A properly functioning client will send the same
  783. // object id we advertised.
  784. //
  785. cmd.setResult(Result.REJECTED_OTHER_REASON,
  786. "invalid old id sent");
  787. continue;
  788. }
  789. // Is this possibly a non-fast-forward style update?
  790. //
  791. RevObject oldObj, newObj;
  792. try {
  793. oldObj = walk.parseAny(cmd.getOldId());
  794. } catch (IOException e) {
  795. cmd.setResult(Result.REJECTED_MISSING_OBJECT, cmd
  796. .getOldId().name());
  797. continue;
  798. }
  799. try {
  800. newObj = walk.parseAny(cmd.getNewId());
  801. } catch (IOException e) {
  802. cmd.setResult(Result.REJECTED_MISSING_OBJECT, cmd
  803. .getNewId().name());
  804. continue;
  805. }
  806. if (oldObj instanceof RevCommit && newObj instanceof RevCommit) {
  807. try {
  808. if (!walk.isMergedInto((RevCommit) oldObj,
  809. (RevCommit) newObj)) {
  810. cmd
  811. .setType(ReceiveCommand.Type.UPDATE_NONFASTFORWARD);
  812. }
  813. } catch (MissingObjectException e) {
  814. cmd.setResult(Result.REJECTED_MISSING_OBJECT, e
  815. .getMessage());
  816. } catch (IOException e) {
  817. cmd.setResult(Result.REJECTED_OTHER_REASON);
  818. }
  819. } else {
  820. cmd.setType(ReceiveCommand.Type.UPDATE_NONFASTFORWARD);
  821. }
  822. }
  823. if (!cmd.getRefName().startsWith(Constants.R_REFS)
  824. || !Repository.isValidRefName(cmd.getRefName())) {
  825. cmd.setResult(Result.REJECTED_OTHER_REASON, "funny refname");
  826. }
  827. }
  828. }
  829. private void executeCommands() {
  830. preReceive.onPreReceive(this, filterCommands(Result.NOT_ATTEMPTED));
  831. for (final ReceiveCommand cmd : filterCommands(Result.NOT_ATTEMPTED))
  832. execute(cmd);
  833. }
  834. private void execute(final ReceiveCommand cmd) {
  835. try {
  836. final RefUpdate ru = db.updateRef(cmd.getRefName());
  837. ru.setRefLogIdent(getRefLogIdent());
  838. switch (cmd.getType()) {
  839. case DELETE:
  840. if (!ObjectId.zeroId().equals(cmd.getOldId())) {
  841. // We can only do a CAS style delete if the client
  842. // didn't bork its delete request by sending the
  843. // wrong zero id rather than the advertised one.
  844. //
  845. ru.setExpectedOldObjectId(cmd.getOldId());
  846. }
  847. ru.setForceUpdate(true);
  848. status(cmd, ru.delete(walk));
  849. break;
  850. case CREATE:
  851. case UPDATE:
  852. case UPDATE_NONFASTFORWARD:
  853. ru.setForceUpdate(isAllowNonFastForwards());
  854. ru.setExpectedOldObjectId(cmd.getOldId());
  855. ru.setNewObjectId(cmd.getNewId());
  856. ru.setRefLogMessage("push", true);
  857. status(cmd, ru.update(walk));
  858. break;
  859. }
  860. } catch (IOException err) {
  861. cmd.setResult(Result.REJECTED_OTHER_REASON, "lock error: "
  862. + err.getMessage());
  863. }
  864. }
  865. private void status(final ReceiveCommand cmd, final RefUpdate.Result result) {
  866. switch (result) {
  867. case NOT_ATTEMPTED:
  868. cmd.setResult(Result.NOT_ATTEMPTED);
  869. break;
  870. case LOCK_FAILURE:
  871. case IO_FAILURE:
  872. cmd.setResult(Result.LOCK_FAILURE);
  873. break;
  874. case NO_CHANGE:
  875. case NEW:
  876. case FORCED:
  877. case FAST_FORWARD:
  878. cmd.setResult(Result.OK);
  879. break;
  880. case REJECTED:
  881. cmd.setResult(Result.REJECTED_NONFASTFORWARD);
  882. break;
  883. case REJECTED_CURRENT_BRANCH:
  884. cmd.setResult(Result.REJECTED_CURRENT_BRANCH);
  885. break;
  886. default:
  887. cmd.setResult(Result.REJECTED_OTHER_REASON, result.name());
  888. break;
  889. }
  890. }
  891. private List<ReceiveCommand> filterCommands(final Result want) {
  892. final List<ReceiveCommand> r = new ArrayList<ReceiveCommand>(commands
  893. .size());
  894. for (final ReceiveCommand cmd : commands) {
  895. if (cmd.getResult() == want)
  896. r.add(cmd);
  897. }
  898. return r;
  899. }
  900. private void sendStatusReport(final boolean forClient, final Reporter out)
  901. throws IOException {
  902. if (unpackError != null) {
  903. out.sendString("unpack error " + unpackError.getMessage());
  904. if (forClient) {
  905. for (final ReceiveCommand cmd : commands) {
  906. out.sendString("ng " + cmd.getRefName()
  907. + " n/a (unpacker error)");
  908. }
  909. }
  910. return;
  911. }
  912. if (forClient)
  913. out.sendString("unpack ok");
  914. for (final ReceiveCommand cmd : commands) {
  915. if (cmd.getResult() == Result.OK) {
  916. if (forClient)
  917. out.sendString("ok " + cmd.getRefName());
  918. continue;
  919. }
  920. final StringBuilder r = new StringBuilder();
  921. r.append("ng ");
  922. r.append(cmd.getRefName());
  923. r.append(" ");
  924. switch (cmd.getResult()) {
  925. case NOT_ATTEMPTED:
  926. r.append("server bug; ref not processed");
  927. break;
  928. case REJECTED_NOCREATE:
  929. r.append("creation prohibited");
  930. break;
  931. case REJECTED_NODELETE:
  932. r.append("deletion prohibited");
  933. break;
  934. case REJECTED_NONFASTFORWARD:
  935. r.append("non-fast forward");
  936. break;
  937. case REJECTED_CURRENT_BRANCH:
  938. r.append("branch is currently checked out");
  939. break;
  940. case REJECTED_MISSING_OBJECT:
  941. if (cmd.getMessage() == null)
  942. r.append("missing object(s)");
  943. else if (cmd.getMessage().length() == Constants.OBJECT_ID_STRING_LENGTH)
  944. r.append("object " + cmd.getMessage() + " missing");
  945. else
  946. r.append(cmd.getMessage());
  947. break;
  948. case REJECTED_OTHER_REASON:
  949. if (cmd.getMessage() == null)
  950. r.append("unspecified reason");
  951. else
  952. r.append(cmd.getMessage());
  953. break;
  954. case LOCK_FAILURE:
  955. r.append("failed to lock");
  956. break;
  957. case OK:
  958. // We shouldn't have reached this case (see 'ok' case above).
  959. continue;
  960. }
  961. out.sendString(r.toString());
  962. }
  963. }
  964. static abstract class Reporter {
  965. abstract void sendString(String s) throws IOException;
  966. }
  967. }