| 1 | #!/bin/sh |
| 2 | |
| 3 | test_description='auto squash' |
| 4 | |
| 5 | GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main |
| 6 | export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME |
| 7 | |
| 8 | . ./test-lib.sh |
| 9 | |
| 10 | . "$TEST_DIRECTORY"/lib-rebase.sh |
| 11 | |
| 12 | test_expect_success setup ' |
| 13 | echo 0 >file0 && |
| 14 | git add . && |
| 15 | test_tick && |
| 16 | git commit -m "initial commit" && |
| 17 | echo 0 >file1 && |
| 18 | echo 2 >file2 && |
| 19 | git add . && |
| 20 | test_tick && |
| 21 | git commit -m "first commit" && |
| 22 | git tag first-commit && |
| 23 | echo 3 >file3 && |
| 24 | git add . && |
| 25 | test_tick && |
| 26 | git commit -m "second commit" && |
| 27 | git tag base |
| 28 | ' |
| 29 | |
| 30 | test_auto_fixup () { |
| 31 | no_squash= && |
| 32 | if test "x$1" = 'x!' |
| 33 | then |
| 34 | no_squash=true |
| 35 | shift |
| 36 | fi && |
| 37 | |
| 38 | git reset --hard base && |
| 39 | echo 1 >file1 && |
| 40 | git add -u && |
| 41 | test_tick && |
| 42 | git commit -m "fixup! first" && |
| 43 | |
| 44 | git tag $1 && |
| 45 | test_tick && |
| 46 | git rebase $2 HEAD^^^ && |
| 47 | git log --oneline >actual && |
| 48 | if test -n "$no_squash" |
| 49 | then |
| 50 | test_line_count = 4 actual |
| 51 | else |
| 52 | test_line_count = 3 actual && |
| 53 | git diff --exit-code $1 && |
| 54 | echo 1 >expect && |
| 55 | git cat-file blob HEAD^:file1 >actual && |
| 56 | test_cmp expect actual && |
| 57 | git cat-file commit HEAD^ >commit && |
| 58 | grep first commit >actual && |
| 59 | test_line_count = 1 actual |
| 60 | fi |
| 61 | } |
| 62 | |
| 63 | test_expect_success 'auto fixup (option)' ' |
| 64 | test_auto_fixup fixup-option --autosquash && |
| 65 | test_auto_fixup fixup-option-i "--autosquash -i" |
| 66 | ' |
| 67 | |
| 68 | test_expect_success 'auto fixup (config true)' ' |
| 69 | git config rebase.autosquash true && |
| 70 | test_auto_fixup ! fixup-config-true && |
| 71 | test_auto_fixup fixup-config-true-i -i && |
| 72 | test_auto_fixup ! fixup-config-true-no --no-autosquash && |
| 73 | test_auto_fixup ! fixup-config-true-i-no "-i --no-autosquash" |
| 74 | ' |
| 75 | |
| 76 | test_expect_success 'auto fixup (config false)' ' |
| 77 | git config rebase.autosquash false && |
| 78 | test_auto_fixup ! fixup-config-false && |
| 79 | test_auto_fixup ! fixup-config-false-i -i && |
| 80 | test_auto_fixup fixup-config-false-yes --autosquash && |
| 81 | test_auto_fixup fixup-config-false-i-yes "-i --autosquash" |
| 82 | ' |
| 83 | |
| 84 | test_auto_squash () { |
| 85 | no_squash= && |
| 86 | if test "x$1" = 'x!' |
| 87 | then |
| 88 | no_squash=true |
| 89 | shift |
| 90 | fi && |
| 91 | |
| 92 | git reset --hard base && |
| 93 | echo 1 >file1 && |
| 94 | git add -u && |
| 95 | test_tick && |
| 96 | git commit -m "squash! first" -m "extra para for first" && |
| 97 | git tag $1 && |
| 98 | test_tick && |
| 99 | git rebase $2 HEAD^^^ && |
| 100 | git log --oneline >actual && |
| 101 | if test -n "$no_squash" |
| 102 | then |
| 103 | test_line_count = 4 actual |
| 104 | else |
| 105 | test_line_count = 3 actual && |
| 106 | git diff --exit-code $1 && |
| 107 | echo 1 >expect && |
| 108 | git cat-file blob HEAD^:file1 >actual && |
| 109 | test_cmp expect actual && |
| 110 | git cat-file commit HEAD^ >commit && |
| 111 | grep first commit >actual && |
| 112 | test_line_count = 2 actual |
| 113 | fi |
| 114 | } |
| 115 | |
| 116 | test_expect_success 'auto squash (option)' ' |
| 117 | test_auto_squash squash-option --autosquash && |
| 118 | test_auto_squash squash-option-i "--autosquash -i" |
| 119 | ' |
| 120 | |
| 121 | test_expect_success 'auto squash (config true)' ' |
| 122 | git config rebase.autosquash true && |
| 123 | test_auto_squash ! squash-config-true && |
| 124 | test_auto_squash squash-config-true-i -i && |
| 125 | test_auto_squash ! squash-config-true-no --no-autosquash && |
| 126 | test_auto_squash ! squash-config-true-i-no "-i --no-autosquash" |
| 127 | ' |
| 128 | |
| 129 | test_expect_success 'auto squash (config false)' ' |
| 130 | git config rebase.autosquash false && |
| 131 | test_auto_squash ! squash-config-false && |
| 132 | test_auto_squash ! squash-config-false-i -i && |
| 133 | test_auto_squash squash-config-false-yes --autosquash && |
| 134 | test_auto_squash squash-config-false-i-yes "-i --autosquash" |
| 135 | ' |
| 136 | |
| 137 | test_expect_success 'misspelled auto squash' ' |
| 138 | git reset --hard base && |
| 139 | echo 1 >file1 && |
| 140 | git add -u && |
| 141 | test_tick && |
| 142 | git commit -m "squash! forst" && |
| 143 | git tag final-missquash && |
| 144 | test_tick && |
| 145 | git rebase --autosquash -i HEAD^^^ && |
| 146 | git log --oneline >actual && |
| 147 | test_line_count = 4 actual && |
| 148 | git diff --exit-code final-missquash && |
| 149 | git rev-list final-missquash...HEAD >list && |
| 150 | test_must_be_empty list |
| 151 | ' |
| 152 | |
| 153 | test_expect_success 'auto squash that matches 2 commits' ' |
| 154 | git reset --hard base && |
| 155 | echo 4 >file4 && |
| 156 | git add file4 && |
| 157 | test_tick && |
| 158 | git commit -m "first new commit" && |
| 159 | echo 1 >file1 && |
| 160 | git add -u && |
| 161 | test_tick && |
| 162 | git commit -m "squash! first" -m "extra para for first" && |
| 163 | git tag final-multisquash && |
| 164 | test_tick && |
| 165 | git rebase --autosquash -i HEAD~4 && |
| 166 | git log --oneline >actual && |
| 167 | test_line_count = 4 actual && |
| 168 | git diff --exit-code final-multisquash && |
| 169 | echo 1 >expect && |
| 170 | git cat-file blob HEAD^^:file1 >actual && |
| 171 | test_cmp expect actual && |
| 172 | git cat-file commit HEAD^^ >commit && |
| 173 | grep first commit >actual && |
| 174 | test_line_count = 2 actual && |
| 175 | git cat-file commit HEAD >commit && |
| 176 | grep first commit >actual && |
| 177 | test_line_count = 1 actual |
| 178 | ' |
| 179 | |
| 180 | test_expect_success 'auto squash that matches a commit after the squash' ' |
| 181 | git reset --hard base && |
| 182 | echo 1 >file1 && |
| 183 | git add -u && |
| 184 | test_tick && |
| 185 | git commit -m "squash! third" && |
| 186 | echo 4 >file4 && |
| 187 | git add file4 && |
| 188 | test_tick && |
| 189 | git commit -m "third commit" && |
| 190 | git tag final-presquash && |
| 191 | test_tick && |
| 192 | git rebase --autosquash -i HEAD~4 && |
| 193 | git log --oneline >actual && |
| 194 | test_line_count = 5 actual && |
| 195 | git diff --exit-code final-presquash && |
| 196 | echo 0 >expect && |
| 197 | git cat-file blob HEAD^^:file1 >actual && |
| 198 | test_cmp expect actual && |
| 199 | echo 1 >expect && |
| 200 | git cat-file blob HEAD^:file1 >actual && |
| 201 | test_cmp expect actual && |
| 202 | git cat-file commit HEAD >commit && |
| 203 | grep third commit >actual && |
| 204 | test_line_count = 1 actual && |
| 205 | git cat-file commit HEAD^ >commit && |
| 206 | grep third commit >actual && |
| 207 | test_line_count = 1 actual |
| 208 | ' |
| 209 | test_expect_success 'auto squash that matches a sha1' ' |
| 210 | git reset --hard base && |
| 211 | echo 1 >file1 && |
| 212 | git add -u && |
| 213 | test_tick && |
| 214 | oid=$(git rev-parse --short HEAD^) && |
| 215 | git commit -m "squash! $oid" -m "extra para" && |
| 216 | git tag final-shasquash && |
| 217 | test_tick && |
| 218 | git rebase --autosquash -i HEAD^^^ && |
| 219 | git log --oneline >actual && |
| 220 | test_line_count = 3 actual && |
| 221 | git diff --exit-code final-shasquash && |
| 222 | echo 1 >expect && |
| 223 | git cat-file blob HEAD^:file1 >actual && |
| 224 | test_cmp expect actual && |
| 225 | git cat-file commit HEAD^ >commit && |
| 226 | test_grep ! "squash" commit && |
| 227 | grep "^extra para" commit >actual && |
| 228 | test_line_count = 1 actual |
| 229 | ' |
| 230 | |
| 231 | test_expect_success 'auto squash that matches longer sha1' ' |
| 232 | git reset --hard base && |
| 233 | echo 1 >file1 && |
| 234 | git add -u && |
| 235 | test_tick && |
| 236 | oid=$(git rev-parse --short=11 HEAD^) && |
| 237 | git commit -m "squash! $oid" -m "extra para" && |
| 238 | git tag final-longshasquash && |
| 239 | test_tick && |
| 240 | git rebase --autosquash -i HEAD^^^ && |
| 241 | git log --oneline >actual && |
| 242 | test_line_count = 3 actual && |
| 243 | git diff --exit-code final-longshasquash && |
| 244 | echo 1 >expect && |
| 245 | git cat-file blob HEAD^:file1 >actual && |
| 246 | test_cmp expect actual && |
| 247 | git cat-file commit HEAD^ >commit && |
| 248 | test_grep ! "squash" commit && |
| 249 | grep "^extra para" commit >actual && |
| 250 | test_line_count = 1 actual |
| 251 | ' |
| 252 | |
| 253 | test_expect_success 'auto squash of fixup commit that matches branch name which points back to fixup commit' ' |
| 254 | git reset --hard base && |
| 255 | git commit --allow-empty -m "fixup! self-cycle" && |
| 256 | git branch self-cycle && |
| 257 | GIT_SEQUENCE_EDITOR="cat >tmp" git rebase --autosquash -i HEAD^^ && |
| 258 | sed -ne "/^[^#]/{s/[0-9a-f]\{7,\}/HASH/g;p;}" tmp >actual && |
| 259 | cat <<-EOF >expect && |
| 260 | pick HASH # second commit |
| 261 | pick HASH # fixup! self-cycle # empty |
| 262 | EOF |
| 263 | test_cmp expect actual |
| 264 | ' |
| 265 | |
| 266 | test_auto_commit_flags () { |
| 267 | git reset --hard base && |
| 268 | echo 1 >file1 && |
| 269 | git add -u && |
| 270 | test_tick && |
| 271 | git commit --$1 first-commit -m "extra para for first" && |
| 272 | git tag final-commit-$1 && |
| 273 | test_tick && |
| 274 | git rebase --autosquash -i HEAD^^^ && |
| 275 | git log --oneline >actual && |
| 276 | test_line_count = 3 actual && |
| 277 | git diff --exit-code final-commit-$1 && |
| 278 | echo 1 >expect && |
| 279 | git cat-file blob HEAD^:file1 >actual && |
| 280 | test_cmp expect actual && |
| 281 | git cat-file commit HEAD^ >commit && |
| 282 | grep first commit >actual && |
| 283 | test_line_count = $2 actual |
| 284 | } |
| 285 | |
| 286 | test_expect_success 'use commit --fixup' ' |
| 287 | test_auto_commit_flags fixup 1 |
| 288 | ' |
| 289 | |
| 290 | test_expect_success 'use commit --squash' ' |
| 291 | test_auto_commit_flags squash 2 |
| 292 | ' |
| 293 | |
| 294 | test_auto_fixup_fixup () { |
| 295 | git reset --hard base && |
| 296 | echo 1 >file1 && |
| 297 | git add -u && |
| 298 | test_tick && |
| 299 | git commit -m "$1! first" -m "extra para for first" && |
| 300 | echo 2 >file1 && |
| 301 | git add -u && |
| 302 | test_tick && |
| 303 | git commit -m "$1! $2! first" -m "second extra para for first" && |
| 304 | git tag "final-$1-$2" && |
| 305 | test_tick && |
| 306 | ( |
| 307 | set_cat_todo_editor && |
| 308 | test_must_fail git rebase --autosquash -i HEAD^^^^ >actual && |
| 309 | head=$(git rev-parse --short HEAD) && |
| 310 | parent1=$(git rev-parse --short HEAD^) && |
| 311 | parent2=$(git rev-parse --short HEAD^^) && |
| 312 | parent3=$(git rev-parse --short HEAD^^^) && |
| 313 | cat >expected <<-EOF && |
| 314 | pick $parent3 # first commit |
| 315 | $1 $parent1 # $1! first |
| 316 | $1 $head # $1! $2! first |
| 317 | pick $parent2 # second commit |
| 318 | EOF |
| 319 | test_cmp expected actual |
| 320 | ) && |
| 321 | git rebase --autosquash -i HEAD^^^^ && |
| 322 | git log --oneline >actual && |
| 323 | test_line_count = 3 actual |
| 324 | git diff --exit-code "final-$1-$2" && |
| 325 | echo 2 >expect && |
| 326 | git cat-file blob HEAD^:file1 >actual && |
| 327 | test_cmp expect actual && |
| 328 | git cat-file commit HEAD^ >commit && |
| 329 | grep first commit >actual && |
| 330 | if test "$1" = "fixup" |
| 331 | then |
| 332 | test_line_count = 1 actual |
| 333 | elif test "$1" = "squash" |
| 334 | then |
| 335 | test_line_count = 3 actual |
| 336 | else |
| 337 | false |
| 338 | fi |
| 339 | } |
| 340 | |
| 341 | test_expect_success 'fixup! fixup!' ' |
| 342 | test_auto_fixup_fixup fixup fixup |
| 343 | ' |
| 344 | |
| 345 | test_expect_success 'fixup! squash!' ' |
| 346 | test_auto_fixup_fixup fixup squash |
| 347 | ' |
| 348 | |
| 349 | test_expect_success 'squash! squash!' ' |
| 350 | test_auto_fixup_fixup squash squash |
| 351 | ' |
| 352 | |
| 353 | test_expect_success 'squash! fixup!' ' |
| 354 | test_auto_fixup_fixup squash fixup |
| 355 | ' |
| 356 | |
| 357 | test_expect_success 'autosquash with custom inst format' ' |
| 358 | git reset --hard base && |
| 359 | git config --add rebase.instructionFormat "[%an @ %ar] %s" && |
| 360 | echo 2 >file1 && |
| 361 | git add -u && |
| 362 | test_tick && |
| 363 | oid=$(git rev-parse --short HEAD^) && |
| 364 | git commit -m "squash! $oid" -m "extra para for first" && |
| 365 | echo 1 >file1 && |
| 366 | git add -u && |
| 367 | test_tick && |
| 368 | subject=$(git log -n 1 --format=%s HEAD~2) && |
| 369 | git commit -m "squash! $subject" -m "second extra para for first" && |
| 370 | git tag final-squash-instFmt && |
| 371 | test_tick && |
| 372 | git rebase --autosquash -i HEAD~4 && |
| 373 | git log --oneline >actual && |
| 374 | test_line_count = 3 actual && |
| 375 | git diff --exit-code final-squash-instFmt && |
| 376 | echo 1 >expect && |
| 377 | git cat-file blob HEAD^:file1 >actual && |
| 378 | test_cmp expect actual && |
| 379 | git cat-file commit HEAD^ >commit && |
| 380 | test_grep ! "squash" commit && |
| 381 | grep first commit >actual && |
| 382 | test_line_count = 3 actual |
| 383 | ' |
| 384 | |
| 385 | test_expect_success 'autosquash with empty custom instructionFormat' ' |
| 386 | git reset --hard base && |
| 387 | test_commit empty-instructionFormat-test && |
| 388 | ( |
| 389 | set_cat_todo_editor && |
| 390 | test_must_fail git -c rebase.instructionFormat= \ |
| 391 | rebase --autosquash --force-rebase -i HEAD^ >actual && |
| 392 | git log -1 --format="pick %h # %s" >expect && |
| 393 | test_cmp expect actual |
| 394 | ) |
| 395 | ' |
| 396 | |
| 397 | test_expect_success 'autosquash with invalid custom instructionFormat' ' |
| 398 | git reset --hard base && |
| 399 | test_commit invalid-instructionFormat-test && |
| 400 | ( |
| 401 | test_must_fail git -c rebase.instructionFormat=blah \ |
| 402 | rebase --autosquash --force-rebase -i HEAD^ && |
| 403 | test_path_is_missing .git/rebase-merge |
| 404 | ) |
| 405 | ' |
| 406 | |
| 407 | set_backup_editor () { |
| 408 | write_script backup-editor.sh <<-\EOF |
| 409 | cp "$1" .git/backup-"$(basename "$1")" |
| 410 | EOF |
| 411 | test_set_editor "$PWD/backup-editor.sh" |
| 412 | } |
| 413 | |
| 414 | test_expect_success 'autosquash with multiple empty patches' ' |
| 415 | test_tick && |
| 416 | git commit --allow-empty -m "empty" && |
| 417 | test_tick && |
| 418 | git commit --allow-empty -m "empty2" && |
| 419 | test_tick && |
| 420 | >fixup && |
| 421 | git add fixup && |
| 422 | git commit --fixup HEAD^^ && |
| 423 | ( |
| 424 | set_backup_editor && |
| 425 | GIT_USE_REBASE_HELPER=false \ |
| 426 | git rebase -i --force-rebase --autosquash HEAD~4 && |
| 427 | test_grep empty2 .git/backup-git-rebase-todo |
| 428 | ) |
| 429 | ' |
| 430 | |
| 431 | test_expect_success 'extra spaces after fixup!' ' |
| 432 | base=$(git rev-parse HEAD) && |
| 433 | test_commit to-fixup && |
| 434 | git commit --allow-empty -m "fixup! to-fixup" && |
| 435 | git rebase -i --autosquash --keep-empty HEAD~2 && |
| 436 | parent=$(git rev-parse HEAD^) && |
| 437 | test $base = $parent |
| 438 | ' |
| 439 | |
| 440 | test_expect_success 'wrapped original subject' ' |
| 441 | if test -d .git/rebase-merge; then git rebase --abort; fi && |
| 442 | base=$(git rev-parse HEAD) && |
| 443 | echo "wrapped subject" >wrapped && |
| 444 | git add wrapped && |
| 445 | test_tick && |
| 446 | git commit --allow-empty -m "$(printf "To\nfixup")" && |
| 447 | test_tick && |
| 448 | git commit --allow-empty -m "fixup! To fixup" && |
| 449 | git rebase -i --autosquash --keep-empty HEAD~2 && |
| 450 | parent=$(git rev-parse HEAD^) && |
| 451 | test $base = $parent |
| 452 | ' |
| 453 | |
| 454 | test_expect_success 'abort last squash' ' |
| 455 | test_when_finished "test_might_fail git rebase --abort" && |
| 456 | test_when_finished "git checkout main" && |
| 457 | |
| 458 | git checkout -b some-squashes && |
| 459 | git commit --allow-empty -m first && |
| 460 | git commit --allow-empty --squash HEAD && |
| 461 | git commit --allow-empty -m second && |
| 462 | git commit --allow-empty --squash HEAD && |
| 463 | |
| 464 | test_must_fail git -c core.editor="grep -q ^pick" \ |
| 465 | rebase -ki --autosquash HEAD~4 && |
| 466 | : do not finish the squash, but resolve it manually && |
| 467 | git commit --allow-empty --amend -m edited-first && |
| 468 | git rebase --skip && |
| 469 | git show >actual && |
| 470 | test_grep ! first actual |
| 471 | ' |
| 472 | |
| 473 | test_expect_success 'fixup a fixup' ' |
| 474 | echo 0to-fixup >file0 && |
| 475 | test_tick && |
| 476 | git commit -m "to-fixup" file0 && |
| 477 | test_tick && |
| 478 | git commit --squash HEAD -m X --allow-empty && |
| 479 | test_tick && |
| 480 | git commit --squash HEAD^ -m Y --allow-empty && |
| 481 | test_tick && |
| 482 | git commit -m "squash! $(git rev-parse HEAD^)" -m Z --allow-empty && |
| 483 | test_tick && |
| 484 | git commit -m "squash! $(git rev-parse HEAD^^)" -m W --allow-empty && |
| 485 | git rebase -ki --autosquash HEAD~5 && |
| 486 | test XZWY = $(git show | tr -cd W-Z) |
| 487 | ' |
| 488 | |
| 489 | test_expect_success 'pick and fixup respect commit.cleanup' ' |
| 490 | git reset --hard base && |
| 491 | test_commit --no-tag "fixup! second commit" file1 fixup && |
| 492 | test_commit something && |
| 493 | write_script .git/hooks/prepare-commit-msg <<-\EOF && |
| 494 | printf "\n# Prepared\n" >> "$1" |
| 495 | EOF |
| 496 | git rebase -i --autosquash HEAD~3 && |
| 497 | test_commit_message HEAD~1 <<-\EOF && |
| 498 | second commit |
| 499 | |
| 500 | # Prepared |
| 501 | EOF |
| 502 | test_commit_message HEAD <<-\EOF && |
| 503 | something |
| 504 | |
| 505 | # Prepared |
| 506 | EOF |
| 507 | git reset --hard something && |
| 508 | git -c commit.cleanup=strip rebase -i --autosquash HEAD~3 && |
| 509 | test_commit_message HEAD~1 -m "second commit" && |
| 510 | test_commit_message HEAD -m "something" |
| 511 | ' |
| 512 | |
| 513 | test_done |