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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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_REPORT_STATUS;
  46. import java.io.IOException;
  47. import java.io.InputStream;
  48. import java.io.OutputStream;
  49. import org.eclipse.jgit.errors.UnpackException;
  50. import org.eclipse.jgit.lib.Constants;
  51. import org.eclipse.jgit.lib.Repository;
  52. import org.eclipse.jgit.transport.ReceiveCommand.Result;
  53. import org.eclipse.jgit.transport.RefAdvertiser.PacketLineOutRefAdvertiser;
  54. /**
  55. * Implements the server side of a push connection, receiving objects.
  56. */
  57. public class ReceivePack extends BaseReceivePack {
  58. /** Hook to validate the update commands before execution. */
  59. private PreReceiveHook preReceive;
  60. /** Hook to report on the commands after execution. */
  61. private PostReceiveHook postReceive;
  62. /** If {@link BasePackPushConnection#CAPABILITY_REPORT_STATUS} is enabled. */
  63. private boolean reportStatus;
  64. private boolean echoCommandFailures;
  65. /**
  66. * Create a new pack receive for an open repository.
  67. *
  68. * @param into
  69. * the destination repository.
  70. */
  71. public ReceivePack(final Repository into) {
  72. super(into);
  73. preReceive = PreReceiveHook.NULL;
  74. postReceive = PostReceiveHook.NULL;
  75. }
  76. /** @return the hook invoked before updates occur. */
  77. public PreReceiveHook getPreReceiveHook() {
  78. return preReceive;
  79. }
  80. /**
  81. * Set the hook which is invoked prior to commands being executed.
  82. * <p>
  83. * Only valid commands (those which have no obvious errors according to the
  84. * received input and this instance's configuration) are passed into the
  85. * hook. The hook may mark a command with a result of any value other than
  86. * {@link Result#NOT_ATTEMPTED} to block its execution.
  87. * <p>
  88. * The hook may be called with an empty command collection if the current
  89. * set is completely invalid.
  90. *
  91. * @param h
  92. * the hook instance; may be null to disable the hook.
  93. */
  94. public void setPreReceiveHook(final PreReceiveHook h) {
  95. preReceive = h != null ? h : PreReceiveHook.NULL;
  96. }
  97. /** @return the hook invoked after updates occur. */
  98. public PostReceiveHook getPostReceiveHook() {
  99. return postReceive;
  100. }
  101. /**
  102. * Set the hook which is invoked after commands are executed.
  103. * <p>
  104. * Only successful commands (type is {@link Result#OK}) are passed into the
  105. * hook. The hook may be called with an empty command collection if the
  106. * current set all resulted in an error.
  107. *
  108. * @param h
  109. * the hook instance; may be null to disable the hook.
  110. */
  111. public void setPostReceiveHook(final PostReceiveHook h) {
  112. postReceive = h != null ? h : PostReceiveHook.NULL;
  113. }
  114. /**
  115. * @param echo
  116. * if true this class will report command failures as warning
  117. * messages before sending the command results. This is usually
  118. * not necessary, but may help buggy Git clients that discard the
  119. * errors when all branches fail.
  120. */
  121. public void setEchoCommandFailures(boolean echo) {
  122. echoCommandFailures = echo;
  123. }
  124. /**
  125. * Execute the receive task on the socket.
  126. *
  127. * @param input
  128. * raw input to read client commands and pack data from. Caller
  129. * must ensure the input is buffered, otherwise read performance
  130. * may suffer.
  131. * @param output
  132. * response back to the Git network client. Caller must ensure
  133. * the output is buffered, otherwise write performance may
  134. * suffer.
  135. * @param messages
  136. * secondary "notice" channel to send additional messages out
  137. * through. When run over SSH this should be tied back to the
  138. * standard error channel of the command execution. For most
  139. * other network connections this should be null.
  140. * @throws IOException
  141. */
  142. public void receive(final InputStream input, final OutputStream output,
  143. final OutputStream messages) throws IOException {
  144. init(input, output, messages);
  145. try {
  146. service();
  147. } finally {
  148. try {
  149. close();
  150. } finally {
  151. release();
  152. }
  153. }
  154. }
  155. @Override
  156. protected void enableCapabilities() {
  157. reportStatus = isCapabilityEnabled(CAPABILITY_REPORT_STATUS);
  158. super.enableCapabilities();
  159. }
  160. private void service() throws IOException {
  161. if (isBiDirectionalPipe()) {
  162. sendAdvertisedRefs(new PacketLineOutRefAdvertiser(pckOut));
  163. pckOut.flush();
  164. } else
  165. getAdvertisedOrDefaultRefs();
  166. if (hasError())
  167. return;
  168. recvCommands();
  169. if (hasCommands()) {
  170. enableCapabilities();
  171. Throwable unpackError = null;
  172. if (needPack()) {
  173. try {
  174. receivePackAndCheckConnectivity();
  175. } catch (IOException err) {
  176. unpackError = err;
  177. } catch (RuntimeException err) {
  178. unpackError = err;
  179. } catch (Error err) {
  180. unpackError = err;
  181. }
  182. }
  183. if (unpackError == null) {
  184. boolean atomic = isCapabilityEnabled(CAPABILITY_ATOMIC);
  185. validateCommands();
  186. if (atomic && anyRejects())
  187. failPendingCommands();
  188. preReceive.onPreReceive(this, filterCommands(Result.NOT_ATTEMPTED));
  189. if (atomic && anyRejects())
  190. failPendingCommands();
  191. executeCommands();
  192. }
  193. unlockPack();
  194. if (reportStatus) {
  195. if (echoCommandFailures && msgOut != null) {
  196. sendStatusReport(false, unpackError, new Reporter() {
  197. void sendString(final String s) throws IOException {
  198. msgOut.write(Constants.encode(s + "\n")); //$NON-NLS-1$
  199. }
  200. });
  201. msgOut.flush();
  202. try {
  203. Thread.sleep(500);
  204. } catch (InterruptedException wakeUp) {
  205. // Ignore an early wake up.
  206. }
  207. }
  208. sendStatusReport(true, unpackError, new Reporter() {
  209. void sendString(final String s) throws IOException {
  210. pckOut.writeString(s + "\n"); //$NON-NLS-1$
  211. }
  212. });
  213. pckOut.end();
  214. } else if (msgOut != null) {
  215. sendStatusReport(false, unpackError, new Reporter() {
  216. void sendString(final String s) throws IOException {
  217. msgOut.write(Constants.encode(s + "\n")); //$NON-NLS-1$
  218. }
  219. });
  220. }
  221. if (unpackError != null) {
  222. // we already know which exception to throw. Ignore
  223. // potential additional exceptions raised in postReceiveHooks
  224. try {
  225. postReceive.onPostReceive(this, filterCommands(Result.OK));
  226. } catch (Throwable e) {
  227. // empty
  228. }
  229. throw new UnpackException(unpackError);
  230. }
  231. postReceive.onPostReceive(this, filterCommands(Result.OK));
  232. }
  233. }
  234. @Override
  235. protected String getLockMessageProcessName() {
  236. return "jgit receive-pack"; //$NON-NLS-1$
  237. }
  238. }