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

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