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.4KB

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