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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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.GitProtocolConstants.CAPABILITY_ATOMIC;
  45. import static org.eclipse.jgit.transport.GitProtocolConstants.CAPABILITY_PUSH_OPTIONS;
  46. import static org.eclipse.jgit.transport.GitProtocolConstants.CAPABILITY_REPORT_STATUS;
  47. import java.io.IOException;
  48. import java.io.InputStream;
  49. import java.io.OutputStream;
  50. import java.util.ArrayList;
  51. import java.util.Collections;
  52. import java.util.List;
  53. import org.eclipse.jgit.annotations.Nullable;
  54. import org.eclipse.jgit.errors.UnpackException;
  55. import org.eclipse.jgit.lib.ConfigConstants;
  56. import org.eclipse.jgit.lib.Constants;
  57. import org.eclipse.jgit.lib.NullProgressMonitor;
  58. import org.eclipse.jgit.lib.Repository;
  59. import org.eclipse.jgit.transport.ReceiveCommand.Result;
  60. import org.eclipse.jgit.transport.RefAdvertiser.PacketLineOutRefAdvertiser;
  61. /**
  62. * Implements the server side of a push connection, receiving objects.
  63. */
  64. public class ReceivePack extends BaseReceivePack {
  65. /** Hook to validate the update commands before execution. */
  66. private PreReceiveHook preReceive;
  67. /** Hook to report on the commands after execution. */
  68. private PostReceiveHook postReceive;
  69. /** If {@link BasePackPushConnection#CAPABILITY_REPORT_STATUS} is enabled. */
  70. private boolean reportStatus;
  71. /** Whether the client intends to use push options. */
  72. private boolean usePushOptions;
  73. private List<String> pushOptions;
  74. /**
  75. * Create a new pack receive for an open repository.
  76. *
  77. * @param into
  78. * the destination repository.
  79. */
  80. public ReceivePack(Repository into) {
  81. super(into);
  82. preReceive = PreReceiveHook.NULL;
  83. postReceive = PostReceiveHook.NULL;
  84. }
  85. /**
  86. * Get the repository this receive completes into.
  87. *
  88. * @return the repository this receive completes into.
  89. */
  90. @Override
  91. public final Repository getRepository() {
  92. return db;
  93. }
  94. /**
  95. * Get the push certificate used to verify the pusher's identity.
  96. * <p>
  97. * Only valid after commands are read from the wire.
  98. *
  99. * @return the parsed certificate, or null if push certificates are disabled
  100. * or no cert was presented by the client.
  101. * @since 4.1
  102. */
  103. @Override
  104. public PushCertificate getPushCertificate() {
  105. return pushCert;
  106. }
  107. /**
  108. * Set the push certificate used to verify the pusher's identity.
  109. * <p>
  110. * Should only be called if reconstructing an instance without going through
  111. * the normal {@link #recvCommands()} flow.
  112. *
  113. * @param cert
  114. * the push certificate to set.
  115. * @since 4.1
  116. */
  117. @Override
  118. public void setPushCertificate(PushCertificate cert) {
  119. pushCert = cert;
  120. }
  121. /**
  122. * Gets an unmodifiable view of the option strings associated with the push.
  123. *
  124. * @return an unmodifiable view of pushOptions, or null (if pushOptions is).
  125. * @since 4.5
  126. */
  127. @Nullable
  128. public List<String> getPushOptions() {
  129. if (isAllowPushOptions() && usePushOptions) {
  130. return Collections.unmodifiableList(pushOptions);
  131. }
  132. // The client doesn't support push options. Return null to
  133. // distinguish this from the case where the client declared support
  134. // for push options and sent an empty list of them.
  135. return null;
  136. }
  137. /**
  138. * Set the push options supplied by the client.
  139. * <p>
  140. * Should only be called if reconstructing an instance without going through
  141. * the normal {@link #recvCommands()} flow.
  142. *
  143. * @param options
  144. * the list of options supplied by the client. The
  145. * {@code ReceivePack} instance takes ownership of this list.
  146. * Callers are encouraged to first create a copy if the list may
  147. * be modified later.
  148. * @since 4.5
  149. */
  150. public void setPushOptions(@Nullable List<String> options) {
  151. usePushOptions = options != null;
  152. pushOptions = options;
  153. }
  154. /**
  155. * Get the hook invoked before updates occur.
  156. *
  157. * @return the hook invoked before updates occur.
  158. */
  159. public PreReceiveHook getPreReceiveHook() {
  160. return preReceive;
  161. }
  162. /**
  163. * Set the hook which is invoked prior to commands being executed.
  164. * <p>
  165. * Only valid commands (those which have no obvious errors according to the
  166. * received input and this instance's configuration) are passed into the
  167. * hook. The hook may mark a command with a result of any value other than
  168. * {@link org.eclipse.jgit.transport.ReceiveCommand.Result#NOT_ATTEMPTED} to
  169. * block its execution.
  170. * <p>
  171. * The hook may be called with an empty command collection if the current
  172. * set is completely invalid.
  173. *
  174. * @param h
  175. * the hook instance; may be null to disable the hook.
  176. */
  177. public void setPreReceiveHook(PreReceiveHook h) {
  178. preReceive = h != null ? h : PreReceiveHook.NULL;
  179. }
  180. /**
  181. * Get the hook invoked after updates occur.
  182. *
  183. * @return the hook invoked after updates occur.
  184. */
  185. public PostReceiveHook getPostReceiveHook() {
  186. return postReceive;
  187. }
  188. /**
  189. * Set the hook which is invoked after commands are executed.
  190. * <p>
  191. * Only successful commands (type is
  192. * {@link org.eclipse.jgit.transport.ReceiveCommand.Result#OK}) are passed
  193. * into the hook. The hook may be called with an empty command collection if
  194. * the current set all resulted in an error.
  195. *
  196. * @param h
  197. * the hook instance; may be null to disable the hook.
  198. */
  199. public void setPostReceiveHook(PostReceiveHook h) {
  200. postReceive = h != null ? h : PostReceiveHook.NULL;
  201. }
  202. /**
  203. * Set whether this class will report command failures as warning messages
  204. * before sending the command results.
  205. *
  206. * @param echo
  207. * if true this class will report command failures as warning
  208. * messages before sending the command results. This is usually
  209. * not necessary, but may help buggy Git clients that discard the
  210. * errors when all branches fail.
  211. * @deprecated no widely used Git versions need this any more
  212. */
  213. @Deprecated
  214. public void setEchoCommandFailures(boolean echo) {
  215. // No-op.
  216. }
  217. /**
  218. * Execute the receive task on the socket.
  219. *
  220. * @param input
  221. * raw input to read client commands and pack data from. Caller
  222. * must ensure the input is buffered, otherwise read performance
  223. * may suffer.
  224. * @param output
  225. * response back to the Git network client. Caller must ensure
  226. * the output is buffered, otherwise write performance may
  227. * suffer.
  228. * @param messages
  229. * secondary "notice" channel to send additional messages out
  230. * through. When run over SSH this should be tied back to the
  231. * standard error channel of the command execution. For most
  232. * other network connections this should be null.
  233. * @throws java.io.IOException
  234. */
  235. public void receive(final InputStream input, final OutputStream output,
  236. final OutputStream messages) throws IOException {
  237. init(input, output, messages);
  238. try {
  239. service();
  240. } finally {
  241. try {
  242. close();
  243. } finally {
  244. release();
  245. }
  246. }
  247. }
  248. /** {@inheritDoc} */
  249. @Override
  250. protected void enableCapabilities() {
  251. reportStatus = isCapabilityEnabled(CAPABILITY_REPORT_STATUS);
  252. usePushOptions = isCapabilityEnabled(CAPABILITY_PUSH_OPTIONS);
  253. super.enableCapabilities();
  254. }
  255. @Override
  256. void readPostCommands(PacketLineIn in) throws IOException {
  257. if (usePushOptions) {
  258. pushOptions = new ArrayList<>(4);
  259. for (;;) {
  260. String option = in.readString();
  261. if (option == PacketLineIn.END) {
  262. break;
  263. }
  264. pushOptions.add(option);
  265. }
  266. }
  267. }
  268. private void service() throws IOException {
  269. if (isBiDirectionalPipe()) {
  270. sendAdvertisedRefs(new PacketLineOutRefAdvertiser(pckOut));
  271. pckOut.flush();
  272. } else
  273. getAdvertisedOrDefaultRefs();
  274. if (hasError())
  275. return;
  276. recvCommands();
  277. if (hasCommands()) {
  278. Throwable unpackError = null;
  279. if (needPack()) {
  280. try {
  281. receivePackAndCheckConnectivity();
  282. } catch (IOException | RuntimeException | Error err) {
  283. unpackError = err;
  284. }
  285. }
  286. try {
  287. if (unpackError == null) {
  288. boolean atomic = isCapabilityEnabled(CAPABILITY_ATOMIC);
  289. setAtomic(atomic);
  290. validateCommands();
  291. if (atomic && anyRejects()) {
  292. failPendingCommands();
  293. }
  294. preReceive.onPreReceive(
  295. this, filterCommands(Result.NOT_ATTEMPTED));
  296. if (atomic && anyRejects()) {
  297. failPendingCommands();
  298. }
  299. executeCommands();
  300. }
  301. } finally {
  302. unlockPack();
  303. }
  304. if (reportStatus) {
  305. sendStatusReport(true, unpackError, new Reporter() {
  306. @Override
  307. void sendString(String s) throws IOException {
  308. pckOut.writeString(s + "\n"); //$NON-NLS-1$
  309. }
  310. });
  311. pckOut.end();
  312. } else if (msgOut != null) {
  313. sendStatusReport(false, unpackError, new Reporter() {
  314. @Override
  315. void sendString(String s) throws IOException {
  316. msgOut.write(Constants.encode(s + "\n")); //$NON-NLS-1$
  317. }
  318. });
  319. }
  320. if (unpackError != null) {
  321. // we already know which exception to throw. Ignore
  322. // potential additional exceptions raised in postReceiveHooks
  323. try {
  324. postReceive.onPostReceive(this, filterCommands(Result.OK));
  325. } catch (Throwable e) {
  326. // empty
  327. }
  328. throw new UnpackException(unpackError);
  329. }
  330. postReceive.onPostReceive(this, filterCommands(Result.OK));
  331. autoGc();
  332. }
  333. }
  334. private void autoGc() {
  335. Repository repo = getRepository();
  336. if (!repo.getConfig().getBoolean(ConfigConstants.CONFIG_RECEIVE_SECTION,
  337. ConfigConstants.CONFIG_KEY_AUTOGC, true)) {
  338. return;
  339. }
  340. repo.autoGC(NullProgressMonitor.INSTANCE);
  341. }
  342. /** {@inheritDoc} */
  343. @Override
  344. protected String getLockMessageProcessName() {
  345. return "jgit receive-pack"; //$NON-NLS-1$
  346. }
  347. }