From 8089c85bcba89464b9b2a32fa948ed85eb367e70 Mon Sep 17 00:00:00 2001 From: Paolo Bonzini Date: Tue, 5 Feb 2008 08:04:18 +0100 Subject: git-commit: add a prepare-commit-msg hook The prepare-commit-msg hook is run whenever a "fresh" commit message is prepared, just before it is shown in the editor (if it is). Its purpose is to modify the commit message in-place. It takes one to three parameters. The first is the name of the file that the commit log message. The second is the source of the commit message, and can be: "message" (if a -m or -F option was given); "template" (if a -t option was given or the configuration option commit.template is set); "merge" (if the commit is a merge or a .git/MERGE_MSG file exists); "squash" (if a .git/SQUASH_MSG file exists); or "commit", followed by a commit SHA1 as the third parameter (if a -c, -C or --amend option was given). If its exit status is non-zero, git-commit will abort. The hook is not suppressed by the --no-verify option, so it should not be used as a replacement for the pre-commit hook. The sample prepare-commit-msg comments out the `Conflicts:` part of a merge's commit message; other examples are commented out, including adding a Signed-off-by line at the bottom of the commit messsage, that the user can then edit or discard altogether. Signed-off-by: Paolo Bonzini Signed-off-by: Junio C Hamano --- t/t7505-prepare-commit-msg-hook.sh | 155 +++++++++++++++++++++++++++++++++++++ 1 file changed, 155 insertions(+) create mode 100755 t/t7505-prepare-commit-msg-hook.sh (limited to 't/t7505-prepare-commit-msg-hook.sh') diff --git a/t/t7505-prepare-commit-msg-hook.sh b/t/t7505-prepare-commit-msg-hook.sh new file mode 100755 index 0000000000..7ddec99a64 --- /dev/null +++ b/t/t7505-prepare-commit-msg-hook.sh @@ -0,0 +1,155 @@ +#!/bin/sh + +test_description='prepare-commit-msg hook' + +. ./test-lib.sh + +test_expect_success 'with no hook' ' + + echo "foo" > file && + git add file && + git commit -m "first" + +' + +# set up fake editor for interactive editing +cat > fake-editor <<'EOF' +#!/bin/sh +exit 0 +EOF +chmod +x fake-editor +FAKE_EDITOR="$(pwd)/fake-editor" +export FAKE_EDITOR + +# now install hook that always succeeds and adds a message +HOOKDIR="$(git rev-parse --git-dir)/hooks" +HOOK="$HOOKDIR/prepare-commit-msg" +mkdir -p "$HOOKDIR" +cat > "$HOOK" <<'EOF' +#!/bin/sh +if test "$2" = commit; then + source=$(git-rev-parse "$3") +else + source=${2-default} +fi +if test "$GIT_EDITOR" = :; then + sed -e "1s/.*/$source (no editor)/" "$1" > msg.tmp +else + sed -e "1s/.*/$source/" "$1" > msg.tmp +fi +mv msg.tmp "$1" +exit 0 +EOF +chmod +x "$HOOK" + +echo dummy template > "$(git rev-parse --git-dir)/template" + +test_expect_success 'with hook (-m)' ' + + echo "more" >> file && + git add file && + git commit -m "more" && + test "`git log -1 --pretty=format:%s`" = "message (no editor)" + +' + +test_expect_success 'with hook (-m editor)' ' + + echo "more" >> file && + git add file && + GIT_EDITOR="$FAKE_EDITOR" git commit -e -m "more more" && + test "`git log -1 --pretty=format:%s`" = message + +' + +test_expect_success 'with hook (-t)' ' + + echo "more" >> file && + git add file && + git commit -t "$(git rev-parse --git-dir)/template" && + test "`git log -1 --pretty=format:%s`" = template + +' + +test_expect_success 'with hook (-F)' ' + + echo "more" >> file && + git add file && + (echo more | git commit -F -) && + test "`git log -1 --pretty=format:%s`" = "message (no editor)" + +' + +test_expect_success 'with hook (-F editor)' ' + + echo "more" >> file && + git add file && + (echo more more | GIT_EDITOR="$FAKE_EDITOR" git commit -e -F -) && + test "`git log -1 --pretty=format:%s`" = message + +' + +test_expect_success 'with hook (-C)' ' + + head=`git rev-parse HEAD` && + echo "more" >> file && + git add file && + git commit -C $head && + test "`git log -1 --pretty=format:%s`" = "$head (no editor)" + +' + +test_expect_success 'with hook (editor)' ' + + echo "more more" >> file && + git add file && + GIT_EDITOR="$FAKE_EDITOR" git commit && + test "`git log -1 --pretty=format:%s`" = default + +' + +test_expect_success 'with hook (--amend)' ' + + head=`git rev-parse HEAD` && + echo "more" >> file && + git add file && + GIT_EDITOR="$FAKE_EDITOR" git commit --amend && + test "`git log -1 --pretty=format:%s`" = "$head" + +' + +test_expect_success 'with hook (-c)' ' + + head=`git rev-parse HEAD` && + echo "more" >> file && + git add file && + GIT_EDITOR="$FAKE_EDITOR" git commit -c $head && + test "`git log -1 --pretty=format:%s`" = "$head" + +' + +cat > "$HOOK" <<'EOF' +#!/bin/sh +exit 1 +EOF + +test_expect_success 'with failing hook' ' + + head=`git rev-parse HEAD` && + echo "more" >> file && + git add file && + ! GIT_EDITOR="$FAKE_EDITOR" git commit -c $head + +' + +test_expect_success 'with failing hook (--no-verify)' ' + + head=`git rev-parse HEAD` && + echo "more" >> file && + git add file && + ! GIT_EDITOR="$FAKE_EDITOR" git commit --no-verify -c $head + +' + + +test_done -- cgit v1.2.3 From 462f8caf248178121ef705dd1b6980eb3b846833 Mon Sep 17 00:00:00 2001 From: Jeff King Date: Wed, 12 Mar 2008 17:42:43 -0400 Subject: t7505: use SHELL_PATH in hook The hook doesn't run properly under Solaris /bin/sh. Let's use the SHELL_PATH the user told us about already instead. Signed-off-by: Jeff King Signed-off-by: Junio C Hamano --- t/t7505-prepare-commit-msg-hook.sh | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 't/t7505-prepare-commit-msg-hook.sh') diff --git a/t/t7505-prepare-commit-msg-hook.sh b/t/t7505-prepare-commit-msg-hook.sh index 7ddec99a64..802aa624d0 100755 --- a/t/t7505-prepare-commit-msg-hook.sh +++ b/t/t7505-prepare-commit-msg-hook.sh @@ -25,8 +25,9 @@ export FAKE_EDITOR HOOKDIR="$(git rev-parse --git-dir)/hooks" HOOK="$HOOKDIR/prepare-commit-msg" mkdir -p "$HOOKDIR" -cat > "$HOOK" <<'EOF' -#!/bin/sh +echo "#!$SHELL_PATH" > "$HOOK" +cat >> "$HOOK" <<'EOF' + if test "$2" = commit; then source=$(git-rev-parse "$3") else -- cgit v1.2.3 From f69e836fab2b634281d92a0d304de4d768e479cc Mon Sep 17 00:00:00 2001 From: Bryan Donlan Date: Sun, 4 May 2008 01:37:59 -0400 Subject: Fix tests breaking when checkout path contains shell metacharacters This fixes the remainder of the issues where the test script itself is at fault for failing when the git checkout path contains whitespace or other shell metacharacters. The majority of git svn tests used the idiom test_expect_success "title" "test script using $svnrepo" These were changed to have the test script in single-quotes: test_expect_success "title" 'test script using "$svnrepo"' which unfortunately makes the patch appear larger than it really is. One consequence of this change is that in the verbose test output the value of $svnrepo (and in some cases other variables, too) is no longer expanded, i.e. previously we saw * expecting success: test script using /path/to/git/t/trash/svnrepo but now it is: * expecting success: test script using "$svnrepo" Signed-off-by: Bryan Donlan Signed-off-by: Junio C Hamano --- t/t7505-prepare-commit-msg-hook.sh | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) (limited to 't/t7505-prepare-commit-msg-hook.sh') diff --git a/t/t7505-prepare-commit-msg-hook.sh b/t/t7505-prepare-commit-msg-hook.sh index 802aa624d0..cd6c7c8342 100755 --- a/t/t7505-prepare-commit-msg-hook.sh +++ b/t/t7505-prepare-commit-msg-hook.sh @@ -18,6 +18,9 @@ cat > fake-editor <<'EOF' exit 0 EOF chmod +x fake-editor + +## Not using test_set_editor here so we can easily ensure the editor variable +## is only set for the editor tests FAKE_EDITOR="$(pwd)/fake-editor" export FAKE_EDITOR @@ -58,7 +61,7 @@ test_expect_success 'with hook (-m editor)' ' echo "more" >> file && git add file && - GIT_EDITOR="$FAKE_EDITOR" git commit -e -m "more more" && + GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit -e -m "more more" && test "`git log -1 --pretty=format:%s`" = message ' @@ -85,7 +88,7 @@ test_expect_success 'with hook (-F editor)' ' echo "more" >> file && git add file && - (echo more more | GIT_EDITOR="$FAKE_EDITOR" git commit -e -F -) && + (echo more more | GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit -e -F -) && test "`git log -1 --pretty=format:%s`" = message ' @@ -104,7 +107,7 @@ test_expect_success 'with hook (editor)' ' echo "more more" >> file && git add file && - GIT_EDITOR="$FAKE_EDITOR" git commit && + GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit && test "`git log -1 --pretty=format:%s`" = default ' @@ -114,7 +117,7 @@ test_expect_success 'with hook (--amend)' ' head=`git rev-parse HEAD` && echo "more" >> file && git add file && - GIT_EDITOR="$FAKE_EDITOR" git commit --amend && + GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --amend && test "`git log -1 --pretty=format:%s`" = "$head" ' @@ -124,7 +127,7 @@ test_expect_success 'with hook (-c)' ' head=`git rev-parse HEAD` && echo "more" >> file && git add file && - GIT_EDITOR="$FAKE_EDITOR" git commit -c $head && + GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit -c $head && test "`git log -1 --pretty=format:%s`" = "$head" ' @@ -139,7 +142,7 @@ test_expect_success 'with failing hook' ' head=`git rev-parse HEAD` && echo "more" >> file && git add file && - ! GIT_EDITOR="$FAKE_EDITOR" git commit -c $head + ! GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit -c $head ' @@ -148,7 +151,7 @@ test_expect_success 'with failing hook (--no-verify)' ' head=`git rev-parse HEAD` && echo "more" >> file && git add file && - ! GIT_EDITOR="$FAKE_EDITOR" git commit --no-verify -c $head + ! GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --no-verify -c $head ' -- cgit v1.2.3 From 47a528ad24185133867ebb5bb7692db2cb8bef39 Mon Sep 17 00:00:00 2001 From: Nanako Shiraishi Date: Wed, 3 Sep 2008 17:59:33 +0900 Subject: tests: use "git xyzzy" form (t7200 - t9001) Converts tests between t7201-t9001. Signed-off-by: Nanako Shiraishi Signed-off-by: Junio C Hamano --- t/t7505-prepare-commit-msg-hook.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 't/t7505-prepare-commit-msg-hook.sh') diff --git a/t/t7505-prepare-commit-msg-hook.sh b/t/t7505-prepare-commit-msg-hook.sh index cd6c7c8342..ff189624d4 100755 --- a/t/t7505-prepare-commit-msg-hook.sh +++ b/t/t7505-prepare-commit-msg-hook.sh @@ -32,7 +32,7 @@ echo "#!$SHELL_PATH" > "$HOOK" cat >> "$HOOK" <<'EOF' if test "$2" = commit; then - source=$(git-rev-parse "$3") + source=$(git rev-parse "$3") else source=${2-default} fi -- cgit v1.2.3