interplane.Get Omega
Read in your language

git

[Ported from github.com/git/git] Sync with Git 2.56.1

Brought by drakestapleton

branch master Branches Tags History

git / t / t3453-history-fixup.sh

719 lines · 14992 bytes Raw Blame History
1#!/bin/sh
2
3test_description='tests for git-history fixup subcommand'
4
5. ./test-lib.sh
6. "$TEST_DIRECTORY/lib-gpg.sh"
7
8fixup_with_message () {
9 cat >message &&
10 write_script fake-editor.sh <<-\EOF &&
11 cp message "$1"
12 EOF
13 test_set_editor "$(pwd)"/fake-editor.sh &&
14 git history fixup --reedit-message "$@" &&
15 rm fake-editor.sh message
16}
17
18expect_changes () {
19 git log --format="%s" --numstat "$@" >actual.raw &&
20 sed '/^$/d' <actual.raw >actual &&
21 cat >expect &&
22 test_cmp expect actual
23}
24
25test_fixup_gpg_sign () {
26 must_fail= will=will
27 if test "x$1" = "x!"
28 then
29 must_fail=test_must_fail
30 will="will not"
31 shift
32 fi
33 conf=$1
34 shift
35
36 test_expect_success GPG "fixup $* with commit.gpgsign=$conf $will sign rewritten history" "
37 test_when_finished 'rm -rf repo' &&
38 git init repo &&
39 (
40 cd repo &&
41 test_commit first &&
42 test_commit second &&
43 test_commit third &&
44
45 git config commit.gpgsign $conf &&
46 echo fix >>second.t &&
47 git add second.t &&
48 git history fixup $* HEAD~ &&
49
50 $must_fail git verify-commit HEAD~ &&
51 $must_fail git verify-commit HEAD
52 )
53 "
54}
55
56test_expect_success 'errors on missing commit argument' '
57 test_when_finished "rm -rf repo" &&
58 git init repo &&
59 (
60 cd repo &&
61 test_commit initial &&
62 test_must_fail git history fixup 2>err &&
63 test_grep "command expects a single revision" err
64 )
65'
66
67test_expect_success 'errors on too many arguments' '
68 test_when_finished "rm -rf repo" &&
69 git init repo &&
70 (
71 cd repo &&
72 test_commit initial &&
73 test_must_fail git history fixup HEAD HEAD 2>err &&
74 test_grep "command expects a single revision" err
75 )
76'
77
78test_expect_success 'errors on unknown revision' '
79 test_when_finished "rm -rf repo" &&
80 git init repo &&
81 (
82 cd repo &&
83 test_commit initial &&
84 test_must_fail git history fixup does-not-exist 2>err &&
85 test_grep "commit cannot be found: does-not-exist" err
86 )
87'
88
89test_expect_success 'errors when nothing is staged' '
90 test_when_finished "rm -rf repo" &&
91 git init repo &&
92 (
93 cd repo &&
94 test_commit initial &&
95 test_must_fail git history fixup HEAD 2>err &&
96 test_grep "nothing to fixup: no staged changes" err
97 )
98'
99
100test_expect_success 'errors in a bare repository' '
101 test_when_finished "rm -rf repo repo.git" &&
102 git init repo &&
103 test_commit -C repo initial &&
104 git clone --bare repo repo.git &&
105 test_must_fail git -C repo.git history fixup HEAD 2>err &&
106 test_grep "cannot run fixup in a bare repository" err
107'
108
109test_expect_success 'errors with invalid --empty= value' '
110 test_when_finished "rm -rf repo" &&
111 git init repo &&
112 test_must_fail git -C repo history fixup --empty=bogus HEAD 2>err &&
113 test_grep "unrecognized.*--empty.*bogus" err
114'
115
116test_expect_success 'can fixup the tip commit' '
117 test_when_finished "rm -rf repo" &&
118 git init repo &&
119 (
120 cd repo &&
121 test_commit initial &&
122 echo content >file.txt &&
123 git add file.txt &&
124 git commit -m "add file" &&
125
126 echo fix >>file.txt &&
127 git add file.txt &&
128
129 expect_changes <<-\EOF &&
130 add file
131 1 0 file.txt
132 initial
133 1 0 initial.t
134 EOF
135
136 git symbolic-ref HEAD >branch-expect &&
137 git history fixup HEAD &&
138 git symbolic-ref HEAD >branch-actual &&
139 test_cmp branch-expect branch-actual &&
140
141 expect_changes <<-\EOF &&
142 add file
143 2 0 file.txt
144 initial
145 1 0 initial.t
146 EOF
147
148 # Verify the fix is in the tip commit tree
149 git show HEAD:file.txt >actual &&
150 printf "content\nfix\n" >expect &&
151 test_cmp expect actual &&
152
153 git reflog >reflog &&
154 test_grep "fixup: updating HEAD" reflog
155 )
156'
157
158test_expect_success 'can fixup a commit in the middle of history' '
159 test_when_finished "rm -rf repo" &&
160 git init repo &&
161 (
162 cd repo &&
163 test_commit first &&
164 echo content >file.txt &&
165 git add file.txt &&
166 git commit -m "add file" &&
167 test_commit third &&
168
169 echo fix >>file.txt &&
170 git add file.txt &&
171
172 expect_changes <<-\EOF &&
173 third
174 1 0 third.t
175 add file
176 1 0 file.txt
177 first
178 1 0 first.t
179 EOF
180
181 git history fixup HEAD~ &&
182
183 expect_changes <<-\EOF &&
184 third
185 1 0 third.t
186 add file
187 2 0 file.txt
188 first
189 1 0 first.t
190 EOF
191
192 # Verify the fix landed in the "add file" commit.
193 git show HEAD~:file.txt >actual &&
194 printf "content\nfix\n" >expect &&
195 test_cmp expect actual &&
196
197 # And verify that the replayed commit also has the change.
198 git show HEAD:file.txt >actual &&
199 printf "content\nfix\n" >expect &&
200 test_cmp expect actual
201 )
202'
203
204test_expect_success 'can fixup root commit' '
205 test_when_finished "rm -rf repo" &&
206 git init repo &&
207 (
208 cd repo &&
209 echo initial >root.txt &&
210 git add root.txt &&
211 git commit -m "root" &&
212 test_commit second &&
213
214 expect_changes <<-\EOF &&
215 second
216 1 0 second.t
217 root
218 1 0 root.txt
219 EOF
220
221 echo fix >>root.txt &&
222 git add root.txt &&
223 git history fixup HEAD~ &&
224
225 expect_changes <<-\EOF &&
226 second
227 1 0 second.t
228 root
229 2 0 root.txt
230 EOF
231
232 git show HEAD~:root.txt >actual &&
233 printf "initial\nfix\n" >expect &&
234 test_cmp expect actual
235 )
236'
237
238test_expect_success 'preserves commit message and authorship' '
239 test_when_finished "rm -rf repo" &&
240 git init repo &&
241 (
242 cd repo &&
243 test_commit initial &&
244 echo content >file.txt &&
245 git add file.txt &&
246 git commit --author="Original <original@example.com>" -m "original message" &&
247
248 echo fix >>file.txt &&
249 git add file.txt &&
250 git history fixup HEAD &&
251
252 # Message preserved
253 git log -1 --format="%s" >actual &&
254 echo "original message" >expect &&
255 test_cmp expect actual &&
256
257 # Authorship preserved
258 git log -1 --format="%an <%ae>" >actual &&
259 echo "Original <original@example.com>" >expect &&
260 test_cmp expect actual
261 )
262'
263
264test_fixup_gpg_sign ! false
265test_fixup_gpg_sign true
266test_fixup_gpg_sign false --gpg-sign
267test_fixup_gpg_sign ! true --no-gpg-sign
268test_fixup_gpg_sign ! true --gpg-sign --no-gpg-sign
269test_fixup_gpg_sign false --no-gpg-sign --gpg-sign
270
271test_expect_success 'updates all descendant branches by default' '
272 test_when_finished "rm -rf repo" &&
273 git init repo --initial-branch=main &&
274 (
275 cd repo &&
276 test_commit base &&
277 git branch branch &&
278 test_commit ours &&
279 git switch branch &&
280 test_commit theirs &&
281 git switch main &&
282
283 echo fix >fix.txt &&
284 git add fix.txt &&
285 git history fixup base &&
286
287 expect_changes --branches <<-\EOF &&
288 theirs
289 1 0 theirs.t
290 ours
291 1 0 ours.t
292 base
293 1 0 base.t
294 1 0 fix.txt
295 EOF
296
297 # Both branches should have the fix in the base
298 git show main~:fix.txt >actual &&
299 echo fix >expect &&
300 test_cmp expect actual &&
301 git show branch~:fix.txt >actual &&
302 test_cmp expect actual
303 )
304'
305
306test_expect_success 'can fixup commit on a different branch' '
307 test_when_finished "rm -rf repo" &&
308 git init repo &&
309 (
310 cd repo &&
311 test_commit base &&
312 git branch theirs &&
313 test_commit ours &&
314 git switch theirs &&
315 test_commit theirs &&
316
317 # Stage a change while on "theirs"
318 echo fix >fix.txt &&
319 git add fix.txt &&
320
321 # Ensure that "ours" does not change, as it does not contain
322 # the commit in question.
323 git rev-parse ours >ours-before &&
324 git history fixup theirs &&
325 git rev-parse ours >ours-after &&
326 test_cmp ours-before ours-after &&
327
328 git show HEAD:fix.txt >actual &&
329 echo fix >expect &&
330 test_cmp expect actual
331 )
332'
333
334test_expect_success '--dry-run prints ref updates without modifying repo' '
335 test_when_finished "rm -rf repo" &&
336 git init repo --initial-branch=main &&
337 (
338 cd repo &&
339 test_commit base &&
340 git branch branch &&
341 test_commit main-tip &&
342 git switch branch &&
343 test_commit branch-tip &&
344 git switch main &&
345
346 echo fix >fix.txt &&
347 git add fix.txt &&
348
349 git refs list >refs-before &&
350 git history fixup --dry-run base >updates &&
351 git refs list >refs-after &&
352 test_cmp refs-before refs-after &&
353
354 test_grep "update refs/heads/main" updates &&
355 test_grep "update refs/heads/branch" updates &&
356
357 expect_changes --branches <<-\EOF &&
358 branch-tip
359 1 0 branch-tip.t
360 main-tip
361 1 0 main-tip.t
362 base
363 1 0 base.t
364 EOF
365
366 git update-ref --stdin <updates &&
367 expect_changes --branches <<-\EOF
368 branch-tip
369 1 0 branch-tip.t
370 main-tip
371 1 0 main-tip.t
372 base
373 1 0 base.t
374 1 0 fix.txt
375 EOF
376 )
377'
378
379test_expect_success '--update-refs=head updates only HEAD' '
380 test_when_finished "rm -rf repo" &&
381 git init repo --initial-branch=main &&
382 (
383 cd repo &&
384 test_commit base &&
385 git branch branch &&
386 test_commit main-tip &&
387 git switch branch &&
388 test_commit branch-tip &&
389
390 echo fix >fix.txt &&
391 git add fix.txt &&
392
393 # Only HEAD (branch) should be updated
394 git history fixup --update-refs=head base &&
395
396 # The main branch should be unaffected.
397 expect_changes main <<-\EOF &&
398 main-tip
399 1 0 main-tip.t
400 base
401 1 0 base.t
402 EOF
403
404 # But the currently checked out branch should be modified.
405 expect_changes branch <<-\EOF
406 branch-tip
407 1 0 branch-tip.t
408 base
409 1 0 base.t
410 1 0 fix.txt
411 EOF
412 )
413'
414
415test_expect_success '--update-refs=head refuses to rewrite commits not in HEAD ancestry' '
416 test_when_finished "rm -rf repo" &&
417 git init repo --initial-branch=main &&
418 (
419 cd repo &&
420 test_commit base &&
421 git branch other &&
422 test_commit main-tip &&
423 git switch other &&
424 test_commit other-tip &&
425
426 echo fix >fix.txt &&
427 git add fix.txt &&
428
429 test_must_fail git history fixup --update-refs=head main-tip 2>err &&
430 test_grep "rewritten commit must be an ancestor of HEAD" err
431 )
432'
433
434test_expect_success 'aborts when fixup would produce conflicts' '
435 test_when_finished "rm -rf repo" &&
436 git init repo &&
437 (
438 cd repo &&
439
440 echo "line one" >file.txt &&
441 git add file.txt &&
442 git commit -m "first" &&
443
444 echo "line two" >file.txt &&
445 git add file.txt &&
446 git commit -m "second" &&
447
448 echo "conflicting change" >file.txt &&
449 git add file.txt &&
450
451 git refs list >refs-before &&
452 test_must_fail git history fixup HEAD~ 2>err &&
453 test_grep "fixup would produce conflicts" err &&
454 git refs list >refs-after &&
455 test_cmp refs-before refs-after
456 )
457'
458
459test_expect_success '--reedit-message opens editor for the commit message' '
460 test_when_finished "rm -rf repo" &&
461 git init repo &&
462 (
463 cd repo &&
464 test_commit initial &&
465 echo content >file.txt &&
466 git add file.txt &&
467 git commit -m "add file" &&
468
469 echo fix >>file.txt &&
470 git add file.txt &&
471
472 fixup_with_message HEAD <<-\EOF &&
473 add file with fix
474 EOF
475
476 expect_changes --branches <<-\EOF
477 add file with fix
478 2 0 file.txt
479 initial
480 1 0 initial.t
481 EOF
482 )
483'
484
485test_expect_success 'retains unstaged working tree changes after fixup' '
486 test_when_finished "rm -rf repo" &&
487 git init repo &&
488 (
489 cd repo &&
490 touch a b &&
491 git add . &&
492 git commit -m "initial commit" &&
493 echo staged >a &&
494 echo unstaged >b &&
495 git add a &&
496 git history fixup HEAD &&
497
498 # b is still modified in the worktree but not staged
499 cat >expect <<-\EOF &&
500 M b
501 EOF
502 git status --porcelain --untracked-files=no >actual &&
503 test_cmp expect actual
504 )
505'
506
507test_expect_success 'index is clean after fixup when target is HEAD' '
508 test_when_finished "rm -rf repo" &&
509 git init repo &&
510 (
511 cd repo &&
512
513 test_commit initial &&
514 echo fix >fix.txt &&
515 git add fix.txt &&
516 git history fixup HEAD &&
517
518 git status --porcelain --untracked-files=no >actual &&
519 test_must_be_empty actual
520 )
521'
522
523test_expect_success 'index is unchanged on conflict' '
524 test_when_finished "rm -rf repo" &&
525 git init repo &&
526 (
527 cd repo &&
528
529 echo base >file.txt &&
530 git add file.txt &&
531 git commit -m base &&
532 echo change >file.txt &&
533 git add file.txt &&
534 git commit -m change &&
535
536 echo conflict >file.txt &&
537 git add file.txt &&
538
539 git diff --cached >index-before &&
540 test_must_fail git history fixup HEAD~ &&
541 git diff --cached >index-after &&
542 test_cmp index-before index-after
543 )
544'
545
546test_expect_success '--empty=drop removes target commit and replays descendants onto its parent' '
547 test_when_finished "rm -rf repo" &&
548 git init repo --initial-branch=main &&
549 (
550 cd repo &&
551
552 test_commit first &&
553 test_commit second &&
554 test_commit third &&
555
556 git rm second.t &&
557 git history fixup --empty=drop HEAD~ &&
558
559 expect_changes <<-\EOF &&
560 third
561 1 0 third.t
562 first
563 1 0 first.t
564 EOF
565 test_must_fail git show HEAD:second.t
566 )
567'
568
569test_expect_success '--empty=drop errors out when dropping the root commit' '
570 test_when_finished "rm -rf repo" &&
571 git init repo &&
572 (
573 cd repo &&
574
575 test_commit first &&
576 test_commit second &&
577
578 git rm first.t &&
579 test_must_fail git history fixup --empty=drop HEAD~ 2>err &&
580 test_grep "cannot drop root commit" err
581 )
582'
583
584test_expect_success '--empty=drop can drop the HEAD commit' '
585 test_when_finished "rm -rf repo" &&
586 git init repo &&
587 (
588 cd repo &&
589
590 test_commit first &&
591 test_commit second &&
592
593 git rm second.t &&
594 git history fixup --empty=drop HEAD &&
595
596 expect_changes <<-\EOF
597 first
598 1 0 first.t
599 EOF
600 )
601'
602
603test_expect_success '--empty=drop drops empty replayed commits' '
604 test_when_finished "rm -rf repo" &&
605 git init repo &&
606 (
607 cd repo &&
608
609 touch base remove-me &&
610 git add . &&
611 git commit -m "base" &&
612 git rm remove-me &&
613 git commit -m "remove" &&
614 touch reintroduce remove-me &&
615 git add . &&
616 git commit -m "reintroduce" &&
617
618 git rm remove-me &&
619 git history fixup --empty=drop HEAD~2 &&
620
621 expect_changes <<-\EOF
622 reintroduce
623 0 0 reintroduce
624 0 0 remove-me
625 base
626 0 0 base
627 EOF
628 )
629'
630
631test_expect_success '--empty=keep keeps commit when fixup target becomes empty' '
632 test_when_finished "rm -rf repo" &&
633 git init repo &&
634 (
635 cd repo &&
636
637 test_commit first &&
638 test_commit second &&
639 test_commit third &&
640
641 git rm second.t &&
642 git history fixup --empty=keep HEAD~ &&
643
644 expect_changes <<-\EOF
645 third
646 1 0 third.t
647 second
648 first
649 1 0 first.t
650 EOF
651 )
652'
653
654test_expect_success '--empty=keep keeps commit when replayed commit becomes empty' '
655 test_when_finished "rm -rf repo" &&
656 git init repo &&
657 (
658 cd repo &&
659
660 touch base remove-me &&
661 git add . &&
662 git commit -m "base" &&
663 git rm remove-me &&
664 git commit -m "remove" &&
665 touch reintroduce remove-me &&
666 git add . &&
667 git commit -m "reintroduce" &&
668
669 git rm remove-me &&
670 git history fixup --empty=keep HEAD~2 &&
671
672 expect_changes <<-\EOF
673 reintroduce
674 0 0 reintroduce
675 0 0 remove-me
676 remove
677 base
678 0 0 base
679 EOF
680 )
681'
682
683test_expect_success '--empty=abort errors out when fixup target becomes empty' '
684 test_when_finished "rm -rf repo" &&
685 git init repo &&
686 (
687 cd repo &&
688
689 test_commit first &&
690 test_commit second &&
691
692 git rm first.t &&
693 test_must_fail git history fixup --empty=abort HEAD~ 2>err &&
694 test_grep "fixup makes commit.*empty" err
695 )
696'
697
698test_expect_success '--empty=abort errors out when a descendant becomes empty during replay' '
699 test_when_finished "rm -rf repo" &&
700 git init repo --initial-branch=main &&
701 (
702 cd repo &&
703
704 touch base remove-me &&
705 git add . &&
706 git commit -m "base" &&
707 git rm remove-me &&
708 git commit -m "remove" &&
709 touch reintroduce remove-me &&
710 git add . &&
711 git commit -m "reintroduce" &&
712
713 git rm remove-me &&
714 test_must_fail git history fixup --empty=abort HEAD~2 2>err &&
715 test_grep "became empty after replay" err
716 )
717'
718
719test_done