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

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