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