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

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