Commit | Line | Data |
---|---|---|
d1cc130a SG |
1 | #!/bin/sh |
2 | # | |
3 | # Copyright (c) 2007 Steven Grimm | |
4 | # | |
5 | ||
47a528ad | 6 | test_description='git commit |
d1cc130a SG |
7 | |
8 | Tests for selected commit options.' | |
9 | ||
10 | . ./test-lib.sh | |
11 | ||
12 | commit_msg_is () { | |
d5a719e4 ÆAB |
13 | expect=commit_msg_is.expect |
14 | actual=commit_msg_is.actual | |
15 | ||
16 | printf "%s" "$(git log --pretty=format:%s%b -1)" >$expect && | |
17 | printf "%s" "$1" >$actual && | |
18 | test_cmp $expect $actual | |
d1cc130a SG |
19 | } |
20 | ||
21 | # A sanity check to see if commit is working at all. | |
22 | test_expect_success 'a basic commit in an empty tree should succeed' ' | |
23 | echo content > foo && | |
24 | git add foo && | |
25 | git commit -m "initial commit" | |
26 | ' | |
27 | ||
28 | test_expect_success 'nonexistent template file should return error' ' | |
29 | echo changes >> foo && | |
30 | git add foo && | |
2140b140 JN |
31 | ( |
32 | GIT_EDITOR="echo hello >\"\$1\"" && | |
33 | export GIT_EDITOR && | |
34 | test_must_fail git commit --template "$PWD"/notexist | |
35 | ) | |
d1cc130a SG |
36 | ' |
37 | ||
38 | test_expect_success 'nonexistent template file in config should return error' ' | |
39 | git config commit.template "$PWD"/notexist && | |
2140b140 JN |
40 | test_when_finished "git config --unset commit.template" && |
41 | ( | |
42 | GIT_EDITOR="echo hello >\"\$1\"" && | |
43 | export GIT_EDITOR && | |
44 | test_must_fail git commit | |
45 | ) | |
d1cc130a SG |
46 | ' |
47 | ||
48 | # From now on we'll use a template file that exists. | |
49 | TEMPLATE="$PWD"/template | |
50 | ||
51 | test_expect_success 'unedited template should not commit' ' | |
52 | echo "template line" > "$TEMPLATE" && | |
d492b31c | 53 | test_must_fail git commit --template "$TEMPLATE" |
d1cc130a SG |
54 | ' |
55 | ||
56 | test_expect_success 'unedited template with comments should not commit' ' | |
57 | echo "# comment in template" >> "$TEMPLATE" && | |
d492b31c | 58 | test_must_fail git commit --template "$TEMPLATE" |
d1cc130a SG |
59 | ' |
60 | ||
61 | test_expect_success 'a Signed-off-by line by itself should not commit' ' | |
bfdbee98 JH |
62 | ( |
63 | test_set_editor "$TEST_DIRECTORY"/t7500/add-signed-off && | |
64 | test_must_fail git commit --template "$TEMPLATE" | |
65 | ) | |
d1cc130a SG |
66 | ' |
67 | ||
68 | test_expect_success 'adding comments to a template should not commit' ' | |
bfdbee98 JH |
69 | ( |
70 | test_set_editor "$TEST_DIRECTORY"/t7500/add-comments && | |
71 | test_must_fail git commit --template "$TEMPLATE" | |
72 | ) | |
d1cc130a SG |
73 | ' |
74 | ||
0b430a17 | 75 | test_expect_success C_LOCALE_OUTPUT 'adding real content to a template should commit' ' |
bfdbee98 JH |
76 | ( |
77 | test_set_editor "$TEST_DIRECTORY"/t7500/add-content && | |
78 | git commit --template "$TEMPLATE" | |
79 | ) && | |
d1cc130a SG |
80 | commit_msg_is "template linecommit message" |
81 | ' | |
82 | ||
0b430a17 | 83 | test_expect_success C_LOCALE_OUTPUT '-t option should be short for --template' ' |
d1cc130a SG |
84 | echo "short template" > "$TEMPLATE" && |
85 | echo "new content" >> foo && | |
86 | git add foo && | |
bfdbee98 JH |
87 | ( |
88 | test_set_editor "$TEST_DIRECTORY"/t7500/add-content && | |
89 | git commit -t "$TEMPLATE" | |
90 | ) && | |
d1cc130a SG |
91 | commit_msg_is "short templatecommit message" |
92 | ' | |
93 | ||
0b430a17 | 94 | test_expect_success C_LOCALE_OUTPUT 'config-specified template should commit' ' |
d1cc130a SG |
95 | echo "new template" > "$TEMPLATE" && |
96 | git config commit.template "$TEMPLATE" && | |
97 | echo "more content" >> foo && | |
98 | git add foo && | |
bfdbee98 JH |
99 | ( |
100 | test_set_editor "$TEST_DIRECTORY"/t7500/add-content && | |
101 | git commit | |
102 | ) && | |
d1cc130a SG |
103 | git config --unset commit.template && |
104 | commit_msg_is "new templatecommit message" | |
105 | ' | |
106 | ||
107 | test_expect_success 'explicit commit message should override template' ' | |
108 | echo "still more content" >> foo && | |
109 | git add foo && | |
bfdbee98 | 110 | GIT_EDITOR="$TEST_DIRECTORY"/t7500/add-content git commit --template "$TEMPLATE" \ |
d1cc130a | 111 | -m "command line msg" && |
55246aac | 112 | commit_msg_is "command line msg" |
d1cc130a SG |
113 | ' |
114 | ||
115 | test_expect_success 'commit message from file should override template' ' | |
116 | echo "content galore" >> foo && | |
117 | git add foo && | |
118 | echo "standard input msg" | | |
bfdbee98 JH |
119 | ( |
120 | test_set_editor "$TEST_DIRECTORY"/t7500/add-content && | |
121 | git commit --template "$TEMPLATE" --file - | |
122 | ) && | |
55246aac | 123 | commit_msg_is "standard input msg" |
d1cc130a SG |
124 | ' |
125 | ||
859a4dbc RV |
126 | test_expect_success 'using alternate GIT_INDEX_FILE (1)' ' |
127 | ||
128 | cp .git/index saved-index && | |
129 | ( | |
130 | echo some new content >file && | |
131 | GIT_INDEX_FILE=.git/another_index && | |
132 | export GIT_INDEX_FILE && | |
133 | git add file && | |
134 | git commit -m "commit using another index" && | |
135 | git diff-index --exit-code HEAD && | |
136 | git diff-files --exit-code | |
137 | ) && | |
138 | cmp .git/index saved-index >/dev/null | |
139 | ||
140 | ' | |
141 | ||
142 | test_expect_success 'using alternate GIT_INDEX_FILE (2)' ' | |
143 | ||
144 | cp .git/index saved-index && | |
145 | ( | |
146 | rm -f .git/no-such-index && | |
147 | GIT_INDEX_FILE=.git/no-such-index && | |
148 | export GIT_INDEX_FILE && | |
149 | git commit -m "commit using nonexistent index" && | |
150 | test -z "$(git ls-files)" && | |
151 | test -z "$(git ls-tree HEAD)" | |
152 | ||
153 | ) && | |
154 | cmp .git/index saved-index >/dev/null | |
13208572 | 155 | ' |
859a4dbc | 156 | |
13208572 JS |
157 | cat > expect << EOF |
158 | zort | |
2150554b | 159 | |
13208572 JS |
160 | Signed-off-by: C O Mitter <committer@example.com> |
161 | EOF | |
162 | ||
163 | test_expect_success '--signoff' ' | |
164 | echo "yet another content *narf*" >> foo && | |
7eb5bbdb | 165 | echo "zort" | git commit -s -F - foo && |
9524cf29 | 166 | git cat-file commit HEAD | sed "1,/^\$/d" > output && |
bfdbee98 | 167 | test_cmp expect output |
859a4dbc RV |
168 | ' |
169 | ||
dbd0f5c7 JH |
170 | test_expect_success 'commit message from file (1)' ' |
171 | mkdir subdir && | |
172 | echo "Log in top directory" >log && | |
173 | echo "Log in sub directory" >subdir/log && | |
174 | ( | |
175 | cd subdir && | |
176 | git commit --allow-empty -F log | |
177 | ) && | |
178 | commit_msg_is "Log in sub directory" | |
179 | ' | |
180 | ||
181 | test_expect_success 'commit message from file (2)' ' | |
182 | rm -f log && | |
183 | echo "Log in sub directory" >subdir/log && | |
184 | ( | |
185 | cd subdir && | |
186 | git commit --allow-empty -F log | |
187 | ) && | |
188 | commit_msg_is "Log in sub directory" | |
189 | ' | |
190 | ||
191 | test_expect_success 'commit message from stdin' ' | |
192 | ( | |
193 | cd subdir && | |
194 | echo "Log with foo word" | git commit --allow-empty -F - | |
195 | ) && | |
196 | commit_msg_is "Log with foo word" | |
197 | ' | |
198 | ||
aae94ffb SB |
199 | test_expect_success 'commit -F overrides -t' ' |
200 | ( | |
201 | cd subdir && | |
202 | echo "-F log" > f.log && | |
203 | echo "-t template" > t.template && | |
204 | git commit --allow-empty -F f.log -t t.template | |
205 | ) && | |
206 | commit_msg_is "-F log" | |
207 | ' | |
208 | ||
c9b5fde7 ÆAB |
209 | test_expect_success 'Commit without message is allowed with --allow-empty-message' ' |
210 | echo "more content" >>foo && | |
211 | git add foo && | |
212 | >empty && | |
213 | git commit --allow-empty-message <empty && | |
214 | commit_msg_is "" | |
215 | ' | |
216 | ||
217 | test_expect_success 'Commit without message is no-no without --allow-empty-message' ' | |
218 | echo "more content" >>foo && | |
219 | git add foo && | |
220 | >empty && | |
221 | test_must_fail git commit <empty | |
222 | ' | |
223 | ||
224 | test_expect_success 'Commit a message with --allow-empty-message' ' | |
225 | echo "even more content" >>foo && | |
226 | git add foo && | |
227 | git commit --allow-empty-message -m"hello there" && | |
228 | commit_msg_is "hello there" | |
229 | ' | |
230 | ||
b1a6c0a9 PN |
231 | commit_for_rebase_autosquash_setup () { |
232 | echo "first content line" >>foo && | |
233 | git add foo && | |
234 | cat >log <<EOF && | |
235 | target message subject line | |
236 | ||
237 | target message body line 1 | |
238 | target message body line 2 | |
239 | EOF | |
240 | git commit -F log && | |
241 | echo "second content line" >>foo && | |
242 | git add foo && | |
243 | git commit -m "intermediate commit" && | |
244 | echo "third content line" >>foo && | |
245 | git add foo | |
246 | } | |
247 | ||
248 | test_expect_success 'commit --fixup provides correct one-line commit message' ' | |
249 | commit_for_rebase_autosquash_setup && | |
250 | git commit --fixup HEAD~1 && | |
251 | commit_msg_is "fixup! target message subject line" | |
252 | ' | |
253 | ||
7951bd30 PN |
254 | test_expect_success 'commit --squash works with -F' ' |
255 | commit_for_rebase_autosquash_setup && | |
256 | echo "log message from file" >msgfile && | |
257 | git commit --squash HEAD~1 -F msgfile && | |
258 | commit_msg_is "squash! target message subject linelog message from file" | |
259 | ' | |
260 | ||
261 | test_expect_success 'commit --squash works with -m' ' | |
262 | commit_for_rebase_autosquash_setup && | |
263 | git commit --squash HEAD~1 -m "foo bar\nbaz" && | |
264 | commit_msg_is "squash! target message subject linefoo bar\nbaz" | |
265 | ' | |
266 | ||
267 | test_expect_success 'commit --squash works with -C' ' | |
268 | commit_for_rebase_autosquash_setup && | |
269 | git commit --squash HEAD~1 -C HEAD && | |
270 | commit_msg_is "squash! target message subject lineintermediate commit" | |
271 | ' | |
272 | ||
273 | test_expect_success 'commit --squash works with -c' ' | |
274 | commit_for_rebase_autosquash_setup && | |
275 | test_set_editor "$TEST_DIRECTORY"/t7500/edit-content && | |
276 | git commit --squash HEAD~1 -c HEAD && | |
277 | commit_msg_is "squash! target message subject lineedited commit" | |
278 | ' | |
279 | ||
280 | test_expect_success 'commit --squash works with -C for same commit' ' | |
281 | commit_for_rebase_autosquash_setup && | |
282 | git commit --squash HEAD -C HEAD && | |
283 | commit_msg_is "squash! intermediate commit" | |
284 | ' | |
285 | ||
286 | test_expect_success 'commit --squash works with -c for same commit' ' | |
287 | commit_for_rebase_autosquash_setup && | |
288 | test_set_editor "$TEST_DIRECTORY"/t7500/edit-content && | |
289 | git commit --squash HEAD -c HEAD && | |
290 | commit_msg_is "squash! edited commit" | |
291 | ' | |
292 | ||
0b430a17 | 293 | test_expect_success C_LOCALE_OUTPUT 'commit --squash works with editor' ' |
7951bd30 PN |
294 | commit_for_rebase_autosquash_setup && |
295 | test_set_editor "$TEST_DIRECTORY"/t7500/add-content && | |
296 | git commit --squash HEAD~1 && | |
297 | commit_msg_is "squash! target message subject linecommit message" | |
298 | ' | |
299 | ||
b1a6c0a9 PN |
300 | test_expect_success 'invalid message options when using --fixup' ' |
301 | echo changes >>foo && | |
302 | echo "message" >log && | |
303 | git add foo && | |
7951bd30 | 304 | test_must_fail git commit --fixup HEAD~1 --squash HEAD~2 && |
b1a6c0a9 PN |
305 | test_must_fail git commit --fixup HEAD~1 -C HEAD~2 && |
306 | test_must_fail git commit --fixup HEAD~1 -c HEAD~2 && | |
307 | test_must_fail git commit --fixup HEAD~1 -m "cmdline message" && | |
308 | test_must_fail git commit --fixup HEAD~1 -F log | |
309 | ' | |
310 | ||
d1cc130a | 311 | test_done |