blob: 512ae2781fe2c9b02a5a37f5230ec14992114ae7 [file] [log] [blame]
Kristian Høgsberg12ace0b2007-07-31 15:37:30 -04001#!/bin/sh
2#
3# Copyright (c) 2007 Kristian Høgsberg <krh@redhat.com>
4#
5
6# FIXME: Test the various index usages, -i and -o, test reflog,
Wincent Colaiuta264474f2007-12-08 13:29:47 +01007# signoff
Kristian Høgsberg12ace0b2007-07-31 15:37:30 -04008
Nanako Shiraishi47a528a2008-09-03 17:59:33 +09009test_description='git commit'
Johannes Schindelin1e2ae142020-11-18 23:44:40 +000010GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
Johannes Schindelin334afbc2020-11-18 23:44:19 +000011export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
12
Kristian Høgsberg12ace0b2007-07-31 15:37:30 -040013. ./test-lib.sh
Ævar Arnfjörð Bjarmasonebd73f52021-02-12 14:29:40 +010014. "$TEST_DIRECTORY/lib-diff.sh"
Jonathan Nieder1af524e2011-12-07 08:50:23 -060015
16author='The Real Author <someguy@his.email.org>'
Kristian Høgsberg12ace0b2007-07-31 15:37:30 -040017
18test_tick
19
Jonathan Nieder1af524e2011-12-07 08:50:23 -060020test_expect_success 'initial status' '
21 echo bongo bongo >file &&
22 git add file &&
Junio C Hamano32177ba2011-04-13 16:17:29 -070023 git status >actual &&
Kaartic Sivaraam4ddb1352017-06-21 23:46:14 +053024 test_i18ngrep "No commits yet" actual
Junio C Hamano32177ba2011-04-13 16:17:29 -070025'
Kristian Høgsberg12ace0b2007-07-31 15:37:30 -040026
Jonathan Nieder1af524e2011-12-07 08:50:23 -060027test_expect_success 'fail initial amend' '
28 test_must_fail git commit --amend
29'
Kristian Høgsberg12ace0b2007-07-31 15:37:30 -040030
Jonathan Nieder1af524e2011-12-07 08:50:23 -060031test_expect_success 'setup: initial commit' '
32 git commit -m initial
33'
Kristian Høgsberg12ace0b2007-07-31 15:37:30 -040034
Jonathan Nieder1af524e2011-12-07 08:50:23 -060035test_expect_success '-m and -F do not mix' '
Junio C Hamanoc65dc352012-03-30 11:04:08 -070036 git checkout HEAD file && echo >>file && git add file &&
Jonathan Nieder1af524e2011-12-07 08:50:23 -060037 test_must_fail git commit -m foo -m bar -F file
38'
Kristian Høgsberg12ace0b2007-07-31 15:37:30 -040039
Jonathan Nieder1af524e2011-12-07 08:50:23 -060040test_expect_success '-m and -C do not mix' '
Junio C Hamanoc65dc352012-03-30 11:04:08 -070041 git checkout HEAD file && echo >>file && git add file &&
Jonathan Nieder1af524e2011-12-07 08:50:23 -060042 test_must_fail git commit -C HEAD -m illegal
43'
Kristian Høgsberg12ace0b2007-07-31 15:37:30 -040044
Jonathan Nieder1af524e2011-12-07 08:50:23 -060045test_expect_success 'paths and -a do not mix' '
46 echo King of the bongo >file &&
47 test_must_fail git commit -m foo -a file
48'
Björn Steinbrink9d874422007-11-12 16:15:39 +010049
Conrad Irwine41fcfe2011-05-06 23:00:00 -070050test_expect_success PERL 'can use paths with --interactive' '
Jeff King587ac8c2011-05-09 16:53:00 -070051 echo bong-o-bong >file &&
52 # 2: update, 1:st path, that is all, 7: quit
Eric Sunshine0590ff22018-07-01 20:23:42 -040053 test_write_lines 2 1 "" 7 |
Conrad Irwine41fcfe2011-05-06 23:00:00 -070054 git commit -m foo --interactive file &&
55 git reset --hard HEAD^
Jeff King587ac8c2011-05-09 16:53:00 -070056'
Björn Steinbrink9d874422007-11-12 16:15:39 +010057
Brandon Williams86238e02018-04-03 10:57:45 -070058test_expect_success 'removed files and relative paths' '
59 test_when_finished "rm -rf foo" &&
60 git init foo &&
61 >foo/foo.txt &&
62 git -C foo add foo.txt &&
63 git -C foo commit -m first &&
64 git -C foo rm foo.txt &&
65
66 mkdir -p foo/bar &&
67 git -C foo/bar commit -m second ../foo.txt
68'
69
Jonathan Nieder1af524e2011-12-07 08:50:23 -060070test_expect_success 'using invalid commit with -C' '
Kacper Kornet0bb0c152013-08-24 06:01:44 +020071 test_must_fail git commit --allow-empty -C bogus
Jonathan Nieder1af524e2011-12-07 08:50:23 -060072'
Kristian Høgsberg12ace0b2007-07-31 15:37:30 -040073
Jonathan Nieder1af524e2011-12-07 08:50:23 -060074test_expect_success 'nothing to commit' '
Jeff King088304b2014-01-23 14:54:57 -050075 git reset --hard &&
Jonathan Nieder1af524e2011-12-07 08:50:23 -060076 test_must_fail git commit -m initial
77'
Kristian Høgsberg12ace0b2007-07-31 15:37:30 -040078
Tay Ray Chuan9cbcc2a2014-02-22 03:16:54 +080079test_expect_success '--dry-run fails with nothing to commit' '
80 test_must_fail git commit -m initial --dry-run
81'
82
83test_expect_success '--short fails with nothing to commit' '
84 test_must_fail git commit -m initial --short
85'
86
87test_expect_success '--porcelain fails with nothing to commit' '
88 test_must_fail git commit -m initial --porcelain
89'
90
91test_expect_success '--long fails with nothing to commit' '
92 test_must_fail git commit -m initial --long
93'
94
Jonathan Nieder1af524e2011-12-07 08:50:23 -060095test_expect_success 'setup: non-initial commit' '
96 echo bongo bongo bongo >file &&
97 git commit -m next -a
98'
Kristian Høgsberg12ace0b2007-07-31 15:37:30 -040099
Tay Ray Chuan9cbcc2a2014-02-22 03:16:54 +0800100test_expect_success '--dry-run with stuff to commit returns ok' '
101 echo bongo bongo bongo >>file &&
102 git commit -m next -a --dry-run
103'
104
Stephen P. Smithf3bd35f2018-09-05 17:53:29 -0700105test_expect_success '--short with stuff to commit returns ok' '
Tay Ray Chuan9cbcc2a2014-02-22 03:16:54 +0800106 echo bongo bongo bongo >>file &&
107 git commit -m next -a --short
108'
109
Stephen P. Smithf3bd35f2018-09-05 17:53:29 -0700110test_expect_success '--porcelain with stuff to commit returns ok' '
Tay Ray Chuan9cbcc2a2014-02-22 03:16:54 +0800111 echo bongo bongo bongo >>file &&
112 git commit -m next -a --porcelain
113'
114
115test_expect_success '--long with stuff to commit returns ok' '
116 echo bongo bongo bongo >>file &&
117 git commit -m next -a --long
118'
119
Jonathan Nieder1af524e2011-12-07 08:50:23 -0600120test_expect_success 'commit message from non-existing file' '
121 echo more bongo: bongo bongo bongo bongo >file &&
122 test_must_fail git commit -F gah -a
123'
Kristian Høgsberg12ace0b2007-07-31 15:37:30 -0400124
Jonathan Nieder1af524e2011-12-07 08:50:23 -0600125test_expect_success 'empty commit message' '
126 # Empty except stray tabs and spaces on a few lines.
127 sed -e "s/@//g" >msg <<-\EOF &&
128 @ @
129 @@
130 @ @
131 @Signed-off-by: hula@
132 EOF
133 test_must_fail git commit -F msg -a
134'
Kristian Høgsberg12ace0b2007-07-31 15:37:30 -0400135
Junio C Hamano010c7db2012-03-30 11:30:59 -0700136test_expect_success 'template "emptyness" check does not kick in with -F' '
137 git checkout HEAD file && echo >>file && git add file &&
138 git commit -t file -F file
139'
140
Junio C Hamanob2eda9b2012-03-30 12:14:33 -0700141test_expect_success 'template "emptyness" check' '
142 git checkout HEAD file && echo >>file && git add file &&
143 test_must_fail git commit -t file 2>err &&
144 test_i18ngrep "did not edit" err
145'
146
Jonathan Nieder1af524e2011-12-07 08:50:23 -0600147test_expect_success 'setup: commit message from file' '
Junio C Hamano010c7db2012-03-30 11:30:59 -0700148 git checkout HEAD file && echo >>file && git add file &&
Jonathan Nieder1af524e2011-12-07 08:50:23 -0600149 echo this is the commit message, coming from a file >msg &&
150 git commit -F msg -a
151'
Kristian Høgsberg12ace0b2007-07-31 15:37:30 -0400152
Jonathan Nieder1af524e2011-12-07 08:50:23 -0600153test_expect_success 'amend commit' '
154 cat >editor <<-\EOF &&
155 #!/bin/sh
Denton Liu38c1aa02019-11-27 11:53:40 -0800156 sed -e "s/a file/an amend commit/g" <"$1" >"$1-"
Jonathan Nieder1af524e2011-12-07 08:50:23 -0600157 mv "$1-" "$1"
158 EOF
159 chmod 755 editor &&
160 EDITOR=./editor git commit --amend
161'
Kristian Høgsberg12ace0b2007-07-31 15:37:30 -0400162
Jeff Kingea2d4ed2012-07-10 16:40:29 -0400163test_expect_success 'amend --only ignores staged contents' '
164 cp file file.expect &&
165 echo changed >file &&
166 git add file &&
167 git commit --no-edit --amend --only &&
168 git cat-file blob HEAD:file >file.actual &&
169 test_cmp file.expect file.actual &&
170 git diff --exit-code
171'
172
Andreas Krey319d8352016-12-02 23:15:13 +0100173test_expect_success 'allow-empty --only ignores staged contents' '
174 echo changed-again >file &&
175 git add file &&
176 git commit --allow-empty --only -m "empty" &&
177 git cat-file blob HEAD:file >file.actual &&
178 test_cmp file.expect file.actual &&
179 git diff --exit-code
180'
181
Jonathan Niederbc821892011-12-07 08:54:14 -0600182test_expect_success 'set up editor' '
183 cat >editor <<-\EOF &&
184 #!/bin/sh
185 sed -e "s/unamended/amended/g" <"$1" >"$1-"
186 mv "$1-" "$1"
187 EOF
188 chmod 755 editor
189'
190
191test_expect_success 'amend without launching editor' '
192 echo unamended >expect &&
193 git commit --allow-empty -m "unamended" &&
194 echo needs more bongo >file &&
195 git add file &&
196 EDITOR=./editor git commit --no-edit --amend &&
197 git diff --exit-code HEAD -- file &&
198 git diff-tree -s --format=%s HEAD >msg &&
199 test_cmp expect msg
200'
201
202test_expect_success '--amend --edit' '
203 echo amended >expect &&
204 git commit --allow-empty -m "unamended" &&
205 echo bongo again >file &&
206 git add file &&
207 EDITOR=./editor git commit --edit --amend &&
208 git diff-tree -s --format=%s HEAD >msg &&
209 test_cmp expect msg
210'
211
Chris Webbd9a93572012-07-09 19:53:26 +0100212test_expect_success '--amend --edit of empty message' '
213 cat >replace <<-\EOF &&
214 #!/bin/sh
215 echo "amended" >"$1"
216 EOF
217 chmod 755 replace &&
218 git commit --allow-empty --allow-empty-message -m "" &&
219 echo more bongo >file &&
220 git add file &&
221 EDITOR=./replace git commit --edit --amend &&
222 git diff-tree -s --format=%s HEAD >msg &&
223 ./replace expect &&
224 test_cmp expect msg
225'
226
Jeff King27014cb2016-04-07 12:56:26 -0700227test_expect_success '--amend to set message to empty' '
228 echo bata >file &&
Adam Dinwoodie178e8142016-04-06 18:15:03 +0100229 git add file &&
230 git commit -m "unamended" &&
231 git commit --amend --allow-empty-message -m "" &&
232 git diff-tree -s --format=%s HEAD >msg &&
233 echo "" >expect &&
234 test_cmp expect msg
235'
236
Jeff King27014cb2016-04-07 12:56:26 -0700237test_expect_success '--amend to set empty message needs --allow-empty-message' '
Adam Dinwoodie178e8142016-04-06 18:15:03 +0100238 echo conga >file &&
239 git add file &&
240 git commit -m "unamended" &&
241 test_must_fail git commit --amend -m "" &&
242 git diff-tree -s --format=%s HEAD >msg &&
243 echo "unamended" >expect &&
244 test_cmp expect msg
245'
246
Jonathan Niederbc821892011-12-07 08:54:14 -0600247test_expect_success '-m --edit' '
248 echo amended >expect &&
249 git commit --allow-empty -m buffer &&
250 echo bongo bongo >file &&
251 git add file &&
252 EDITOR=./editor git commit -m unamended --edit &&
253 git diff-tree -s --format=%s HEAD >msg &&
254 test_cmp expect msg
255'
256
Jonathan Nieder1af524e2011-12-07 08:50:23 -0600257test_expect_success '-m and -F do not mix' '
258 echo enough with the bongos >file &&
259 test_must_fail git commit -F msg -m amending .
260'
Kristian Høgsberg12ace0b2007-07-31 15:37:30 -0400261
Jonathan Nieder1af524e2011-12-07 08:50:23 -0600262test_expect_success 'using message from other commit' '
263 git commit -C HEAD^ .
264'
Kristian Høgsberg12ace0b2007-07-31 15:37:30 -0400265
Jonathan Nieder1af524e2011-12-07 08:50:23 -0600266test_expect_success 'editing message from other commit' '
267 cat >editor <<-\EOF &&
268 #!/bin/sh
Denton Liu38c1aa02019-11-27 11:53:40 -0800269 sed -e "s/amend/older/g" <"$1" >"$1-"
Jonathan Nieder1af524e2011-12-07 08:50:23 -0600270 mv "$1-" "$1"
271 EOF
272 chmod 755 editor &&
273 echo hula hula >file &&
274 EDITOR=./editor git commit -c HEAD^ -a
275'
Kristian Høgsberg12ace0b2007-07-31 15:37:30 -0400276
Jonathan Nieder1af524e2011-12-07 08:50:23 -0600277test_expect_success 'message from stdin' '
278 echo silly new contents >file &&
279 echo commit message from stdin |
280 git commit -F - -a
281'
Kristian Høgsberg12ace0b2007-07-31 15:37:30 -0400282
Jonathan Nieder1af524e2011-12-07 08:50:23 -0600283test_expect_success 'overriding author from command line' '
284 echo gak >file &&
285 git commit -m author \
286 --author "Rubber Duck <rduck@convoy.org>" -a >output 2>&1 &&
287 grep Rubber.Duck output
288'
Kristian Høgsberg12ace0b2007-07-31 15:37:30 -0400289
Jonathan Nieder1af524e2011-12-07 08:50:23 -0600290test_expect_success PERL 'interactive add' '
Denton Liu756ee7f2019-11-27 11:53:43 -0800291 echo 7 | test_must_fail git commit --interactive >out &&
292 grep "What now" out
Jonathan Nieder1af524e2011-12-07 08:50:23 -0600293'
Kristian Høgsberg12ace0b2007-07-31 15:37:30 -0400294
Jonathan Nieder1af524e2011-12-07 08:50:23 -0600295test_expect_success PERL "commit --interactive doesn't change index if editor aborts" '
296 echo zoo >file &&
Conrad Irwin02a481f2011-05-10 12:12:31 -0700297 test_must_fail git diff --exit-code >diff1 &&
Eric Sunshine0590ff22018-07-01 20:23:42 -0400298 test_write_lines u "*" q |
Jonathan Nieder1af524e2011-12-07 08:50:23 -0600299 (
300 EDITOR=: &&
301 export EDITOR &&
302 test_must_fail git commit --interactive
303 ) &&
Conrad Irwin02a481f2011-05-10 12:12:31 -0700304 git diff >diff2 &&
Jonathan Nieder1af524e2011-12-07 08:50:23 -0600305 compare_diff_patch diff1 diff2
306'
Conrad Irwin02a481f2011-05-10 12:12:31 -0700307
Jonathan Nieder1af524e2011-12-07 08:50:23 -0600308test_expect_success 'editor not invoked if -F is given' '
309 cat >editor <<-\EOF &&
310 #!/bin/sh
311 sed -e s/good/bad/g <"$1" >"$1-"
312 mv "$1-" "$1"
313 EOF
314 chmod 755 editor &&
Adeodato Simó7eb5bbd2009-01-09 18:30:05 +0100315
Jonathan Nieder1af524e2011-12-07 08:50:23 -0600316 echo A good commit message. >msg &&
317 echo moo >file &&
Adeodato Simó7eb5bbd2009-01-09 18:30:05 +0100318
Jonathan Nieder1af524e2011-12-07 08:50:23 -0600319 EDITOR=./editor git commit -a -F msg &&
320 git show -s --pretty=format:%s >subject &&
321 grep -q good subject &&
322
323 echo quack >file &&
324 echo Another good message. |
325 EDITOR=./editor git commit -a -F - &&
326 git show -s --pretty=format:%s >subject &&
327 grep -q good subject
328'
Kristian Høgsberg12ace0b2007-07-31 15:37:30 -0400329
Junio C Hamanodb33af02007-09-14 16:53:58 -0700330test_expect_success 'partial commit that involves removal (1)' '
Junio C Hamano80bffaf2007-09-12 16:04:22 -0700331
332 git rm --cached file &&
333 mv file elif &&
334 git add elif &&
335 git commit -m "Partial: add elif" elif &&
336 git diff-tree --name-status HEAD^ HEAD >current &&
337 echo "A elif" >expected &&
Miklos Vajna1e368682008-09-10 19:32:44 +0200338 test_cmp expected current
Junio C Hamano80bffaf2007-09-12 16:04:22 -0700339
340'
341
Junio C Hamanodb33af02007-09-14 16:53:58 -0700342test_expect_success 'partial commit that involves removal (2)' '
Junio C Hamano80bffaf2007-09-12 16:04:22 -0700343
344 git commit -m "Partial: remove file" file &&
345 git diff-tree --name-status HEAD^ HEAD >current &&
346 echo "D file" >expected &&
Miklos Vajna1e368682008-09-10 19:32:44 +0200347 test_cmp expected current
Junio C Hamano80bffaf2007-09-12 16:04:22 -0700348
349'
350
Junio C Hamanodb33af02007-09-14 16:53:58 -0700351test_expect_success 'partial commit that involves removal (3)' '
352
353 git rm --cached elif &&
354 echo elif >elif &&
355 git commit -m "Partial: modify elif" elif &&
356 git diff-tree --name-status HEAD^ HEAD >current &&
357 echo "M elif" >expected &&
Miklos Vajna1e368682008-09-10 19:32:44 +0200358 test_cmp expected current
Junio C Hamanodb33af02007-09-14 16:53:58 -0700359
360'
361
Junio C Hamano5aa5cd42007-11-10 01:49:34 -0800362test_expect_success 'amend commit to fix author' '
363
364 oldtick=$GIT_AUTHOR_DATE &&
365 test_tick &&
366 git reset --hard &&
Denton Liu756ee7f2019-11-27 11:53:43 -0800367 git cat-file -p HEAD >commit &&
Junio C Hamano5aa5cd42007-11-10 01:49:34 -0800368 sed -e "s/author.*/author $author $oldtick/" \
Denton Liu756ee7f2019-11-27 11:53:43 -0800369 -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" \
370 commit >expected &&
Junio C Hamano5aa5cd42007-11-10 01:49:34 -0800371 git commit --amend --author="$author" &&
Denton Liu38c1aa02019-11-27 11:53:40 -0800372 git cat-file -p HEAD >current &&
Miklos Vajna1e368682008-09-10 19:32:44 +0200373 test_cmp expected current
Junio C Hamano5aa5cd42007-11-10 01:49:34 -0800374
375'
376
Miklos Vajna02b47cd2009-12-02 23:16:18 +0100377test_expect_success 'amend commit to fix date' '
378
379 test_tick &&
380 newtick=$GIT_AUTHOR_DATE &&
381 git reset --hard &&
Denton Liu756ee7f2019-11-27 11:53:43 -0800382 git cat-file -p HEAD >commit &&
Miklos Vajna02b47cd2009-12-02 23:16:18 +0100383 sed -e "s/author.*/author $author $newtick/" \
Denton Liu756ee7f2019-11-27 11:53:43 -0800384 -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" \
385 commit >expected &&
Miklos Vajna02b47cd2009-12-02 23:16:18 +0100386 git commit --amend --date="$newtick" &&
Denton Liu38c1aa02019-11-27 11:53:40 -0800387 git cat-file -p HEAD >current &&
Miklos Vajna02b47cd2009-12-02 23:16:18 +0100388 test_cmp expected current
389
390'
391
Jeff Kingb7242b82014-05-01 21:10:01 -0400392test_expect_success 'commit mentions forced date in output' '
393 git commit --amend --date=2010-01-02T03:04:05 >output &&
394 grep "Date: *Sat Jan 2 03:04:05 2010" output
395'
396
Jeff King14ac2862014-05-01 21:12:42 -0400397test_expect_success 'commit complains about completely bogus dates' '
398 test_must_fail git commit --amend --date=seventeen
399'
400
401test_expect_success 'commit --date allows approxidate' '
402 git commit --amend \
403 --date="midnight the 12th of october, anno domini 1979" &&
404 echo "Fri Oct 12 00:00:00 1979 +0000" >expect &&
405 git log -1 --format=%ad >actual &&
406 test_cmp expect actual
Jeff King4579bb42010-12-13 12:02:25 -0500407'
408
Junio C Hamano5aa5cd42007-11-10 01:49:34 -0800409test_expect_success 'sign off (1)' '
410
411 echo 1 >positive &&
412 git add positive &&
413 git commit -s -m "thank you" &&
Denton Liu756ee7f2019-11-27 11:53:43 -0800414 git cat-file commit HEAD >commit &&
415 sed -e "1,/^\$/d" commit >actual &&
Junio C Hamano5aa5cd42007-11-10 01:49:34 -0800416 (
Eric Sunshinee974e062018-07-01 20:24:03 -0400417 echo thank you &&
418 echo &&
Denton Liu756ee7f2019-11-27 11:53:43 -0800419 git var GIT_COMMITTER_IDENT >ident &&
420 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /" ident
Junio C Hamano5aa5cd42007-11-10 01:49:34 -0800421 ) >expected &&
Jeff King82ebb0b2008-03-12 17:36:36 -0400422 test_cmp expected actual
Junio C Hamano5aa5cd42007-11-10 01:49:34 -0800423
424'
425
426test_expect_success 'sign off (2)' '
427
428 echo 2 >positive &&
429 git add positive &&
430 existing="Signed-off-by: Watch This <watchthis@example.com>" &&
431 git commit -s -m "thank you
432
433$existing" &&
Denton Liu756ee7f2019-11-27 11:53:43 -0800434 git cat-file commit HEAD >commit &&
435 sed -e "1,/^\$/d" commit >actual &&
Junio C Hamano5aa5cd42007-11-10 01:49:34 -0800436 (
Eric Sunshinee974e062018-07-01 20:24:03 -0400437 echo thank you &&
438 echo &&
439 echo $existing &&
Denton Liu756ee7f2019-11-27 11:53:43 -0800440 git var GIT_COMMITTER_IDENT >ident &&
441 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /" ident
Junio C Hamano5aa5cd42007-11-10 01:49:34 -0800442 ) >expected &&
Jeff King82ebb0b2008-03-12 17:36:36 -0400443 test_cmp expected actual
Junio C Hamano5aa5cd42007-11-10 01:49:34 -0800444
445'
446
David Brownc1e01b02009-10-28 10:13:44 -0700447test_expect_success 'signoff gap' '
448
449 echo 3 >positive &&
450 git add positive &&
451 alt="Alt-RFC-822-Header: Value" &&
452 git commit -s -m "welcome
453
454$alt" &&
Denton Liu756ee7f2019-11-27 11:53:43 -0800455 git cat-file commit HEAD >commit &&
456 sed -e "1,/^\$/d" commit >actual &&
David Brownc1e01b02009-10-28 10:13:44 -0700457 (
Eric Sunshinee974e062018-07-01 20:24:03 -0400458 echo welcome &&
459 echo &&
460 echo $alt &&
Denton Liu756ee7f2019-11-27 11:53:43 -0800461 git var GIT_COMMITTER_IDENT >ident &&
462 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /" ident
David Brownc1e01b02009-10-28 10:13:44 -0700463 ) >expected &&
464 test_cmp expected actual
465'
466
467test_expect_success 'signoff gap 2' '
468
469 echo 4 >positive &&
470 git add positive &&
471 alt="fixed: 34" &&
472 git commit -s -m "welcome
473
474We have now
475$alt" &&
Denton Liu756ee7f2019-11-27 11:53:43 -0800476 git cat-file commit HEAD >commit &&
477 sed -e "1,/^\$/d" commit >actual &&
David Brownc1e01b02009-10-28 10:13:44 -0700478 (
Eric Sunshinee974e062018-07-01 20:24:03 -0400479 echo welcome &&
480 echo &&
481 echo We have now &&
482 echo $alt &&
483 echo &&
Denton Liu756ee7f2019-11-27 11:53:43 -0800484 git var GIT_COMMITTER_IDENT >ident &&
485 sed -e "s/>.*/>/" -e "s/^/Signed-off-by: /" ident
David Brownc1e01b02009-10-28 10:13:44 -0700486 ) >expected &&
487 test_cmp expected actual
488'
489
Jonathan Tan967dfd42016-11-02 10:29:20 -0700490test_expect_success 'signoff respects trailer config' '
491
492 echo 5 >positive &&
493 git add positive &&
494 git commit -s -m "subject
495
496non-trailer line
497Myfooter: x" &&
Denton Liu756ee7f2019-11-27 11:53:43 -0800498 git cat-file commit HEAD >commit &&
499 sed -e "1,/^\$/d" commit >actual &&
Jonathan Tan967dfd42016-11-02 10:29:20 -0700500 (
Eric Sunshinee974e062018-07-01 20:24:03 -0400501 echo subject &&
502 echo &&
503 echo non-trailer line &&
504 echo Myfooter: x &&
505 echo &&
Jonathan Tan967dfd42016-11-02 10:29:20 -0700506 echo "Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>"
507 ) >expected &&
508 test_cmp expected actual &&
509
510 echo 6 >positive &&
511 git add positive &&
512 git -c "trailer.Myfooter.ifexists=add" commit -s -m "subject
513
514non-trailer line
515Myfooter: x" &&
Denton Liu756ee7f2019-11-27 11:53:43 -0800516 git cat-file commit HEAD >commit &&
517 sed -e "1,/^\$/d" commit >actual &&
Jonathan Tan967dfd42016-11-02 10:29:20 -0700518 (
Eric Sunshinee974e062018-07-01 20:24:03 -0400519 echo subject &&
520 echo &&
521 echo non-trailer line &&
522 echo Myfooter: x &&
Jonathan Tan967dfd42016-11-02 10:29:20 -0700523 echo "Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>"
524 ) >expected &&
525 test_cmp expected actual
526'
527
Jeff Kingffce7f52018-08-22 20:50:37 -0400528test_expect_success 'signoff not confused by ---' '
529 cat >expected <<-EOF &&
530 subject
531
532 body
533 ---
534 these dashes confuse the parser!
535
536 Signed-off-by: $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>
537 EOF
538 # should be a noop, since we already signed
539 git commit --allow-empty --signoff -F expected &&
540 git log -1 --pretty=format:%B >actual &&
541 test_cmp expected actual
542'
543
Junio C Hamano5aa5cd42007-11-10 01:49:34 -0800544test_expect_success 'multiple -m' '
545
546 >negative &&
547 git add negative &&
548 git commit -m "one" -m "two" -m "three" &&
Denton Liu756ee7f2019-11-27 11:53:43 -0800549 git cat-file commit HEAD >commit &&
550 sed -e "1,/^\$/d" commit >actual &&
Junio C Hamano5aa5cd42007-11-10 01:49:34 -0800551 (
Eric Sunshinee974e062018-07-01 20:24:03 -0400552 echo one &&
553 echo &&
554 echo two &&
555 echo &&
Junio C Hamano5aa5cd42007-11-10 01:49:34 -0800556 echo three
557 ) >expected &&
Jeff King82ebb0b2008-03-12 17:36:36 -0400558 test_cmp expected actual
Junio C Hamano5aa5cd42007-11-10 01:49:34 -0800559
560'
561
Kristian Høgsbergd63c2fd2007-11-02 11:33:06 -0400562test_expect_success 'amend commit to fix author' '
563
564 oldtick=$GIT_AUTHOR_DATE &&
565 test_tick &&
566 git reset --hard &&
Denton Liu756ee7f2019-11-27 11:53:43 -0800567 git cat-file -p HEAD >commit &&
Kristian Høgsbergd63c2fd2007-11-02 11:33:06 -0400568 sed -e "s/author.*/author $author $oldtick/" \
Denton Liu756ee7f2019-11-27 11:53:43 -0800569 -e "s/^\(committer.*> \).*$/\1$GIT_COMMITTER_DATE/" \
570 commit >expected &&
Kristian Høgsbergd63c2fd2007-11-02 11:33:06 -0400571 git commit --amend --author="$author" &&
Denton Liu38c1aa02019-11-27 11:53:40 -0800572 git cat-file -p HEAD >current &&
Miklos Vajna1e368682008-09-10 19:32:44 +0200573 test_cmp expected current
Kristian Høgsbergd63c2fd2007-11-02 11:33:06 -0400574
575'
Kristian Høgsberg12009932007-11-15 09:49:58 -0500576
577test_expect_success 'git commit <file> with dirty index' '
Denton Liu38c1aa02019-11-27 11:53:40 -0800578 echo tacocat >elif &&
579 echo tehlulz >chz &&
Kristian Høgsberg12009932007-11-15 09:49:58 -0500580 git add chz &&
581 git commit elif -m "tacocat is a palindrome" &&
Denton Liu756ee7f2019-11-27 11:53:43 -0800582 git show --stat >stat &&
583 grep elif stat &&
584 git diff --cached >diff &&
585 grep chz diff
Kristian Høgsberg12009932007-11-15 09:49:58 -0500586'
587
Johannes Sixt13aba1e2007-12-03 08:24:50 +0100588test_expect_success 'same tree (single parent)' '
589
Jonathan Nieder1af524e2011-12-07 08:50:23 -0600590 git reset --hard &&
591 test_must_fail git commit -m empty
Johannes Sixt13aba1e2007-12-03 08:24:50 +0100592
593'
594
Junio C Hamano36863af2007-12-03 00:03:10 -0800595test_expect_success 'same tree (single parent) --allow-empty' '
596
597 git commit --allow-empty -m "forced empty" &&
Denton Liu756ee7f2019-11-27 11:53:43 -0800598 git cat-file commit HEAD >commit &&
599 grep forced commit
Junio C Hamano36863af2007-12-03 00:03:10 -0800600
601'
602
Johannes Sixt13aba1e2007-12-03 08:24:50 +0100603test_expect_success 'same tree (merge and amend merge)' '
604
605 git checkout -b side HEAD^ &&
606 echo zero >zero &&
607 git add zero &&
608 git commit -m "add zero" &&
Johannes Schindelin1e2ae142020-11-18 23:44:40 +0000609 git checkout main &&
Johannes Sixt13aba1e2007-12-03 08:24:50 +0100610
611 git merge -s ours side -m "empty ok" &&
612 git diff HEAD^ HEAD >actual &&
SZEDER Gábor1c5e94f2018-08-19 23:57:25 +0200613 test_must_be_empty actual &&
Johannes Sixt13aba1e2007-12-03 08:24:50 +0100614
615 git commit --amend -m "empty really ok" &&
616 git diff HEAD^ HEAD >actual &&
SZEDER Gábor1c5e94f2018-08-19 23:57:25 +0200617 test_must_be_empty actual
Johannes Sixt13aba1e2007-12-03 08:24:50 +0100618
619'
620
Junio C Hamano1eb1e9e2007-12-14 11:57:22 -0800621test_expect_success 'amend using the message from another commit' '
622
623 git reset --hard &&
624 test_tick &&
625 git commit --allow-empty -m "old commit" &&
626 old=$(git rev-parse --verify HEAD) &&
627 test_tick &&
628 git commit --allow-empty -m "new commit" &&
629 new=$(git rev-parse --verify HEAD) &&
630 test_tick &&
631 git commit --allow-empty --amend -C "$old" &&
632 git show --pretty="format:%ad %s" "$old" >expected &&
633 git show --pretty="format:%ad %s" HEAD >actual &&
Jeff King82ebb0b2008-03-12 17:36:36 -0400634 test_cmp expected actual
Junio C Hamano1eb1e9e2007-12-14 11:57:22 -0800635
636'
637
Junio C Hamano8a2f8732008-02-03 00:00:09 -0800638test_expect_success 'amend using the message from a commit named with tag' '
639
640 git reset --hard &&
641 test_tick &&
642 git commit --allow-empty -m "old commit" &&
643 old=$(git rev-parse --verify HEAD) &&
644 git tag -a -m "tag on old" tagged-old HEAD &&
645 test_tick &&
646 git commit --allow-empty -m "new commit" &&
647 new=$(git rev-parse --verify HEAD) &&
648 test_tick &&
649 git commit --allow-empty --amend -C tagged-old &&
650 git show --pretty="format:%ad %s" "$old" >expected &&
651 git show --pretty="format:%ad %s" HEAD >actual &&
Jeff King82ebb0b2008-03-12 17:36:36 -0400652 test_cmp expected actual
Junio C Hamano8a2f8732008-02-03 00:00:09 -0800653
654'
655
Thomas Rast6360d342010-03-12 18:04:34 +0100656test_expect_success 'amend can copy notes' '
657
658 git config notes.rewrite.amend true &&
659 git config notes.rewriteRef "refs/notes/*" &&
660 test_commit foo &&
661 git notes add -m"a note" &&
662 test_tick &&
663 git commit --amend -m"new foo" &&
664 test "$(git notes show)" = "a note"
665
666'
667
Junio C Hamano4682d852012-06-27 20:14:47 -0700668test_expect_success 'commit a file whose name is a dash' '
669 git reset --hard &&
670 for i in 1 2 3 4 5
671 do
672 echo $i
673 done >./- &&
674 git add ./- &&
675 test_tick &&
676 git commit -m "add dash" >output </dev/null &&
677 test_i18ngrep " changed, 5 insertions" output
678'
679
Thomas Rastf0c73202013-06-01 13:02:00 +0200680test_expect_success '--only works on to-be-born branch' '
681 # This test relies on having something in the index, as it
682 # would not otherwise actually prove much. So check this.
683 test -n "$(git ls-files)" &&
684 git checkout --orphan orphan &&
685 echo foo >newfile &&
686 git add newfile &&
687 git commit --only newfile -m"--only on unborn branch" &&
688 echo newfile >expected &&
689 git ls-tree -r --name-only HEAD >actual &&
690 test_cmp expected actual
691'
692
Stephen P. Smith8dc874b2016-02-15 19:38:25 -0700693test_expect_success '--dry-run with conflicts fixed from a merge' '
694 # setup two branches with conflicting information
695 # in the same file, resolve the conflict,
696 # call commit with --dry-run
697 echo "Initial contents, unimportant" >test-file &&
698 git add test-file &&
699 git commit -m "Initial commit" &&
700 echo "commit-1-state" >test-file &&
701 git commit -m "commit 1" -i test-file &&
702 git tag commit-1 &&
703 git checkout -b branch-2 HEAD^1 &&
704 echo "commit-2-state" >test-file &&
705 git commit -m "commit 2" -i test-file &&
SZEDER Gábor15da7532018-08-22 01:28:11 +0200706 test_must_fail git merge --no-commit commit-1 &&
Stephen P. Smith8dc874b2016-02-15 19:38:25 -0700707 echo "commit-2-state" >test-file &&
708 git add test-file &&
709 git commit --dry-run &&
710 git commit -m "conflicts fixed from merge."
711'
712
Stephen P. Smithf3bd35f2018-09-05 17:53:29 -0700713test_expect_success '--dry-run --short' '
Stephen P. Smith8282f592018-09-05 17:53:28 -0700714 >test-file &&
715 git add test-file &&
716 git commit --dry-run --short
717'
718
Kristian Høgsberg12ace0b2007-07-31 15:37:30 -0400719test_done