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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. /**
  61. * Create a new pack receive for an open repository.
  62. *
  63. * @param into
  64. * the destination repository.
  65. */
  66. public ReceivePack(final Repository into) {
  67. super(into);
  68. preReceive = PreReceiveHook.NULL;
  69. postReceive = PostReceiveHook.NULL;
  70. }
  71. /** @return the hook invoked before updates occur. */
  72. public PreReceiveHook getPreReceiveHook() {
  73. return preReceive;
  74. }
  75. /**
  76. * Set the hook which is invoked prior to commands being executed.
  77. * <p>
  78. * Only valid commands (those which have no obvious errors according to the
  79. * received input and this instance's configuration) are passed into the
  80. * hook. The hook may mark a command with a result of any value other than
  81. * {@link Result#NOT_ATTEMPTED} to block its execution.
  82. * <p>
  83. * The hook may be called with an empty command collection if the current
  84. * set is completely invalid.
  85. *
  86. * @param h
  87. * the hook instance; may be null to disable the hook.
  88. */
  89. public void setPreReceiveHook(final PreReceiveHook h) {
  90. preReceive = h != null ? h : PreReceiveHook.NULL;
  91. }
  92. /** @return the hook invoked after updates occur. */
  93. public PostReceiveHook getPostReceiveHook() {
  94. return postReceive;
  95. }
  96. /**
  97. * Set the hook which is invoked after commands are executed.
  98. * <p>
  99. * Only successful commands (type is {@link Result#OK}) are passed into the
  100. * hook. The hook may be called with an empty command collection if the
  101. * current set all resulted in an error.
  102. *
  103. * @param h
  104. * the hook instance; may be null to disable the hook.
  105. */
  106. public void setPostReceiveHook(final PostReceiveHook h) {
  107. postReceive = h != null ? h : PostReceiveHook.NULL;
  108. }
  109. /**
  110. * Execute the receive task on the socket.
  111. *
  112. * @param input
  113. * raw input to read client commands and pack data from. Caller
  114. * must ensure the input is buffered, otherwise read performance
  115. * may suffer.
  116. * @param output
  117. * response back to the Git network client. Caller must ensure
  118. * the output is buffered, otherwise write performance may
  119. * suffer.
  120. * @param messages
  121. * secondary "notice" channel to send additional messages out
  122. * through. When run over SSH this should be tied back to the
  123. * standard error channel of the command execution. For most
  124. * other network connections this should be null.
  125. * @throws IOException
  126. */
  127. public void receive(final InputStream input, final OutputStream output,
  128. final OutputStream messages) throws IOException {
  129. init(input, output, messages);
  130. try {
  131. service();
  132. } finally {
  133. try {
  134. close();
  135. } finally {
  136. release();
  137. }
  138. }
  139. }
  140. private void service() throws IOException {
  141. if (biDirectionalPipe) {
  142. sendAdvertisedRefs(new PacketLineOutRefAdvertiser(pckOut));
  143. pckOut.flush();
  144. } else
  145. getAdvertisedOrDefaultRefs();
  146. if (hasError())
  147. return;
  148. recvCommands();
  149. if (hasCommands()) {
  150. enableCapabilities();
  151. Throwable unpackError = null;
  152. if (needPack()) {
  153. try {
  154. receivePackAndCheckConnectivity();
  155. } catch (IOException err) {
  156. unpackError = err;
  157. } catch (RuntimeException err) {
  158. unpackError = err;
  159. } catch (Error err) {
  160. unpackError = err;
  161. }
  162. }
  163. if (unpackError == null) {
  164. validateCommands();
  165. preReceive.onPreReceive(this, filterCommands(Result.NOT_ATTEMPTED));
  166. executeCommands();
  167. }
  168. unlockPack();
  169. if (reportStatus) {
  170. sendStatusReport(true, unpackError, new Reporter() {
  171. void sendString(final String s) throws IOException {
  172. pckOut.writeString(s + "\n");
  173. }
  174. });
  175. pckOut.end();
  176. } else if (msgOut != null) {
  177. sendStatusReport(false, unpackError, new Reporter() {
  178. void sendString(final String s) throws IOException {
  179. msgOut.write(Constants.encode(s + "\n"));
  180. }
  181. });
  182. }
  183. postReceive.onPostReceive(this, filterCommands(Result.OK));
  184. if (unpackError != null)
  185. throw new UnpackException(unpackError);
  186. }
  187. }
  188. @Override
  189. protected String getLockMessageProcessName() {
  190. return "jgit receive-pack";
  191. }
  192. }