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

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