3 test_description='prepare-commit-msg hook'
7 test_expect_success 'with no hook' '
15 # set up fake editor for interactive editing
16 cat > fake-editor <<'EOF'
22 ## Not using test_set_editor here so we can easily ensure the editor variable
23 ## is only set for the editor tests
24 FAKE_EDITOR="$(pwd)/fake-editor"
27 # now install hook that always succeeds and adds a message
28 HOOKDIR="$(git rev-parse --git-dir)/hooks"
29 HOOK="$HOOKDIR/prepare-commit-msg"
31 echo "#!$SHELL_PATH" > "$HOOK"
32 cat >> "$HOOK" <<'EOF'
34 if test "$2" = commit; then
35 source=$(git rev-parse "$3")
39 if test "$GIT_EDITOR" = :; then
40 sed -e "1s/.*/$source (no editor)/" "$1" > msg.tmp
42 sed -e "1s/.*/$source/" "$1" > msg.tmp
49 echo dummy template > "$(git rev-parse --git-dir)/template"
51 test_expect_success 'with hook (-m)' '
53 echo "more" >> file &&
55 git commit -m "more" &&
56 test "`git log -1 --pretty=format:%s`" = "message (no editor)"
60 test_expect_success 'with hook (-m editor)' '
62 echo "more" >> file &&
64 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit -e -m "more more" &&
65 test "`git log -1 --pretty=format:%s`" = message
69 test_expect_success 'with hook (-t)' '
71 echo "more" >> file &&
73 git commit -t "$(git rev-parse --git-dir)/template" &&
74 test "`git log -1 --pretty=format:%s`" = template
78 test_expect_success 'with hook (-F)' '
80 echo "more" >> file &&
82 (echo more | git commit -F -) &&
83 test "`git log -1 --pretty=format:%s`" = "message (no editor)"
87 test_expect_success 'with hook (-F editor)' '
89 echo "more" >> file &&
91 (echo more more | GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit -e -F -) &&
92 test "`git log -1 --pretty=format:%s`" = message
96 test_expect_success 'with hook (-C)' '
98 head=`git rev-parse HEAD` &&
99 echo "more" >> file &&
101 git commit -C $head &&
102 test "`git log -1 --pretty=format:%s`" = "$head (no editor)"
106 test_expect_success 'with hook (editor)' '
108 echo "more more" >> file &&
110 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit &&
111 test "`git log -1 --pretty=format:%s`" = default
115 test_expect_success 'with hook (--amend)' '
117 head=`git rev-parse HEAD` &&
118 echo "more" >> file &&
120 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --amend &&
121 test "`git log -1 --pretty=format:%s`" = "$head"
125 test_expect_success 'with hook (-c)' '
127 head=`git rev-parse HEAD` &&
128 echo "more" >> file &&
130 GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit -c $head &&
131 test "`git log -1 --pretty=format:%s`" = "$head"
135 test_expect_success 'with hook (merge)' '
137 test_when_finished "git checkout -f master" &&
138 git checkout -B other HEAD@{1} &&
139 echo "more" >>file &&
141 git commit -m other &&
143 git merge --no-ff other &&
144 test "`git log -1 --pretty=format:%s`" = "merge (no editor)"
147 test_expect_success 'with hook and editor (merge)' '
149 test_when_finished "git checkout -f master" &&
150 git checkout -B other HEAD@{1} &&
151 echo "more" >>file &&
153 git commit -m other &&
155 env GIT_EDITOR="\"\$FAKE_EDITOR\"" git merge --no-ff -e other &&
156 test "`git log -1 --pretty=format:%s`" = "merge"
159 cat > "$HOOK" <<'EOF'
164 test_expect_success 'with failing hook' '
166 test_when_finished "git checkout -f master" &&
167 head=`git rev-parse HEAD` &&
168 echo "more" >> file &&
170 test_must_fail env GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit -c $head
174 test_expect_success 'with failing hook (--no-verify)' '
176 test_when_finished "git checkout -f master" &&
177 head=`git rev-parse HEAD` &&
178 echo "more" >> file &&
180 test_must_fail env GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --no-verify -c $head
184 test_expect_success 'with failing hook (merge)' '
186 test_when_finished "git checkout -f master" &&
187 git checkout -B other HEAD@{1} &&
188 echo "more" >> file &&
191 git commit -m other &&
192 write_script "$HOOK" <<-EOF &&
196 test_must_fail git merge --no-ff other