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

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