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

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