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

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