2008-02-05 08:04:18 +01:00
|
|
|
#!/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
|
2008-05-04 07:37:59 +02:00
|
|
|
|
|
|
|
## Not using test_set_editor here so we can easily ensure the editor variable
|
|
|
|
## is only set for the editor tests
|
2008-02-05 08:04:18 +01:00
|
|
|
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"
|
2008-03-12 22:42:43 +01:00
|
|
|
echo "#!$SHELL_PATH" > "$HOOK"
|
|
|
|
cat >> "$HOOK" <<'EOF'
|
|
|
|
|
2008-02-05 08:04:18 +01:00
|
|
|
if test "$2" = commit; then
|
2008-09-03 10:59:33 +02:00
|
|
|
source=$(git rev-parse "$3")
|
2008-02-05 08:04:18 +01:00
|
|
|
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" &&
|
2016-01-08 12:06:23 +01:00
|
|
|
test "$(git log -1 --pretty=format:%s)" = "message (no editor)"
|
2008-02-05 08:04:18 +01:00
|
|
|
|
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success 'with hook (-m editor)' '
|
|
|
|
|
|
|
|
echo "more" >> file &&
|
|
|
|
git add file &&
|
2008-05-04 07:37:59 +02:00
|
|
|
GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit -e -m "more more" &&
|
2016-01-08 12:06:23 +01:00
|
|
|
test "$(git log -1 --pretty=format:%s)" = message
|
2008-02-05 08:04:18 +01:00
|
|
|
|
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success 'with hook (-t)' '
|
|
|
|
|
|
|
|
echo "more" >> file &&
|
|
|
|
git add file &&
|
|
|
|
git commit -t "$(git rev-parse --git-dir)/template" &&
|
2016-01-08 12:06:23 +01:00
|
|
|
test "$(git log -1 --pretty=format:%s)" = template
|
2008-02-05 08:04:18 +01:00
|
|
|
|
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success 'with hook (-F)' '
|
|
|
|
|
|
|
|
echo "more" >> file &&
|
|
|
|
git add file &&
|
|
|
|
(echo more | git commit -F -) &&
|
2016-01-08 12:06:23 +01:00
|
|
|
test "$(git log -1 --pretty=format:%s)" = "message (no editor)"
|
2008-02-05 08:04:18 +01:00
|
|
|
|
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success 'with hook (-F editor)' '
|
|
|
|
|
|
|
|
echo "more" >> file &&
|
|
|
|
git add file &&
|
2008-05-04 07:37:59 +02:00
|
|
|
(echo more more | GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit -e -F -) &&
|
2016-01-08 12:06:23 +01:00
|
|
|
test "$(git log -1 --pretty=format:%s)" = message
|
2008-02-05 08:04:18 +01:00
|
|
|
|
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success 'with hook (-C)' '
|
|
|
|
|
2016-01-08 12:06:23 +01:00
|
|
|
head=$(git rev-parse HEAD) &&
|
2008-02-05 08:04:18 +01:00
|
|
|
echo "more" >> file &&
|
|
|
|
git add file &&
|
|
|
|
git commit -C $head &&
|
2016-01-08 12:06:23 +01:00
|
|
|
test "$(git log -1 --pretty=format:%s)" = "$head (no editor)"
|
2008-02-05 08:04:18 +01:00
|
|
|
|
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success 'with hook (editor)' '
|
|
|
|
|
|
|
|
echo "more more" >> file &&
|
|
|
|
git add file &&
|
2008-05-04 07:37:59 +02:00
|
|
|
GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit &&
|
2016-01-08 12:06:23 +01:00
|
|
|
test "$(git log -1 --pretty=format:%s)" = default
|
2008-02-05 08:04:18 +01:00
|
|
|
|
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success 'with hook (--amend)' '
|
|
|
|
|
2016-01-08 12:06:23 +01:00
|
|
|
head=$(git rev-parse HEAD) &&
|
2008-02-05 08:04:18 +01:00
|
|
|
echo "more" >> file &&
|
|
|
|
git add file &&
|
2008-05-04 07:37:59 +02:00
|
|
|
GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --amend &&
|
2016-01-08 12:06:23 +01:00
|
|
|
test "$(git log -1 --pretty=format:%s)" = "$head"
|
2008-02-05 08:04:18 +01:00
|
|
|
|
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success 'with hook (-c)' '
|
|
|
|
|
2016-01-08 12:06:23 +01:00
|
|
|
head=$(git rev-parse HEAD) &&
|
2008-02-05 08:04:18 +01:00
|
|
|
echo "more" >> file &&
|
|
|
|
git add file &&
|
2008-05-04 07:37:59 +02:00
|
|
|
GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit -c $head &&
|
2016-01-08 12:06:23 +01:00
|
|
|
test "$(git log -1 --pretty=format:%s)" = "$head"
|
2008-02-05 08:04:18 +01:00
|
|
|
|
|
|
|
'
|
|
|
|
|
2011-02-15 02:07:50 +01:00
|
|
|
test_expect_success 'with hook (merge)' '
|
|
|
|
|
2014-03-18 11:00:55 +01:00
|
|
|
test_when_finished "git checkout -f master" &&
|
|
|
|
git checkout -B other HEAD@{1} &&
|
|
|
|
echo "more" >>file &&
|
|
|
|
git add file &&
|
|
|
|
git commit -m other &&
|
|
|
|
git checkout - &&
|
|
|
|
git merge --no-ff other &&
|
2016-01-08 12:06:23 +01:00
|
|
|
test "$(git log -1 --pretty=format:%s)" = "merge (no editor)"
|
2014-03-18 11:00:55 +01:00
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success 'with hook and editor (merge)' '
|
|
|
|
|
|
|
|
test_when_finished "git checkout -f master" &&
|
|
|
|
git checkout -B other HEAD@{1} &&
|
|
|
|
echo "more" >>file &&
|
2011-02-15 02:07:50 +01:00
|
|
|
git add file &&
|
|
|
|
git commit -m other &&
|
|
|
|
git checkout - &&
|
2014-03-18 11:00:55 +01:00
|
|
|
env GIT_EDITOR="\"\$FAKE_EDITOR\"" git merge --no-ff -e other &&
|
2016-01-08 12:06:23 +01:00
|
|
|
test "$(git log -1 --pretty=format:%s)" = "merge"
|
2011-02-15 02:07:50 +01:00
|
|
|
'
|
|
|
|
|
2008-02-05 08:04:18 +01:00
|
|
|
cat > "$HOOK" <<'EOF'
|
|
|
|
#!/bin/sh
|
|
|
|
exit 1
|
|
|
|
EOF
|
|
|
|
|
|
|
|
test_expect_success 'with failing hook' '
|
|
|
|
|
2014-03-18 11:00:55 +01:00
|
|
|
test_when_finished "git checkout -f master" &&
|
2016-01-08 12:06:23 +01:00
|
|
|
head=$(git rev-parse HEAD) &&
|
2008-02-05 08:04:18 +01:00
|
|
|
echo "more" >> file &&
|
|
|
|
git add file &&
|
2014-03-10 19:49:32 +01:00
|
|
|
test_must_fail env GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit -c $head
|
2008-02-05 08:04:18 +01:00
|
|
|
|
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success 'with failing hook (--no-verify)' '
|
|
|
|
|
2014-03-18 11:00:55 +01:00
|
|
|
test_when_finished "git checkout -f master" &&
|
2016-01-08 12:06:23 +01:00
|
|
|
head=$(git rev-parse HEAD) &&
|
2008-02-05 08:04:18 +01:00
|
|
|
echo "more" >> file &&
|
|
|
|
git add file &&
|
2014-03-10 19:49:32 +01:00
|
|
|
test_must_fail env GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --no-verify -c $head
|
2008-02-05 08:04:18 +01:00
|
|
|
|
|
|
|
'
|
|
|
|
|
2013-01-02 19:42:50 +01:00
|
|
|
test_expect_success 'with failing hook (merge)' '
|
|
|
|
|
2014-03-18 11:00:55 +01:00
|
|
|
test_when_finished "git checkout -f master" &&
|
2013-01-02 19:42:50 +01:00
|
|
|
git checkout -B other HEAD@{1} &&
|
|
|
|
echo "more" >> file &&
|
|
|
|
git add file &&
|
|
|
|
rm -f "$HOOK" &&
|
|
|
|
git commit -m other &&
|
2014-03-10 19:49:31 +01:00
|
|
|
write_script "$HOOK" <<-EOF &&
|
2013-01-02 19:42:50 +01:00
|
|
|
exit 1
|
|
|
|
EOF
|
|
|
|
git checkout - &&
|
2014-03-18 11:00:55 +01:00
|
|
|
test_must_fail git merge --no-ff other
|
2013-01-02 19:42:50 +01:00
|
|
|
|
|
|
|
'
|
2008-02-05 08:04:18 +01:00
|
|
|
|
|
|
|
test_done
|