選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

ReceivePack.java 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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_PUSH_OPTIONS;
  46. import static org.eclipse.jgit.transport.GitProtocolConstants.CAPABILITY_REPORT_STATUS;
  47. import java.io.IOException;
  48. import java.io.InputStream;
  49. import java.io.OutputStream;
  50. import java.util.ArrayList;
  51. import java.util.Collections;
  52. import java.util.List;
  53. import org.eclipse.jgit.annotations.Nullable;
  54. import org.eclipse.jgit.errors.UnpackException;
  55. import org.eclipse.jgit.lib.ConfigConstants;
  56. import org.eclipse.jgit.lib.Constants;
  57. import org.eclipse.jgit.lib.NullProgressMonitor;
  58. import org.eclipse.jgit.lib.Repository;
  59. import org.eclipse.jgit.transport.ReceiveCommand.Result;
  60. import org.eclipse.jgit.transport.RefAdvertiser.PacketLineOutRefAdvertiser;
  61. /**
  62. * Implements the server side of a push connection, receiving objects.
  63. */
  64. public class ReceivePack extends BaseReceivePack {
  65. /** Hook to validate the update commands before execution. */
  66. private PreReceiveHook preReceive;
  67. /** Hook to report on the commands after execution. */
  68. private PostReceiveHook postReceive;
  69. /** If {@link BasePackPushConnection#CAPABILITY_REPORT_STATUS} is enabled. */
  70. private boolean reportStatus;
  71. private boolean echoCommandFailures;
  72. /** Whether the client intends to use push options. */
  73. private boolean usePushOptions;
  74. private List<String> pushOptions;
  75. /**
  76. * Create a new pack receive for an open repository.
  77. *
  78. * @param into
  79. * the destination repository.
  80. */
  81. public ReceivePack(final Repository into) {
  82. super(into);
  83. preReceive = PreReceiveHook.NULL;
  84. postReceive = PostReceiveHook.NULL;
  85. }
  86. /**
  87. * Gets an unmodifiable view of the option strings associated with the push.
  88. *
  89. * @return an unmodifiable view of pushOptions, or null (if pushOptions is).
  90. * @since 4.5
  91. */
  92. @Nullable
  93. public List<String> getPushOptions() {
  94. if (isAllowPushOptions() && usePushOptions) {
  95. return Collections.unmodifiableList(pushOptions);
  96. }
  97. // The client doesn't support push options. Return null to
  98. // distinguish this from the case where the client declared support
  99. // for push options and sent an empty list of them.
  100. return null;
  101. }
  102. /**
  103. * Set the push options supplied by the client.
  104. * <p>
  105. * Should only be called if reconstructing an instance without going through
  106. * the normal {@link #recvCommands()} flow.
  107. *
  108. * @param options
  109. * the list of options supplied by the client. The
  110. * {@code ReceivePack} instance takes ownership of this list.
  111. * Callers are encouraged to first create a copy if the list may
  112. * be modified later.
  113. * @since 4.5
  114. */
  115. public void setPushOptions(@Nullable List<String> options) {
  116. usePushOptions = options != null;
  117. pushOptions = options;
  118. }
  119. /** @return the hook invoked before updates occur. */
  120. public PreReceiveHook getPreReceiveHook() {
  121. return preReceive;
  122. }
  123. /**
  124. * Set the hook which is invoked prior to commands being executed.
  125. * <p>
  126. * Only valid commands (those which have no obvious errors according to the
  127. * received input and this instance's configuration) are passed into the
  128. * hook. The hook may mark a command with a result of any value other than
  129. * {@link Result#NOT_ATTEMPTED} to block its execution.
  130. * <p>
  131. * The hook may be called with an empty command collection if the current
  132. * set is completely invalid.
  133. *
  134. * @param h
  135. * the hook instance; may be null to disable the hook.
  136. */
  137. public void setPreReceiveHook(final PreReceiveHook h) {
  138. preReceive = h != null ? h : PreReceiveHook.NULL;
  139. }
  140. /** @return the hook invoked after updates occur. */
  141. public PostReceiveHook getPostReceiveHook() {
  142. return postReceive;
  143. }
  144. /**
  145. * Set the hook which is invoked after commands are executed.
  146. * <p>
  147. * Only successful commands (type is {@link Result#OK}) are passed into the
  148. * hook. The hook may be called with an empty command collection if the
  149. * current set all resulted in an error.
  150. *
  151. * @param h
  152. * the hook instance; may be null to disable the hook.
  153. */
  154. public void setPostReceiveHook(final PostReceiveHook h) {
  155. postReceive = h != null ? h : PostReceiveHook.NULL;
  156. }
  157. /**
  158. * @param echo
  159. * if true this class will report command failures as warning
  160. * messages before sending the command results. This is usually
  161. * not necessary, but may help buggy Git clients that discard the
  162. * errors when all branches fail.
  163. */
  164. public void setEchoCommandFailures(boolean echo) {
  165. echoCommandFailures = echo;
  166. }
  167. /**
  168. * Execute the receive task on the socket.
  169. *
  170. * @param input
  171. * raw input to read client commands and pack data from. Caller
  172. * must ensure the input is buffered, otherwise read performance
  173. * may suffer.
  174. * @param output
  175. * response back to the Git network client. Caller must ensure
  176. * the output is buffered, otherwise write performance may
  177. * suffer.
  178. * @param messages
  179. * secondary "notice" channel to send additional messages out
  180. * through. When run over SSH this should be tied back to the
  181. * standard error channel of the command execution. For most
  182. * other network connections this should be null.
  183. * @throws IOException
  184. */
  185. public void receive(final InputStream input, final OutputStream output,
  186. final OutputStream messages) throws IOException {
  187. init(input, output, messages);
  188. try {
  189. service();
  190. } finally {
  191. try {
  192. close();
  193. } finally {
  194. release();
  195. }
  196. }
  197. }
  198. @Override
  199. protected void enableCapabilities() {
  200. reportStatus = isCapabilityEnabled(CAPABILITY_REPORT_STATUS);
  201. usePushOptions = isCapabilityEnabled(CAPABILITY_PUSH_OPTIONS);
  202. super.enableCapabilities();
  203. }
  204. @Override
  205. void readPostCommands(PacketLineIn in) throws IOException {
  206. if (usePushOptions) {
  207. pushOptions = new ArrayList<>(4);
  208. for (;;) {
  209. String option = in.readString();
  210. if (option == PacketLineIn.END) {
  211. break;
  212. }
  213. pushOptions.add(option);
  214. }
  215. }
  216. }
  217. private void service() throws IOException {
  218. if (isBiDirectionalPipe()) {
  219. sendAdvertisedRefs(new PacketLineOutRefAdvertiser(pckOut));
  220. pckOut.flush();
  221. } else
  222. getAdvertisedOrDefaultRefs();
  223. if (hasError())
  224. return;
  225. recvCommands();
  226. if (hasCommands()) {
  227. Throwable unpackError = null;
  228. if (needPack()) {
  229. try {
  230. receivePackAndCheckConnectivity();
  231. } catch (IOException | RuntimeException | Error err) {
  232. unpackError = err;
  233. }
  234. }
  235. if (unpackError == null) {
  236. boolean atomic = isCapabilityEnabled(CAPABILITY_ATOMIC);
  237. setAtomic(atomic);
  238. validateCommands();
  239. if (atomic && anyRejects())
  240. failPendingCommands();
  241. preReceive.onPreReceive(this, filterCommands(Result.NOT_ATTEMPTED));
  242. if (atomic && anyRejects())
  243. failPendingCommands();
  244. executeCommands();
  245. }
  246. unlockPack();
  247. if (reportStatus) {
  248. if (echoCommandFailures && msgOut != null) {
  249. sendStatusReport(false, unpackError, new Reporter() {
  250. void sendString(final String s) throws IOException {
  251. msgOut.write(Constants.encode(s + "\n")); //$NON-NLS-1$
  252. }
  253. });
  254. msgOut.flush();
  255. try {
  256. Thread.sleep(500);
  257. } catch (InterruptedException wakeUp) {
  258. // Ignore an early wake up.
  259. }
  260. }
  261. sendStatusReport(true, unpackError, new Reporter() {
  262. void sendString(final String s) throws IOException {
  263. pckOut.writeString(s + "\n"); //$NON-NLS-1$
  264. }
  265. });
  266. pckOut.end();
  267. } else if (msgOut != null) {
  268. sendStatusReport(false, unpackError, new Reporter() {
  269. void sendString(final String s) throws IOException {
  270. msgOut.write(Constants.encode(s + "\n")); //$NON-NLS-1$
  271. }
  272. });
  273. }
  274. if (unpackError != null) {
  275. // we already know which exception to throw. Ignore
  276. // potential additional exceptions raised in postReceiveHooks
  277. try {
  278. postReceive.onPostReceive(this, filterCommands(Result.OK));
  279. } catch (Throwable e) {
  280. // empty
  281. }
  282. throw new UnpackException(unpackError);
  283. }
  284. postReceive.onPostReceive(this, filterCommands(Result.OK));
  285. autoGc();
  286. }
  287. }
  288. private void autoGc() {
  289. Repository repo = getRepository();
  290. if (!repo.getConfig().getBoolean(ConfigConstants.CONFIG_RECEIVE_SECTION,
  291. ConfigConstants.CONFIG_KEY_AUTOGC, true)) {
  292. return;
  293. }
  294. repo.autoGC(NullProgressMonitor.INSTANCE);
  295. }
  296. @Override
  297. protected String getLockMessageProcessName() {
  298. return "jgit receive-pack"; //$NON-NLS-1$
  299. }
  300. }