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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * Copyright (C) 2014 Matthias Sohn <matthias.sohn@sap.com>
  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.util;
  44. import static org.junit.Assert.assertEquals;
  45. import static org.junit.Assert.assertNull;
  46. import static org.junit.Assert.fail;
  47. import java.io.ByteArrayOutputStream;
  48. import java.io.File;
  49. import java.io.IOException;
  50. import java.io.PrintStream;
  51. import org.eclipse.jgit.api.Git;
  52. import org.eclipse.jgit.api.errors.AbortedByHookException;
  53. import org.eclipse.jgit.hooks.CommitMsgHook;
  54. import org.eclipse.jgit.hooks.PreCommitHook;
  55. import org.eclipse.jgit.junit.JGitTestUtil;
  56. import org.eclipse.jgit.junit.RepositoryTestCase;
  57. import org.eclipse.jgit.revwalk.RevCommit;
  58. import org.junit.Assume;
  59. import org.junit.Test;
  60. public class HookTest extends RepositoryTestCase {
  61. @Test
  62. public void testFindHook() throws Exception {
  63. assumeSupportedPlatform();
  64. assertNull("no hook should be installed",
  65. FS.DETECTED.findHook(db, PreCommitHook.NAME));
  66. File hookFile = writeHookFile(PreCommitHook.NAME,
  67. "#!/bin/bash\necho \"test $1 $2\"");
  68. assertEquals("expected to find pre-commit hook", hookFile,
  69. FS.DETECTED.findHook(db, PreCommitHook.NAME));
  70. }
  71. @Test
  72. public void testFailedCommitMsgHookBlocksCommit() throws Exception {
  73. assumeSupportedPlatform();
  74. writeHookFile(CommitMsgHook.NAME,
  75. "#!/bin/sh\necho \"test\"\n\necho 1>&2 \"stderr\"\nexit 1");
  76. Git git = Git.wrap(db);
  77. String path = "a.txt";
  78. writeTrashFile(path, "content");
  79. git.add().addFilepattern(path).call();
  80. ByteArrayOutputStream out = new ByteArrayOutputStream();
  81. try {
  82. git.commit().setMessage("commit")
  83. .setHookOutputStream(new PrintStream(out)).call();
  84. fail("expected commit-msg hook to abort commit");
  85. } catch (AbortedByHookException e) {
  86. assertEquals("unexpected error message from commit-msg hook",
  87. "Rejected by \"commit-msg\" hook.\nstderr\n",
  88. e.getMessage());
  89. assertEquals("unexpected output from commit-msg hook", "test\n",
  90. out.toString());
  91. }
  92. }
  93. @Test
  94. public void testCommitMsgHookReceivesCorrectParameter() throws Exception {
  95. assumeSupportedPlatform();
  96. writeHookFile(CommitMsgHook.NAME,
  97. "#!/bin/sh\necho $1\n\necho 1>&2 \"stderr\"\nexit 0");
  98. Git git = Git.wrap(db);
  99. String path = "a.txt";
  100. writeTrashFile(path, "content");
  101. git.add().addFilepattern(path).call();
  102. ByteArrayOutputStream out = new ByteArrayOutputStream();
  103. git.commit().setMessage("commit")
  104. .setHookOutputStream(new PrintStream(out)).call();
  105. assertEquals(".git/COMMIT_EDITMSG\n",
  106. out.toString("UTF-8"));
  107. }
  108. @Test
  109. public void testCommitMsgHookCanModifyCommitMessage() throws Exception {
  110. assumeSupportedPlatform();
  111. writeHookFile(CommitMsgHook.NAME,
  112. "#!/bin/sh\necho \"new message\" > $1\nexit 0");
  113. Git git = Git.wrap(db);
  114. String path = "a.txt";
  115. writeTrashFile(path, "content");
  116. git.add().addFilepattern(path).call();
  117. ByteArrayOutputStream out = new ByteArrayOutputStream();
  118. RevCommit revCommit = git.commit().setMessage("commit")
  119. .setHookOutputStream(new PrintStream(out)).call();
  120. assertEquals("new message\n", revCommit.getFullMessage());
  121. }
  122. @Test
  123. public void testRunHook() throws Exception {
  124. assumeSupportedPlatform();
  125. writeHookFile(PreCommitHook.NAME,
  126. "#!/bin/sh\necho \"test $1 $2\"\nread INPUT\necho $INPUT\necho 1>&2 \"stderr\"");
  127. ByteArrayOutputStream out = new ByteArrayOutputStream();
  128. ByteArrayOutputStream err = new ByteArrayOutputStream();
  129. ProcessResult res = FS.DETECTED.runHookIfPresent(db,
  130. PreCommitHook.NAME,
  131. new String[] {
  132. "arg1", "arg2" },
  133. new PrintStream(out), new PrintStream(err), "stdin");
  134. assertEquals("unexpected hook output", "test arg1 arg2\nstdin\n",
  135. out.toString("UTF-8"));
  136. assertEquals("unexpected output on stderr stream", "stderr\n",
  137. err.toString("UTF-8"));
  138. assertEquals("unexpected exit code", 0, res.getExitCode());
  139. assertEquals("unexpected process status", ProcessResult.Status.OK,
  140. res.getStatus());
  141. }
  142. @Test
  143. public void testFailedPreCommitHookBlockCommit() throws Exception {
  144. assumeSupportedPlatform();
  145. writeHookFile(PreCommitHook.NAME,
  146. "#!/bin/sh\necho \"test\"\n\necho 1>&2 \"stderr\"\nexit 1");
  147. Git git = Git.wrap(db);
  148. String path = "a.txt";
  149. writeTrashFile(path, "content");
  150. git.add().addFilepattern(path).call();
  151. ByteArrayOutputStream out = new ByteArrayOutputStream();
  152. try {
  153. git.commit().setMessage("commit")
  154. .setHookOutputStream(new PrintStream(out)).call();
  155. fail("expected pre-commit hook to abort commit");
  156. } catch (AbortedByHookException e) {
  157. assertEquals("unexpected error message from pre-commit hook",
  158. "Rejected by \"pre-commit\" hook.\nstderr\n",
  159. e.getMessage());
  160. assertEquals("unexpected output from pre-commit hook", "test\n",
  161. out.toString());
  162. }
  163. }
  164. private File writeHookFile(final String name, final String data)
  165. throws IOException {
  166. File path = new File(db.getWorkTree() + "/.git/hooks/", name);
  167. JGitTestUtil.write(path, data);
  168. FS.DETECTED.setExecute(path, true);
  169. return path;
  170. }
  171. private void assumeSupportedPlatform() {
  172. Assume.assumeTrue(FS.DETECTED instanceof FS_POSIX
  173. || FS.DETECTED instanceof FS_Win32_Cygwin);
  174. }
  175. }