1#!/bin/sh
2
3test_description='prepare-commit-msg hook'
4
5. ./test-lib.sh
6
7test_expect_success 'with no hook' '
8
9 echo "foo" > file &&
10 git add file &&
11 git commit -m "first"
12
13'
14
15# set up fake editor for interactive editing
16cat > fake-editor <<'EOF'
17#!/bin/sh
18exit 0
19EOF
20chmod +x fake-editor
21FAKE_EDITOR="$(pwd)/fake-editor"
22export FAKE_EDITOR
23
24# now install hook that always succeeds and adds a message
25HOOKDIR="$(git rev-parse --git-dir)/hooks"
26HOOK="$HOOKDIR/prepare-commit-msg"
27mkdir -p "$HOOKDIR"
28echo "#!$SHELL_PATH" > "$HOOK"
29cat >> "$HOOK" <<'EOF'
30
31if test "$2" = commit; then
32 source=$(git-rev-parse "$3")
33else
34 source=${2-default}
35fi
36if test "$GIT_EDITOR" = :; then
37 sed -e "1s/.*/$source (no editor)/" "$1" > msg.tmp
38else
39 sed -e "1s/.*/$source/" "$1" > msg.tmp
40fi
41mv msg.tmp "$1"
42exit 0
43EOF
44chmod +x "$HOOK"
45
46echo dummy template > "$(git rev-parse --git-dir)/template"
47
48test_expect_success 'with hook (-m)' '
49
50 echo "more" >> file &&
51 git add file &&
52 git commit -m "more" &&
53 test "`git log -1 --pretty=format:%s`" = "message (no editor)"
54
55'
56
57test_expect_success 'with hook (-m editor)' '
58
59 echo "more" >> file &&
60 git add file &&
61 GIT_EDITOR="$FAKE_EDITOR" git commit -e -m "more more" &&
62 test "`git log -1 --pretty=format:%s`" = message
63
64'
65
66test_expect_success 'with hook (-t)' '
67
68 echo "more" >> file &&
69 git add file &&
70 git commit -t "$(git rev-parse --git-dir)/template" &&
71 test "`git log -1 --pretty=format:%s`" = template
72
73'
74
75test_expect_success 'with hook (-F)' '
76
77 echo "more" >> file &&
78 git add file &&
79 (echo more | git commit -F -) &&
80 test "`git log -1 --pretty=format:%s`" = "message (no editor)"
81
82'
83
84test_expect_success 'with hook (-F editor)' '
85
86 echo "more" >> file &&
87 git add file &&
88 (echo more more | GIT_EDITOR="$FAKE_EDITOR" git commit -e -F -) &&
89 test "`git log -1 --pretty=format:%s`" = message
90
91'
92
93test_expect_success 'with hook (-C)' '
94
95 head=`git rev-parse HEAD` &&
96 echo "more" >> file &&
97 git add file &&
98 git commit -C $head &&
99 test "`git log -1 --pretty=format:%s`" = "$head (no editor)"
100
101'
102
103test_expect_success 'with hook (editor)' '
104
105 echo "more more" >> file &&
106 git add file &&
107 GIT_EDITOR="$FAKE_EDITOR" git commit &&
108 test "`git log -1 --pretty=format:%s`" = default
109
110'
111
112test_expect_success 'with hook (--amend)' '
113
114 head=`git rev-parse HEAD` &&
115 echo "more" >> file &&
116 git add file &&
117 GIT_EDITOR="$FAKE_EDITOR" git commit --amend &&
118 test "`git log -1 --pretty=format:%s`" = "$head"
119
120'
121
122test_expect_success 'with hook (-c)' '
123
124 head=`git rev-parse HEAD` &&
125 echo "more" >> file &&
126 git add file &&
127 GIT_EDITOR="$FAKE_EDITOR" git commit -c $head &&
128 test "`git log -1 --pretty=format:%s`" = "$head"
129
130'
131
132cat > "$HOOK" <<'EOF'
133#!/bin/sh
134exit 1
135EOF
136
137test_expect_success 'with failing hook' '
138
139 head=`git rev-parse HEAD` &&
140 echo "more" >> file &&
141 git add file &&
142 ! GIT_EDITOR="$FAKE_EDITOR" git commit -c $head
143
144'
145
146test_expect_success 'with failing hook (--no-verify)' '
147
148 head=`git rev-parse HEAD` &&
149 echo "more" >> file &&
150 git add file &&
151 ! GIT_EDITOR="$FAKE_EDITOR" git commit --no-verify -c $head
152
153'
154
155
156test_done