aboutsummaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit.java7.test
diff options
context:
space:
mode:
authorLaurent Delaigue <laurent.delaigue@obeo.fr>2015-02-23 11:19:45 +0100
committerMatthias Sohn <matthias.sohn@sap.com>2015-03-02 17:45:15 +0100
commitefeb02bf2bed32ef94d5c4891404f551cdc6957f (patch)
tree58b7bfac45098f0ab6a7fce1dccb14d6cc093887 /org.eclipse.jgit.java7.test
parent26fd56f167e6377777e6d46c14779183e4bcb55a (diff)
downloadjgit-efeb02bf2bed32ef94d5c4891404f551cdc6957f.tar.gz
jgit-efeb02bf2bed32ef94d5c4891404f551cdc6957f.zip
Support for the commit-msg hook.
This hook uses the file .git/COMMIT_EDITMSG to receive and potentially modify the commit message. Change-Id: Ibe2faadfb5d3932a5a3da2252d8156c4c04856c7 Signed-off-by: Laurent Delaigue <laurent.delaigue@obeo.fr> Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
Diffstat (limited to 'org.eclipse.jgit.java7.test')
-rw-r--r--org.eclipse.jgit.java7.test/src/org/eclipse/jgit/util/HookTest.java58
1 files changed, 58 insertions, 0 deletions
diff --git a/org.eclipse.jgit.java7.test/src/org/eclipse/jgit/util/HookTest.java b/org.eclipse.jgit.java7.test/src/org/eclipse/jgit/util/HookTest.java
index 0324efca6a..d3cc0ce0d6 100644
--- a/org.eclipse.jgit.java7.test/src/org/eclipse/jgit/util/HookTest.java
+++ b/org.eclipse.jgit.java7.test/src/org/eclipse/jgit/util/HookTest.java
@@ -53,9 +53,11 @@ import java.io.PrintStream;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.AbortedByHookException;
+import org.eclipse.jgit.hooks.CommitMsgHook;
import org.eclipse.jgit.hooks.PreCommitHook;
import org.eclipse.jgit.junit.JGitTestUtil;
import org.eclipse.jgit.junit.RepositoryTestCase;
+import org.eclipse.jgit.revwalk.RevCommit;
import org.junit.Assume;
import org.junit.Test;
@@ -74,6 +76,62 @@ public class HookTest extends RepositoryTestCase {
}
@Test
+ public void testFailedCommitMsgHookBlocksCommit() throws Exception {
+ assumeSupportedPlatform();
+
+ writeHookFile(CommitMsgHook.NAME,
+ "#!/bin/sh\necho \"test\"\n\necho 1>&2 \"stderr\"\nexit 1");
+ Git git = Git.wrap(db);
+ String path = "a.txt";
+ writeTrashFile(path, "content");
+ git.add().addFilepattern(path).call();
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ try {
+ git.commit().setMessage("commit")
+ .setHookOutputStream(new PrintStream(out)).call();
+ fail("expected commit-msg hook to abort commit");
+ } catch (AbortedByHookException e) {
+ assertEquals("unexpected error message from commit-msg hook",
+ "Rejected by \"commit-msg\" hook.\nstderr\n",
+ e.getMessage());
+ assertEquals("unexpected output from commit-msg hook", "test\n",
+ out.toString());
+ }
+ }
+
+ @Test
+ public void testCommitMsgHookReceivesCorrectParameter() throws Exception {
+ assumeSupportedPlatform();
+
+ writeHookFile(CommitMsgHook.NAME,
+ "#!/bin/sh\necho $1\n\necho 1>&2 \"stderr\"\nexit 0");
+ Git git = Git.wrap(db);
+ String path = "a.txt";
+ writeTrashFile(path, "content");
+ git.add().addFilepattern(path).call();
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ git.commit().setMessage("commit")
+ .setHookOutputStream(new PrintStream(out)).call();
+ assertEquals(".git/COMMIT_EDITMSG\n", out.toString("UTF-8"));
+ }
+
+ @Test
+ public void testCommitMsgHookCanModifyCommitMessage() throws Exception {
+ assumeSupportedPlatform();
+
+ writeHookFile(CommitMsgHook.NAME,
+ "#!/bin/sh\necho \"new message\" > $1\nexit 0");
+ Git git = Git.wrap(db);
+ String path = "a.txt";
+ writeTrashFile(path, "content");
+ git.add().addFilepattern(path).call();
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ RevCommit revCommit = git.commit().setMessage("commit")
+ .setHookOutputStream(new PrintStream(out)).call();
+ assertEquals("new message\n", revCommit.getFullMessage());
+ }
+
+ @Test
public void testRunHook() throws Exception {
assumeSupportedPlatform();