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

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