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 / t3452-history-split.sh

801 lines · 14616 bytes Raw Blame History
1#!/bin/sh
2
3test_description='tests for git-history split subcommand'
4
5. ./test-lib.sh
6. "$TEST_DIRECTORY/lib-log-graph.sh"
7. "$TEST_DIRECTORY/lib-gpg.sh"
8
9# The fake editor takes multiple arguments, each of which represents a commit
10# message. Subsequent invocations of the editor will then yield those messages
11# in order.
12#
13set_fake_editor () {
14 printf "%s\n" "$@" >fake-input &&
15 write_script fake-editor.sh <<-\EOF &&
16 head -n1 fake-input >"$1"
17 sed 1d fake-input >fake-input.trimmed &&
18 mv fake-input.trimmed fake-input
19 EOF
20 test_set_editor "$(pwd)"/fake-editor.sh
21}
22
23expect_graph () {
24 cat >expect &&
25 lib_test_cmp_graph --graph --format=%s "$@"
26}
27
28expect_log () {
29 git log --format="%s" >actual &&
30 cat >expect &&
31 test_cmp expect actual
32}
33
34expect_tree_entries () {
35 git ls-tree --name-only "$1" >actual &&
36 cat >expect &&
37 test_cmp expect actual
38}
39
40test_split_gpg_sign () {
41 must_fail= will=will
42 if test "x$1" = "x!"
43 then
44 must_fail=test_must_fail
45 will="will not"
46 shift
47 fi
48 conf=$1
49 shift
50
51 test_expect_success GPG "split $* with commit.gpgsign=$conf $will sign rewritten history" "
52 test_when_finished 'rm -rf repo' &&
53 git init repo &&
54 (
55 cd repo &&
56 test_commit initial &&
57 touch bar foo &&
58 git add . &&
59 git commit -m split-me &&
60 test_commit tip &&
61
62 git config commit.gpgsign $conf &&
63 set_fake_editor 'first' 'second' &&
64 git history split $* HEAD~ <<-EOF &&
65 y
66 n
67 EOF
68
69 $must_fail git verify-commit HEAD~2 &&
70 $must_fail git verify-commit HEAD~ &&
71 $must_fail git verify-commit HEAD
72 )
73 "
74}
75
76test_expect_success 'refuses to work with merge commits' '
77 test_when_finished "rm -rf repo" &&
78 git init repo &&
79 (
80 cd repo &&
81 test_commit base &&
82 git branch branch &&
83 test_commit ours &&
84 git switch branch &&
85 test_commit theirs &&
86 git switch - &&
87 git merge theirs &&
88 test_must_fail git history split HEAD 2>err &&
89 test_grep "cannot split up merge commit" err &&
90 test_must_fail git history split HEAD~ 2>err &&
91 test_grep "replaying merge commits is not supported yet" err
92 )
93'
94
95test_expect_success 'errors on missing commit argument' '
96 test_when_finished "rm -rf repo" &&
97 git init repo &&
98 (
99 cd repo &&
100 test_commit initial &&
101 test_must_fail git history split 2>err &&
102 test_grep "command expects a committish" err
103 )
104'
105
106test_expect_success 'errors on unknown revision' '
107 test_when_finished "rm -rf repo" &&
108 git init repo &&
109 (
110 cd repo &&
111 test_commit initial &&
112 test_must_fail git history split does-not-exist 2>err &&
113 test_grep "commit cannot be found" err
114 )
115'
116
117test_expect_success '--dry-run does not modify any refs' '
118 test_when_finished "rm -rf repo" &&
119 git init repo &&
120 (
121 cd repo &&
122 test_commit base &&
123 touch bar foo &&
124 git add . &&
125 git commit -m split-me &&
126
127 git refs list --include-root-refs >before &&
128
129 set_fake_editor "first" "second" &&
130 git history split --dry-run HEAD <<-EOF &&
131 y
132 n
133 EOF
134
135 git refs list --include-root-refs >after &&
136 test_cmp before after
137 )
138'
139
140test_expect_success 'can split up tip commit' '
141 test_when_finished "rm -rf repo" &&
142 git init repo &&
143 (
144 cd repo &&
145 test_commit initial &&
146 touch bar foo &&
147 git add . &&
148 git commit -m split-me &&
149
150 git symbolic-ref HEAD >expect &&
151 set_fake_editor "first" "second" &&
152 git history split HEAD <<-EOF &&
153 y
154 n
155 EOF
156 git symbolic-ref HEAD >actual &&
157 test_cmp expect actual &&
158
159 expect_log <<-EOF &&
160 second
161 first
162 initial
163 EOF
164
165 expect_tree_entries HEAD~ <<-EOF &&
166 bar
167 initial.t
168 EOF
169
170 expect_tree_entries HEAD <<-EOF &&
171 bar
172 foo
173 initial.t
174 EOF
175
176 git reflog >reflog &&
177 test_grep "split: updating HEAD" reflog
178 )
179'
180
181test_split_gpg_sign ! false
182test_split_gpg_sign true
183test_split_gpg_sign false --gpg-sign
184test_split_gpg_sign ! true --no-gpg-sign
185test_split_gpg_sign ! true --gpg-sign --no-gpg-sign
186test_split_gpg_sign false --no-gpg-sign --gpg-sign
187
188test_expect_success 'can split up root commit' '
189 test_when_finished "rm -rf repo" &&
190 git init repo &&
191 (
192 cd repo &&
193 touch bar foo &&
194 git add . &&
195 git commit -m root &&
196 test_commit tip &&
197
198 set_fake_editor "first" "second" &&
199 git history split HEAD~ <<-EOF &&
200 y
201 n
202 EOF
203
204 expect_log <<-EOF &&
205 tip
206 second
207 first
208 EOF
209
210 expect_tree_entries HEAD~2 <<-EOF &&
211 bar
212 EOF
213
214 expect_tree_entries HEAD~ <<-EOF &&
215 bar
216 foo
217 EOF
218
219 expect_tree_entries HEAD <<-EOF
220 bar
221 foo
222 tip.t
223 EOF
224 )
225'
226
227test_expect_success 'can split up in-between commit' '
228 test_when_finished "rm -rf repo" &&
229 git init repo &&
230 (
231 cd repo &&
232 test_commit initial &&
233 touch bar foo &&
234 git add . &&
235 git commit -m split-me &&
236 test_commit tip &&
237
238 set_fake_editor "first" "second" &&
239 git history split HEAD~ <<-EOF &&
240 y
241 n
242 EOF
243
244 expect_log <<-EOF &&
245 tip
246 second
247 first
248 initial
249 EOF
250
251 expect_tree_entries HEAD~2 <<-EOF &&
252 bar
253 initial.t
254 EOF
255
256 expect_tree_entries HEAD~ <<-EOF &&
257 bar
258 foo
259 initial.t
260 EOF
261
262 expect_tree_entries HEAD <<-EOF
263 bar
264 foo
265 initial.t
266 tip.t
267 EOF
268 )
269'
270
271test_expect_success 'can split HEAD only' '
272 test_when_finished "rm -rf repo" &&
273 git init repo &&
274 (
275 cd repo &&
276 test_commit base &&
277 touch a b &&
278 git add . &&
279 git commit -m split-me &&
280 git branch unrelated &&
281
282 set_fake_editor "ours-a" "ours-b" &&
283 git history split --update-refs=head HEAD <<-EOF &&
284 y
285 n
286 EOF
287 expect_graph --branches <<-EOF
288 * ours-b
289 * ours-a
290 | * split-me
291 |/
292 * base
293 EOF
294 )
295'
296
297test_expect_success 'can split detached HEAD' '
298 test_when_finished "rm -rf repo" &&
299 git init repo &&
300 (
301 cd repo &&
302 test_commit initial &&
303 touch bar foo &&
304 git add . &&
305 git commit -m split-me &&
306 git checkout --detach HEAD &&
307
308 set_fake_editor "first" "second" &&
309 git history split --update-refs=head HEAD <<-EOF &&
310 y
311 n
312 EOF
313
314 # HEAD should be detached and updated.
315 test_must_fail git symbolic-ref HEAD &&
316
317 expect_log <<-EOF
318 second
319 first
320 initial
321 EOF
322 )
323'
324
325test_expect_success 'can split commit in unrelated branch' '
326 test_when_finished "rm -rf repo" &&
327 git init repo &&
328 (
329 cd repo &&
330 test_commit base &&
331 git branch ours &&
332 git switch --create theirs &&
333 touch theirs-a theirs-b &&
334 git add . &&
335 git commit -m theirs &&
336 git switch ours &&
337 test_commit ours &&
338
339 # With --update-refs=head it is not possible to split up a
340 # commit that is unrelated to HEAD.
341 test_must_fail git history split --update-refs=head theirs 2>err &&
342 test_grep "rewritten commit must be an ancestor of HEAD" err &&
343
344 set_fake_editor "theirs-rewritten-a" "theirs-rewritten-b" &&
345 git history split theirs <<-EOF &&
346 y
347 n
348 EOF
349 expect_graph --branches <<-EOF &&
350 * ours
351 | * theirs-rewritten-b
352 | * theirs-rewritten-a
353 |/
354 * base
355 EOF
356
357 expect_tree_entries theirs~ <<-EOF &&
358 base.t
359 theirs-a
360 EOF
361
362 expect_tree_entries theirs <<-EOF
363 base.t
364 theirs-a
365 theirs-b
366 EOF
367 )
368'
369
370test_expect_success 'updates multiple descendant branches' '
371 test_when_finished "rm -rf repo" &&
372 git init repo --initial-branch=main &&
373 (
374 cd repo &&
375 test_commit base &&
376 touch file-a file-b &&
377 git add . &&
378 git commit -m split-me &&
379 git branch branch &&
380 test_commit on-main &&
381 git switch branch &&
382 test_commit on-branch &&
383 git switch main &&
384
385 set_fake_editor "split-a" "split-b" &&
386 git history split HEAD~ <<-EOF &&
387 y
388 n
389 EOF
390
391 # Both branches should now descend from the split commits.
392 expect_graph --branches <<-EOF
393 * on-branch
394 | * on-main
395 |/
396 * split-b
397 * split-a
398 * base
399 EOF
400 )
401'
402
403test_expect_success 'can pick multiple hunks' '
404 test_when_finished "rm -rf repo" &&
405 git init repo &&
406 (
407 cd repo &&
408 touch bar baz foo qux &&
409 git add . &&
410 git commit -m split-me &&
411
412 set_fake_editor "first" "second" &&
413 git history split HEAD <<-EOF &&
414 y
415 n
416 y
417 n
418 EOF
419
420 expect_tree_entries HEAD~ <<-EOF &&
421 bar
422 foo
423 EOF
424
425 expect_tree_entries HEAD <<-EOF
426 bar
427 baz
428 foo
429 qux
430 EOF
431 )
432'
433
434test_expect_success 'can use only last hunk' '
435 test_when_finished "rm -rf repo" &&
436 git init repo &&
437 (
438 cd repo &&
439 touch bar foo &&
440 git add . &&
441 git commit -m split-me &&
442
443 set_fake_editor "first" "second" &&
444 git history split HEAD <<-EOF &&
445 n
446 y
447 EOF
448
449 expect_log <<-EOF &&
450 second
451 first
452 EOF
453
454 expect_tree_entries HEAD~ <<-EOF &&
455 foo
456 EOF
457
458 expect_tree_entries HEAD <<-EOF
459 bar
460 foo
461 EOF
462 )
463'
464
465test_expect_success 'can split commit with file deletions' '
466 test_when_finished "rm -rf repo" &&
467 git init repo &&
468 (
469 cd repo &&
470 echo a >a &&
471 echo b >b &&
472 echo c >c &&
473 git add . &&
474 git commit -m base &&
475 git rm a b &&
476 git commit -m delete-both &&
477
478 set_fake_editor "delete-a" "delete-b" &&
479 git history split HEAD <<-EOF &&
480 y
481 n
482 EOF
483
484 expect_log <<-EOF &&
485 delete-b
486 delete-a
487 base
488 EOF
489
490 expect_tree_entries HEAD~ <<-EOF &&
491 b
492 c
493 EOF
494
495 expect_tree_entries HEAD <<-EOF
496 c
497 EOF
498 )
499'
500
501test_expect_success 'preserves original authorship' '
502 test_when_finished "rm -rf repo" &&
503 git init repo &&
504 (
505 cd repo &&
506 test_commit initial &&
507 touch bar foo &&
508 git add . &&
509 GIT_AUTHOR_NAME="Other Author" \
510 GIT_AUTHOR_EMAIL="other@example.com" \
511 git commit -m split-me &&
512
513 set_fake_editor "first" "second" &&
514 git history split HEAD <<-EOF &&
515 y
516 n
517 EOF
518
519 git log -1 --format="%an <%ae>" HEAD~ >actual &&
520 echo "Other Author <other@example.com>" >expect &&
521 test_cmp expect actual &&
522
523 git log -1 --format="%an <%ae>" HEAD >actual &&
524 test_cmp expect actual
525 )
526'
527
528test_expect_success 'aborts with empty commit message' '
529 test_when_finished "rm -rf repo" &&
530 git init repo &&
531 (
532 cd repo &&
533 touch bar foo &&
534 git add . &&
535 git commit -m split-me &&
536
537 set_fake_editor "" &&
538 test_must_fail git history split HEAD <<-EOF 2>err &&
539 y
540 n
541 EOF
542 test_grep "Aborting commit due to empty commit message." err
543 )
544'
545
546test_expect_success 'commit message editor sees split-out changes' '
547 test_when_finished "rm -rf repo" &&
548 git init repo &&
549 (
550 cd repo &&
551 touch bar foo &&
552 git add . &&
553 git commit -m split-me &&
554
555 write_script fake-editor.sh <<-\EOF &&
556 cat "$1" >>MESSAGES &&
557 echo "some commit message" >"$1"
558 EOF
559 test_set_editor "$(pwd)"/fake-editor.sh &&
560
561 git history split HEAD <<-EOF &&
562 y
563 n
564 EOF
565
566 # Note that we expect to see the messages twice, once for each
567 # of the commits. The committed files are different though.
568 cat >expect <<-EOF &&
569 split-me
570
571 # Please enter the commit message for the split-out changes. Lines starting
572 # with ${SQ}#${SQ} will be ignored, and an empty message aborts the commit.
573 # Changes to be committed:
574 # new file: bar
575 #
576 split-me
577
578 # Please enter the commit message for the split-out changes. Lines starting
579 # with ${SQ}#${SQ} will be ignored, and an empty message aborts the commit.
580 # Changes to be committed:
581 # new file: foo
582 #
583 EOF
584 test_cmp expect MESSAGES &&
585
586 expect_log <<-EOF
587 some commit message
588 some commit message
589 EOF
590 )
591'
592
593test_expect_success 'can use pathspec to limit what gets split' '
594 test_when_finished "rm -rf repo" &&
595 git init repo &&
596 (
597 cd repo &&
598 touch bar foo &&
599 git add . &&
600 git commit -m split-me &&
601
602 set_fake_editor "first" "second" &&
603 git history split HEAD -- foo <<-EOF &&
604 y
605 EOF
606
607 expect_tree_entries HEAD~ <<-EOF &&
608 foo
609 EOF
610
611 expect_tree_entries HEAD <<-EOF
612 bar
613 foo
614 EOF
615 )
616'
617
618test_expect_success 'pathspec matching no files produces empty split error' '
619 test_when_finished "rm -rf repo" &&
620 git init repo &&
621 (
622 cd repo &&
623 test_commit initial &&
624 touch bar foo &&
625 git add . &&
626 git commit -m split-me &&
627
628 set_fake_editor "first" "second" &&
629 test_must_fail git history split HEAD -- nonexistent 2>err &&
630 test_grep "split commit is empty" err
631 )
632'
633
634test_expect_success 'split with multiple pathspecs' '
635 test_when_finished "rm -rf repo" &&
636 git init repo &&
637 (
638 cd repo &&
639 test_commit initial &&
640 touch a b c d &&
641 git add . &&
642 git commit -m split-me &&
643
644 # Only a and c should be offered for splitting.
645 set_fake_editor "split-ac" "remainder" &&
646 git history split HEAD -- a c <<-EOF &&
647 y
648 y
649 EOF
650
651 expect_tree_entries HEAD~ <<-EOF &&
652 a
653 c
654 initial.t
655 EOF
656
657 expect_tree_entries HEAD <<-EOF
658 a
659 b
660 c
661 d
662 initial.t
663 EOF
664 )
665'
666
667test_expect_success 'split with file mode change' '
668 test_when_finished "rm -rf repo" &&
669 git init repo &&
670 (
671 cd repo &&
672 echo content >script &&
673 git add . &&
674 git commit -m base &&
675 test_chmod +x script &&
676 echo change >script &&
677 git commit -a -m "mode and content change" &&
678
679 set_fake_editor "mode-change" "content-change" &&
680 git history split HEAD <<-EOF &&
681 y
682 n
683 EOF
684
685 expect_log <<-EOF
686 content-change
687 mode-change
688 base
689 EOF
690 )
691'
692
693test_expect_success 'refuses to create empty split-out commit' '
694 test_when_finished "rm -rf repo" &&
695 git init repo &&
696 (
697 cd repo &&
698 test_commit base &&
699 touch bar foo &&
700 git add . &&
701 git commit -m split-me &&
702
703 test_must_fail git history split HEAD 2>err <<-EOF &&
704 n
705 n
706 EOF
707 test_grep "split commit is empty" err
708 )
709'
710
711test_expect_success 'hooks are not executed for rewritten commits' '
712 test_when_finished "rm -rf repo" &&
713 git init repo &&
714 (
715 cd repo &&
716 touch bar foo &&
717 git add . &&
718 git commit -m split-me &&
719 old_head=$(git rev-parse HEAD) &&
720
721 ORIG_PATH="$(pwd)" &&
722 export ORIG_PATH &&
723 for hook in prepare-commit-msg pre-commit post-commit post-rewrite commit-msg
724 do
725 write_script .git/hooks/$hook <<-\EOF || exit 1
726 touch "$ORIG_PATH"/hooks.log
727 EOF
728 done &&
729
730 set_fake_editor "first" "second" &&
731 git history split HEAD <<-EOF &&
732 y
733 n
734 EOF
735
736 expect_log <<-EOF &&
737 second
738 first
739 EOF
740
741 test_path_is_missing hooks.log
742 )
743'
744
745test_expect_success 'refuses to create empty original commit' '
746 test_when_finished "rm -rf repo" &&
747 git init repo &&
748 (
749 cd repo &&
750 touch bar foo &&
751 git add . &&
752 git commit -m split-me &&
753
754 test_must_fail git history split HEAD 2>err <<-EOF &&
755 y
756 y
757 EOF
758 test_grep "split commit tree matches original commit" err
759 )
760'
761
762test_expect_success 'retains changes in the worktree and index' '
763 test_when_finished "rm -rf repo" &&
764 git init repo &&
765 (
766 cd repo &&
767 echo a >a &&
768 echo b >b &&
769 git add . &&
770 git commit -m "initial commit" &&
771 echo a-modified >a &&
772 echo b-modified >b &&
773 git add b &&
774 set_fake_editor "a-only" "remainder" &&
775 git history split HEAD <<-EOF &&
776 y
777 n
778 EOF
779
780 expect_tree_entries HEAD~ <<-EOF &&
781 a
782 EOF
783 expect_tree_entries HEAD <<-EOF &&
784 a
785 b
786 EOF
787
788 cat >expect <<-\EOF &&
789 M a
790 M b
791 ?? actual
792 ?? expect
793 ?? fake-editor.sh
794 ?? fake-input
795 EOF
796 git status --porcelain >actual &&
797 test_cmp expect actual
798 )
799'
800
801test_done