]>
Commit | Line | Data |
---|---|---|
8089c85b PB |
1 | #!/bin/sh |
2 | ||
3 | test_description='prepare-commit-msg hook' | |
4 | ||
5 | . ./test-lib.sh | |
6 | ||
7 | test_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 | |
16 | cat > fake-editor <<'EOF' | |
17 | #!/bin/sh | |
18 | exit 0 | |
19 | EOF | |
20 | chmod +x fake-editor | |
f69e836f BD |
21 | |
22 | ## Not using test_set_editor here so we can easily ensure the editor variable | |
23 | ## is only set for the editor tests | |
8089c85b PB |
24 | FAKE_EDITOR="$(pwd)/fake-editor" |
25 | export FAKE_EDITOR | |
26 | ||
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" | |
30 | mkdir -p "$HOOKDIR" | |
462f8caf JK |
31 | echo "#!$SHELL_PATH" > "$HOOK" |
32 | cat >> "$HOOK" <<'EOF' | |
33 | ||
4f8cbf2b PW |
34 | if test "$2" = commit |
35 | then | |
36 | source=$(git rev-parse "$3") | |
8089c85b | 37 | else |
4f8cbf2b | 38 | source=${2-default} |
8089c85b | 39 | fi |
4f8cbf2b PW |
40 | if test "$GIT_EDITOR" = : |
41 | then | |
42 | sed -e "1s/.*/$source (no editor)/" "$1" >msg.tmp | |
8089c85b | 43 | else |
4f8cbf2b | 44 | sed -e "1s/.*/$source/" "$1" >msg.tmp |
8089c85b PB |
45 | fi |
46 | mv msg.tmp "$1" | |
47 | exit 0 | |
48 | EOF | |
49 | chmod +x "$HOOK" | |
50 | ||
51 | echo dummy template > "$(git rev-parse --git-dir)/template" | |
52 | ||
53 | test_expect_success 'with hook (-m)' ' | |
54 | ||
55 | echo "more" >> file && | |
56 | git add file && | |
57 | git commit -m "more" && | |
0c923256 | 58 | test "$(git log -1 --pretty=format:%s)" = "message (no editor)" |
8089c85b PB |
59 | |
60 | ' | |
61 | ||
62 | test_expect_success 'with hook (-m editor)' ' | |
63 | ||
64 | echo "more" >> file && | |
65 | git add file && | |
f69e836f | 66 | GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit -e -m "more more" && |
0c923256 | 67 | test "$(git log -1 --pretty=format:%s)" = message |
8089c85b PB |
68 | |
69 | ' | |
70 | ||
71 | test_expect_success 'with hook (-t)' ' | |
72 | ||
73 | echo "more" >> file && | |
74 | git add file && | |
75 | git commit -t "$(git rev-parse --git-dir)/template" && | |
0c923256 | 76 | test "$(git log -1 --pretty=format:%s)" = template |
8089c85b PB |
77 | |
78 | ' | |
79 | ||
80 | test_expect_success 'with hook (-F)' ' | |
81 | ||
82 | echo "more" >> file && | |
83 | git add file && | |
84 | (echo more | git commit -F -) && | |
0c923256 | 85 | test "$(git log -1 --pretty=format:%s)" = "message (no editor)" |
8089c85b PB |
86 | |
87 | ' | |
88 | ||
89 | test_expect_success 'with hook (-F editor)' ' | |
90 | ||
91 | echo "more" >> file && | |
92 | git add file && | |
f69e836f | 93 | (echo more more | GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit -e -F -) && |
0c923256 | 94 | test "$(git log -1 --pretty=format:%s)" = message |
8089c85b PB |
95 | |
96 | ' | |
97 | ||
98 | test_expect_success 'with hook (-C)' ' | |
99 | ||
0c923256 | 100 | head=$(git rev-parse HEAD) && |
8089c85b PB |
101 | echo "more" >> file && |
102 | git add file && | |
103 | git commit -C $head && | |
0c923256 | 104 | test "$(git log -1 --pretty=format:%s)" = "$head (no editor)" |
8089c85b PB |
105 | |
106 | ' | |
107 | ||
108 | test_expect_success 'with hook (editor)' ' | |
109 | ||
110 | echo "more more" >> file && | |
111 | git add file && | |
f69e836f | 112 | GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit && |
0c923256 | 113 | test "$(git log -1 --pretty=format:%s)" = default |
8089c85b PB |
114 | |
115 | ' | |
116 | ||
117 | test_expect_success 'with hook (--amend)' ' | |
118 | ||
0c923256 | 119 | head=$(git rev-parse HEAD) && |
8089c85b PB |
120 | echo "more" >> file && |
121 | git add file && | |
f69e836f | 122 | GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --amend && |
0c923256 | 123 | test "$(git log -1 --pretty=format:%s)" = "$head" |
8089c85b PB |
124 | |
125 | ' | |
126 | ||
127 | test_expect_success 'with hook (-c)' ' | |
128 | ||
0c923256 | 129 | head=$(git rev-parse HEAD) && |
8089c85b PB |
130 | echo "more" >> file && |
131 | git add file && | |
f69e836f | 132 | GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit -c $head && |
0c923256 | 133 | test "$(git log -1 --pretty=format:%s)" = "$head" |
8089c85b PB |
134 | |
135 | ' | |
136 | ||
65969d43 JS |
137 | test_expect_success 'with hook (merge)' ' |
138 | ||
1fc4f97d BP |
139 | test_when_finished "git checkout -f master" && |
140 | git checkout -B other HEAD@{1} && | |
141 | echo "more" >>file && | |
142 | git add file && | |
143 | git commit -m other && | |
144 | git checkout - && | |
145 | git merge --no-ff other && | |
0c923256 | 146 | test "$(git log -1 --pretty=format:%s)" = "merge (no editor)" |
1fc4f97d BP |
147 | ' |
148 | ||
149 | test_expect_success 'with hook and editor (merge)' ' | |
150 | ||
151 | test_when_finished "git checkout -f master" && | |
152 | git checkout -B other HEAD@{1} && | |
153 | echo "more" >>file && | |
65969d43 JS |
154 | git add file && |
155 | git commit -m other && | |
156 | git checkout - && | |
1fc4f97d | 157 | env GIT_EDITOR="\"\$FAKE_EDITOR\"" git merge --no-ff -e other && |
0c923256 | 158 | test "$(git log -1 --pretty=format:%s)" = "merge" |
65969d43 JS |
159 | ' |
160 | ||
8089c85b PB |
161 | cat > "$HOOK" <<'EOF' |
162 | #!/bin/sh | |
163 | exit 1 | |
164 | EOF | |
165 | ||
166 | test_expect_success 'with failing hook' ' | |
167 | ||
1fc4f97d | 168 | test_when_finished "git checkout -f master" && |
0c923256 | 169 | head=$(git rev-parse HEAD) && |
8089c85b PB |
170 | echo "more" >> file && |
171 | git add file && | |
b7ae1414 | 172 | test_must_fail env GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit -c $head |
8089c85b PB |
173 | |
174 | ' | |
175 | ||
176 | test_expect_success 'with failing hook (--no-verify)' ' | |
177 | ||
1fc4f97d | 178 | test_when_finished "git checkout -f master" && |
0c923256 | 179 | head=$(git rev-parse HEAD) && |
8089c85b PB |
180 | echo "more" >> file && |
181 | git add file && | |
b7ae1414 | 182 | test_must_fail env GIT_EDITOR="\"\$FAKE_EDITOR\"" git commit --no-verify -c $head |
8089c85b PB |
183 | |
184 | ' | |
185 | ||
3e4141d0 AP |
186 | test_expect_success 'with failing hook (merge)' ' |
187 | ||
1fc4f97d | 188 | test_when_finished "git checkout -f master" && |
3e4141d0 AP |
189 | git checkout -B other HEAD@{1} && |
190 | echo "more" >> file && | |
191 | git add file && | |
192 | rm -f "$HOOK" && | |
193 | git commit -m other && | |
3219bad9 | 194 | write_script "$HOOK" <<-EOF && |
3e4141d0 AP |
195 | exit 1 |
196 | EOF | |
197 | git checkout - && | |
1fc4f97d | 198 | test_must_fail git merge --no-ff other |
3e4141d0 AP |
199 | |
200 | ' | |
8089c85b PB |
201 | |
202 | test_done |