1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642
|
#!/bin/sh
#
# Copyright (c) 2018 Johannes E. Schindelin
#
test_description='git rebase -i --rebase-merges
This test runs git rebase "interactively", retaining the branch structure by
recreating merge commits.
Initial setup:
-- B -- (first)
/ \
A - C - D - E - H (main)
\ \ /
\ F - G (second)
\
Conflicting-G
'
GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
. ./test-lib.sh
. "$TEST_DIRECTORY"/lib-rebase.sh
. "$TEST_DIRECTORY"/lib-log-graph.sh
test_cmp_graph () {
cat >expect &&
lib_test_cmp_graph --boundary --format=%s "$@"
}
test_expect_success 'setup' '
write_script replace-editor.sh <<-\EOF &&
mv "$1" "$(git rev-parse --git-path ORIGINAL-TODO)"
cp script-from-scratch "$1"
EOF
test_commit A &&
git checkout -b first &&
test_commit B &&
b=$(git rev-parse --short HEAD) &&
git checkout main &&
test_commit C &&
c=$(git rev-parse --short HEAD) &&
test_commit D &&
d=$(git rev-parse --short HEAD) &&
git merge --no-commit B &&
test_tick &&
git commit -m E &&
git tag -m E E &&
e=$(git rev-parse --short HEAD) &&
git checkout -b second C &&
test_commit F &&
f=$(git rev-parse --short HEAD) &&
test_commit G &&
g=$(git rev-parse --short HEAD) &&
git checkout main &&
git merge --no-commit G &&
test_tick &&
git commit -m H &&
h=$(git rev-parse --short HEAD) &&
git tag -m H H &&
git checkout A &&
test_commit conflicting-G G.t
'
test_expect_success 'create completely different structure' '
cat >script-from-scratch <<-\EOF &&
label onto
# onebranch
pick G
pick D
label onebranch
# second
reset onto
pick B
label second
reset onto
merge -C H second
merge onebranch # Merge the topic branch '\''onebranch'\''
EOF
test_config sequence.editor \""$PWD"/replace-editor.sh\" &&
test_tick &&
git rebase -i -r A main &&
test_cmp_graph <<-\EOF &&
* Merge the topic branch '\''onebranch'\''
|\
| * D
| * G
* | H
|\ \
| |/
|/|
| * B
|/
* A
EOF
head="$(git show-ref --verify -s --abbrev HEAD)" &&
cat >expect <<-EOF &&
$head HEAD@{0}: rebase (finish): returning to refs/heads/main
$head HEAD@{1}: rebase (merge): Merge the topic branch ${SQ}onebranch${SQ}
EOF
git reflog -n2 HEAD >actual &&
test_cmp expect actual
'
test_expect_success 'generate correct todo list' '
cat >expect <<-EOF &&
label onto
reset onto
pick $b # B
label first
reset onto
pick $c # C
label branch-point
pick $f # F
pick $g # G
label second
reset branch-point # C
pick $d # D
merge -C $e first # E
merge -C $h second # H
EOF
grep -v "^#" <.git/ORIGINAL-TODO >output &&
test_cmp expect output
'
test_expect_success '`reset` refuses to overwrite untracked files' '
git checkout B &&
test_commit dont-overwrite-untracked &&
cat >script-from-scratch <<-EOF &&
exec >dont-overwrite-untracked.t
pick $(git rev-parse B) B
reset refs/tags/dont-overwrite-untracked
pick $(git rev-parse C) C
exec cat .git/rebase-merge/done >actual
EOF
test_config sequence.editor \""$PWD"/replace-editor.sh\" &&
test_must_fail git rebase -ir A &&
test_cmp_rev HEAD B &&
head -n3 script-from-scratch >expect &&
test_cmp expect .git/rebase-merge/done &&
rm dont-overwrite-untracked.t &&
git rebase --continue &&
tail -n3 script-from-scratch >>expect &&
test_cmp expect actual
'
test_expect_success '`reset` rejects trees' '
test_when_finished "test_might_fail git rebase --abort" &&
test_must_fail env GIT_SEQUENCE_EDITOR="echo reset A^{tree} >" \
git rebase -i B C >out 2>err &&
grep "object .* is a tree" err &&
test_must_be_empty out
'
test_expect_success '`reset` only looks for labels under refs/rewritten/' '
test_when_finished "test_might_fail git rebase --abort" &&
git branch refs/rewritten/my-label A &&
test_must_fail env GIT_SEQUENCE_EDITOR="echo reset my-label >" \
git rebase -i B C >out 2>err &&
grep "could not resolve ${SQ}my-label${SQ}" err &&
test_must_be_empty out
'
test_expect_success 'failed `merge -C` writes patch (may be rescheduled, too)' '
test_when_finished "test_might_fail git rebase --abort" &&
git checkout -b conflicting-merge A &&
: fail because of conflicting untracked file &&
>G.t &&
echo "merge -C H G" >script-from-scratch &&
test_config sequence.editor \""$PWD"/replace-editor.sh\" &&
test_tick &&
test_must_fail git rebase -ir HEAD &&
test_cmp_rev REBASE_HEAD H^0 &&
grep "^merge -C .* G$" .git/rebase-merge/done &&
grep "^merge -C .* G$" .git/rebase-merge/git-rebase-todo &&
test_path_is_missing .git/rebase-merge/patch &&
echo changed >file1 &&
git add file1 &&
test_must_fail git rebase --continue 2>err &&
grep "error: you have staged changes in your working tree" err &&
: fail because of merge conflict &&
git reset --hard conflicting-G &&
test_must_fail git rebase --continue &&
! grep "^merge -C .* G$" .git/rebase-merge/git-rebase-todo &&
test_path_is_file .git/rebase-merge/patch
'
test_expect_success 'failed `merge <branch>` does not crash' '
test_when_finished "test_might_fail git rebase --abort" &&
git checkout conflicting-G &&
echo "merge G" >script-from-scratch &&
test_config sequence.editor \""$PWD"/replace-editor.sh\" &&
test_tick &&
test_must_fail git rebase -ir HEAD &&
! grep "^merge G$" .git/rebase-merge/git-rebase-todo &&
grep "^Merge branch ${SQ}G${SQ}$" .git/rebase-merge/message
'
test_expect_success 'merge -c commits before rewording and reloads todo-list' '
cat >script-from-scratch <<-\EOF &&
merge -c E B
merge -c H G
EOF
git checkout -b merge-c H &&
(
set_reword_editor &&
GIT_SEQUENCE_EDITOR="\"$PWD/replace-editor.sh\"" \
git rebase -i -r D
) &&
check_reworded_commits E H
'
test_expect_success 'merge -c rewords when a strategy is given' '
git checkout -b merge-c-with-strategy H &&
write_script git-merge-override <<-\EOF &&
echo overridden$1 >G.t
git add G.t
EOF
PATH="$PWD:$PATH" \
GIT_SEQUENCE_EDITOR="echo merge -c H G >" \
GIT_EDITOR="echo edited >>" \
git rebase --no-ff -ir -s override -Xxopt E &&
test_write_lines overridden--xopt >expect &&
test_cmp expect G.t &&
test_write_lines H "" edited "" >expect &&
git log --format=%B -1 >actual &&
test_cmp expect actual
'
test_expect_success 'with a branch tip that was cherry-picked already' '
git checkout -b already-upstream main &&
base="$(git rev-parse --verify HEAD)" &&
test_commit A1 &&
test_commit A2 &&
git reset --hard $base &&
test_commit B1 &&
test_tick &&
git merge -m "Merge branch A" A2 &&
git checkout -b upstream-with-a2 $base &&
test_tick &&
git cherry-pick A2 &&
git checkout already-upstream &&
test_tick &&
git rebase -i -r upstream-with-a2 &&
test_cmp_graph upstream-with-a2.. <<-\EOF
* Merge branch A
|\
| * A1
* | B1
|/
o A2
EOF
'
test_expect_success '--no-rebase-merges countermands --rebase-merges' '
git checkout -b no-rebase-merges E &&
git rebase --rebase-merges --no-rebase-merges C &&
test_cmp_graph C.. <<-\EOF
* B
* D
o C
EOF
'
test_expect_success 'do not rebase cousins unless asked for' '
git checkout -b cousins main &&
before="$(git rev-parse --verify HEAD)" &&
test_tick &&
git rebase -r HEAD^ &&
test_cmp_rev HEAD $before &&
test_tick &&
git rebase --rebase-merges=rebase-cousins HEAD^ &&
test_cmp_graph HEAD^.. <<-\EOF
* Merge the topic branch '\''onebranch'\''
|\
| * D
| * G
|/
o H
EOF
'
test_expect_success 'rebase.rebaseMerges=rebase-cousins is equivalent to --rebase-merges=rebase-cousins' '
test_config rebase.rebaseMerges rebase-cousins &&
git checkout -b config-rebase-cousins main &&
git rebase HEAD^ &&
test_cmp_graph HEAD^.. <<-\EOF
* Merge the topic branch '\''onebranch'\''
|\
| * D
| * G
|/
o H
EOF
'
test_expect_success '--no-rebase-merges overrides rebase.rebaseMerges=no-rebase-cousins' '
test_config rebase.rebaseMerges no-rebase-cousins &&
git checkout -b override-config-no-rebase-cousins E &&
git rebase --no-rebase-merges C &&
test_cmp_graph C.. <<-\EOF
* B
* D
o C
EOF
'
test_expect_success '--rebase-merges overrides rebase.rebaseMerges=rebase-cousins' '
test_config rebase.rebaseMerges rebase-cousins &&
git checkout -b override-config-rebase-cousins E &&
before="$(git rev-parse --verify HEAD)" &&
test_tick &&
git rebase --rebase-merges C &&
test_cmp_rev HEAD $before
'
test_expect_success 'refs/rewritten/* is worktree-local' '
git worktree add wt &&
cat >wt/script-from-scratch <<-\EOF &&
label xyz
exec GIT_DIR=../.git git rev-parse --verify refs/rewritten/xyz >a || :
exec git rev-parse --verify refs/rewritten/xyz >b
EOF
test_config -C wt sequence.editor \""$PWD"/replace-editor.sh\" &&
git -C wt rebase -i HEAD &&
test_must_be_empty wt/a &&
test_cmp_rev HEAD "$(cat wt/b)"
'
test_expect_success '--abort cleans up refs/rewritten' '
git checkout -b abort-cleans-refs-rewritten H &&
GIT_SEQUENCE_EDITOR="echo break >>" git rebase -ir @^ &&
git rev-parse --verify refs/rewritten/onto &&
git rebase --abort &&
test_must_fail git rev-parse --verify refs/rewritten/onto
'
test_expect_success '--quit cleans up refs/rewritten' '
git checkout -b quit-cleans-refs-rewritten H &&
GIT_SEQUENCE_EDITOR="echo break >>" git rebase -ir @^ &&
git rev-parse --verify refs/rewritten/onto &&
git rebase --quit &&
test_must_fail git rev-parse --verify refs/rewritten/onto
'
test_expect_success 'post-rewrite hook and fixups work for merges' '
git checkout -b post-rewrite H &&
test_commit same1 &&
git reset --hard HEAD^ &&
test_commit same2 &&
git merge -m "to fix up" same1 &&
echo same old same old >same2.t &&
test_tick &&
git commit --fixup HEAD same2.t &&
fixup="$(git rev-parse HEAD)" &&
test_hook post-rewrite <<-\EOF &&
cat >actual
EOF
test_tick &&
git rebase -i --autosquash -r HEAD^^^ &&
printf "%s %s\n%s %s\n%s %s\n%s %s\n" >expect $(git rev-parse \
$fixup^^2 HEAD^2 \
$fixup^^ HEAD^ \
$fixup^ HEAD \
$fixup HEAD) &&
test_cmp expect actual
'
test_expect_success 'refuse to merge ancestors of HEAD' '
echo "merge HEAD^" >script-from-scratch &&
test_config -C wt sequence.editor \""$PWD"/replace-editor.sh\" &&
before="$(git rev-parse HEAD)" &&
git rebase -i HEAD &&
test_cmp_rev HEAD $before
'
test_expect_success 'root commits' '
git checkout --orphan unrelated &&
test_commit --author "Parsnip <root@example.com>" second-root &&
test_commit third-root &&
cat >script-from-scratch <<-\EOF &&
pick third-root
label first-branch
reset [new root]
pick second-root
merge first-branch # Merge the 3rd root
EOF
test_config sequence.editor \""$PWD"/replace-editor.sh\" &&
test_tick &&
git rebase -i --force-rebase --root -r &&
test "Parsnip" = "$(git show -s --format=%an HEAD^)" &&
test $(git rev-parse second-root^0) != $(git rev-parse HEAD^) &&
test $(git rev-parse second-root:second-root.t) = \
$(git rev-parse HEAD^:second-root.t) &&
test_cmp_graph HEAD <<-\EOF &&
* Merge the 3rd root
|\
| * third-root
* second-root
EOF
: fast forward if possible &&
before="$(git rev-parse --verify HEAD)" &&
test_might_fail git config --unset sequence.editor &&
test_tick &&
git rebase -i --root -r &&
test_cmp_rev HEAD $before
'
test_expect_success 'a "merge" into a root commit is a fast-forward' '
head=$(git rev-parse HEAD) &&
cat >script-from-scratch <<-EOF &&
reset [new root]
merge $head
EOF
test_config sequence.editor \""$PWD"/replace-editor.sh\" &&
test_tick &&
git rebase -i -r HEAD^ &&
test_cmp_rev HEAD $head
'
test_expect_success 'A root commit can be a cousin, treat it that way' '
git checkout --orphan khnum &&
test_commit yama &&
git checkout -b asherah main &&
test_commit shamkat &&
git merge --allow-unrelated-histories khnum &&
test_tick &&
git rebase -f -r HEAD^ &&
test_cmp_rev ! HEAD^2 khnum &&
test_cmp_graph HEAD^.. <<-\EOF &&
* Merge branch '\''khnum'\'' into asherah
|\
| * yama
o shamkat
EOF
test_tick &&
git rebase --rebase-merges=rebase-cousins HEAD^ &&
test_cmp_graph HEAD^.. <<-\EOF
* Merge branch '\''khnum'\'' into asherah
|\
| * yama
|/
o shamkat
EOF
'
test_expect_success 'labels that are object IDs are rewritten' '
git checkout --detach B &&
test_commit I &&
third=$(git rev-parse HEAD) &&
git checkout -b labels main &&
git merge --no-commit $third &&
test_tick &&
git commit -m "Merge commit '\''$third'\'' into labels" &&
echo noop >script-from-scratch &&
test_config sequence.editor \""$PWD"/replace-editor.sh\" &&
test_tick &&
git rebase -i -r A &&
grep "^label $third-" .git/ORIGINAL-TODO &&
! grep "^label $third$" .git/ORIGINAL-TODO
'
test_expect_success 'octopus merges' '
git checkout -b three &&
test_commit before-octopus &&
test_commit three &&
git checkout -b two HEAD^ &&
test_commit two &&
git checkout -b one HEAD^ &&
test_commit one &&
test_tick &&
(GIT_AUTHOR_NAME="Hank" GIT_AUTHOR_EMAIL="hank@sea.world" \
git merge -m "Tüntenfüsch" two three) &&
: fast forward if possible &&
before="$(git rev-parse --verify HEAD)" &&
test_tick &&
git rebase -i -r HEAD^^ &&
test_cmp_rev HEAD $before &&
test_tick &&
git rebase -i --force-rebase -r HEAD^^ &&
test "Hank" = "$(git show -s --format=%an HEAD)" &&
test "$before" != $(git rev-parse HEAD) &&
test_cmp_graph HEAD^^.. <<-\EOF
*-. Tüntenfüsch
|\ \
| | * three
| * | two
| |/
* / one
|/
o before-octopus
EOF
'
test_expect_success 'with --autosquash and --exec' '
git checkout -b with-exec H &&
echo Booh >B.t &&
test_tick &&
git commit --fixup B B.t &&
write_script show.sh <<-\EOF &&
subject="$(git show -s --format=%s HEAD)"
content="$(git diff HEAD^ HEAD | tail -n 1)"
echo "$subject: $content"
EOF
test_tick &&
git rebase -ir --autosquash --exec ./show.sh A >actual &&
grep "B: +Booh" actual &&
grep "E: +Booh" actual &&
grep "G: +G" actual
'
test_expect_success '--continue after resolving conflicts after a merge' '
git checkout -b already-has-g E &&
git cherry-pick E..G &&
test_commit H2 &&
git checkout -b conflicts-in-merge H &&
test_commit H2 H2.t conflicts H2-conflict &&
test_must_fail git rebase -r already-has-g &&
grep conflicts H2.t &&
echo resolved >H2.t &&
git add -u &&
git rebase --continue &&
test_must_fail git rev-parse --verify HEAD^2 &&
test_path_is_missing .git/MERGE_HEAD
'
test_expect_success '--rebase-merges with strategies' '
git checkout -b with-a-strategy F &&
test_tick &&
git merge -m "Merge conflicting-G" conflicting-G &&
: first, test with a merge strategy option &&
git rebase -ir -Xtheirs G &&
echo conflicting-G >expect &&
test_cmp expect G.t &&
: now, try with a merge strategy other than recursive &&
git reset --hard @{1} &&
write_script git-merge-override <<-\EOF &&
echo overridden$1 >>G.t
git add G.t
EOF
PATH="$PWD:$PATH" git rebase -ir -s override -Xxopt G &&
test_write_lines G overridden--xopt >expect &&
test_cmp expect G.t
'
test_expect_success '--rebase-merges with commit that can generate bad characters for filename' '
git checkout -b colon-in-label E &&
git merge -m "colon: this should work" G &&
git rebase --rebase-merges --force-rebase E
'
test_expect_success '--rebase-merges with message matched with onto label' '
git checkout -b onto-label E &&
git merge -m onto G &&
git rebase --rebase-merges --force-rebase E &&
test_cmp_graph <<-\EOF
* onto
|\
| * G
| * F
* | E
|\ \
| * | B
* | | D
| |/
|/|
* | C
|/
* A
EOF
'
test_expect_success 'progress shows the correct total' '
git checkout -b progress H &&
git rebase --rebase-merges --force-rebase --verbose A 2> err &&
# Expecting "Rebasing (N/14)" here, no bogus total number
grep "^Rebasing.*/14.$" err >progress &&
test_line_count = 14 progress
'
test_expect_success 'truncate label names' '
commit=$(git commit-tree -p HEAD^ -p HEAD -m "0123456789 我 123" HEAD^{tree}) &&
git merge --ff-only $commit &&
done="$(git rev-parse --git-path rebase-merge/done)" &&
git -c rebase.maxLabelLength=14 rebase --rebase-merges -x "cp \"$done\" out" --root &&
grep "label 0123456789-我$" out &&
git -c rebase.maxLabelLength=13 rebase --rebase-merges -x "cp \"$done\" out" --root &&
grep "label 0123456789-$" out
'
test_expect_success 'reword fast-forwarded empty merge commit' '
oid="$(git commit-tree -m "D1" -p A D^{tree})" &&
oid="$(git commit-tree -m "empty merge" -p D -p $oid D^{tree})" &&
write_script sequence-editor.sh <<-\EOF &&
sed /^merge/s/-C/-c/ "$1" >"$1.tmp"
mv "$1.tmp" "$1"
EOF
(
test_set_sequence_editor "$(pwd)/sequence-editor.sh" &&
GIT_EDITOR="echo edited >>" git rebase -i -r D $oid
) &&
test_commit_message HEAD <<-\EOF
empty merge
edited
EOF
'
test_done
|