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