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 / t3455-history-squash.sh

947 lines · 26212 bytes Raw Blame History
1#!/bin/sh
2
3test_description='tests for git-history squash subcommand'
4
5. ./test-lib.sh
6. "$TEST_DIRECTORY/lib-gpg.sh"
7
8stage_file () {
9 printf "%s\n" "$1" >file &&
10 git add file
11}
12
13commit_with_message () {
14 printf "%b" "$1" >msg &&
15 git commit --allow-empty -qF msg
16}
17
18check_commit_count () {
19 git rev-list --count "$1" >actual &&
20 echo "$2" >expect &&
21 test_cmp expect actual
22}
23
24check_log_subjects () {
25 git log --format="%s" "$1" >actual &&
26 cat >expect &&
27 test_cmp expect actual
28}
29
30check_log_messages () {
31 git log --format="%B" "$1" >actual &&
32 cat >expect &&
33 test_cmp expect actual
34}
35
36# Checks that the author data of two commits matches
37# Usage: check_commit_author <rev1> <rev2>
38check_commit_author () {
39 git show -s --format="%an <%ae> %ad" "$1" >expect &&
40 git show -s --format="%an <%ae> %ad" "$2" >actual &&
41 test_cmp expect actual
42}
43
44test_squash_gpg_sign () {
45 must_fail= will=will
46 if test "x$1" = "x!"
47 then
48 must_fail=test_must_fail
49 will="will not"
50 shift
51 fi
52 conf=$1
53 shift
54
55 test_expect_success GPG "squash $* with commit.gpgsign=$conf $will sign rewritten history" "
56 test_when_finished 'rm -rf repo' &&
57 git init repo &&
58 (
59 cd repo &&
60 test_commit first &&
61 test_commit second &&
62 test_commit third &&
63 test_commit fourth &&
64
65 git config commit.gpgsign $conf &&
66 git history squash --no-edit $* HEAD~3..HEAD~1 &&
67
68 $must_fail git verify-commit HEAD~ &&
69 $must_fail git verify-commit HEAD
70 )
71 "
72}
73
74test_squash_gpg_sign ! false
75test_squash_gpg_sign true
76test_squash_gpg_sign false --gpg-sign
77test_squash_gpg_sign ! true --no-gpg-sign
78test_squash_gpg_sign ! true --gpg-sign --no-gpg-sign
79test_squash_gpg_sign false --no-gpg-sign --gpg-sign
80
81test_expect_success GPG 'squash uses an explicit signing key for rewritten history' '
82 test_when_finished "rm -rf repo" &&
83 git init repo &&
84 (
85 cd repo &&
86 test_commit first &&
87 test_commit second &&
88 test_commit third &&
89 test_commit fourth &&
90
91 git history squash --no-edit -SB7227189 HEAD~3..HEAD~1 &&
92
93 git verify-commit HEAD~ &&
94 git verify-commit HEAD &&
95 git log -2 --format=%GK >actual &&
96 cat >expect <<-\EOF &&
97 65A0EEA02E30CAD7
98 65A0EEA02E30CAD7
99 EOF
100 test_cmp expect actual
101 )
102'
103
104test_expect_success 'setup linear history touching two files' '
105 test_commit base file a start &&
106 GIT_AUTHOR_NAME=One GIT_AUTHOR_EMAIL=one@example.com \
107 test_commit one other x &&
108 GIT_AUTHOR_NAME=Two GIT_AUTHOR_EMAIL=two@example.com \
109 test_commit two file c &&
110 GIT_AUTHOR_NAME=Three GIT_AUTHOR_EMAIL=three@example.com \
111 test_commit three file d
112'
113
114test_expect_success 'errors on missing range argument' '
115 test_must_fail git history squash 2>err &&
116 test_grep "expects a revision range" err
117'
118
119test_expect_success 'errors on an empty range' '
120 test_must_fail git history squash HEAD..HEAD 2>err &&
121 test_grep "the revision range is empty" err
122'
123
124test_expect_success 'errors on a single revision that is not a range' '
125 test_must_fail git history squash HEAD 2>err &&
126 test_grep "not a .*range" err &&
127 test_must_fail git history squash HEAD~1 2>err &&
128 test_grep "not a .*range" err
129'
130
131test_expect_success 'errors on a range holding a single commit' '
132 git reset --hard three &&
133 head_before=$(git rev-parse HEAD) &&
134
135 test_must_fail git history squash "HEAD^!" 2>err &&
136 test_grep "single commit; nothing to squash" err &&
137 test_cmp_rev "$head_before" HEAD
138'
139
140test_expect_success 'rejects root commit' '
141 # create a disconnected root commit
142 oid=$(git commit-tree -m root three^{tree}) &&
143 # because we pass --ancestry-path when calling setup_revs() it the
144 # revision walk will only include commits decended from $oid so
145 # we need to give it another --ancestry-path commit to actually walk
146 # any commits.
147 test_must_fail git history squash --ancestry-path=start $oid..three 2>err &&
148 echo "error: cannot squash down to root commit" >expect &&
149 test_cmp expect err
150'
151
152test_expect_success 'rejects multiple tips' '
153 oid=$(git commit-tree -m tip -p start^0 three^{tree}) &&
154 test_must_fail git history squash ^start $oid three~1 2>err &&
155 echo "error: the revision range contains more than one tip commit" >expect &&
156 test_cmp expect err &&
157
158 git reset --hard three &&
159 git history squash --no-edit ^start three~1 three &&
160 test_cmp_rev HEAD~1 start^0 &&
161 test_cmp_rev HEAD^{tree} three^{tree}
162'
163
164test_expect_success 'accepts multiple revision arguments with an exclusion' '
165 git reset --hard three &&
166 git branch -f keep HEAD~2 &&
167 tip_tree=$(git rev-parse HEAD^{tree}) &&
168
169 git history squash --no-edit start..HEAD ^keep &&
170
171 git reflog -1 --format=%gs >actual &&
172 echo "squash: updating start..HEAD ^keep" >expect &&
173 test_cmp expect actual &&
174
175 check_log_subjects start..HEAD <<-\EOF &&
176 two
177 one
178 EOF
179 test_cmp_rev keep HEAD~1 &&
180 test "$tip_tree" = "$(git rev-parse HEAD^{tree})" &&
181
182 git branch -D keep
183'
184
185test_expect_success 'squashes a branch the current branch is not on' '
186 git reset --hard three &&
187 main=$(git symbolic-ref --short HEAD) &&
188 head_before=$(git rev-parse HEAD) &&
189 git checkout -b off-history start &&
190 test_commit --no-tag off-one off a &&
191 test_commit --no-tag off-two off b &&
192 git checkout "$main" &&
193
194 git history squash --no-edit start..off-history &&
195
196 check_commit_count start..off-history 1 &&
197 test_cmp_rev "$head_before" HEAD &&
198
199 git branch -D off-history
200'
201
202test_expect_success 'squashes a range into a single commit without changing the tree' '
203 git reset --hard three &&
204 head_before=$(git rev-parse HEAD) &&
205 tip_tree=$(git rev-parse HEAD^{tree}) &&
206
207 git history squash --no-edit --dry-run start.. >out &&
208 predicted=$(awk "/^update refs\/heads\// {print \$3}" out) &&
209 test_cmp_rev "$head_before" HEAD &&
210
211 git history squash --no-edit start.. &&
212
213 test "$predicted" = "$(git rev-parse HEAD)" &&
214 check_commit_count start..HEAD 1 &&
215 test_cmp_rev start HEAD^ &&
216 test "$tip_tree" = "$(git rev-parse HEAD^{tree})" &&
217 check_log_subjects -1 <<-\EOF &&
218 one
219 EOF
220 git reflog >reflog &&
221 test_grep "squash: updating" reflog
222'
223
224test_expect_success 'sanitizes rev-list walk options, before and after --' '
225 git reset --hard three &&
226 tip_tree=$(git rev-parse HEAD^{tree}) &&
227
228 git history squash --no-edit --date-order start.. 2>err &&
229 test_grep "ignoring rev-list options" err &&
230 test_cmp_rev start HEAD^ &&
231 test "$tip_tree" = "$(git rev-parse HEAD^{tree})" &&
232
233 git reset --hard three &&
234 git history squash --no-edit -- --reverse start.. 2>err &&
235 test_grep "ignoring rev-list options" err &&
236 test_cmp_rev start HEAD^ &&
237 test "$tip_tree" = "$(git rev-parse HEAD^{tree})"
238'
239
240test_expect_success 'squashes an interior range and replays descendants verbatim' '
241 git reset --hard three &&
242 final_tree=$(git rev-parse HEAD^{tree}) &&
243
244 git history squash --no-edit start..@~1 &&
245
246 check_log_subjects start..HEAD <<-\EOF &&
247 three
248 one
249 EOF
250
251 test_cmp_rev start HEAD~2 &&
252 test "$final_tree" = "$(git rev-parse HEAD^{tree})"
253'
254
255test_expect_success 'squashes when the base is the root commit' '
256 git reset --hard three &&
257 root=$(git rev-list --max-parents=0 HEAD) &&
258 tip_tree=$(git rev-parse HEAD^{tree}) &&
259
260 git history squash --no-edit "$root.." &&
261
262 check_commit_count "$root..HEAD" 1 &&
263 test_cmp_rev "$root" HEAD^ &&
264 test "$tip_tree" = "$(git rev-parse HEAD^{tree})"
265'
266
267test_expect_success 'squashing a mix of fixups' '
268 git reset --hard three &&
269 echo fix >file &&
270 git commit --fixup=two -a &&
271 echo really fix >file &&
272 git commit --fixup=one -a &&
273 echo really really fix >file &&
274 git commit --fixup=HEAD~1 -a && # fixup! two
275 echo really really really fix >file &&
276 git commit --fixup=HEAD~1 -a && # fixup! one
277
278 # squashing fixup! with a target that is not being squashed fails
279 test_must_fail git history squash one.. 2>err &&
280 test_grep "^error: cannot squash .* (fixup! one): its target is not being squashed" err &&
281
282 # squashing fixup! into fixup! with a different target fails
283 test_must_fail git history squash HEAD~4.. 2>err && # HEAD~4 is fixup! two
284 test_grep "^error: cannot squash .* (fixup! one): its target is not being squashed" err &&
285
286 # squashing a sequence of fixup! commits into their targets
287 git history squash --no-edit start..HEAD~1 &&
288 test_cmp_rev start HEAD~2 &&
289 check_commit_author one HEAD~1 &&
290 test_commit_message HEAD~1 -m one &&
291
292 # squashing "fixup! fixup! <target>" into "<target>"
293 git history squash --no-edit start.. &&
294 test_cmp_rev start HEAD~1 &&
295 check_commit_author one HEAD &&
296 test_commit_message HEAD -m one
297'
298
299test_expect_success 'squashing "squash!" messages' '
300 git reset --hard two &&
301 echo fix >file &&
302 git commit --fixup=HEAD -a &&
303 oldest=$(git rev-parse HEAD) &&
304 echo better fix >file &&
305 git commit -a -F - <<-EOF &&
306 squash! $(git rev-parse two)
307
308 Append this
309 EOF
310
311 echo an even better fix >file &&
312 git commit -a -F - <<-EOF &&
313 squash! squash! two
314
315 Append this as well
316 EOF
317
318 # must edit when squashing "squash!" into its target
319 test_must_fail git history squash --no-edit two^.. 2>err &&
320 test_grep "^error: squashing .* (squash! [a-f0-9]*) would discard its message" err &&
321
322 # squashing "squash!" into "fixup!" appends messages and changes
323 # subject prefix
324 git history squash --no-edit two.. &&
325 test_cmp_rev HEAD^ two &&
326 test_commit_message HEAD <<-\EOF &&
327 squash! two
328
329 Append this
330
331 Append this as well
332 EOF
333 check_commit_author "$oldest" HEAD &&
334
335 git commit --allow-empty -F - <<-\EOF &&
336 amend! two
337
338 A new message
339 EOF
340
341 # "amend!" does not replace "squash!"
342 test_must_fail git history squash --no-edit HEAD~2.. 2>err &&
343 test_grep "^error: squashing .* (amend! two) would overwrite .squash!. message" err
344'
345
346test_expect_success '--no-edit uses last "amend!" message without an editor' '
347 git reset --hard three &&
348 write_script editor <<-\EOF &&
349 exit 1
350 EOF
351 test_set_editor "$(pwd)/editor" &&
352 echo fix >file &&
353 git commit --author="Fix Me <fix.me@example.com>" --fixup=HEAD -a &&
354 git commit --allow-empty -F - <<-EOF &&
355 amend! $(git rev-parse --short HEAD)
356
357 The first reword
358
359 More detail
360 EOF
361
362 git commit --allow-empty -F - <<-\EOF &&
363 amend! three
364
365 The second reword
366
367 Extra detail
368 EOF
369
370 test_commit WIP &&
371
372 cat >msg <<-EOF &&
373 amend! $(git rev-parse --short HEAD^)
374
375 The third reword
376
377 Excruciating detail
378 EOF
379
380 git commit --author="Someone Else <s.else@example.com>" --allow-empty \
381 -F msg &&
382
383 # squashing amend! updates the commit message
384 git history squash --no-edit three^.. &&
385 sed -e 1,2d msg | test_commit_message HEAD &&
386 check_commit_author three HEAD &&
387 test_cmp_rev HEAD^ three^ &&
388
389 # squashing amend! into fixup! updates subject prefix
390 git reset --hard HEAD@{1} &&
391 git history squash --no-edit three.. &&
392 sed "1s/.*/amend! three/" msg | test_commit_message HEAD &&
393 check_commit_author HEAD@{1}~4 HEAD &&
394 test_cmp_rev HEAD^ three &&
395
396 # squashing amend! into amend! keeps original subject line
397 git reset --hard HEAD@{1} &&
398 git history squash --no-edit HEAD~3.. &&
399 sed "1s/.*/amend! three/" msg | test_commit_message HEAD &&
400 test_cmp_rev HEAD~3 three &&
401
402 # all amend! messages must target the first commit
403 git reset --hard HEAD@{1} &&
404 git commit --allow-empty -F - <<-\EOF &&
405 amend! WIP
406
407 The real message
408 EOF
409
410 test_must_fail git history squash --no-edit HEAD~4.. 2>err &&
411 test_grep "^error: cannot squash .* that does not target" err &&
412
413 # amend! message that targets commit that is not in range is rejected
414 test_must_fail git history squash --no-edit HEAD~3.. 2>err &&
415 test_grep "^error: cannot squash .* target is not being squashed" err &&
416 test_set_editor :
417'
418
419test_expect_success 'squashing fixups into a merge' '
420 test_when_finished \
421 "git switch -f $GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME; \
422 git branch -D feature" &&
423 git checkout -f start &&
424 test_commit F1 &&
425 git checkout -b feature start &&
426 test_commit F2 &&
427 git merge F1 &&
428 echo fixed >F1.t &&
429 cat >msg <<-EOF &&
430 amend! $(git rev-parse HEAD)
431
432 merge F1 and F2
433
434 reworded
435 EOF
436
437 git commit -a -F msg &&
438 git history squash --no-edit HEAD^^! HEAD &&
439 test_cmp_rev HEAD^1 F2 &&
440 test_cmp_rev HEAD^2 F1 &&
441 test_cmp_rev HEAD@{1}^{tree} HEAD^{tree} &&
442 sed 1,2d msg | test_commit_message HEAD
443'
444
445test_expect_success 'edits every message and aborts on an empty result' '
446 git reset --hard start &&
447 stage_file b &&
448 git commit -m "re-one subject" -m "re-one body line" &&
449 test_commit --no-tag re-two file c &&
450 test_commit re-three file d &&
451 head_before=$(git rev-parse HEAD) &&
452
453 write_script empty-editor <<-\EOF &&
454 >"$1"
455 EOF
456 test_set_editor "$(pwd)/empty-editor" &&
457 test_must_fail git history squash start.. 2>err &&
458 test_grep "Aborting commit due to empty commit message" err &&
459 test_cmp_rev "$head_before" HEAD &&
460
461 write_script editor <<-\EOF &&
462 cat "$1" >edited &&
463 echo combined >"$1"
464 EOF
465 test_set_editor "$(pwd)/editor" &&
466 git history squash start.. &&
467
468 cat >expect <<-EOF &&
469 # This is a combination of 3 commits.
470 # This is the 1st commit message:
471
472 re-one subject
473
474 re-one body line
475
476 # This is the commit message #2:
477
478 re-two
479
480 # This is the commit message #3:
481
482 re-three
483
484 # Please enter the commit message for the squash changes. Lines starting
485 # with ${SQ}#${SQ} will be ignored, and an empty message aborts the commit.
486 # Changes to be committed:
487 # modified: file
488 #
489 EOF
490 test_cmp expect edited &&
491 check_log_subjects -1 <<-\EOF
492 combined
493 EOF
494'
495
496test_expect_success 'handles fixup!, squash! and amend! like rebase' '
497 git reset --hard start &&
498 test_commit --no-tag mark-base file b &&
499 stage_file c &&
500 commit_with_message "fixup! mark-base\n\nfixup body\n" &&
501 stage_file d &&
502 commit_with_message "squash! mark-base\n\nsquash remark\n" &&
503 stage_file e &&
504 commit_with_message "amend! mark-base\n\namended message\n" &&
505
506 write_script editor <<-\EOF &&
507 cat "$1" >edited
508 EOF
509 test_set_editor "$(pwd)/editor" &&
510 git history squash start.. &&
511
512 cat >expect <<-EOF &&
513 # This is a combination of 4 commits.
514 # This is the 1st commit message:
515
516 mark-base
517
518 # The commit message #2 will be skipped:
519
520 # fixup! mark-base
521 #
522 # fixup body
523
524 # This is the commit message #3:
525
526 # squash! mark-base
527
528 squash remark
529
530 # This is the commit message #4:
531
532 # amend! mark-base
533
534 amended message
535
536 # Please enter the commit message for the squash changes. Lines starting
537 # with ${SQ}#${SQ} will be ignored, and an empty message aborts the commit.
538 # Changes to be committed:
539 # modified: file
540 #
541 EOF
542 test_cmp expect edited &&
543 check_log_messages -1 <<-\EOF
544 mark-base
545
546 squash remark
547
548 amended message
549
550 EOF
551'
552
553test_expect_success 'groups fixups under their targets in the editor' '
554 git reset --hard start &&
555 test_commit --no-tag alpha file a1 &&
556 test_commit --no-tag beta file b1 &&
557 stage_file a2 &&
558 commit_with_message "fixup! alpha\n" &&
559 stage_file b2 &&
560 commit_with_message "fixup! beta\n" &&
561
562 write_script editor <<-\EOF &&
563 cat "$1" >edited
564 EOF
565 test_set_editor "$(pwd)/editor" &&
566 git history squash start.. &&
567
568 cat >expect <<-EOF &&
569 # This is a combination of 4 commits.
570 # This is the 1st commit message:
571
572 alpha
573
574 # The commit message #2 will be skipped:
575
576 # fixup! alpha
577
578 # This is the commit message #3:
579
580 beta
581
582 # The commit message #4 will be skipped:
583
584 # fixup! beta
585
586 # Please enter the commit message for the squash changes. Lines starting
587 # with ${SQ}#${SQ} will be ignored, and an empty message aborts the commit.
588 # Changes to be committed:
589 # modified: file
590 #
591 EOF
592 test_cmp expect edited
593'
594
595test_expect_success 'lets amend! replace its target message in the editor' '
596 git reset --hard start &&
597 test_commit --no-tag mark-base file b &&
598 stage_file c &&
599 commit_with_message "amend! mark-base\n\namended message\n" &&
600 stage_file d &&
601 commit_with_message "squash! mark-base\n\nsquash remark\n" &&
602
603 write_script editor <<-\EOF &&
604 cat "$1" >edited
605 EOF
606 test_set_editor "$(pwd)/editor" &&
607 git history squash start.. &&
608
609 cat >expect <<-EOF &&
610 # This is a combination of 3 commits.
611 # The 1st commit message will be skipped:
612
613 # mark-base
614
615 # This is the commit message #2:
616
617 # amend! mark-base
618
619 amended message
620
621 # This is the commit message #3:
622
623 # squash! mark-base
624
625 squash remark
626
627 # Please enter the commit message for the squash changes. Lines starting
628 # with ${SQ}#${SQ} will be ignored, and an empty message aborts the commit.
629 # Changes to be committed:
630 # modified: file
631 #
632 EOF
633 test_cmp expect edited &&
634 check_log_messages -1 <<-\EOF
635 amended message
636
637 squash remark
638
639 EOF
640'
641
642test_expect_success '--update-refs=head only moves HEAD' '
643 git reset --hard three &&
644 git branch -f other HEAD &&
645 other_before=$(git rev-parse other) &&
646
647 git history squash --no-edit --update-refs=head start.. &&
648
649 check_commit_count start..HEAD 1 &&
650 test_cmp_rev "$other_before" other
651'
652
653test_expect_success 'refuses to fold a range a branch points into' '
654 test_when_finished \
655 "git switch -f $GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME; \
656 git branch -D feature" &&
657 git checkout -f -b feature start &&
658 test_commit C1 &&
659 test_commit C2 &&
660 git checkout -b topic-1 start &&
661 test_commit C3 &&
662 test_commit C4 &&
663 git checkout C3 &&
664 test_commit C5 &&
665 git checkout feature &&
666 git merge C5 &&
667 test_commit C6 &&
668 git checkout -b topic-2 C2 &&
669 test_commit C7 &&
670 git checkout feature &&
671
672 test_must_fail git history squash start.. 2>err &&
673 test_grep "^error: the following branches cannot be rewritten" err &&
674 test_grep "^ topic-1$" err &&
675 test_grep "^ topic-2$" err &&
676 test_grep "^hint: .* --update-refs=head" err &&
677 test_cmp_rev C6 HEAD &&
678
679 # squash succeeds with --update-refs=head
680 git history squash --no-edit --update-refs=head start.. &&
681 test_cmp_rev start HEAD^ &&
682 test_cmp_rev C6^{tree} HEAD^{tree} &&
683 test_cmp_rev C6 HEAD@{1}
684'
685
686test_expect_success 'advice.historyUpdateRefs silences the hint' '
687 git reset --hard three &&
688 git branch -f mid HEAD~1 &&
689 head_before=$(git rev-parse HEAD) &&
690
691 test_must_fail git -c advice.historyUpdateRefs=false \
692 history squash start.. 2>err &&
693 test_grep "^error: the following branches cannot be rewritten" err &&
694 test_grep "^ mid$" err &&
695 test_grep ! "hint:" err &&
696 test_cmp_rev "$head_before" HEAD &&
697
698 git branch -D mid
699'
700
701test_expect_success 'leaves tags and remote-tracking refs unchanged' '
702 git reset --hard three &&
703 git tag -f mark HEAD~1 &&
704 git update-ref refs/remotes/origin/mark HEAD~1 &&
705 mark_before=$(git rev-parse mark) &&
706
707 git history squash --no-edit start.. &&
708
709 test_cmp_rev "$mark_before" mark &&
710 test_cmp_rev "$mark_before" refs/remotes/origin/mark &&
711
712 git tag -d mark &&
713 git update-ref -d refs/remotes/origin/mark
714'
715
716test_expect_success 'squashes a range whose internal merge has a single base' '
717 git reset --hard start &&
718 main=$(git symbolic-ref --short HEAD) &&
719 test_commit --no-tag before-side file b &&
720 git checkout -b inner-side &&
721 test_commit --no-tag on-inner-side inner x &&
722 git checkout "$main" &&
723 test_commit --no-tag after-side file c &&
724 git merge --no-ff -m merge inner-side &&
725 git branch -D inner-side &&
726 test_commit --no-tag after-merge file d &&
727 tip_tree=$(git rev-parse HEAD^{tree}) &&
728
729 git history squash --no-edit start.. &&
730
731 check_commit_count start..HEAD 1 &&
732 check_log_subjects -1 <<-\EOF &&
733 before-side
734 EOF
735 test "$tip_tree" = "$(git rev-parse HEAD^{tree})" &&
736 test_path_is_file inner
737'
738
739test_expect_success 'folds a merge of a branch that forked at the base' '
740 git reset --hard start &&
741 main=$(git symbolic-ref --short HEAD) &&
742 git checkout -b base-fork-side &&
743 test_commit --no-tag base-fork-side side x &&
744 git checkout "$main" &&
745 test_commit --no-tag base-fork-main file b &&
746 git merge --no-ff -m "merge base-fork-side" base-fork-side &&
747 git branch -D base-fork-side &&
748 test_commit --no-tag base-fork-tail file c &&
749 tip_tree=$(git rev-parse HEAD^{tree}) &&
750
751 git history squash --no-edit start.. &&
752
753 check_commit_count start..HEAD 1 &&
754 test_cmp_rev start HEAD^ &&
755 test "$tip_tree" = "$(git rev-parse HEAD^{tree})" &&
756 test_path_is_file side
757'
758
759test_expect_success 'refuses a merge whose other parent is outside the range' '
760 git reset --hard start &&
761 main=$(git symbolic-ref --short HEAD) &&
762 git checkout -b outside-parent &&
763 test_commit --no-tag outside-parent outside x &&
764 git checkout "$main" &&
765 test_commit --no-tag outside-main file b &&
766 base=$(git rev-parse HEAD) &&
767 test_commit --no-tag outside-mid file c &&
768 git merge --no-ff -m "merge outside-parent" outside-parent &&
769 git branch -D outside-parent &&
770 merged=$(git rev-parse HEAD) &&
771
772 test_must_fail git history squash "$base.." 2>err &&
773 test_grep "parent .* of commit .* is outside the revision range" err &&
774 test_cmp_rev "$merged" HEAD
775'
776
777test_expect_success 'folds a range whose tip is a merge commit' '
778 git reset --hard start &&
779 main=$(git symbolic-ref --short HEAD) &&
780 test_commit --no-tag tipmerge-base file b &&
781 git checkout -b tipmerge-side &&
782 test_commit --no-tag tipmerge-side side x &&
783 git checkout "$main" &&
784 test_commit --no-tag tipmerge-main file c &&
785 git merge --no-ff -m "merge tipmerge-side" tipmerge-side &&
786 git branch -D tipmerge-side &&
787 tip_tree=$(git rev-parse HEAD^{tree}) &&
788
789 git history squash --no-edit start.. &&
790
791 check_commit_count start..HEAD 1 &&
792 test "$tip_tree" = "$(git rev-parse HEAD^{tree})" &&
793 test_path_is_file side
794'
795
796test_expect_success 'folds a range whose base is a merge commit' '
797 git reset --hard start &&
798 main=$(git symbolic-ref --short HEAD) &&
799 git checkout -b basemerge-side &&
800 test_commit --no-tag basemerge-side side x &&
801 git checkout "$main" &&
802 test_commit --no-tag basemerge-main file b &&
803 git merge --no-ff -m "merge basemerge-side" basemerge-side &&
804 git branch -D basemerge-side &&
805 base=$(git rev-parse HEAD) &&
806 test_commit --no-tag basemerge-one file c &&
807 test_commit --no-tag basemerge-two file d &&
808 tip_tree=$(git rev-parse HEAD^{tree}) &&
809
810 git history squash --no-edit "$base.." &&
811
812 check_commit_count "$base..HEAD" 1 &&
813 test_cmp_rev "$base" HEAD^ &&
814 test "$tip_tree" = "$(git rev-parse HEAD^{tree})"
815'
816
817test_expect_success 'folds a range with two interior merges' '
818 git reset --hard start &&
819 main=$(git symbolic-ref --short HEAD) &&
820 test_commit --no-tag two-merge-a file a1 &&
821 git checkout -b two-merge-s1 &&
822 test_commit --no-tag two-merge-s1 s1 x &&
823 git checkout "$main" &&
824 git merge --no-ff -m "merge s1" two-merge-s1 &&
825 test_commit --no-tag two-merge-b file b1 &&
826 git checkout -b two-merge-s2 &&
827 test_commit --no-tag two-merge-s2 s2 y &&
828 git checkout "$main" &&
829 git merge --no-ff -m "merge s2" two-merge-s2 &&
830 git branch -D two-merge-s1 two-merge-s2 &&
831 tip_tree=$(git rev-parse HEAD^{tree}) &&
832
833 git history squash --no-edit start.. &&
834
835 check_commit_count start..HEAD 1 &&
836 test "$tip_tree" = "$(git rev-parse HEAD^{tree})" &&
837 test_path_is_file s1 &&
838 test_path_is_file s2
839'
840
841test_expect_success 'folds a range with a nested merge' '
842 git reset --hard start &&
843 main=$(git symbolic-ref --short HEAD) &&
844 git checkout -b nested-outer &&
845 test_commit --no-tag nested-outer outer x &&
846 git checkout -b nested-inner &&
847 test_commit --no-tag nested-inner inner y &&
848 git checkout nested-outer &&
849 git merge --no-ff -m "merge inner" nested-inner &&
850 git checkout "$main" &&
851 test_commit --no-tag nested-main file b1 &&
852 git merge --no-ff -m "merge outer" nested-outer &&
853 git branch -D nested-outer nested-inner &&
854 tip_tree=$(git rev-parse HEAD^{tree}) &&
855
856 git history squash --no-edit start.. &&
857
858 check_commit_count start..HEAD 1 &&
859 test "$tip_tree" = "$(git rev-parse HEAD^{tree})" &&
860 test_path_is_file outer &&
861 test_path_is_file inner
862'
863
864test_expect_success 'folds a range with an octopus merge' '
865 git reset --hard start &&
866 main=$(git symbolic-ref --short HEAD) &&
867 test_commit --no-tag octo-base file a1 &&
868 git checkout -b octo-1 &&
869 test_commit --no-tag octo-1 o1 x &&
870 git checkout "$main" &&
871 git checkout -b octo-2 &&
872 test_commit --no-tag octo-2 o2 y &&
873 git checkout "$main" &&
874 git merge --no-ff -m octopus octo-1 octo-2 &&
875 git branch -D octo-1 octo-2 &&
876 tip_tree=$(git rev-parse HEAD^{tree}) &&
877
878 git history squash --no-edit start.. &&
879
880 check_commit_count start..HEAD 1 &&
881 test "$tip_tree" = "$(git rev-parse HEAD^{tree})" &&
882 test_path_is_file o1 &&
883 test_path_is_file o2
884'
885
886test_expect_success 'refuses an octopus merge with an arm forked before the base' '
887 git reset --hard start &&
888 main=$(git symbolic-ref --short HEAD) &&
889 git checkout -b octo-pre &&
890 test_commit octo-pre-side pside x &&
891 git checkout "$main" &&
892 test_commit octo-pre-main file b1 &&
893 octo_base=$(git rev-parse HEAD) &&
894 git checkout -b octo-within &&
895 test_commit --no-tag octo-within wside y &&
896 git checkout "$main" &&
897 git merge --no-ff -m octopus octo-pre octo-within &&
898 merged=$(git rev-parse HEAD) &&
899 git branch -D octo-pre octo-within &&
900
901 test_must_fail git history squash "$octo_base.." 2>err &&
902 test_grep "parent .* of commit .* is outside the revision range" err &&
903 test_cmp_rev "$merged" HEAD
904'
905
906test_expect_success 'refuses when a descendant above the range is a merge' '
907 git reset --hard start &&
908 main=$(git symbolic-ref --short HEAD) &&
909 test_commit --no-tag desc-one file b &&
910 test_commit --no-tag desc-two file c &&
911 git tag desc-tip &&
912 git checkout -b desc-above &&
913 test_commit --no-tag desc-above above x &&
914 git checkout "$main" &&
915 test_commit --no-tag desc-main file d &&
916 git merge --no-ff -m "merge desc-above" desc-above &&
917 git branch -D desc-above &&
918 head_before=$(git rev-parse HEAD) &&
919
920 test_must_fail git history squash --no-edit start..desc-tip 2>err &&
921 test_grep "merge commits is not supported" err &&
922 test_cmp_rev "$head_before" HEAD
923'
924
925test_expect_success 'refuses to fold a range a ref points into at a merge' '
926 git reset --hard start &&
927 main=$(git symbolic-ref --short HEAD) &&
928 test_commit --no-tag refmerge-base file b &&
929 git checkout -b refmerge-side &&
930 test_commit --no-tag refmerge-side side x &&
931 git checkout "$main" &&
932 test_commit --no-tag refmerge-main file c &&
933 git merge --no-ff -m "interior merge" refmerge-side &&
934 git branch -D refmerge-side &&
935 git branch at-merge HEAD &&
936 test_commit --no-tag refmerge-tail file d &&
937 head_before=$(git rev-parse HEAD) &&
938
939 test_must_fail git history squash start.. 2>err &&
940 test_grep "^error: the following branches cannot be rewritten" err &&
941 test_grep "^ at-merge$" err &&
942 test_cmp_rev "$head_before" HEAD &&
943
944 git branch -D at-merge
945'
946
947test_done