t7500: add tests for --fixup=[amend|reword] options
Mentored-by: Christian Couder <chriscool@tuxfamily.org> Mentored-by: Phillip Wood <phillip.wood@dunelm.org.uk> Signed-off-by: Charvi Mendiratta <charvi077@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Charvi Mendiratta committed
Mar 15, 2021 at 13:24 UTC
3d1bda6b5bed6b29723178b1b417b3a5abe62565
1 file changed
+159
t/t7500-commit-template-squash-signoff.sh
+159
@@ -9,6 +9,8 @@ Tests for template, signoff, squash and -F functions.'
9
10
. ./test-lib.sh
11
12
+. "$TEST_DIRECTORY"/lib-rebase.sh
13
+
14
commit_msg_is () {
15
expect=commit_msg_is.expect
16
actual=commit_msg_is.actual
@@ -279,6 +281,163 @@ test_expect_success 'commit --fixup -m"something" -m"extra"' '
281
282
extra"
283
'
284
+get_commit_msg () {
285
+ rev="$1" &&
286
+ git log -1 --pretty=format:"%B" "$rev"
287
+}
288
+
289
+test_expect_success 'commit --fixup=amend: creates amend! commit' '
290
+ commit_for_rebase_autosquash_setup &&
291
+ cat >expected <<-EOF &&
292
+ amend! $(git log -1 --format=%s HEAD~)
293
+
294
+ $(get_commit_msg HEAD~)
295
+
296
+ edited
297
+ EOF
298
+ (
299
+ set_fake_editor &&
300
+ FAKE_COMMIT_AMEND="edited" \
301
+ git commit --fixup=amend:HEAD~
302
+ ) &&
303
+ get_commit_msg HEAD >actual &&
304
+ test_cmp expected actual
305
+'
306
+
307
+test_expect_success '--fixup=amend: --only ignores staged changes' '
308
+ commit_for_rebase_autosquash_setup &&
309
+ cat >expected <<-EOF &&
310
+ amend! $(git log -1 --format=%s HEAD~)
311
+
312
+ $(get_commit_msg HEAD~)
313
+
314
+ edited
315
+ EOF
316
+ (
317
+ set_fake_editor &&
318
+ FAKE_COMMIT_AMEND="edited" \
319
+ git commit --fixup=amend:HEAD~ --only
320
+ ) &&
321
+ get_commit_msg HEAD >actual &&
322
+ test_cmp expected actual &&
323
+ test_cmp_rev HEAD@{1}^{tree} HEAD^{tree} &&
324
+ test_cmp_rev HEAD@{1} HEAD^ &&
325
+ test_expect_code 1 git diff --cached --exit-code &&
326
+ git cat-file blob :foo >actual &&
327
+ test_cmp foo actual
328
+'
329
+
330
+test_expect_success '--fixup=reword: ignores staged changes' '
331
+ commit_for_rebase_autosquash_setup &&
332
+ cat >expected <<-EOF &&
333
+ amend! $(git log -1 --format=%s HEAD~)
334
+
335
+ $(get_commit_msg HEAD~)
336
+
337
+ edited
338
+ EOF
339
+ (
340
+ set_fake_editor &&
341
+ FAKE_COMMIT_AMEND="edited" \
342
+ git commit --fixup=reword:HEAD~
343
+ ) &&
344
+ get_commit_msg HEAD >actual &&
345
+ test_cmp expected actual &&
346
+ test_cmp_rev HEAD@{1}^{tree} HEAD^{tree} &&
347
+ test_cmp_rev HEAD@{1} HEAD^ &&
348
+ test_expect_code 1 git diff --cached --exit-code &&
349
+ git cat-file blob :foo >actual &&
350
+ test_cmp foo actual
351
+'
352
+
353
+test_expect_success '--fixup=reword: error out with -m option' '
354
+ commit_for_rebase_autosquash_setup &&
355
+ echo "fatal: cannot combine -m with --fixup:reword" >expect &&
356
+ test_must_fail git commit --fixup=reword:HEAD~ -m "reword commit message" 2>actual &&
357
+ test_cmp expect actual
358
+'
359
+
360
+test_expect_success '--fixup=amend: error out with -m option' '
361
+ commit_for_rebase_autosquash_setup &&
362
+ echo "fatal: cannot combine -m with --fixup:amend" >expect &&
363
+ test_must_fail git commit --fixup=amend:HEAD~ -m "amend commit message" 2>actual &&
364
+ test_cmp expect actual
365
+'
366
+
367
+test_expect_success 'consecutive amend! commits remove amend! line from commit msg body' '
368
+ commit_for_rebase_autosquash_setup &&
369
+ cat >expected <<-EOF &&
370
+ amend! amend! $(git log -1 --format=%s HEAD~)
371
+
372
+ $(get_commit_msg HEAD~)
373
+
374
+ edited 1
375
+
376
+ edited 2
377
+ EOF
378
+ echo "reword new commit message" >actual &&
379
+ (
380
+ set_fake_editor &&
381
+ FAKE_COMMIT_AMEND="edited 1" \
382
+ git commit --fixup=reword:HEAD~ &&
383
+ FAKE_COMMIT_AMEND="edited 2" \
384
+ git commit --fixup=reword:HEAD
385
+ ) &&
386
+ get_commit_msg HEAD >actual &&
387
+ test_cmp expected actual
388
+'
389
+
390
+test_expect_success 'deny to create amend! commit if its commit msg body is empty' '
391
+ commit_for_rebase_autosquash_setup &&
392
+ echo "Aborting commit due to empty commit message body." >expected &&
393
+ (
394
+ set_fake_editor &&
395
+ test_must_fail env FAKE_COMMIT_MESSAGE="amend! target message subject line" \
396
+ git commit --fixup=amend:HEAD~ 2>actual
397
+ ) &&
398
+ test_cmp expected actual
399
+'
400
+
401
+test_expect_success 'amend! commit allows empty commit msg body with --allow-empty-message' '
402
+ commit_for_rebase_autosquash_setup &&
403
+ cat >expected <<-EOF &&
404
+ amend! $(git log -1 --format=%s HEAD~)
405
+ EOF
406
+ (
407
+ set_fake_editor &&
408
+ FAKE_COMMIT_MESSAGE="amend! target message subject line" \
409
+ git commit --fixup=amend:HEAD~ --allow-empty-message &&
410
+ get_commit_msg HEAD >actual
411
+ ) &&
412
+ test_cmp expected actual
413
+'
414
+
415
+test_fixup_reword_opt () {
416
+ test_expect_success C_LOCALE_OUTPUT "--fixup=reword: incompatible with $1" "
417
+ echo 'fatal: reword option of --fixup is mutually exclusive with'\
418
+ '--patch/--interactive/--all/--include/--only' >expect &&
419
+ test_must_fail git commit --fixup=reword:HEAD~ $1 2>actual &&
420
+ test_cmp expect actual
421
+ "
422
+}
423
+
424
+for opt in --all --include --only --interactive --patch
425
+do
426
+ test_fixup_reword_opt $opt
427
+done
428
+
429
+test_expect_success '--fixup=reword: give error with pathsec' '
430
+ commit_for_rebase_autosquash_setup &&
431
+ echo "fatal: cannot combine reword option of --fixup with path '\''foo'\''" >expect &&
432
+ test_must_fail git commit --fixup=reword:HEAD~ -- foo 2>actual &&
433
+ test_cmp expect actual
434
+'
435
+
436
+test_expect_success '--fixup=reword: -F give error message' '
437
+ echo "fatal: Only one of -c/-C/-F/--fixup can be used." >expect &&
438
+ test_must_fail git commit --fixup=reword:HEAD~ -F msg 2>actual &&
439
+ test_cmp expect actual
440
+'
441
442
test_expect_success 'commit --squash works with -F' '
443
commit_for_rebase_autosquash_setup &&