| 1 | #!/bin/sh |
| 2 | # |
| 3 | # Copyright (c) 2007 Johannes E. Schindelin |
| 4 | # |
| 5 | |
| 6 | test_description='git rebase interactive |
| 7 | |
| 8 | This test runs git rebase "interactively", by faking an edit, and verifies |
| 9 | that the result still makes sense. |
| 10 | |
| 11 | Initial setup: |
| 12 | |
| 13 | one - two - three - four (conflict-branch) |
| 14 | / |
| 15 | A - B - C - D - E (primary) |
| 16 | | \ |
| 17 | | F - G - H (branch1) |
| 18 | | \ |
| 19 | |\ I (branch2) |
| 20 | | \ |
| 21 | | J - K - L - M (no-conflict-branch) |
| 22 | \ |
| 23 | N - O - P (no-ff-branch) |
| 24 | |
| 25 | where A, B, D and G all touch file1, and one, two, three, four all |
| 26 | touch file "conflict". |
| 27 | ' |
| 28 | |
| 29 | . ./test-lib.sh |
| 30 | |
| 31 | . "$TEST_DIRECTORY"/lib-rebase.sh |
| 32 | |
| 33 | test_expect_success 'setup' ' |
| 34 | # Commit dates are hardcoded to 2005, and the reflog entries will have |
| 35 | # a matching timestamp. Maintenance may thus immediately expire |
| 36 | # reflogs if it was running. |
| 37 | git config set gc.reflogExpire never && |
| 38 | git config set gc.reflogExpireUnreachable never && |
| 39 | |
| 40 | git switch -C primary && |
| 41 | test_commit A file1 && |
| 42 | test_commit B file1 && |
| 43 | test_commit C file2 && |
| 44 | test_commit D file1 && |
| 45 | test_commit E file3 && |
| 46 | git checkout -b branch1 A && |
| 47 | test_commit F file4 && |
| 48 | test_commit G file1 && |
| 49 | test_commit H file5 && |
| 50 | git checkout -b branch2 F && |
| 51 | test_commit I file6 && |
| 52 | git checkout -b conflict-branch A && |
| 53 | test_commit one conflict && |
| 54 | test_commit two conflict && |
| 55 | test_commit three conflict && |
| 56 | test_commit four conflict && |
| 57 | git checkout -b no-conflict-branch A && |
| 58 | test_commit J fileJ && |
| 59 | test_commit K fileK && |
| 60 | test_commit L fileL && |
| 61 | test_commit M fileM && |
| 62 | git checkout -b no-ff-branch A && |
| 63 | test_commit N fileN && |
| 64 | test_commit O fileO && |
| 65 | test_commit P fileP |
| 66 | ' |
| 67 | |
| 68 | # "exec" commands are run with the user shell by default, but this may |
| 69 | # be non-POSIX. For example, if SHELL=zsh then ">file" doesn't work |
| 70 | # to create a file. Unsetting SHELL avoids such non-portable behavior |
| 71 | # in tests. It must be exported for it to take effect where needed. |
| 72 | SHELL= |
| 73 | export SHELL |
| 74 | |
| 75 | test_expect_success 'rebase --keep-empty' ' |
| 76 | git checkout -b emptybranch primary && |
| 77 | git commit --allow-empty -m "empty" && |
| 78 | git rebase --keep-empty -i HEAD~2 && |
| 79 | git log --oneline >actual && |
| 80 | test_line_count = 6 actual |
| 81 | ' |
| 82 | |
| 83 | test_expect_success 'rebase -i with empty todo list' ' |
| 84 | cat >expect <<-\EOF && |
| 85 | error: nothing to do |
| 86 | EOF |
| 87 | ( |
| 88 | set_fake_editor && |
| 89 | test_must_fail env FAKE_LINES="#" \ |
| 90 | git rebase -i HEAD^ >output 2>&1 |
| 91 | ) && |
| 92 | tail -n 1 output >actual && # Ignore output about changing todo list |
| 93 | test_cmp expect actual |
| 94 | ' |
| 95 | |
| 96 | test_expect_success 'rebase -i with the exec command' ' |
| 97 | git checkout primary && |
| 98 | ( |
| 99 | set_fake_editor && |
| 100 | FAKE_LINES="1 exec_>touch-one |
| 101 | 2 exec_>touch-two exec_false exec_>touch-three |
| 102 | 3 4 exec_>\"touch-file__name_with_spaces\";_>touch-after-semicolon 5" && |
| 103 | export FAKE_LINES && |
| 104 | test_must_fail git rebase -i A |
| 105 | ) && |
| 106 | test_path_is_file touch-one && |
| 107 | test_path_is_file touch-two && |
| 108 | # Missing because we should have stopped by now. |
| 109 | test_path_is_missing touch-three && |
| 110 | test_cmp_rev C HEAD && |
| 111 | git rebase --continue && |
| 112 | test_path_is_file touch-three && |
| 113 | test_path_is_file "touch-file name with spaces" && |
| 114 | test_path_is_file touch-after-semicolon && |
| 115 | test_cmp_rev primary HEAD && |
| 116 | rm -f touch-* |
| 117 | ' |
| 118 | |
| 119 | test_expect_success 'rebase -i with the exec command runs from tree root' ' |
| 120 | git checkout primary && |
| 121 | mkdir subdir && (cd subdir && |
| 122 | set_fake_editor && |
| 123 | FAKE_LINES="1 exec_>touch-subdir" \ |
| 124 | git rebase -i HEAD^ |
| 125 | ) && |
| 126 | test_path_is_file touch-subdir && |
| 127 | rm -fr subdir |
| 128 | ' |
| 129 | |
| 130 | test_expect_success 'rebase -i with exec allows git commands in subdirs' ' |
| 131 | test_when_finished "rm -rf subdir" && |
| 132 | test_when_finished "git rebase --abort ||:" && |
| 133 | git checkout primary && |
| 134 | mkdir subdir && (cd subdir && |
| 135 | set_fake_editor && |
| 136 | FAKE_LINES="1 x_cd_subdir_&&_git_rev-parse_--is-inside-work-tree" \ |
| 137 | git rebase -i HEAD^ |
| 138 | ) |
| 139 | ' |
| 140 | |
| 141 | test_expect_success 'rebase -i sets work tree properly' ' |
| 142 | test_when_finished "rm -rf subdir" && |
| 143 | test_when_finished "test_might_fail git rebase --abort" && |
| 144 | mkdir subdir && |
| 145 | git rebase -x "(cd subdir && git rev-parse --show-toplevel)" HEAD^ \ |
| 146 | >actual && |
| 147 | test_grep ! "/subdir$" actual |
| 148 | ' |
| 149 | |
| 150 | test_expect_success 'rebase -i with the exec command checks tree cleanness' ' |
| 151 | git checkout primary && |
| 152 | ( |
| 153 | set_fake_editor && |
| 154 | test_must_fail env FAKE_LINES="exec_echo_foo_>file1 1" \ |
| 155 | git rebase -i HEAD^ |
| 156 | ) && |
| 157 | test_cmp_rev primary^ HEAD && |
| 158 | git reset --hard && |
| 159 | git rebase --continue |
| 160 | ' |
| 161 | |
| 162 | test_expect_success 'cherry-pick works with rebase --exec' ' |
| 163 | test_when_finished "git cherry-pick --abort; \ |
| 164 | git rebase --abort; \ |
| 165 | git checkout primary" && |
| 166 | echo "exec git cherry-pick G" >todo && |
| 167 | ( |
| 168 | set_replace_editor todo && |
| 169 | test_must_fail git rebase -i D D |
| 170 | ) && |
| 171 | test_cmp_rev G CHERRY_PICK_HEAD |
| 172 | ' |
| 173 | |
| 174 | test_expect_success 'rebase -x with empty command fails' ' |
| 175 | test_when_finished "git rebase --abort ||:" && |
| 176 | test_must_fail env git rebase -x "" @ 2>actual && |
| 177 | test_write_lines "error: empty exec command" >expected && |
| 178 | test_cmp expected actual && |
| 179 | test_must_fail env git rebase -x " " @ 2>actual && |
| 180 | test_cmp expected actual |
| 181 | ' |
| 182 | |
| 183 | test_expect_success 'rebase -x with newline in command fails' ' |
| 184 | test_when_finished "git rebase --abort ||:" && |
| 185 | test_must_fail env git rebase -x "a${LF}b" @ 2>actual && |
| 186 | test_write_lines "error: exec commands cannot contain newlines" \ |
| 187 | >expected && |
| 188 | test_cmp expected actual |
| 189 | ' |
| 190 | |
| 191 | test_expect_success 'rebase -i with exec of inexistent command' ' |
| 192 | git checkout primary && |
| 193 | test_when_finished "git rebase --abort" && |
| 194 | ( |
| 195 | set_fake_editor && |
| 196 | test_must_fail env FAKE_LINES="exec_this-command-does-not-exist 1" \ |
| 197 | git rebase -i HEAD^ >actual 2>&1 |
| 198 | ) && |
| 199 | test_grep ! "Maybe git-rebase is broken" actual |
| 200 | ' |
| 201 | |
| 202 | test_expect_success 'implicit interactive rebase does not invoke sequence editor' ' |
| 203 | test_when_finished "git rebase --abort ||:" && |
| 204 | GIT_SEQUENCE_EDITOR="echo bad >" git rebase -x"echo one" @^ |
| 205 | ' |
| 206 | |
| 207 | test_expect_success 'no changes are a nop' ' |
| 208 | git checkout branch2 && |
| 209 | git rebase -i F && |
| 210 | test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch2" && |
| 211 | test_cmp_rev I HEAD |
| 212 | ' |
| 213 | |
| 214 | test_expect_success 'test the [branch] option' ' |
| 215 | git checkout -b dead-end && |
| 216 | git rm file6 && |
| 217 | git commit -m "stop here" && |
| 218 | git rebase -i F branch2 && |
| 219 | test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch2" && |
| 220 | test_cmp_rev I branch2 && |
| 221 | test_cmp_rev I HEAD |
| 222 | ' |
| 223 | |
| 224 | test_expect_success 'test --onto <branch>' ' |
| 225 | git checkout -b test-onto branch2 && |
| 226 | git rebase -i --onto branch1 F && |
| 227 | test "$(git symbolic-ref -q HEAD)" = "refs/heads/test-onto" && |
| 228 | test_cmp_rev HEAD^ branch1 && |
| 229 | test_cmp_rev I branch2 |
| 230 | ' |
| 231 | |
| 232 | test_expect_success 'rebase on top of a non-conflicting commit' ' |
| 233 | git checkout branch1 && |
| 234 | git tag original-branch1 && |
| 235 | git rebase -i branch2 && |
| 236 | test file6 = $(git diff --name-only original-branch1) && |
| 237 | test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch1" && |
| 238 | test_cmp_rev I branch2 && |
| 239 | test_cmp_rev I HEAD~2 |
| 240 | ' |
| 241 | |
| 242 | test_expect_success 'reflog for the branch shows state before rebase' ' |
| 243 | test_cmp_rev branch1@{1} original-branch1 |
| 244 | ' |
| 245 | |
| 246 | test_expect_success 'reflog for the branch shows correct finish message' ' |
| 247 | printf "rebase (finish): refs/heads/branch1 onto %s\n" \ |
| 248 | "$(git rev-parse branch2)" >expected && |
| 249 | git log -g --pretty=%gs -1 refs/heads/branch1 >actual && |
| 250 | test_cmp expected actual |
| 251 | ' |
| 252 | |
| 253 | test_expect_success 'exchange two commits' ' |
| 254 | ( |
| 255 | set_fake_editor && |
| 256 | FAKE_LINES="2 1" git rebase -i HEAD~2 |
| 257 | ) && |
| 258 | test H = $(git cat-file commit HEAD^ | sed -ne \$p) && |
| 259 | test G = $(git cat-file commit HEAD | sed -ne \$p) && |
| 260 | blob1=$(git rev-parse --short HEAD^:file1) && |
| 261 | blob2=$(git rev-parse --short HEAD:file1) && |
| 262 | commit=$(git rev-parse --short HEAD) |
| 263 | ' |
| 264 | |
| 265 | test_expect_success 'stop on conflicting pick' ' |
| 266 | cat >expect <<-EOF && |
| 267 | diff --git a/file1 b/file1 |
| 268 | index $blob1..$blob2 100644 |
| 269 | --- a/file1 |
| 270 | +++ b/file1 |
| 271 | @@ -1 +1 @@ |
| 272 | -A |
| 273 | +G |
| 274 | EOF |
| 275 | cat >expect2 <<-EOF && |
| 276 | <<<<<<< HEAD |
| 277 | D |
| 278 | ======= |
| 279 | G |
| 280 | >>>>>>> $commit (G) |
| 281 | EOF |
| 282 | git tag new-branch1 && |
| 283 | test_must_fail git rebase -i primary && |
| 284 | test "$(git rev-parse HEAD~3)" = "$(git rev-parse primary)" && |
| 285 | test_cmp expect .git/rebase-merge/patch && |
| 286 | test_cmp expect2 file1 && |
| 287 | test "$(git diff --name-status | |
| 288 | sed -n -e "/^U/s/^U[^a-z]*//p")" = file1 && |
| 289 | grep -v "^#" <.git/rebase-merge/done >actual && |
| 290 | test_line_count = 4 actual && |
| 291 | test 0 = $(grep -c "^[^#]" <.git/rebase-merge/git-rebase-todo) |
| 292 | ' |
| 293 | |
| 294 | test_expect_success 'show conflicted patch' ' |
| 295 | GIT_TRACE=1 git rebase --show-current-patch >/dev/null 2>stderr && |
| 296 | test_grep "show.*REBASE_HEAD" stderr && |
| 297 | # the original stopped-sha1 is abbreviated |
| 298 | stopped_sha1="$(git rev-parse $(cat ".git/rebase-merge/stopped-sha"))" && |
| 299 | test "$(git rev-parse REBASE_HEAD)" = "$stopped_sha1" |
| 300 | ' |
| 301 | |
| 302 | test_expect_success 'abort' ' |
| 303 | git rebase --abort && |
| 304 | test_cmp_rev new-branch1 HEAD && |
| 305 | test "$(git symbolic-ref -q HEAD)" = "refs/heads/branch1" && |
| 306 | test_path_is_missing .git/rebase-merge |
| 307 | ' |
| 308 | |
| 309 | test_expect_success 'abort with error when new base cannot be checked out' ' |
| 310 | git rm --cached file1 && |
| 311 | git commit -m "remove file in base" && |
| 312 | test_must_fail git rebase -i primary > output 2>&1 && |
| 313 | test_grep "The following untracked working tree files would be overwritten by checkout:" \ |
| 314 | output && |
| 315 | test_grep "file1" output && |
| 316 | test_path_is_missing .git/rebase-merge && |
| 317 | rm file1 && |
| 318 | git reset --hard HEAD^ |
| 319 | ' |
| 320 | |
| 321 | test_expect_success 'retain authorship' ' |
| 322 | echo A > file7 && |
| 323 | git add file7 && |
| 324 | test_tick && |
| 325 | GIT_AUTHOR_NAME="Twerp Snog" git commit -m "different author" && |
| 326 | git tag twerp && |
| 327 | git rebase -i --onto primary HEAD^ && |
| 328 | git show HEAD >actual && |
| 329 | test_grep "^Author: Twerp Snog" actual |
| 330 | ' |
| 331 | |
| 332 | test_expect_success 'retain authorship w/ conflicts' ' |
| 333 | oGIT_AUTHOR_NAME=$GIT_AUTHOR_NAME && |
| 334 | test_when_finished "GIT_AUTHOR_NAME=\$oGIT_AUTHOR_NAME" && |
| 335 | |
| 336 | git reset --hard twerp && |
| 337 | test_commit a conflict a conflict-a && |
| 338 | git reset --hard twerp && |
| 339 | |
| 340 | GIT_AUTHOR_NAME=AttributeMe && |
| 341 | export GIT_AUTHOR_NAME && |
| 342 | test_commit b conflict b conflict-b && |
| 343 | GIT_AUTHOR_NAME=$oGIT_AUTHOR_NAME && |
| 344 | |
| 345 | test_must_fail git rebase -i conflict-a && |
| 346 | echo resolved >conflict && |
| 347 | git add conflict && |
| 348 | git rebase --continue && |
| 349 | test_cmp_rev conflict-a^0 HEAD^ && |
| 350 | git show >out && |
| 351 | test_grep AttributeMe out |
| 352 | ' |
| 353 | |
| 354 | test_expect_success 'squash' ' |
| 355 | git reset --hard twerp && |
| 356 | echo B > file7 && |
| 357 | test_tick && |
| 358 | GIT_AUTHOR_NAME="Nitfol" git commit -m "nitfol" file7 && |
| 359 | echo "******************************" && |
| 360 | ( |
| 361 | set_fake_editor && |
| 362 | FAKE_LINES="1 squash 2" EXPECT_HEADER_COUNT=2 \ |
| 363 | git rebase -i --onto primary HEAD~2 |
| 364 | ) && |
| 365 | test B = $(cat file7) && |
| 366 | test_cmp_rev HEAD^ primary |
| 367 | ' |
| 368 | |
| 369 | test_expect_success 'retain authorship when squashing' ' |
| 370 | git show HEAD >actual && |
| 371 | test_grep "^Author: Twerp Snog" actual |
| 372 | ' |
| 373 | |
| 374 | test_expect_success '--continue tries to commit' ' |
| 375 | git reset --hard D && |
| 376 | test_tick && |
| 377 | ( |
| 378 | set_fake_editor && |
| 379 | test_must_fail git rebase -i --onto new-branch1 HEAD^ && |
| 380 | echo resolved > file1 && |
| 381 | git add file1 && |
| 382 | FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue |
| 383 | ) && |
| 384 | test_cmp_rev HEAD^ new-branch1 && |
| 385 | git show HEAD >actual && |
| 386 | test_grep chouette actual |
| 387 | ' |
| 388 | |
| 389 | test_expect_success 'verbose flag is heeded, even after --continue' ' |
| 390 | git reset --hard primary@{1} && |
| 391 | test_tick && |
| 392 | test_must_fail git rebase -v -i --onto new-branch1 HEAD^ && |
| 393 | echo resolved > file1 && |
| 394 | git add file1 && |
| 395 | git rebase --continue > output && |
| 396 | test_grep "^ file1 | 2 +-$" output |
| 397 | ' |
| 398 | |
| 399 | test_expect_success 'multi-squash only fires up editor once' ' |
| 400 | base=$(git rev-parse HEAD~4) && |
| 401 | ( |
| 402 | set_fake_editor && |
| 403 | FAKE_COMMIT_AMEND="ONCE" \ |
| 404 | FAKE_LINES="1 squash 2 squash 3 squash 4" \ |
| 405 | EXPECT_HEADER_COUNT=4 \ |
| 406 | git rebase -i $base |
| 407 | ) && |
| 408 | test $base = $(git rev-parse HEAD^) && |
| 409 | git show >output && |
| 410 | grep ONCE output >actual && |
| 411 | test_line_count = 1 actual |
| 412 | ' |
| 413 | |
| 414 | test_expect_success 'multi-fixup does not fire up editor' ' |
| 415 | git checkout -b multi-fixup E && |
| 416 | base=$(git rev-parse HEAD~4) && |
| 417 | ( |
| 418 | set_fake_editor && |
| 419 | FAKE_COMMIT_AMEND="NEVER" \ |
| 420 | FAKE_LINES="1 fixup 2 fixup 3 fixup 4" \ |
| 421 | git rebase -i $base |
| 422 | ) && |
| 423 | test $base = $(git rev-parse HEAD^) && |
| 424 | git show >output && |
| 425 | test_grep ! NEVER output && |
| 426 | git checkout @{-1} && |
| 427 | git branch -D multi-fixup |
| 428 | ' |
| 429 | |
| 430 | test_expect_success 'commit message used after conflict' ' |
| 431 | git checkout -b conflict-fixup conflict-branch && |
| 432 | base=$(git rev-parse HEAD~4) && |
| 433 | ( |
| 434 | set_fake_editor && |
| 435 | test_must_fail env FAKE_LINES="1 fixup 3 fixup 4" \ |
| 436 | git rebase -i $base && |
| 437 | echo three > conflict && |
| 438 | git add conflict && |
| 439 | FAKE_COMMIT_AMEND="ONCE" EXPECT_HEADER_COUNT=2 \ |
| 440 | git rebase --continue |
| 441 | ) && |
| 442 | test $base = $(git rev-parse HEAD^) && |
| 443 | git show >output && |
| 444 | grep ONCE output >actual && |
| 445 | test_line_count = 1 actual && |
| 446 | git checkout @{-1} && |
| 447 | git branch -D conflict-fixup |
| 448 | ' |
| 449 | |
| 450 | test_expect_success 'commit message retained after conflict' ' |
| 451 | git checkout -b conflict-squash conflict-branch && |
| 452 | base=$(git rev-parse HEAD~4) && |
| 453 | ( |
| 454 | set_fake_editor && |
| 455 | test_must_fail env FAKE_LINES="1 fixup 3 squash 4" \ |
| 456 | git rebase -i $base && |
| 457 | echo three > conflict && |
| 458 | git add conflict && |
| 459 | FAKE_COMMIT_AMEND="TWICE" EXPECT_HEADER_COUNT=2 \ |
| 460 | git rebase --continue |
| 461 | ) && |
| 462 | test $base = $(git rev-parse HEAD^) && |
| 463 | git show >output && |
| 464 | grep TWICE output >actual && |
| 465 | test_line_count = 2 actual && |
| 466 | git checkout @{-1} && |
| 467 | git branch -D conflict-squash |
| 468 | ' |
| 469 | |
| 470 | test_expect_success 'squash and fixup generate correct log messages' ' |
| 471 | cat >expect-squash-fixup <<-\EOF && |
| 472 | B |
| 473 | |
| 474 | D |
| 475 | |
| 476 | ONCE |
| 477 | EOF |
| 478 | git checkout -b squash-fixup E && |
| 479 | base=$(git rev-parse HEAD~4) && |
| 480 | ( |
| 481 | set_fake_editor && |
| 482 | FAKE_COMMIT_AMEND="ONCE" \ |
| 483 | FAKE_LINES="1 fixup 2 squash 3 fixup 4" \ |
| 484 | EXPECT_HEADER_COUNT=4 \ |
| 485 | git rebase -i $base |
| 486 | ) && |
| 487 | commit_body HEAD >actual-squash-fixup && |
| 488 | test_cmp expect-squash-fixup actual-squash-fixup && |
| 489 | git cat-file commit HEAD@{2} >actual && |
| 490 | test_grep "^# This is a combination of 3 commits\." actual && |
| 491 | git cat-file commit HEAD@{3} >actual && |
| 492 | test_grep "^# This is a combination of 2 commits\." actual && |
| 493 | git checkout @{-1} && |
| 494 | git branch -D squash-fixup |
| 495 | ' |
| 496 | |
| 497 | test_expect_success 'squash ignores comments' ' |
| 498 | git checkout -b skip-comments E && |
| 499 | base=$(git rev-parse HEAD~4) && |
| 500 | ( |
| 501 | set_fake_editor && |
| 502 | FAKE_COMMIT_AMEND="ONCE" \ |
| 503 | FAKE_LINES="# 1 # squash 2 # squash 3 # squash 4 #" \ |
| 504 | EXPECT_HEADER_COUNT=4 \ |
| 505 | git rebase -i $base |
| 506 | ) && |
| 507 | test $base = $(git rev-parse HEAD^) && |
| 508 | git show >output && |
| 509 | grep ONCE output >actual && |
| 510 | test_line_count = 1 actual && |
| 511 | git checkout @{-1} && |
| 512 | git branch -D skip-comments |
| 513 | ' |
| 514 | |
| 515 | test_expect_success 'squash ignores blank lines' ' |
| 516 | git checkout -b skip-blank-lines E && |
| 517 | base=$(git rev-parse HEAD~4) && |
| 518 | ( |
| 519 | set_fake_editor && |
| 520 | FAKE_COMMIT_AMEND="ONCE" \ |
| 521 | FAKE_LINES="> 1 > squash 2 > squash 3 > squash 4 >" \ |
| 522 | EXPECT_HEADER_COUNT=4 \ |
| 523 | git rebase -i $base |
| 524 | ) && |
| 525 | test $base = $(git rev-parse HEAD^) && |
| 526 | git show >output && |
| 527 | grep ONCE output >actual && |
| 528 | test_line_count = 1 actual && |
| 529 | git checkout @{-1} && |
| 530 | git branch -D skip-blank-lines |
| 531 | ' |
| 532 | |
| 533 | test_expect_success 'squash works as expected' ' |
| 534 | git checkout -b squash-works no-conflict-branch && |
| 535 | one=$(git rev-parse HEAD~3) && |
| 536 | ( |
| 537 | set_fake_editor && |
| 538 | FAKE_LINES="1 s 3 2" EXPECT_HEADER_COUNT=2 git rebase -i HEAD~3 |
| 539 | ) && |
| 540 | test $one = $(git rev-parse HEAD~2) |
| 541 | ' |
| 542 | |
| 543 | test_expect_success 'interrupted squash works as expected' ' |
| 544 | git checkout -b interrupted-squash conflict-branch && |
| 545 | one=$(git rev-parse HEAD~3) && |
| 546 | ( |
| 547 | set_fake_editor && |
| 548 | test_must_fail env FAKE_LINES="1 squash 3 2" \ |
| 549 | git rebase -i HEAD~3 |
| 550 | ) && |
| 551 | test_write_lines one two four > conflict && |
| 552 | git add conflict && |
| 553 | test_must_fail git rebase --continue && |
| 554 | echo resolved > conflict && |
| 555 | git add conflict && |
| 556 | git rebase --continue && |
| 557 | test $one = $(git rev-parse HEAD~2) |
| 558 | ' |
| 559 | |
| 560 | test_expect_success 'interrupted squash works as expected (case 2)' ' |
| 561 | git checkout -b interrupted-squash2 conflict-branch && |
| 562 | one=$(git rev-parse HEAD~3) && |
| 563 | ( |
| 564 | set_fake_editor && |
| 565 | test_must_fail env FAKE_LINES="3 squash 1 2" \ |
| 566 | git rebase -i HEAD~3 |
| 567 | ) && |
| 568 | test_write_lines one four > conflict && |
| 569 | git add conflict && |
| 570 | test_must_fail git rebase --continue && |
| 571 | test_write_lines one two four > conflict && |
| 572 | git add conflict && |
| 573 | test_must_fail git rebase --continue && |
| 574 | echo resolved > conflict && |
| 575 | git add conflict && |
| 576 | git rebase --continue && |
| 577 | test $one = $(git rev-parse HEAD~2) |
| 578 | ' |
| 579 | |
| 580 | test_expect_success '--continue tries to commit, even for "edit"' ' |
| 581 | echo unrelated > file7 && |
| 582 | git add file7 && |
| 583 | test_tick && |
| 584 | git commit -m "unrelated change" && |
| 585 | parent=$(git rev-parse HEAD^) && |
| 586 | test_tick && |
| 587 | ( |
| 588 | set_fake_editor && |
| 589 | FAKE_LINES="edit 1" git rebase -i HEAD^ && |
| 590 | echo edited > file7 && |
| 591 | git add file7 && |
| 592 | FAKE_COMMIT_MESSAGE="chouette!" git rebase --continue |
| 593 | ) && |
| 594 | test edited = $(git show HEAD:file7) && |
| 595 | git show HEAD >actual && |
| 596 | test_grep chouette actual && |
| 597 | test $parent = $(git rev-parse HEAD^) |
| 598 | ' |
| 599 | |
| 600 | test_expect_success 'aborted --continue does not squash commits after "edit"' ' |
| 601 | old=$(git rev-parse HEAD) && |
| 602 | test_tick && |
| 603 | ( |
| 604 | set_fake_editor && |
| 605 | FAKE_LINES="edit 1" git rebase -i HEAD^ && |
| 606 | echo "edited again" > file7 && |
| 607 | git add file7 && |
| 608 | test_must_fail env FAKE_COMMIT_MESSAGE=" " git rebase --continue |
| 609 | ) && |
| 610 | test $old = $(git rev-parse HEAD) && |
| 611 | git rebase --abort |
| 612 | ' |
| 613 | |
| 614 | test_expect_success 'auto-amend only edited commits after "edit"' ' |
| 615 | test_tick && |
| 616 | ( |
| 617 | set_fake_editor && |
| 618 | FAKE_LINES="edit 1" git rebase -i HEAD^ && |
| 619 | echo "edited again" > file7 && |
| 620 | git add file7 && |
| 621 | FAKE_COMMIT_MESSAGE="edited file7 again" git commit && |
| 622 | echo "and again" > file7 && |
| 623 | git add file7 && |
| 624 | test_tick && |
| 625 | test_must_fail env FAKE_COMMIT_MESSAGE="and again" \ |
| 626 | git rebase --continue |
| 627 | ) && |
| 628 | git rebase --abort |
| 629 | ' |
| 630 | |
| 631 | test_expect_success 'clean error after failed "exec"' ' |
| 632 | test_tick && |
| 633 | test_when_finished "git rebase --abort || :" && |
| 634 | ( |
| 635 | set_fake_editor && |
| 636 | test_must_fail env FAKE_LINES="1 exec_false" git rebase -i HEAD^ |
| 637 | ) && |
| 638 | echo "edited again" > file7 && |
| 639 | git add file7 && |
| 640 | test_must_fail git rebase --continue 2>error && |
| 641 | test_grep "you have staged changes in your working tree" error && |
| 642 | test_grep ! "could not open.*for reading" error |
| 643 | ' |
| 644 | |
| 645 | test_expect_success 'rebase a detached HEAD' ' |
| 646 | grandparent=$(git rev-parse HEAD~2) && |
| 647 | git checkout $(git rev-parse HEAD) && |
| 648 | test_tick && |
| 649 | ( |
| 650 | set_fake_editor && |
| 651 | FAKE_LINES="2 1" git rebase -i HEAD~2 |
| 652 | ) && |
| 653 | test $grandparent = $(git rev-parse HEAD~2) |
| 654 | ' |
| 655 | |
| 656 | test_expect_success 'rebase a commit violating pre-commit' ' |
| 657 | test_hook pre-commit <<-\EOF && |
| 658 | test -z "$(git diff --cached --check)" |
| 659 | EOF |
| 660 | echo "monde! " >> file1 && |
| 661 | test_tick && |
| 662 | test_must_fail git commit -m doesnt-verify file1 && |
| 663 | git commit -m doesnt-verify --no-verify file1 && |
| 664 | test_tick && |
| 665 | ( |
| 666 | set_fake_editor && |
| 667 | FAKE_LINES=2 git rebase -i HEAD~2 |
| 668 | ) |
| 669 | ' |
| 670 | |
| 671 | test_expect_success 'rebase with a file named HEAD in worktree' ' |
| 672 | git reset --hard && |
| 673 | git checkout -b branch3 A && |
| 674 | |
| 675 | ( |
| 676 | GIT_AUTHOR_NAME="Squashed Away" && |
| 677 | export GIT_AUTHOR_NAME && |
| 678 | >HEAD && |
| 679 | git add HEAD && |
| 680 | git commit -m "Add head" && |
| 681 | >BODY && |
| 682 | git add BODY && |
| 683 | git commit -m "Add body" |
| 684 | ) && |
| 685 | |
| 686 | ( |
| 687 | set_fake_editor && |
| 688 | FAKE_LINES="1 squash 2" git rebase -i @{-1} |
| 689 | ) && |
| 690 | test "$(git show -s --pretty=format:%an)" = "Squashed Away" |
| 691 | |
| 692 | ' |
| 693 | |
| 694 | test_expect_success 'do "noop" when there is nothing to cherry-pick' ' |
| 695 | |
| 696 | git checkout -b branch4 HEAD && |
| 697 | GIT_EDITOR=: git commit --amend \ |
| 698 | --author="Somebody else <somebody@else.com>" && |
| 699 | test $(git rev-parse branch3) != $(git rev-parse branch4) && |
| 700 | git rebase -i branch3 && |
| 701 | test_cmp_rev branch3 branch4 |
| 702 | |
| 703 | ' |
| 704 | |
| 705 | test_expect_success 'submodule rebase setup' ' |
| 706 | git checkout A && |
| 707 | mkdir sub && |
| 708 | ( |
| 709 | cd sub && git init && >elif && |
| 710 | git add elif && git commit -m "submodule initial" |
| 711 | ) && |
| 712 | echo 1 >file1 && |
| 713 | git add file1 sub && |
| 714 | test_tick && |
| 715 | git commit -m "One" && |
| 716 | echo 2 >file1 && |
| 717 | test_tick && |
| 718 | git commit -a -m "Two" && |
| 719 | ( |
| 720 | cd sub && echo 3 >elif && |
| 721 | git commit -a -m "submodule second" |
| 722 | ) && |
| 723 | test_tick && |
| 724 | git commit -a -m "Three changes submodule" |
| 725 | ' |
| 726 | |
| 727 | test_expect_success 'submodule rebase -i' ' |
| 728 | ( |
| 729 | set_fake_editor && |
| 730 | FAKE_LINES="1 squash 2 3" git rebase -i A |
| 731 | ) |
| 732 | ' |
| 733 | |
| 734 | test_expect_success 'submodule conflict setup' ' |
| 735 | git tag submodule-base && |
| 736 | git checkout HEAD^ && |
| 737 | ( |
| 738 | cd sub && git checkout HEAD^ && echo 4 >elif && |
| 739 | git add elif && git commit -m "submodule conflict" |
| 740 | ) && |
| 741 | git add sub && |
| 742 | test_tick && |
| 743 | git commit -m "Conflict in submodule" && |
| 744 | git tag submodule-topic |
| 745 | ' |
| 746 | |
| 747 | test_expect_success 'rebase -i continue with only submodule staged' ' |
| 748 | test_must_fail git rebase -i submodule-base && |
| 749 | git add sub && |
| 750 | git rebase --continue && |
| 751 | test $(git rev-parse submodule-base) != $(git rev-parse HEAD) |
| 752 | ' |
| 753 | |
| 754 | test_expect_success 'rebase -i continue with unstaged submodule' ' |
| 755 | git checkout submodule-topic && |
| 756 | git reset --hard && |
| 757 | test_must_fail git rebase -i submodule-base && |
| 758 | git reset && |
| 759 | git rebase --continue && |
| 760 | test_cmp_rev submodule-base HEAD |
| 761 | ' |
| 762 | |
| 763 | test_expect_success 'avoid unnecessary reset' ' |
| 764 | git checkout primary && |
| 765 | git reset --hard && |
| 766 | test-tool chmtime =123456789 file3 && |
| 767 | git update-index --refresh && |
| 768 | HEAD=$(git rev-parse HEAD) && |
| 769 | git rebase -i HEAD~4 && |
| 770 | test $HEAD = $(git rev-parse HEAD) && |
| 771 | MTIME=$(test-tool chmtime --get file3) && |
| 772 | test 123456789 = $MTIME |
| 773 | ' |
| 774 | |
| 775 | test_expect_success 'reword' ' |
| 776 | git checkout -b reword-branch primary && |
| 777 | ( |
| 778 | set_fake_editor && |
| 779 | FAKE_LINES="1 2 3 reword 4" FAKE_COMMIT_MESSAGE="E changed" \ |
| 780 | git rebase -i A && |
| 781 | git show HEAD >actual && |
| 782 | test_grep "E changed" actual && |
| 783 | test $(git rev-parse primary) != $(git rev-parse HEAD) && |
| 784 | test_cmp_rev primary^ HEAD^ && |
| 785 | FAKE_LINES="1 2 reword 3 4" FAKE_COMMIT_MESSAGE="D changed" \ |
| 786 | git rebase -i A && |
| 787 | git show HEAD^ >actual && |
| 788 | test_grep "D changed" actual && |
| 789 | FAKE_LINES="reword 1 2 3 4" FAKE_COMMIT_MESSAGE="B changed" \ |
| 790 | git rebase -i A && |
| 791 | git show HEAD~3 >actual && |
| 792 | test_grep "B changed" actual && |
| 793 | FAKE_LINES="1 r 2 pick 3 p 4" FAKE_COMMIT_MESSAGE="C changed" \ |
| 794 | git rebase -i A |
| 795 | ) && |
| 796 | git show HEAD~2 >actual && |
| 797 | test_grep "C changed" actual |
| 798 | ' |
| 799 | |
| 800 | test_expect_success 'reword fast-forwarded empty commit' ' |
| 801 | git commit --allow-empty -m "empty commit" --only && |
| 802 | ( |
| 803 | set_fake_editor && |
| 804 | FAKE_COMMIT_AMEND=edited FAKE_LINES="reword 1" \ |
| 805 | git rebase -i HEAD^ |
| 806 | ) && |
| 807 | test_commit_message HEAD <<-\EOF |
| 808 | empty commit |
| 809 | |
| 810 | edited |
| 811 | EOF |
| 812 | ' |
| 813 | |
| 814 | test_expect_success 'no uncommitted changes when rewording and the todo list is reloaded' ' |
| 815 | git checkout E && |
| 816 | test_when_finished "git checkout @{-1}" && |
| 817 | ( |
| 818 | set_fake_editor && |
| 819 | GIT_SEQUENCE_EDITOR="\"$PWD/fake-editor.sh\"" && |
| 820 | export GIT_SEQUENCE_EDITOR && |
| 821 | set_reword_editor && |
| 822 | FAKE_LINES="reword 1 reword 2" git rebase -i C |
| 823 | ) && |
| 824 | check_reworded_commits D E |
| 825 | ' |
| 826 | |
| 827 | test_expect_success 'rebase -i can copy notes' ' |
| 828 | git config notes.rewrite.rebase true && |
| 829 | git config notes.rewriteRef "refs/notes/*" && |
| 830 | test_commit n1 && |
| 831 | test_commit n2 && |
| 832 | test_commit n3 && |
| 833 | git notes add -m"a note" n3 && |
| 834 | git rebase -i --onto n1 n2 && |
| 835 | test "a note" = "$(git notes show HEAD)" |
| 836 | ' |
| 837 | |
| 838 | test_expect_success 'rebase -i can copy notes over a fixup' ' |
| 839 | cat >expect <<-\EOF && |
| 840 | an earlier note |
| 841 | |
| 842 | a note |
| 843 | EOF |
| 844 | git reset --hard n3 && |
| 845 | git notes add -m"an earlier note" n2 && |
| 846 | ( |
| 847 | set_fake_editor && |
| 848 | GIT_NOTES_REWRITE_MODE=concatenate FAKE_LINES="1 f 2" \ |
| 849 | git rebase -i n1 |
| 850 | ) && |
| 851 | git notes show > output && |
| 852 | test_cmp expect output |
| 853 | ' |
| 854 | |
| 855 | test_expect_success 'rebase while detaching HEAD' ' |
| 856 | git symbolic-ref HEAD && |
| 857 | grandparent=$(git rev-parse HEAD~2) && |
| 858 | test_tick && |
| 859 | ( |
| 860 | set_fake_editor && |
| 861 | FAKE_LINES="2 1" git rebase -i HEAD~2 HEAD^0 |
| 862 | ) && |
| 863 | test $grandparent = $(git rev-parse HEAD~2) && |
| 864 | test_must_fail git symbolic-ref HEAD |
| 865 | ' |
| 866 | |
| 867 | test_tick # Ensure that the rebased commits get a different timestamp. |
| 868 | test_expect_success 'always cherry-pick with --no-ff' ' |
| 869 | git checkout no-ff-branch && |
| 870 | git tag original-no-ff-branch && |
| 871 | git rebase -i --no-ff A && |
| 872 | for p in 0 1 2 |
| 873 | do |
| 874 | test ! $(git rev-parse HEAD~$p) = $(git rev-parse original-no-ff-branch~$p) && |
| 875 | git diff HEAD~$p original-no-ff-branch~$p > out && |
| 876 | test_must_be_empty out || return 1 |
| 877 | done && |
| 878 | test_cmp_rev HEAD~3 original-no-ff-branch~3 && |
| 879 | git diff HEAD~3 original-no-ff-branch~3 > out && |
| 880 | test_must_be_empty out |
| 881 | ' |
| 882 | |
| 883 | test_expect_success 'set up commits with funny messages' ' |
| 884 | git checkout -b funny A && |
| 885 | echo >>file1 && |
| 886 | test_tick && |
| 887 | git commit -a -m "end with slash\\" && |
| 888 | echo >>file1 && |
| 889 | test_tick && |
| 890 | git commit -a -m "something (\000) that looks like octal" && |
| 891 | echo >>file1 && |
| 892 | test_tick && |
| 893 | git commit -a -m "something (\n) that looks like a newline" && |
| 894 | echo >>file1 && |
| 895 | test_tick && |
| 896 | git commit -a -m "another commit" |
| 897 | ' |
| 898 | |
| 899 | test_expect_success 'rebase-i history with funny messages' ' |
| 900 | git rev-list A..funny >expect && |
| 901 | test_tick && |
| 902 | ( |
| 903 | set_fake_editor && |
| 904 | FAKE_LINES="1 2 3 4" git rebase -i A |
| 905 | ) && |
| 906 | git rev-list A.. >actual && |
| 907 | test_cmp expect actual |
| 908 | ' |
| 909 | |
| 910 | test_expect_success 'prepare for rebase -i --exec' ' |
| 911 | git checkout primary && |
| 912 | git checkout -b execute && |
| 913 | test_commit one_exec main.txt one_exec && |
| 914 | test_commit two_exec main.txt two_exec && |
| 915 | test_commit three_exec main.txt three_exec |
| 916 | ' |
| 917 | |
| 918 | test_expect_success 'running "git rebase -i --exec git show HEAD"' ' |
| 919 | ( |
| 920 | set_fake_editor && |
| 921 | git rebase -i --exec "git show HEAD" HEAD~2 >actual && |
| 922 | FAKE_LINES="1 exec_git_show_HEAD 2 exec_git_show_HEAD" && |
| 923 | export FAKE_LINES && |
| 924 | git rebase -i HEAD~2 >expect |
| 925 | ) && |
| 926 | sed -e "1,9d" expect >expected && |
| 927 | test_cmp expected actual |
| 928 | ' |
| 929 | |
| 930 | test_expect_success 'running "git rebase --exec git show HEAD -i"' ' |
| 931 | git reset --hard execute && |
| 932 | ( |
| 933 | set_fake_editor && |
| 934 | git rebase --exec "git show HEAD" -i HEAD~2 >actual && |
| 935 | FAKE_LINES="1 exec_git_show_HEAD 2 exec_git_show_HEAD" && |
| 936 | export FAKE_LINES && |
| 937 | git rebase -i HEAD~2 >expect |
| 938 | ) && |
| 939 | sed -e "1,9d" expect >expected && |
| 940 | test_cmp expected actual |
| 941 | ' |
| 942 | |
| 943 | test_expect_success 'running "git rebase -ix git show HEAD"' ' |
| 944 | git reset --hard execute && |
| 945 | ( |
| 946 | set_fake_editor && |
| 947 | git rebase -ix "git show HEAD" HEAD~2 >actual && |
| 948 | FAKE_LINES="1 exec_git_show_HEAD 2 exec_git_show_HEAD" && |
| 949 | export FAKE_LINES && |
| 950 | git rebase -i HEAD~2 >expect |
| 951 | ) && |
| 952 | sed -e "1,9d" expect >expected && |
| 953 | test_cmp expected actual |
| 954 | ' |
| 955 | |
| 956 | |
| 957 | test_expect_success 'rebase -ix with several <CMD>' ' |
| 958 | git reset --hard execute && |
| 959 | ( |
| 960 | set_fake_editor && |
| 961 | git rebase -ix "git show HEAD; pwd" HEAD~2 >actual && |
| 962 | FAKE_LINES="1 exec_git_show_HEAD;_pwd 2 exec_git_show_HEAD;_pwd" && |
| 963 | export FAKE_LINES && |
| 964 | git rebase -i HEAD~2 >expect |
| 965 | ) && |
| 966 | sed -e "1,9d" expect >expected && |
| 967 | test_cmp expected actual |
| 968 | ' |
| 969 | |
| 970 | test_expect_success 'rebase -ix with several instances of --exec' ' |
| 971 | git reset --hard execute && |
| 972 | ( |
| 973 | set_fake_editor && |
| 974 | git rebase -i --exec "git show HEAD" --exec "pwd" HEAD~2 >actual && |
| 975 | FAKE_LINES="1 exec_git_show_HEAD exec_pwd 2 |
| 976 | exec_git_show_HEAD exec_pwd" && |
| 977 | export FAKE_LINES && |
| 978 | git rebase -i HEAD~2 >expect |
| 979 | ) && |
| 980 | sed -e "1,11d" expect >expected && |
| 981 | test_cmp expected actual |
| 982 | ' |
| 983 | |
| 984 | test_expect_success 'rebase -ix with --autosquash' ' |
| 985 | git reset --hard execute && |
| 986 | git checkout -b autosquash && |
| 987 | echo second >second.txt && |
| 988 | git add second.txt && |
| 989 | git commit -m "fixup! two_exec" && |
| 990 | echo bis >bis.txt && |
| 991 | git add bis.txt && |
| 992 | git commit -m "fixup! two_exec" && |
| 993 | git checkout -b autosquash_actual && |
| 994 | git rebase -i --exec "git show HEAD" --autosquash HEAD~4 >actual && |
| 995 | git checkout autosquash && |
| 996 | ( |
| 997 | set_fake_editor && |
| 998 | git checkout -b autosquash_expected && |
| 999 | FAKE_LINES="1 fixup 3 fixup 4 exec_git_show_HEAD 2 exec_git_show_HEAD" && |
| 1000 | export FAKE_LINES && |
| 1001 | git rebase -i HEAD~4 >expect |
| 1002 | ) && |
| 1003 | sed -e "1,13d" expect >expected && |
| 1004 | test_cmp expected actual |
| 1005 | ' |
| 1006 | |
| 1007 | test_expect_success 'rebase --exec works without -i ' ' |
| 1008 | git reset --hard execute && |
| 1009 | rm -rf exec_output && |
| 1010 | EDITOR="echo >invoked_editor" git rebase --exec "echo a line >>exec_output" HEAD~2 2>actual && |
| 1011 | test_grep "Successfully rebased and updated" actual && |
| 1012 | test_line_count = 2 exec_output && |
| 1013 | test_path_is_missing invoked_editor |
| 1014 | ' |
| 1015 | |
| 1016 | test_expect_success 'rebase -i --exec without <CMD>' ' |
| 1017 | git reset --hard execute && |
| 1018 | test_must_fail git rebase -i --exec 2>actual && |
| 1019 | test_grep "requires a value" actual && |
| 1020 | git checkout primary |
| 1021 | ' |
| 1022 | |
| 1023 | test_expect_success 'rebase -i --root re-order and drop commits' ' |
| 1024 | git checkout E && |
| 1025 | ( |
| 1026 | set_fake_editor && |
| 1027 | FAKE_LINES="3 1 2 5" git rebase -i --root |
| 1028 | ) && |
| 1029 | test E = $(git cat-file commit HEAD | sed -ne \$p) && |
| 1030 | test B = $(git cat-file commit HEAD^ | sed -ne \$p) && |
| 1031 | test A = $(git cat-file commit HEAD^^ | sed -ne \$p) && |
| 1032 | test C = $(git cat-file commit HEAD^^^ | sed -ne \$p) && |
| 1033 | test 0 = $(git cat-file commit HEAD^^^ | grep -c ^parent\ ) |
| 1034 | ' |
| 1035 | |
| 1036 | test_expect_success 'rebase -i --root retain root commit author and message' ' |
| 1037 | git checkout A && |
| 1038 | echo B >file7 && |
| 1039 | git add file7 && |
| 1040 | GIT_AUTHOR_NAME="Twerp Snog" git commit -m "different author" && |
| 1041 | ( |
| 1042 | set_fake_editor && |
| 1043 | FAKE_LINES="2" git rebase -i --root |
| 1044 | ) && |
| 1045 | git cat-file commit HEAD >output && |
| 1046 | test_grep -q "^author Twerp Snog" output && |
| 1047 | git cat-file commit HEAD >actual && |
| 1048 | test_grep -q "^different author$" actual |
| 1049 | ' |
| 1050 | |
| 1051 | test_expect_success 'rebase -i --root temporary sentinel commit' ' |
| 1052 | git checkout B && |
| 1053 | ( |
| 1054 | set_fake_editor && |
| 1055 | test_must_fail env FAKE_LINES="2" git rebase -i --root |
| 1056 | ) && |
| 1057 | git cat-file commit HEAD >actual && |
| 1058 | test_grep "^tree $EMPTY_TREE" actual && |
| 1059 | git rebase --abort |
| 1060 | ' |
| 1061 | |
| 1062 | test_expect_success 'rebase -i --root fixup root commit' ' |
| 1063 | git checkout B && |
| 1064 | ( |
| 1065 | set_fake_editor && |
| 1066 | FAKE_LINES="1 fixup 2" git rebase -i --root |
| 1067 | ) && |
| 1068 | test A = $(git cat-file commit HEAD | sed -ne \$p) && |
| 1069 | test B = $(git show HEAD:file1) && |
| 1070 | test 0 = $(git cat-file commit HEAD | grep -c ^parent\ ) |
| 1071 | ' |
| 1072 | |
| 1073 | test_expect_success 'rebase -i --root reword original root commit' ' |
| 1074 | test_when_finished "test_might_fail git rebase --abort" && |
| 1075 | git checkout -b reword-original-root-branch primary && |
| 1076 | ( |
| 1077 | set_fake_editor && |
| 1078 | FAKE_LINES="reword 1 2" FAKE_COMMIT_MESSAGE="A changed" \ |
| 1079 | git rebase -i --root |
| 1080 | ) && |
| 1081 | git show HEAD^ >actual && |
| 1082 | test_grep "A changed" actual && |
| 1083 | test -z "$(git show -s --format=%p HEAD^)" |
| 1084 | ' |
| 1085 | |
| 1086 | test_expect_success 'rebase -i --root reword new root commit' ' |
| 1087 | test_when_finished "test_might_fail git rebase --abort" && |
| 1088 | git checkout -b reword-now-root-branch primary && |
| 1089 | ( |
| 1090 | set_fake_editor && |
| 1091 | FAKE_LINES="reword 3 1" FAKE_COMMIT_MESSAGE="C changed" \ |
| 1092 | git rebase -i --root |
| 1093 | ) && |
| 1094 | git show HEAD^ >actual && |
| 1095 | test_grep "C changed" actual && |
| 1096 | test -z "$(git show -s --format=%p HEAD^)" |
| 1097 | ' |
| 1098 | |
| 1099 | test_expect_success 'rebase -i --root when root has untracked file conflict' ' |
| 1100 | test_when_finished "reset_rebase" && |
| 1101 | git checkout -b failing-root-pick A && |
| 1102 | echo x >file2 && |
| 1103 | git rm file1 && |
| 1104 | git commit -m "remove file 1 add file 2" && |
| 1105 | echo z >file1 && |
| 1106 | ( |
| 1107 | set_fake_editor && |
| 1108 | test_must_fail env FAKE_LINES="1 2" git rebase -i --root |
| 1109 | ) && |
| 1110 | rm file1 && |
| 1111 | git rebase --continue && |
| 1112 | test "$(git log -1 --format=%B)" = "remove file 1 add file 2" && |
| 1113 | test "$(git rev-list --count HEAD)" = 2 |
| 1114 | ' |
| 1115 | |
| 1116 | test_expect_success 'rebase -i --root reword root when root has untracked file conflict' ' |
| 1117 | test_when_finished "reset_rebase" && |
| 1118 | echo z>file1 && |
| 1119 | ( |
| 1120 | set_fake_editor && |
| 1121 | test_must_fail env FAKE_LINES="reword 1 2" \ |
| 1122 | FAKE_COMMIT_MESSAGE="Modified A" git rebase -i --root && |
| 1123 | rm file1 && |
| 1124 | FAKE_COMMIT_MESSAGE="Reworded A" git rebase --continue |
| 1125 | ) && |
| 1126 | test "$(git log -1 --format=%B HEAD^)" = "Reworded A" && |
| 1127 | test "$(git rev-list --count HEAD)" = 2 |
| 1128 | ' |
| 1129 | |
| 1130 | test_expect_success 'rebase --edit-todo does not work on non-interactive rebase' ' |
| 1131 | git checkout reword-original-root-branch && |
| 1132 | git reset --hard && |
| 1133 | git checkout conflict-branch && |
| 1134 | ( |
| 1135 | set_fake_editor && |
| 1136 | test_must_fail git rebase -f --apply --onto HEAD~2 HEAD~ && |
| 1137 | test_must_fail git rebase --edit-todo |
| 1138 | ) && |
| 1139 | git rebase --abort |
| 1140 | ' |
| 1141 | |
| 1142 | test_expect_success 'rebase --edit-todo can be used to modify todo' ' |
| 1143 | git reset --hard && |
| 1144 | git checkout no-conflict-branch^0 && |
| 1145 | ( |
| 1146 | set_fake_editor && |
| 1147 | FAKE_LINES="edit 1 2 3" git rebase -i HEAD~3 && |
| 1148 | FAKE_LINES="2 1" git rebase --edit-todo && |
| 1149 | git rebase --continue |
| 1150 | ) && |
| 1151 | test M = $(git cat-file commit HEAD^ | sed -ne \$p) && |
| 1152 | test L = $(git cat-file commit HEAD | sed -ne \$p) |
| 1153 | ' |
| 1154 | |
| 1155 | test_expect_success 'rebase -i produces readable reflog' ' |
| 1156 | git reset --hard && |
| 1157 | git branch -f branch-reflog-test H && |
| 1158 | git rebase -i --onto I F branch-reflog-test && |
| 1159 | cat >expect <<-\EOF && |
| 1160 | rebase (finish): returning to refs/heads/branch-reflog-test |
| 1161 | rebase (pick): H |
| 1162 | rebase (pick): G |
| 1163 | rebase (start): checkout I |
| 1164 | EOF |
| 1165 | git reflog -n4 HEAD | |
| 1166 | sed "s/[^:]*: //" >actual && |
| 1167 | test_cmp expect actual |
| 1168 | ' |
| 1169 | |
| 1170 | test_expect_success 'rebase -i respects core.commentchar' ' |
| 1171 | git reset --hard && |
| 1172 | git checkout E^0 && |
| 1173 | test_config core.commentchar "\\" && |
| 1174 | write_script remove-all-but-first.sh <<-\EOF && |
| 1175 | sed -e "2,\$s/^/\\\\/" "$1" >"$1.tmp" && |
| 1176 | mv "$1.tmp" "$1" |
| 1177 | EOF |
| 1178 | ( |
| 1179 | test_set_editor "$(pwd)/remove-all-but-first.sh" && |
| 1180 | git rebase -i B |
| 1181 | ) && |
| 1182 | test B = $(git cat-file commit HEAD^ | sed -ne \$p) |
| 1183 | ' |
| 1184 | |
| 1185 | test_expect_success !WITH_BREAKING_CHANGES 'rebase -i respects core.commentchar=auto' ' |
| 1186 | test_config core.commentchar auto && |
| 1187 | write_script copy-edit-script.sh <<-\EOF && |
| 1188 | cp "$1" edit-script |
| 1189 | EOF |
| 1190 | test_when_finished "git rebase --abort || :" && |
| 1191 | ( |
| 1192 | test_set_editor "$(pwd)/copy-edit-script.sh" && |
| 1193 | git rebase -i HEAD^ 2>err |
| 1194 | ) && |
| 1195 | sed -n "s/^hint: *\$//p; s/^hint: //p; s/^warning: //p" err >actual && |
| 1196 | cat >expect <<-EOF && |
| 1197 | Support for ${SQ}core.commentChar=auto${SQ} is deprecated and will be removed in Git 3.0 |
| 1198 | |
| 1199 | To use the default comment string (#) please run |
| 1200 | |
| 1201 | git config unset core.commentChar |
| 1202 | |
| 1203 | To set a custom comment string please run |
| 1204 | |
| 1205 | git config set core.commentChar <comment string> |
| 1206 | |
| 1207 | where ${SQ}<comment string>${SQ} is the string you wish to use. |
| 1208 | EOF |
| 1209 | test_cmp expect actual && |
| 1210 | test -z "$(grep -ve "^#" -e "^\$" -e "^pick" edit-script)" |
| 1211 | ' |
| 1212 | |
| 1213 | test_expect_success 'rebase -i, with <onto> and <upstream> specified as :/quuxery' ' |
| 1214 | test_when_finished "git branch -D torebase" && |
| 1215 | git checkout -b torebase branch1 && |
| 1216 | upstream=$(git rev-parse ":/J") && |
| 1217 | onto=$(git rev-parse ":/A") && |
| 1218 | git rebase --onto $onto $upstream && |
| 1219 | git reset --hard branch1 && |
| 1220 | git rebase --onto ":/A" ":/J" && |
| 1221 | git checkout branch1 |
| 1222 | ' |
| 1223 | |
| 1224 | test_expect_success 'rebase -i with --strategy and -X' ' |
| 1225 | git checkout -b conflict-merge-use-theirs conflict-branch && |
| 1226 | git reset --hard HEAD^ && |
| 1227 | echo five >conflict && |
| 1228 | echo Z >file1 && |
| 1229 | git commit -a -m "one file conflict" && |
| 1230 | EDITOR=true git rebase -i --strategy=recursive -Xours conflict-branch && |
| 1231 | test $(git show conflict-branch:conflict) = $(cat conflict) && |
| 1232 | test $(cat file1) = Z |
| 1233 | ' |
| 1234 | |
| 1235 | test_expect_success 'interrupted rebase -i with --strategy and -X' ' |
| 1236 | git checkout -b conflict-merge-use-theirs-interrupted conflict-branch && |
| 1237 | git reset --hard HEAD^ && |
| 1238 | >breakpoint && |
| 1239 | git add breakpoint && |
| 1240 | git commit -m "breakpoint for interactive mode" && |
| 1241 | echo five >conflict && |
| 1242 | echo Z >file1 && |
| 1243 | git commit -a -m "one file conflict" && |
| 1244 | ( |
| 1245 | set_fake_editor && |
| 1246 | FAKE_LINES="edit 1 2" git rebase -i --strategy=recursive \ |
| 1247 | -Xours conflict-branch |
| 1248 | ) && |
| 1249 | git rebase --continue && |
| 1250 | test $(git show conflict-branch:conflict) = $(cat conflict) && |
| 1251 | test $(cat file1) = Z |
| 1252 | ' |
| 1253 | |
| 1254 | test_expect_success 'failing pick with --strategy is rescheduled' ' |
| 1255 | test_when_finished "rm -rf bin; test_might_fail git rebase --abort" && |
| 1256 | mkdir bin && |
| 1257 | echo exit 2 | write_script bin/git-merge-fail && |
| 1258 | git log -1 --format="pick %H # %s" HEAD >expect && |
| 1259 | test_must_fail env PATH="$PWD/bin:$PATH" \ |
| 1260 | git rebase --no-ff --strategy fail HEAD^ && |
| 1261 | test_cmp expect .git/rebase-merge/git-rebase-todo && |
| 1262 | test_cmp expect .git/rebase-merge/done |
| 1263 | ' |
| 1264 | |
| 1265 | test_expect_success 'rebase -i error on commits with \ in message' ' |
| 1266 | current_head=$(git rev-parse HEAD) && |
| 1267 | test_when_finished "git rebase --abort; git reset --hard $current_head; rm -f error" && |
| 1268 | test_commit TO-REMOVE will-conflict old-content && |
| 1269 | test_commit "\temp" will-conflict new-content dummy && |
| 1270 | test_must_fail env EDITOR=true git rebase -i HEAD^ --onto HEAD^^ 2>error && |
| 1271 | test_expect_code 1 grep " emp" error |
| 1272 | ' |
| 1273 | |
| 1274 | test_expect_success 'short commit ID setup' ' |
| 1275 | test_when_finished "git checkout primary" && |
| 1276 | git checkout --orphan collide && |
| 1277 | git rm -rf . && |
| 1278 | ( |
| 1279 | unset test_tick && |
| 1280 | test_commit collide1 collide && |
| 1281 | test_commit --notick collide2 collide && |
| 1282 | test_commit --notick collide3 collide |
| 1283 | ) |
| 1284 | ' |
| 1285 | |
| 1286 | if test -n "$GIT_TEST_FIND_COLLIDER" |
| 1287 | then |
| 1288 | author="$(unset test_tick; test_tick; git var GIT_AUTHOR_IDENT)" |
| 1289 | committer="$(unset test_tick; test_tick; git var GIT_COMMITTER_IDENT)" |
| 1290 | blob="$(git rev-parse collide2:collide)" |
| 1291 | from="$(git rev-parse collide1^0)" |
| 1292 | repl="commit refs/heads/collider-&\\n" |
| 1293 | repl="${repl}author $author\\ncommitter $committer\\n" |
| 1294 | repl="${repl}data <<EOF\\ncollide2 &\\nEOF\\n" |
| 1295 | repl="${repl}from $from\\nM 100644 $blob collide\\n" |
| 1296 | test_seq 1 32768 | sed "s|.*|$repl|" >script && |
| 1297 | git fast-import <script && |
| 1298 | git pack-refs && |
| 1299 | git for-each-ref >refs && |
| 1300 | grep "^$(test_oid t3404_collision)" <refs >matches && |
| 1301 | cat matches && |
| 1302 | test_line_count -gt 2 matches || { |
| 1303 | echo "Could not find a collider" >&2 |
| 1304 | exit 1 |
| 1305 | } |
| 1306 | fi |
| 1307 | |
| 1308 | test_expect_success 'short commit ID collide' ' |
| 1309 | test_oid_cache <<-EOF && |
| 1310 | # collision-related constants |
| 1311 | t3404_collision sha1:6bcd |
| 1312 | t3404_collision sha256:0161 |
| 1313 | t3404_collider sha1:ac4f2ee |
| 1314 | t3404_collider sha256:16697 |
| 1315 | EOF |
| 1316 | test_when_finished "reset_rebase && git checkout primary" && |
| 1317 | git checkout collide && |
| 1318 | colliding_id=$(test_oid t3404_collision) && |
| 1319 | hexsz=$(test_oid hexsz) && |
| 1320 | test $colliding_id = "$(git rev-parse HEAD | cut -c 1-4)" && |
| 1321 | test_config core.abbrev 4 && |
| 1322 | ( |
| 1323 | unset test_tick && |
| 1324 | test_tick && |
| 1325 | set_fake_editor && |
| 1326 | FAKE_COMMIT_MESSAGE="collide2 $(test_oid t3404_collider)" \ |
| 1327 | FAKE_LINES="reword 1 break 2" git rebase -i HEAD~2 && |
| 1328 | test $colliding_id = "$(git rev-parse HEAD | cut -c 1-4)" && |
| 1329 | test_grep "^pick $colliding_id " \ |
| 1330 | .git/rebase-merge/git-rebase-todo.tmp && |
| 1331 | test_grep -E "^pick [0-9a-f]{$hexsz}" \ |
| 1332 | .git/rebase-merge/git-rebase-todo && |
| 1333 | test_grep -E "^pick [0-9a-f]{$hexsz}" \ |
| 1334 | .git/rebase-merge/git-rebase-todo.backup && |
| 1335 | git rebase --continue |
| 1336 | ) && |
| 1337 | collide2="$(git rev-parse HEAD~1 | cut -c 1-4)" && |
| 1338 | collide3="$(git rev-parse collide3 | cut -c 1-4)" && |
| 1339 | test "$collide2" = "$collide3" |
| 1340 | ' |
| 1341 | |
| 1342 | test_expect_success 'respect core.abbrev' ' |
| 1343 | git config core.abbrev 12 && |
| 1344 | ( |
| 1345 | set_cat_todo_editor && |
| 1346 | test_must_fail git rebase -i HEAD~4 >todo-list |
| 1347 | ) && |
| 1348 | test 4 = $(grep -c -E "pick [0-9a-f]{12,}" todo-list) |
| 1349 | ' |
| 1350 | |
| 1351 | test_expect_success 'todo count' ' |
| 1352 | write_script dump-raw.sh <<-\EOF && |
| 1353 | cat "$1" |
| 1354 | EOF |
| 1355 | ( |
| 1356 | test_set_editor "$(pwd)/dump-raw.sh" && |
| 1357 | git rebase -i HEAD~4 >actual |
| 1358 | ) && |
| 1359 | test_grep "^# Rebase ..* onto ..* ([0-9]" actual |
| 1360 | ' |
| 1361 | |
| 1362 | test_expect_success 'rebase -i commits that overwrite untracked files (pick)' ' |
| 1363 | git checkout --force A && |
| 1364 | git clean -f && |
| 1365 | cat >todo <<-EOF && |
| 1366 | exec >file2 |
| 1367 | pick $(git rev-parse B) B |
| 1368 | pick $(git rev-parse C) C |
| 1369 | pick $(git rev-parse D) D |
| 1370 | exec cat .git/rebase-merge/done >actual |
| 1371 | EOF |
| 1372 | ( |
| 1373 | set_replace_editor todo && |
| 1374 | test_must_fail git rebase -i A |
| 1375 | ) && |
| 1376 | test_cmp_rev HEAD B && |
| 1377 | test_cmp_rev REBASE_HEAD C && |
| 1378 | head -n3 todo >expect && |
| 1379 | test_cmp expect .git/rebase-merge/done && |
| 1380 | rm file2 && |
| 1381 | test_path_is_missing .git/rebase-merge/patch && |
| 1382 | echo changed >file1 && |
| 1383 | git add file1 && |
| 1384 | test_must_fail git rebase --continue 2>err && |
| 1385 | test_grep "error: you have staged changes in your working tree" err && |
| 1386 | git reset --hard HEAD && |
| 1387 | git rebase --continue && |
| 1388 | test_cmp_rev HEAD D && |
| 1389 | tail -n3 todo >>expect && |
| 1390 | test_cmp expect actual |
| 1391 | ' |
| 1392 | |
| 1393 | test_expect_success 'rebase -i commits that overwrite untracked files (squash)' ' |
| 1394 | git checkout --force branch2 && |
| 1395 | git clean -f && |
| 1396 | git tag original-branch2 && |
| 1397 | ( |
| 1398 | set_fake_editor && |
| 1399 | FAKE_LINES="edit 1 squash 2" git rebase -i A |
| 1400 | ) && |
| 1401 | test_cmp_rev HEAD F && |
| 1402 | test_path_is_missing file6 && |
| 1403 | >file6 && |
| 1404 | test_must_fail git rebase --continue && |
| 1405 | test_cmp_rev HEAD F && |
| 1406 | test_cmp_rev REBASE_HEAD I && |
| 1407 | rm file6 && |
| 1408 | test_path_is_missing .git/rebase-merge/patch && |
| 1409 | echo changed >file1 && |
| 1410 | git add file1 && |
| 1411 | test_must_fail git rebase --continue 2>err && |
| 1412 | test_grep "error: you have staged changes in your working tree" err && |
| 1413 | git reset --hard HEAD && |
| 1414 | git rebase --continue && |
| 1415 | test $(git cat-file commit HEAD | sed -ne \$p) = I && |
| 1416 | git reset --hard original-branch2 |
| 1417 | ' |
| 1418 | |
| 1419 | test_expect_success 'rebase -i commits that overwrite untracked files (no ff)' ' |
| 1420 | git checkout --force branch2 && |
| 1421 | git clean -f && |
| 1422 | ( |
| 1423 | set_fake_editor && |
| 1424 | FAKE_LINES="edit 1 2" git rebase -i --no-ff A |
| 1425 | ) && |
| 1426 | test $(git cat-file commit HEAD | sed -ne \$p) = F && |
| 1427 | test_path_is_missing file6 && |
| 1428 | >file6 && |
| 1429 | test_must_fail git rebase --continue && |
| 1430 | test $(git cat-file commit HEAD | sed -ne \$p) = F && |
| 1431 | test_cmp_rev REBASE_HEAD I && |
| 1432 | rm file6 && |
| 1433 | test_path_is_missing .git/rebase-merge/patch && |
| 1434 | echo changed >file1 && |
| 1435 | git add file1 && |
| 1436 | test_must_fail git rebase --continue 2>err && |
| 1437 | test_grep "error: you have staged changes in your working tree" err && |
| 1438 | git reset --hard HEAD && |
| 1439 | git rebase --continue && |
| 1440 | test $(git cat-file commit HEAD | sed -ne \$p) = I |
| 1441 | ' |
| 1442 | |
| 1443 | test_expect_success 'rebase --continue removes CHERRY_PICK_HEAD' ' |
| 1444 | git checkout -b commit-to-skip && |
| 1445 | for double in X 3 1 |
| 1446 | do |
| 1447 | test_seq 5 | sed "s/$double/&&/" >seq && |
| 1448 | git add seq && |
| 1449 | test_tick && |
| 1450 | git commit -m seq-$double || return 1 |
| 1451 | done && |
| 1452 | git tag seq-onto && |
| 1453 | git reset --hard HEAD~2 && |
| 1454 | git cherry-pick seq-onto && |
| 1455 | ( |
| 1456 | set_fake_editor && |
| 1457 | test_must_fail env FAKE_LINES= git rebase -i seq-onto |
| 1458 | ) && |
| 1459 | test -d .git/rebase-merge && |
| 1460 | git rebase --continue && |
| 1461 | git diff --exit-code seq-onto && |
| 1462 | test ! -d .git/rebase-merge && |
| 1463 | test ! -f .git/CHERRY_PICK_HEAD |
| 1464 | ' |
| 1465 | |
| 1466 | rebase_setup_and_clean () { |
| 1467 | test_when_finished " |
| 1468 | git checkout primary && |
| 1469 | test_might_fail git branch -D $1 && |
| 1470 | test_might_fail git rebase --abort |
| 1471 | " && |
| 1472 | git checkout -b $1 ${2:-primary} |
| 1473 | } |
| 1474 | |
| 1475 | test_expect_success 'drop' ' |
| 1476 | rebase_setup_and_clean drop-test && |
| 1477 | ( |
| 1478 | set_fake_editor && |
| 1479 | FAKE_LINES="1 drop 2 3 d 4 5" git rebase -i --root |
| 1480 | ) && |
| 1481 | test E = $(git cat-file commit HEAD | sed -ne \$p) && |
| 1482 | test C = $(git cat-file commit HEAD^ | sed -ne \$p) && |
| 1483 | test A = $(git cat-file commit HEAD^^ | sed -ne \$p) |
| 1484 | ' |
| 1485 | |
| 1486 | test_expect_success 'rebase -i respects rebase.missingCommitsCheck = ignore' ' |
| 1487 | test_config rebase.missingCommitsCheck ignore && |
| 1488 | rebase_setup_and_clean missing-commit && |
| 1489 | ( |
| 1490 | set_fake_editor && |
| 1491 | FAKE_LINES="1 2 3 4" git rebase -i --root 2>actual |
| 1492 | ) && |
| 1493 | test D = $(git cat-file commit HEAD | sed -ne \$p) && |
| 1494 | test_grep \ |
| 1495 | "Successfully rebased and updated refs/heads/missing-commit" \ |
| 1496 | actual |
| 1497 | ' |
| 1498 | |
| 1499 | test_expect_success 'rebase -i respects rebase.missingCommitsCheck = warn' ' |
| 1500 | cat >expect <<-EOF && |
| 1501 | Warning: some commits may have been dropped accidentally. |
| 1502 | Dropped commits (newer to older): |
| 1503 | - $(git log --format="%h # %s" -1 primary) |
| 1504 | To avoid this message, use "drop" to explicitly remove a commit. |
| 1505 | EOF |
| 1506 | test_config rebase.missingCommitsCheck warn && |
| 1507 | rebase_setup_and_clean missing-commit && |
| 1508 | ( |
| 1509 | set_fake_editor && |
| 1510 | FAKE_LINES="1 2 3 4" git rebase -i --root 2>actual.2 |
| 1511 | ) && |
| 1512 | head -n4 actual.2 >actual && |
| 1513 | test_cmp expect actual && |
| 1514 | test D = $(git cat-file commit HEAD | sed -ne \$p) |
| 1515 | ' |
| 1516 | |
| 1517 | test_expect_success 'rebase -i respects rebase.missingCommitsCheck = error' ' |
| 1518 | cat >expect <<-EOF && |
| 1519 | Warning: some commits may have been dropped accidentally. |
| 1520 | Dropped commits (newer to older): |
| 1521 | - $(git log --format="%h # %s" -1 primary) |
| 1522 | - $(git log --format="%h # %s" -1 primary~2) |
| 1523 | To avoid this message, use "drop" to explicitly remove a commit. |
| 1524 | |
| 1525 | Use '\''git config rebase.missingCommitsCheck'\'' to change the level of warnings. |
| 1526 | The possible behaviours are: ignore, warn, error. |
| 1527 | |
| 1528 | You can fix this with '\''git rebase --edit-todo'\'' and then run '\''git rebase --continue'\''. |
| 1529 | Or you can abort the rebase with '\''git rebase --abort'\''. |
| 1530 | EOF |
| 1531 | test_config rebase.missingCommitsCheck error && |
| 1532 | rebase_setup_and_clean missing-commit && |
| 1533 | ( |
| 1534 | set_fake_editor && |
| 1535 | test_must_fail env FAKE_LINES="1 2 4" \ |
| 1536 | git rebase -i --root 2>actual && |
| 1537 | test_cmp expect actual && |
| 1538 | cp .git/rebase-merge/git-rebase-todo.backup \ |
| 1539 | .git/rebase-merge/git-rebase-todo && |
| 1540 | FAKE_LINES="1 2 drop 3 4 drop 5" git rebase --edit-todo |
| 1541 | ) && |
| 1542 | git rebase --continue && |
| 1543 | test D = $(git cat-file commit HEAD | sed -ne \$p) && |
| 1544 | test B = $(git cat-file commit HEAD^ | sed -ne \$p) |
| 1545 | ' |
| 1546 | |
| 1547 | test_expect_success 'rebase --edit-todo respects rebase.missingCommitsCheck = ignore' ' |
| 1548 | test_config rebase.missingCommitsCheck ignore && |
| 1549 | rebase_setup_and_clean missing-commit && |
| 1550 | ( |
| 1551 | set_fake_editor && |
| 1552 | FAKE_LINES="break 1 2 3 4 5" git rebase -i --root && |
| 1553 | FAKE_LINES="1 2 3 4" git rebase --edit-todo && |
| 1554 | git rebase --continue 2>actual |
| 1555 | ) && |
| 1556 | test D = $(git cat-file commit HEAD | sed -ne \$p) && |
| 1557 | test_grep \ |
| 1558 | "Successfully rebased and updated refs/heads/missing-commit" \ |
| 1559 | actual |
| 1560 | ' |
| 1561 | |
| 1562 | test_expect_success 'rebase --edit-todo respects rebase.missingCommitsCheck = warn' ' |
| 1563 | cat >expect <<-EOF && |
| 1564 | error: invalid command '\''pickled'\'' |
| 1565 | error: invalid line 1: pickled $(git log --format="%h # %s" -1 primary~4) |
| 1566 | Warning: some commits may have been dropped accidentally. |
| 1567 | Dropped commits (newer to older): |
| 1568 | - $(git log --format="%h # %s" -1 primary) |
| 1569 | - $(git log --format="%h # %s" -1 primary~4) |
| 1570 | To avoid this message, use "drop" to explicitly remove a commit. |
| 1571 | EOF |
| 1572 | head -n5 expect >expect.2 && |
| 1573 | tail -n1 expect >>expect.2 && |
| 1574 | tail -n4 expect.2 >expect.3 && |
| 1575 | test_config rebase.missingCommitsCheck warn && |
| 1576 | rebase_setup_and_clean missing-commit && |
| 1577 | ( |
| 1578 | set_fake_editor && |
| 1579 | test_must_fail env FAKE_LINES="bad 1 2 3 4 5" \ |
| 1580 | git rebase -i --root && |
| 1581 | cp .git/rebase-merge/git-rebase-todo.backup orig && |
| 1582 | FAKE_LINES="2 3 4" git rebase --edit-todo 2>actual.2 && |
| 1583 | head -n7 actual.2 >actual && |
| 1584 | test_cmp expect actual && |
| 1585 | cp orig .git/rebase-merge/git-rebase-todo && |
| 1586 | FAKE_LINES="1 2 3 4" git rebase --edit-todo 2>actual.2 && |
| 1587 | head -n4 actual.2 >actual && |
| 1588 | test_cmp expect.3 actual && |
| 1589 | git rebase --continue 2>actual |
| 1590 | ) && |
| 1591 | test D = $(git cat-file commit HEAD | sed -ne \$p) && |
| 1592 | test_grep \ |
| 1593 | "Successfully rebased and updated refs/heads/missing-commit" \ |
| 1594 | actual |
| 1595 | ' |
| 1596 | |
| 1597 | test_expect_success 'rebase --edit-todo respects rebase.missingCommitsCheck = error' ' |
| 1598 | cat >expect <<-EOF && |
| 1599 | error: invalid command '\''pickled'\'' |
| 1600 | error: invalid line 1: pickled $(git log --format="%h # %s" -1 primary~4) |
| 1601 | Warning: some commits may have been dropped accidentally. |
| 1602 | Dropped commits (newer to older): |
| 1603 | - $(git log --format="%h # %s" -1 primary) |
| 1604 | - $(git log --format="%h # %s" -1 primary~4) |
| 1605 | To avoid this message, use "drop" to explicitly remove a commit. |
| 1606 | |
| 1607 | Use '\''git config rebase.missingCommitsCheck'\'' to change the level of warnings. |
| 1608 | The possible behaviours are: ignore, warn, error. |
| 1609 | |
| 1610 | You can fix this with '\''git rebase --edit-todo'\'' and then run '\''git rebase --continue'\''. |
| 1611 | Or you can abort the rebase with '\''git rebase --abort'\''. |
| 1612 | EOF |
| 1613 | tail -n11 expect >expect.2 && |
| 1614 | head -n3 expect.2 >expect.3 && |
| 1615 | tail -n7 expect.2 >>expect.3 && |
| 1616 | test_config rebase.missingCommitsCheck error && |
| 1617 | rebase_setup_and_clean missing-commit && |
| 1618 | ( |
| 1619 | set_fake_editor && |
| 1620 | test_must_fail env FAKE_LINES="bad 1 2 3 4 5" \ |
| 1621 | git rebase -i --root && |
| 1622 | cp .git/rebase-merge/git-rebase-todo.backup orig && |
| 1623 | test_must_fail env FAKE_LINES="2 3 4" \ |
| 1624 | git rebase --edit-todo 2>actual && |
| 1625 | test_cmp expect actual && |
| 1626 | test_must_fail git rebase --continue 2>actual && |
| 1627 | test_cmp expect.2 actual && |
| 1628 | test_must_fail git rebase --edit-todo && |
| 1629 | cp orig .git/rebase-merge/git-rebase-todo && |
| 1630 | test_must_fail env FAKE_LINES="1 2 3 4" \ |
| 1631 | git rebase --edit-todo 2>actual && |
| 1632 | test_cmp expect.3 actual && |
| 1633 | test_must_fail git rebase --continue 2>actual && |
| 1634 | test_cmp expect.3 actual && |
| 1635 | cp orig .git/rebase-merge/git-rebase-todo && |
| 1636 | FAKE_LINES="1 2 3 4 drop 5" git rebase --edit-todo && |
| 1637 | git rebase --continue 2>actual |
| 1638 | ) && |
| 1639 | test D = $(git cat-file commit HEAD | sed -ne \$p) && |
| 1640 | test_grep \ |
| 1641 | "Successfully rebased and updated refs/heads/missing-commit" \ |
| 1642 | actual |
| 1643 | ' |
| 1644 | |
| 1645 | test_expect_success 'rebase.missingCommitsCheck = error after resolving conflicts' ' |
| 1646 | test_config rebase.missingCommitsCheck error && |
| 1647 | ( |
| 1648 | set_fake_editor && |
| 1649 | FAKE_LINES="drop 1 break 2 3 4" git rebase -i A E |
| 1650 | ) && |
| 1651 | git rebase --edit-todo && |
| 1652 | test_must_fail git rebase --continue && |
| 1653 | echo x >file1 && |
| 1654 | git add file1 && |
| 1655 | git rebase --continue |
| 1656 | ' |
| 1657 | |
| 1658 | test_expect_success 'rebase.missingCommitsCheck = error when editing for a second time' ' |
| 1659 | test_config rebase.missingCommitsCheck error && |
| 1660 | ( |
| 1661 | set_fake_editor && |
| 1662 | FAKE_LINES="1 break 2 3" git rebase -i A D && |
| 1663 | cp .git/rebase-merge/git-rebase-todo todo && |
| 1664 | test_must_fail env FAKE_LINES=2 git rebase --edit-todo && |
| 1665 | GIT_SEQUENCE_EDITOR="cp todo" git rebase --edit-todo && |
| 1666 | git rebase --continue |
| 1667 | ) |
| 1668 | ' |
| 1669 | |
| 1670 | test_expect_success 'respects rebase.abbreviateCommands with fixup, squash and exec' ' |
| 1671 | rebase_setup_and_clean abbrevcmd && |
| 1672 | test_commit "first" file1.txt "first line" first && |
| 1673 | test_commit "second" file1.txt "another line" second && |
| 1674 | test_commit "fixup! first" file2.txt "first line again" first_fixup && |
| 1675 | test_commit "squash! second" file1.txt "another line here" second_squash && |
| 1676 | cat >expected <<-EOF && |
| 1677 | p $(git rev-list --abbrev-commit -1 first) # first |
| 1678 | f $(git rev-list --abbrev-commit -1 first_fixup) # fixup! first |
| 1679 | x git show HEAD |
| 1680 | p $(git rev-list --abbrev-commit -1 second) # second |
| 1681 | s $(git rev-list --abbrev-commit -1 second_squash) # squash! second |
| 1682 | x git show HEAD |
| 1683 | EOF |
| 1684 | git checkout abbrevcmd && |
| 1685 | test_config rebase.abbreviateCommands true && |
| 1686 | ( |
| 1687 | set_cat_todo_editor && |
| 1688 | test_must_fail git rebase -i --exec "git show HEAD" \ |
| 1689 | --autosquash primary >actual |
| 1690 | ) && |
| 1691 | test_cmp expected actual |
| 1692 | ' |
| 1693 | |
| 1694 | test_expect_success 'static check of bad command' ' |
| 1695 | rebase_setup_and_clean bad-cmd && |
| 1696 | ( |
| 1697 | set_fake_editor && |
| 1698 | test_must_fail env FAKE_LINES="1 2 3 bad 4 5" \ |
| 1699 | git rebase -i --root 2>actual && |
| 1700 | test_grep "pickled $(git log --format="%h # %s" -1 primary~1)" \ |
| 1701 | actual && |
| 1702 | test_grep "You can fix this with .git rebase --edit-todo.." \ |
| 1703 | actual && |
| 1704 | FAKE_LINES="1 2 3 drop 4 5" git rebase --edit-todo |
| 1705 | ) && |
| 1706 | git rebase --continue && |
| 1707 | test E = $(git cat-file commit HEAD | sed -ne \$p) && |
| 1708 | test C = $(git cat-file commit HEAD^ | sed -ne \$p) |
| 1709 | ' |
| 1710 | |
| 1711 | test_expect_success 'the first command cannot be a fixup' ' |
| 1712 | rebase_setup_and_clean fixup-first && |
| 1713 | |
| 1714 | cat >orig <<-EOF && |
| 1715 | fixup $(git log -1 --format="%h %s" B) |
| 1716 | pick $(git log -1 --format="%h %s" C) |
| 1717 | EOF |
| 1718 | |
| 1719 | ( |
| 1720 | set_replace_editor orig && |
| 1721 | test_must_fail git rebase -i A 2>actual |
| 1722 | ) && |
| 1723 | test_grep "cannot .fixup. without a previous commit" actual && |
| 1724 | test_grep "You can fix this with .git rebase --edit-todo.." actual && |
| 1725 | # verify that the todo list has not been truncated |
| 1726 | grep -v "^#" .git/rebase-merge/git-rebase-todo >actual && |
| 1727 | test_cmp orig actual && |
| 1728 | |
| 1729 | test_must_fail git rebase --edit-todo 2>actual && |
| 1730 | test_grep "cannot .fixup. without a previous commit" actual && |
| 1731 | test_grep "You can fix this with .git rebase --edit-todo.." actual && |
| 1732 | # verify that the todo list has not been truncated |
| 1733 | grep -v "^#" .git/rebase-merge/git-rebase-todo >actual && |
| 1734 | test_cmp orig actual |
| 1735 | ' |
| 1736 | |
| 1737 | test_expect_success 'tabs and spaces are accepted in the todolist' ' |
| 1738 | rebase_setup_and_clean indented-comment && |
| 1739 | write_script add-indent.sh <<-\EOF && |
| 1740 | ( |
| 1741 | # Turn single spaces into space/tab mix |
| 1742 | sed "1s/ / /g; 2s/ / /g; 3s/ / /g" "$1" |
| 1743 | printf "\n\t# comment\n #more\n\t # comment\n" |
| 1744 | ) >"$1.new" |
| 1745 | mv "$1.new" "$1" |
| 1746 | EOF |
| 1747 | ( |
| 1748 | test_set_editor "$(pwd)/add-indent.sh" && |
| 1749 | git rebase -i HEAD^^^ |
| 1750 | ) && |
| 1751 | test E = $(git cat-file commit HEAD | sed -ne \$p) |
| 1752 | ' |
| 1753 | |
| 1754 | test_expect_success 'static check of bad SHA-1' ' |
| 1755 | rebase_setup_and_clean bad-sha && |
| 1756 | ( |
| 1757 | set_fake_editor && |
| 1758 | test_must_fail env FAKE_LINES="1 2 edit fakesha 3 4 5 #" \ |
| 1759 | git rebase -i --root 2>actual && |
| 1760 | test_grep "edit XXXXXXX False commit" actual && |
| 1761 | test_grep "You can fix this with .git rebase --edit-todo.." \ |
| 1762 | actual && |
| 1763 | FAKE_LINES="1 2 4 5 6" git rebase --edit-todo |
| 1764 | ) && |
| 1765 | git rebase --continue && |
| 1766 | test E = $(git cat-file commit HEAD | sed -ne \$p) |
| 1767 | ' |
| 1768 | |
| 1769 | test_expect_success 'editor saves as CR/LF' ' |
| 1770 | git checkout -b with-crlf && |
| 1771 | write_script add-crs.sh <<-\EOF && |
| 1772 | sed -e "s/\$/Q/" <"$1" | tr Q "\\015" >"$1".new && |
| 1773 | mv -f "$1".new "$1" |
| 1774 | EOF |
| 1775 | ( |
| 1776 | test_set_editor "$(pwd)/add-crs.sh" && |
| 1777 | git rebase -i HEAD^ |
| 1778 | ) |
| 1779 | ' |
| 1780 | |
| 1781 | test_expect_success 'rebase -i --gpg-sign=<key-id>' ' |
| 1782 | test_when_finished "test_might_fail git rebase --abort" && |
| 1783 | ( |
| 1784 | set_fake_editor && |
| 1785 | FAKE_LINES="edit 1" git rebase -i --gpg-sign="\"S I Gner\"" \ |
| 1786 | HEAD^ >out 2>err |
| 1787 | ) && |
| 1788 | test_grep "$SQ-S\"S I Gner\"$SQ" err |
| 1789 | ' |
| 1790 | |
| 1791 | test_expect_success 'rebase -i --gpg-sign=<key-id> overrides commit.gpgSign' ' |
| 1792 | test_when_finished "test_might_fail git rebase --abort" && |
| 1793 | test_config commit.gpgsign true && |
| 1794 | ( |
| 1795 | set_fake_editor && |
| 1796 | FAKE_LINES="edit 1" git rebase -i --gpg-sign="\"S I Gner\"" \ |
| 1797 | HEAD^ >out 2>err |
| 1798 | ) && |
| 1799 | test_grep "$SQ-S\"S I Gner\"$SQ" err |
| 1800 | ' |
| 1801 | |
| 1802 | test_expect_success 'valid author header after --root swap' ' |
| 1803 | rebase_setup_and_clean author-header no-conflict-branch && |
| 1804 | git commit --amend --author="Au ${SQ}thor <author@example.com>" --no-edit && |
| 1805 | git cat-file commit HEAD | grep ^author >expected && |
| 1806 | ( |
| 1807 | set_fake_editor && |
| 1808 | FAKE_LINES="5 1" git rebase -i --root |
| 1809 | ) && |
| 1810 | git cat-file commit HEAD^ | grep ^author >actual && |
| 1811 | test_cmp expected actual |
| 1812 | ' |
| 1813 | |
| 1814 | test_expect_success 'valid author header when author contains single quote' ' |
| 1815 | rebase_setup_and_clean author-header no-conflict-branch && |
| 1816 | git commit --amend --author="Au ${SQ}thor <author@example.com>" --no-edit && |
| 1817 | git cat-file commit HEAD | grep ^author >expected && |
| 1818 | ( |
| 1819 | set_fake_editor && |
| 1820 | FAKE_LINES="2" git rebase -i HEAD~2 |
| 1821 | ) && |
| 1822 | git cat-file commit HEAD | grep ^author >actual && |
| 1823 | test_cmp expected actual |
| 1824 | ' |
| 1825 | |
| 1826 | test_expect_success 'post-commit hook is called' ' |
| 1827 | >actual && |
| 1828 | test_hook post-commit <<-\EOS && |
| 1829 | git rev-parse HEAD >>actual |
| 1830 | EOS |
| 1831 | ( |
| 1832 | set_fake_editor && |
| 1833 | FAKE_LINES="edit 4 1 reword 2 fixup 3" git rebase -i A E && |
| 1834 | echo x>file3 && |
| 1835 | git add file3 && |
| 1836 | FAKE_COMMIT_MESSAGE=edited git rebase --continue |
| 1837 | ) && |
| 1838 | git rev-parse HEAD@{5} HEAD@{4} HEAD@{3} HEAD@{2} HEAD@{1} HEAD \ |
| 1839 | >expect && |
| 1840 | test_cmp expect actual |
| 1841 | ' |
| 1842 | |
| 1843 | test_expect_success 'correct error message for partial commit after empty pick' ' |
| 1844 | test_when_finished "git rebase --abort" && |
| 1845 | ( |
| 1846 | set_fake_editor && |
| 1847 | FAKE_LINES="2 1 1" && |
| 1848 | export FAKE_LINES && |
| 1849 | test_must_fail git rebase -i A D |
| 1850 | ) && |
| 1851 | echo x >file1 && |
| 1852 | test_must_fail git commit file1 2>err && |
| 1853 | test_grep "cannot do a partial commit during a rebase." err |
| 1854 | ' |
| 1855 | |
| 1856 | test_expect_success 'correct error message for commit --amend after empty pick' ' |
| 1857 | test_when_finished "git rebase --abort" && |
| 1858 | ( |
| 1859 | set_fake_editor && |
| 1860 | FAKE_LINES="1 1" && |
| 1861 | export FAKE_LINES && |
| 1862 | test_must_fail git rebase -i A D |
| 1863 | ) && |
| 1864 | echo x>file1 && |
| 1865 | test_must_fail git commit -a --amend 2>err && |
| 1866 | test_grep "middle of a rebase -- cannot amend." err |
| 1867 | ' |
| 1868 | |
| 1869 | test_expect_success 'todo has correct onto hash' ' |
| 1870 | GIT_SEQUENCE_EDITOR=cat git rebase -i no-conflict-branch~4 no-conflict-branch >actual && |
| 1871 | onto=$(git rev-parse --short HEAD~4) && |
| 1872 | test_grep "^# Rebase ..* onto $onto" actual |
| 1873 | ' |
| 1874 | |
| 1875 | test_expect_success 'ORIG_HEAD is updated correctly' ' |
| 1876 | test_when_finished "git checkout primary && git branch -D test-orig-head" && |
| 1877 | git checkout -b test-orig-head A && |
| 1878 | git commit --allow-empty -m A1 && |
| 1879 | git commit --allow-empty -m A2 && |
| 1880 | git commit --allow-empty -m A3 && |
| 1881 | git commit --allow-empty -m A4 && |
| 1882 | git rebase primary && |
| 1883 | test_cmp_rev ORIG_HEAD test-orig-head@{1} |
| 1884 | ' |
| 1885 | |
| 1886 | test_expect_success '--update-refs adds label and update-ref commands' ' |
| 1887 | git checkout -b update-refs no-conflict-branch && |
| 1888 | git branch -f base HEAD~4 && |
| 1889 | git branch -f first HEAD~3 && |
| 1890 | git branch -f second HEAD~3 && |
| 1891 | git branch -f third HEAD~1 && |
| 1892 | git commit --allow-empty --fixup=third && |
| 1893 | git branch -f is-not-reordered && |
| 1894 | git commit --allow-empty --fixup=HEAD~4 && |
| 1895 | git branch -f shared-tip && |
| 1896 | ( |
| 1897 | set_cat_todo_editor && |
| 1898 | |
| 1899 | cat >expect <<-EOF && |
| 1900 | pick $(git log -1 --format=%h J) # J |
| 1901 | fixup $(git log -1 --format=%h update-refs) # fixup! J # empty |
| 1902 | update-ref refs/heads/second |
| 1903 | update-ref refs/heads/first |
| 1904 | pick $(git log -1 --format=%h K) # K |
| 1905 | pick $(git log -1 --format=%h L) # L |
| 1906 | fixup $(git log -1 --format=%h is-not-reordered) # fixup! L # empty |
| 1907 | update-ref refs/heads/third |
| 1908 | pick $(git log -1 --format=%h M) # M |
| 1909 | update-ref refs/heads/no-conflict-branch |
| 1910 | update-ref refs/heads/is-not-reordered |
| 1911 | update-ref refs/heads/shared-tip |
| 1912 | EOF |
| 1913 | |
| 1914 | test_must_fail git rebase -i --autosquash --update-refs primary >todo && |
| 1915 | test_cmp expect todo && |
| 1916 | |
| 1917 | test_must_fail git -c rebase.autosquash=true \ |
| 1918 | -c rebase.updaterefs=true \ |
| 1919 | rebase -i primary >todo && |
| 1920 | |
| 1921 | test_cmp expect todo |
| 1922 | ) |
| 1923 | ' |
| 1924 | |
| 1925 | test_expect_success '--update-refs adds commands with --rebase-merges' ' |
| 1926 | git checkout -b update-refs-with-merge no-conflict-branch && |
| 1927 | git branch -f base HEAD~4 && |
| 1928 | git branch -f first HEAD~3 && |
| 1929 | git branch -f second HEAD~3 && |
| 1930 | git branch -f third HEAD~1 && |
| 1931 | git merge -m merge branch2 && |
| 1932 | git branch -f merge-branch && |
| 1933 | git commit --fixup=third --allow-empty && |
| 1934 | ( |
| 1935 | set_cat_todo_editor && |
| 1936 | |
| 1937 | cat >expect <<-EOF && |
| 1938 | label onto |
| 1939 | reset onto |
| 1940 | pick $(git log -1 --format=%h branch2~1) # F |
| 1941 | pick $(git log -1 --format=%h branch2) # I |
| 1942 | update-ref refs/heads/branch2 |
| 1943 | label branch2 |
| 1944 | reset onto |
| 1945 | pick $(git log -1 --format=%h refs/heads/second) # J |
| 1946 | update-ref refs/heads/second |
| 1947 | update-ref refs/heads/first |
| 1948 | pick $(git log -1 --format=%h refs/heads/third~1) # K |
| 1949 | pick $(git log -1 --format=%h refs/heads/third) # L |
| 1950 | fixup $(git log -1 --format=%h update-refs-with-merge) # fixup! L # empty |
| 1951 | update-ref refs/heads/third |
| 1952 | pick $(git log -1 --format=%h HEAD~2) # M |
| 1953 | update-ref refs/heads/no-conflict-branch |
| 1954 | merge -C $(git log -1 --format=%h HEAD~1) branch2 # merge |
| 1955 | update-ref refs/heads/merge-branch |
| 1956 | EOF |
| 1957 | |
| 1958 | test_must_fail git rebase -i --autosquash \ |
| 1959 | --rebase-merges=rebase-cousins \ |
| 1960 | --update-refs primary >todo && |
| 1961 | |
| 1962 | test_cmp expect todo && |
| 1963 | |
| 1964 | test_must_fail git -c rebase.autosquash=true \ |
| 1965 | -c rebase.updaterefs=true \ |
| 1966 | rebase -i \ |
| 1967 | --rebase-merges=rebase-cousins \ |
| 1968 | primary >todo && |
| 1969 | |
| 1970 | test_cmp expect todo |
| 1971 | ) |
| 1972 | ' |
| 1973 | |
| 1974 | test_expect_success '--update-refs ignores non-branch decorations' ' |
| 1975 | test_when_finished "git branch -D update-refs" && |
| 1976 | test_when_finished "git checkout primary" && |
| 1977 | git checkout -B update-refs no-conflict-branch && |
| 1978 | ( |
| 1979 | set_cat_todo_editor && |
| 1980 | |
| 1981 | # rebase.instructionFormat=%d loads normal log decorations before |
| 1982 | # --update-refs adds its branch placeholders so we must ignore |
| 1983 | # all non-local decorations. |
| 1984 | test_must_fail git -c rebase.instructionFormat="%s%d" \ |
| 1985 | rebase -i --update-refs HEAD^ >todo |
| 1986 | ) && |
| 1987 | grep ^update-ref todo >actual && |
| 1988 | test_write_lines "update-ref refs/heads/no-conflict-branch" >expect && |
| 1989 | test_grep ! "^# Ref refs/heads/update-refs checked out" todo && |
| 1990 | test_cmp expect actual |
| 1991 | ' |
| 1992 | |
| 1993 | test_expect_success '--update-refs updates refs correctly' ' |
| 1994 | test_when_finished " |
| 1995 | test_might_fail git symbolic-ref -d refs/heads/no-conflict-branch-alias && |
| 1996 | test_might_fail git symbolic-ref -d refs/heads/second-alias |
| 1997 | " && |
| 1998 | git checkout -B update-refs no-conflict-branch && |
| 1999 | git branch -f base HEAD~4 && |
| 2000 | git branch -f first HEAD~3 && |
| 2001 | git branch -f second HEAD~3 && |
| 2002 | git branch -f third HEAD~1 && |
| 2003 | git symbolic-ref refs/heads/no-conflict-branch-alias \ |
| 2004 | refs/heads/no-conflict-branch && |
| 2005 | git symbolic-ref refs/heads/second-alias refs/heads/second && |
| 2006 | test_commit extra2 fileX && |
| 2007 | git commit --amend --fixup=L && |
| 2008 | |
| 2009 | git rebase -i --autosquash --update-refs primary 2>err && |
| 2010 | |
| 2011 | test_cmp_rev HEAD~3 refs/heads/first && |
| 2012 | test_cmp_rev HEAD~3 refs/heads/second && |
| 2013 | test_cmp_rev HEAD~3 refs/heads/second-alias && |
| 2014 | test_cmp_rev HEAD~1 refs/heads/third && |
| 2015 | test_cmp_rev HEAD refs/heads/no-conflict-branch && |
| 2016 | test_cmp_rev HEAD refs/heads/no-conflict-branch-alias && |
| 2017 | test_write_lines refs/heads/no-conflict-branch >expect && |
| 2018 | git symbolic-ref refs/heads/no-conflict-branch-alias >actual && |
| 2019 | test_cmp expect actual && |
| 2020 | test_write_lines refs/heads/second >expect && |
| 2021 | git symbolic-ref refs/heads/second-alias >actual && |
| 2022 | test_cmp expect actual && |
| 2023 | |
| 2024 | q_to_tab >expect <<-\EOF && |
| 2025 | Successfully rebased and updated refs/heads/update-refs. |
| 2026 | Updated the following refs with --update-refs: |
| 2027 | Qrefs/heads/first |
| 2028 | Qrefs/heads/no-conflict-branch |
| 2029 | Qrefs/heads/second |
| 2030 | Qrefs/heads/third |
| 2031 | EOF |
| 2032 | |
| 2033 | # Clear "Rebasing (X/Y)" progress lines and drop leading tabs. |
| 2034 | sed "s/Rebasing.*Successfully/Successfully/g" <err >err.trimmed && |
| 2035 | test_cmp expect err.trimmed |
| 2036 | ' |
| 2037 | |
| 2038 | test_expect_success '--update-refs checks resolved non-branch symref target' ' |
| 2039 | test_when_finished " |
| 2040 | git worktree remove --force checked-out-target-wt && |
| 2041 | git symbolic-ref -d refs/heads/non-branch-alias && |
| 2042 | git tag -d checked-out-target |
| 2043 | " && |
| 2044 | git tag checked-out-target HEAD~1 && |
| 2045 | git symbolic-ref refs/heads/non-branch-alias refs/tags/checked-out-target && |
| 2046 | git worktree add --detach checked-out-target-wt checked-out-target && |
| 2047 | git -C checked-out-target-wt symbolic-ref HEAD refs/tags/checked-out-target && |
| 2048 | |
| 2049 | GIT_SEQUENCE_EDITOR="cat >todo" git rebase -i --update-refs HEAD~2 && |
| 2050 | |
| 2051 | test_grep "^# Ref refs/heads/non-branch-alias checked out at" todo && |
| 2052 | test_write_lines refs/tags/checked-out-target >expect && |
| 2053 | git symbolic-ref refs/heads/non-branch-alias >actual && |
| 2054 | test_cmp expect actual |
| 2055 | ' |
| 2056 | |
| 2057 | test_expect_success '--update-refs deduplicates non-branch symref targets' ' |
| 2058 | test_when_finished " |
| 2059 | git symbolic-ref -d refs/heads/non-branch-alias-one && |
| 2060 | git symbolic-ref -d refs/heads/non-branch-alias-two && |
| 2061 | git tag -d shared-non-branch-target |
| 2062 | " && |
| 2063 | git tag shared-non-branch-target HEAD~1 && |
| 2064 | git symbolic-ref refs/heads/non-branch-alias-one \ |
| 2065 | refs/tags/shared-non-branch-target && |
| 2066 | git symbolic-ref refs/heads/non-branch-alias-two \ |
| 2067 | refs/tags/shared-non-branch-target && |
| 2068 | |
| 2069 | GIT_SEQUENCE_EDITOR=: git rebase -i --force-rebase --update-refs HEAD~2 && |
| 2070 | |
| 2071 | test_cmp_rev HEAD~1 refs/heads/non-branch-alias-one && |
| 2072 | test_cmp_rev HEAD~1 refs/heads/non-branch-alias-two && |
| 2073 | test_write_lines refs/tags/shared-non-branch-target >expect && |
| 2074 | git symbolic-ref refs/heads/non-branch-alias-one >actual && |
| 2075 | test_cmp expect actual && |
| 2076 | git symbolic-ref refs/heads/non-branch-alias-two >actual && |
| 2077 | test_cmp expect actual |
| 2078 | ' |
| 2079 | |
| 2080 | test_expect_success '--update-refs honors non-branch symref reservations' ' |
| 2081 | test_when_finished " |
| 2082 | test_might_fail git worktree remove --force reserved-target-wt && |
| 2083 | test_might_fail git symbolic-ref -d \ |
| 2084 | refs/heads/reserved-non-branch-alias-one && |
| 2085 | test_might_fail git symbolic-ref -d \ |
| 2086 | refs/heads/reserved-non-branch-alias-two && |
| 2087 | test_might_fail git tag -d reserved-non-branch-target |
| 2088 | " && |
| 2089 | git tag reserved-non-branch-target HEAD~1 && |
| 2090 | git symbolic-ref refs/heads/reserved-non-branch-alias-one \ |
| 2091 | refs/tags/reserved-non-branch-target && |
| 2092 | git symbolic-ref refs/heads/reserved-non-branch-alias-two \ |
| 2093 | refs/tags/reserved-non-branch-target && |
| 2094 | git worktree add --detach reserved-target-wt HEAD && |
| 2095 | wt_gitdir=$(git -C reserved-target-wt rev-parse --absolute-git-dir) && |
| 2096 | mkdir -p "$wt_gitdir/rebase-merge" && |
| 2097 | old_oid=$(git rev-parse refs/heads/reserved-non-branch-alias-one) && |
| 2098 | test_write_lines refs/heads/reserved-non-branch-alias-one \ |
| 2099 | "$old_oid" "$old_oid" >"$wt_gitdir/rebase-merge/update-refs" && |
| 2100 | |
| 2101 | GIT_SEQUENCE_EDITOR="cat >todo" git rebase -i --update-refs HEAD~2 && |
| 2102 | |
| 2103 | test_grep "^# Ref refs/heads/reserved-non-branch-alias-one checked out at" \ |
| 2104 | todo && |
| 2105 | test_grep "^# Ref refs/heads/reserved-non-branch-alias-two checked out at" \ |
| 2106 | todo && |
| 2107 | test_grep ! "^update-ref refs/heads/reserved-non-branch-alias" todo |
| 2108 | ' |
| 2109 | |
| 2110 | test_expect_success 'respect user edits to update-ref steps' ' |
| 2111 | git checkout -B update-refs-break no-conflict-branch && |
| 2112 | git branch -f base HEAD~4 && |
| 2113 | git branch -f first HEAD~3 && |
| 2114 | git branch -f second HEAD~3 && |
| 2115 | git branch -f third HEAD~1 && |
| 2116 | git branch -f unseen base && |
| 2117 | |
| 2118 | # First, we will add breaks to the expected todo file |
| 2119 | cat >fake-todo-1 <<-EOF && |
| 2120 | pick $(git rev-parse HEAD~3) |
| 2121 | break |
| 2122 | update-ref refs/heads/second |
| 2123 | update-ref refs/heads/first |
| 2124 | |
| 2125 | pick $(git rev-parse HEAD~2) |
| 2126 | pick $(git rev-parse HEAD~1) |
| 2127 | update-ref refs/heads/third |
| 2128 | |
| 2129 | pick $(git rev-parse HEAD) |
| 2130 | update-ref refs/heads/no-conflict-branch |
| 2131 | EOF |
| 2132 | |
| 2133 | # Second, we will drop some update-refs commands (and move one) |
| 2134 | cat >fake-todo-2 <<-EOF && |
| 2135 | update-ref refs/heads/second |
| 2136 | |
| 2137 | pick $(git rev-parse HEAD~2) |
| 2138 | update-ref refs/heads/third |
| 2139 | pick $(git rev-parse HEAD~1) |
| 2140 | break |
| 2141 | |
| 2142 | pick $(git rev-parse HEAD) |
| 2143 | EOF |
| 2144 | |
| 2145 | # Third, we will: |
| 2146 | # * insert a new one (new-branch), |
| 2147 | # * re-add an old one (first), and |
| 2148 | # * add a second instance of a previously-stored one (second) |
| 2149 | cat >fake-todo-3 <<-EOF && |
| 2150 | update-ref refs/heads/unseen |
| 2151 | update-ref refs/heads/new-branch |
| 2152 | pick $(git rev-parse HEAD) |
| 2153 | update-ref refs/heads/first |
| 2154 | update-ref refs/heads/second |
| 2155 | EOF |
| 2156 | |
| 2157 | ( |
| 2158 | set_replace_editor fake-todo-1 && |
| 2159 | git rebase -i --update-refs primary && |
| 2160 | |
| 2161 | # These branches are currently locked. |
| 2162 | for b in first second third no-conflict-branch |
| 2163 | do |
| 2164 | test_must_fail git branch -f $b base || return 1 |
| 2165 | done && |
| 2166 | |
| 2167 | set_replace_editor fake-todo-2 && |
| 2168 | git rebase --edit-todo && |
| 2169 | |
| 2170 | # These branches are currently locked. |
| 2171 | for b in second third |
| 2172 | do |
| 2173 | test_must_fail git branch -f $b base || return 1 |
| 2174 | done && |
| 2175 | |
| 2176 | # These branches are currently unlocked for checkout. |
| 2177 | for b in first no-conflict-branch |
| 2178 | do |
| 2179 | git worktree add wt-$b $b && |
| 2180 | git worktree remove wt-$b || return 1 |
| 2181 | done && |
| 2182 | |
| 2183 | git rebase --continue && |
| 2184 | |
| 2185 | set_replace_editor fake-todo-3 && |
| 2186 | git rebase --edit-todo && |
| 2187 | |
| 2188 | # These branches are currently locked. |
| 2189 | for b in second third first unseen |
| 2190 | do |
| 2191 | test_must_fail git branch -f $b base || return 1 |
| 2192 | done && |
| 2193 | |
| 2194 | # These branches are currently unlocked for checkout. |
| 2195 | for b in no-conflict-branch |
| 2196 | do |
| 2197 | git worktree add wt-$b $b && |
| 2198 | git worktree remove wt-$b || return 1 |
| 2199 | done && |
| 2200 | |
| 2201 | git rebase --continue |
| 2202 | ) && |
| 2203 | |
| 2204 | test_cmp_rev HEAD~2 refs/heads/third && |
| 2205 | test_cmp_rev HEAD~1 refs/heads/unseen && |
| 2206 | test_cmp_rev HEAD~1 refs/heads/new-branch && |
| 2207 | test_cmp_rev HEAD refs/heads/first && |
| 2208 | test_cmp_rev HEAD refs/heads/second && |
| 2209 | test_cmp_rev HEAD refs/heads/no-conflict-branch |
| 2210 | ' |
| 2211 | |
| 2212 | test_expect_success '--update-refs: all update-ref lines removed' ' |
| 2213 | git checkout -b test-refs-not-removed no-conflict-branch && |
| 2214 | git branch -f base HEAD~4 && |
| 2215 | git branch -f first HEAD~3 && |
| 2216 | git branch -f second HEAD~3 && |
| 2217 | git branch -f third HEAD~1 && |
| 2218 | git branch -f tip && |
| 2219 | |
| 2220 | test_commit test-refs-not-removed && |
| 2221 | git commit --amend --fixup first && |
| 2222 | |
| 2223 | git rev-parse first second third tip no-conflict-branch >expect-oids && |
| 2224 | |
| 2225 | ( |
| 2226 | set_cat_todo_editor && |
| 2227 | test_must_fail git rebase -i --update-refs base >todo.raw && |
| 2228 | sed -e "/^update-ref/d" <todo.raw >todo |
| 2229 | ) && |
| 2230 | ( |
| 2231 | set_replace_editor todo && |
| 2232 | git rebase -i --update-refs base |
| 2233 | ) && |
| 2234 | |
| 2235 | # Ensure refs are not deleted and their OIDs have not changed |
| 2236 | git rev-parse first second third tip no-conflict-branch >actual-oids && |
| 2237 | test_cmp expect-oids actual-oids |
| 2238 | ' |
| 2239 | |
| 2240 | test_expect_success '--update-refs: all update-ref lines removed, then some re-added' ' |
| 2241 | git checkout -b test-refs-not-removed2 no-conflict-branch && |
| 2242 | git branch -f base HEAD~4 && |
| 2243 | git branch -f first HEAD~3 && |
| 2244 | git branch -f second HEAD~3 && |
| 2245 | git branch -f third HEAD~1 && |
| 2246 | git branch -f tip && |
| 2247 | |
| 2248 | test_commit test-refs-not-removed2 && |
| 2249 | git commit --amend --fixup first && |
| 2250 | |
| 2251 | git rev-parse first second third >expect-oids && |
| 2252 | |
| 2253 | ( |
| 2254 | set_cat_todo_editor && |
| 2255 | test_must_fail git rebase -i \ |
| 2256 | --autosquash --update-refs \ |
| 2257 | base >todo.raw && |
| 2258 | sed -e "/^update-ref/d" <todo.raw >todo |
| 2259 | ) && |
| 2260 | |
| 2261 | # Add a break to the end of the todo so we can edit later |
| 2262 | echo "break" >>todo && |
| 2263 | |
| 2264 | ( |
| 2265 | set_replace_editor todo && |
| 2266 | git rebase -i --autosquash --update-refs base && |
| 2267 | echo "update-ref refs/heads/tip" >todo && |
| 2268 | git rebase --edit-todo && |
| 2269 | git rebase --continue |
| 2270 | ) && |
| 2271 | |
| 2272 | # Ensure first/second/third are unchanged, but tip is updated |
| 2273 | git rev-parse first second third >actual-oids && |
| 2274 | test_cmp expect-oids actual-oids && |
| 2275 | test_cmp_rev HEAD tip |
| 2276 | ' |
| 2277 | |
| 2278 | test_expect_success '--update-refs: --edit-todo with no update-ref lines' ' |
| 2279 | git checkout -b test-refs-not-removed3 no-conflict-branch && |
| 2280 | git branch -f base HEAD~4 && |
| 2281 | git branch -f first HEAD~3 && |
| 2282 | git branch -f second HEAD~3 && |
| 2283 | git branch -f third HEAD~1 && |
| 2284 | git branch -f tip && |
| 2285 | |
| 2286 | test_commit test-refs-not-removed3 && |
| 2287 | git commit --amend --fixup first && |
| 2288 | |
| 2289 | git rev-parse first second third tip no-conflict-branch >expect-oids && |
| 2290 | |
| 2291 | ( |
| 2292 | set_cat_todo_editor && |
| 2293 | test_must_fail git rebase -i \ |
| 2294 | --autosquash --update-refs \ |
| 2295 | base >todo.raw && |
| 2296 | sed -e "/^update-ref/d" <todo.raw >todo |
| 2297 | ) && |
| 2298 | |
| 2299 | # Add a break to the beginning of the todo so we can resume with no |
| 2300 | # update-ref lines |
| 2301 | echo "break" >todo.new && |
| 2302 | cat todo >>todo.new && |
| 2303 | |
| 2304 | ( |
| 2305 | set_replace_editor todo.new && |
| 2306 | git rebase -i --autosquash --update-refs base && |
| 2307 | |
| 2308 | # Make no changes when editing so update-refs is still empty |
| 2309 | cat todo >todo.new && |
| 2310 | git rebase --edit-todo && |
| 2311 | git rebase --continue |
| 2312 | ) && |
| 2313 | |
| 2314 | # Ensure refs are not deleted and their OIDs have not changed |
| 2315 | git rev-parse first second third tip no-conflict-branch >actual-oids && |
| 2316 | test_cmp expect-oids actual-oids |
| 2317 | ' |
| 2318 | |
| 2319 | test_expect_success '--update-refs: check failed ref update' ' |
| 2320 | test_when_finished "test_might_fail git rebase --abort" && |
| 2321 | git checkout -B update-refs-error no-conflict-branch && |
| 2322 | git branch -f base HEAD~4 && |
| 2323 | git branch -f first HEAD~3 && |
| 2324 | git branch -f second HEAD~2 && |
| 2325 | git branch -f third HEAD~1 && |
| 2326 | |
| 2327 | cat >fake-todo <<-EOF && |
| 2328 | pick $(git rev-parse HEAD~3) |
| 2329 | break |
| 2330 | update-ref refs/heads/first |
| 2331 | |
| 2332 | pick $(git rev-parse HEAD~2) |
| 2333 | update-ref refs/heads/second |
| 2334 | |
| 2335 | pick $(git rev-parse HEAD~1) |
| 2336 | update-ref refs/heads/third |
| 2337 | |
| 2338 | pick $(git rev-parse HEAD) |
| 2339 | update-ref refs/heads/no-conflict-branch |
| 2340 | EOF |
| 2341 | |
| 2342 | ( |
| 2343 | set_replace_editor fake-todo && |
| 2344 | git rebase -i --update-refs base |
| 2345 | ) && |
| 2346 | |
| 2347 | # At this point, the values of first, second, and third are |
| 2348 | # recorded in the update-refs file. We will force-update the |
| 2349 | # "second" ref, but "git branch -f" will not work because of |
| 2350 | # the lock in the update-refs file. |
| 2351 | git update-ref refs/heads/second third && |
| 2352 | |
| 2353 | test_must_fail git rebase --continue 2>err && |
| 2354 | test_grep "update_ref failed for ref '\''refs/heads/second'\''" err && |
| 2355 | |
| 2356 | q_to_tab >expect <<-\EOF && |
| 2357 | Updated the following refs with --update-refs: |
| 2358 | Qrefs/heads/first |
| 2359 | Qrefs/heads/no-conflict-branch |
| 2360 | Qrefs/heads/third |
| 2361 | Failed to update the following refs with --update-refs: |
| 2362 | Qrefs/heads/second |
| 2363 | EOF |
| 2364 | |
| 2365 | # Clear "Rebasing (X/Y)" progress lines and drop leading tabs. |
| 2366 | tail -n 6 err >err.last && |
| 2367 | sed "s/Rebasing.*Successfully/Successfully/g" <err.last >err.trimmed && |
| 2368 | test_cmp expect err.trimmed |
| 2369 | ' |
| 2370 | |
| 2371 | test_expect_success 'bad labels and refs rejected when parsing todo list' ' |
| 2372 | test_when_finished "test_might_fail git rebase --abort" && |
| 2373 | cat >todo <<-\EOF && |
| 2374 | exec >execed |
| 2375 | label # |
| 2376 | label :invalid |
| 2377 | update-ref :bad |
| 2378 | update-ref topic |
| 2379 | EOF |
| 2380 | rm -f execed && |
| 2381 | ( |
| 2382 | set_replace_editor todo && |
| 2383 | test_must_fail git rebase -i HEAD 2>err |
| 2384 | ) && |
| 2385 | test_grep "'\''#'\'' is not a valid label" err && |
| 2386 | test_grep "'\'':invalid'\'' is not a valid label" err && |
| 2387 | test_grep "'\'':bad'\'' is not a valid refname" err && |
| 2388 | test_grep "update-ref requires a fully qualified refname e.g. refs/heads/topic" \ |
| 2389 | err && |
| 2390 | test_path_is_missing execed |
| 2391 | ' |
| 2392 | |
| 2393 | test_expect_success 'non-merge commands reject merge commits' ' |
| 2394 | test_when_finished "test_might_fail git rebase --abort" && |
| 2395 | git checkout E && |
| 2396 | git merge I && |
| 2397 | oid=$(git rev-parse HEAD) && |
| 2398 | cat >todo <<-EOF && |
| 2399 | pick $oid |
| 2400 | reword $oid |
| 2401 | edit $oid |
| 2402 | fixup $oid |
| 2403 | squash $oid |
| 2404 | drop $oid # acceptable, no advice |
| 2405 | EOF |
| 2406 | ( |
| 2407 | set_replace_editor todo && |
| 2408 | test_must_fail git rebase -i HEAD 2>actual |
| 2409 | ) && |
| 2410 | cat >expect <<-EOF && |
| 2411 | error: ${SQ}pick${SQ} does not accept merge commits |
| 2412 | hint: ${SQ}pick${SQ} does not take a merge commit. If you wanted to |
| 2413 | hint: replay the merge, use ${SQ}merge -C${SQ} on the commit. |
| 2414 | hint: Disable this message with "git config set advice.rebaseTodoError false" |
| 2415 | error: invalid line 1: pick $oid |
| 2416 | error: ${SQ}reword${SQ} does not accept merge commits |
| 2417 | hint: ${SQ}reword${SQ} does not take a merge commit. If you wanted to |
| 2418 | hint: replay the merge and reword the commit message, use |
| 2419 | hint: ${SQ}merge -c${SQ} on the commit |
| 2420 | hint: Disable this message with "git config set advice.rebaseTodoError false" |
| 2421 | error: invalid line 2: reword $oid |
| 2422 | error: ${SQ}edit${SQ} does not accept merge commits |
| 2423 | hint: ${SQ}edit${SQ} does not take a merge commit. If you wanted to |
| 2424 | hint: replay the merge, use ${SQ}merge -C${SQ} on the commit, and then |
| 2425 | hint: ${SQ}break${SQ} to give the control back to you so that you can |
| 2426 | hint: do ${SQ}git commit --amend && git rebase --continue${SQ}. |
| 2427 | hint: Disable this message with "git config set advice.rebaseTodoError false" |
| 2428 | error: invalid line 3: edit $oid |
| 2429 | error: cannot squash merge commit into another commit |
| 2430 | error: invalid line 4: fixup $oid |
| 2431 | error: cannot squash merge commit into another commit |
| 2432 | error: invalid line 5: squash $oid |
| 2433 | You can fix this with ${SQ}git rebase --edit-todo${SQ} and then run ${SQ}git rebase --continue${SQ}. |
| 2434 | Or you can abort the rebase with ${SQ}git rebase --abort${SQ}. |
| 2435 | EOF |
| 2436 | test_cmp expect actual |
| 2437 | ' |
| 2438 | |
| 2439 | # This must be the last test in this file |
| 2440 | test_expect_success '$EDITOR and friends are unchanged' ' |
| 2441 | test_editor_unchanged |
| 2442 | ' |
| 2443 | |
| 2444 | test_done |