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.

GitHook.java 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. * Copyright (C) 2015 Obeo.
  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.hooks;
  44. import static java.nio.charset.StandardCharsets.UTF_8;
  45. import java.io.ByteArrayOutputStream;
  46. import java.io.IOException;
  47. import java.io.PrintStream;
  48. import java.io.UnsupportedEncodingException;
  49. import java.util.concurrent.Callable;
  50. import org.bouncycastle.util.io.TeeOutputStream;
  51. import org.eclipse.jgit.api.errors.AbortedByHookException;
  52. import org.eclipse.jgit.lib.Repository;
  53. import org.eclipse.jgit.util.FS;
  54. import org.eclipse.jgit.util.ProcessResult;
  55. /**
  56. * Git can fire off custom scripts when certain important actions occur. These
  57. * custom scripts are called "hooks". There are two groups of hooks: client-side
  58. * (that run on local operations such as committing and merging), and
  59. * server-side (that run on network operations such as receiving pushed
  60. * commits). This is the abstract super-class of the different hook
  61. * implementations in JGit.
  62. *
  63. * @param <T>
  64. * the return type which is expected from {@link #call()}
  65. * @see <a href="http://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks">Git
  66. * Hooks on the git-scm official site</a>
  67. * @since 4.0
  68. */
  69. abstract class GitHook<T> implements Callable<T> {
  70. private final Repository repo;
  71. /**
  72. * The output stream to be used by the hook.
  73. */
  74. protected final PrintStream outputStream;
  75. /**
  76. * The error stream to be used by the hook.
  77. */
  78. protected final PrintStream errorStream;
  79. /**
  80. * Constructor for GitHook.
  81. * <p>
  82. * This constructor will use stderr for the error stream.
  83. * </p>
  84. *
  85. * @param repo
  86. * a {@link org.eclipse.jgit.lib.Repository} object.
  87. * @param outputStream
  88. * The output stream the hook must use. {@code null} is allowed,
  89. * in which case the hook will use {@code System.out}.
  90. */
  91. protected GitHook(Repository repo, PrintStream outputStream) {
  92. this(repo, outputStream, null);
  93. }
  94. /**
  95. * Constructor for GitHook
  96. *
  97. * @param repo
  98. * a {@link org.eclipse.jgit.lib.Repository} object.
  99. * @param outputStream
  100. * The output stream the hook must use. {@code null} is allowed,
  101. * in which case the hook will use {@code System.out}.
  102. * @param errorStream
  103. * The error stream the hook must use. {@code null} is allowed,
  104. * in which case the hook will use {@code System.err}.
  105. */
  106. protected GitHook(Repository repo, PrintStream outputStream,
  107. PrintStream errorStream) {
  108. this.repo = repo;
  109. this.outputStream = outputStream;
  110. this.errorStream = errorStream;
  111. }
  112. /**
  113. * {@inheritDoc}
  114. * <p>
  115. * Run the hook.
  116. */
  117. @Override
  118. public abstract T call() throws IOException, AbortedByHookException;
  119. /**
  120. * Get name of the hook
  121. *
  122. * @return The name of the hook, which must not be {@code null}.
  123. */
  124. public abstract String getHookName();
  125. /**
  126. * Get the repository
  127. *
  128. * @return The repository.
  129. */
  130. protected Repository getRepository() {
  131. return repo;
  132. }
  133. /**
  134. * Override this method when needed to provide relevant parameters to the
  135. * underlying hook script. The default implementation returns an empty
  136. * array.
  137. *
  138. * @return The parameters the hook receives.
  139. */
  140. protected String[] getParameters() {
  141. return new String[0];
  142. }
  143. /**
  144. * Override to provide relevant arguments via stdin to the underlying hook
  145. * script. The default implementation returns {@code null}.
  146. *
  147. * @return The parameters the hook receives.
  148. */
  149. protected String getStdinArgs() {
  150. return null;
  151. }
  152. /**
  153. * Get output stream
  154. *
  155. * @return The output stream the hook must use. Never {@code null},
  156. * {@code System.out} is returned by default.
  157. */
  158. protected PrintStream getOutputStream() {
  159. return outputStream == null ? System.out : outputStream;
  160. }
  161. /**
  162. * Get error stream
  163. *
  164. * @return The error stream the hook must use. Never {@code null},
  165. * {@code System.err} is returned by default.
  166. */
  167. protected PrintStream getErrorStream() {
  168. return errorStream == null ? System.err : errorStream;
  169. }
  170. /**
  171. * Runs the hook, without performing any validity checks.
  172. *
  173. * @throws org.eclipse.jgit.api.errors.AbortedByHookException
  174. * If the underlying hook script exited with non-zero.
  175. */
  176. protected void doRun() throws AbortedByHookException {
  177. final ByteArrayOutputStream errorByteArray = new ByteArrayOutputStream();
  178. final TeeOutputStream stderrStream = new TeeOutputStream(errorByteArray,
  179. getErrorStream());
  180. PrintStream hookErrRedirect = null;
  181. try {
  182. hookErrRedirect = new PrintStream(stderrStream, false,
  183. UTF_8.name());
  184. } catch (UnsupportedEncodingException e) {
  185. // UTF-8 is guaranteed to be available
  186. }
  187. Repository repository = getRepository();
  188. FS fs = repository.getFS();
  189. if (fs == null) {
  190. fs = FS.DETECTED;
  191. }
  192. ProcessResult result = fs.runHookIfPresent(repository, getHookName(),
  193. getParameters(), getOutputStream(), hookErrRedirect,
  194. getStdinArgs());
  195. if (result.isExecutedWithError()) {
  196. throw new AbortedByHookException(
  197. new String(errorByteArray.toByteArray(), UTF_8),
  198. getHookName(), result.getExitCode());
  199. }
  200. }
  201. /**
  202. * Check whether a 'native' (i.e. script) hook is installed in the
  203. * repository.
  204. *
  205. * @return whether a native hook script is installed in the repository.
  206. * @since 4.11
  207. */
  208. public boolean isNativeHookPresent() {
  209. FS fs = getRepository().getFS();
  210. if (fs == null) {
  211. fs = FS.DETECTED;
  212. }
  213. return fs.findHook(getRepository(), getHookName()) != null;
  214. }
  215. }