| 1 | #!/bin/shscip-shell filesystem . . `/bin/sh`. |
| 2 | # |
| 3 | # Copyright (c) 2013 Ramkumar Ramachandra |
| 4 | # |
| 5 | |
| 6 | test_description='git rebase --autostash tests' |
| 7 | GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main |
| 8 | export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME |
| 9 | |
| 10 | . ./test-lib.shscip-shell source . . `./test-lib.sh`. |
| 11 | |
| 12 | test_expect_success setup ' |
| 13 | echo hello-world >file0 && |
| 14 | git add . && |
| 15 | test_tick && |
| 16 | git commit -m "initial commit" && |
| 17 | git checkout -b feature-branch && |
| 18 | echo another-hello >file1 && |
| 19 | echo goodbye >file2 && |
| 20 | git add . && |
| 21 | test_tick && |
| 22 | git commit -m "second commit" && |
| 23 | echo final-goodbye >file3 && |
| 24 | git add . && |
| 25 | test_tick && |
| 26 | git commit -m "third commit" && |
| 27 | git checkout -b unrelated-onto-branch main && |
| 28 | echo unrelated >file4 && |
| 29 | git add . && |
| 30 | test_tick && |
| 31 | git commit -m "unrelated commit" && |
| 32 | git checkout -b related-onto-branch main && |
| 33 | echo conflicting-change >file2 && |
| 34 | git add . && |
| 35 | test_tick && |
| 36 | git commit -m "related commit" && |
| 37 | remove_progress_re="$(printf "s/.*\\r//")" |
| 38 | ' |
| 39 | |
| 40 | create_expected_success_applyscip-shell shell cgit 1.2.3+git20250818.80.3346409+git2.51.0-1 create_expected_success_apply(). () { |
| 41 | catscip-shell system . . cat. >expected <<-EOF |
| 42 | $(grepscip-shell system . . grep. "^Created autostash: [0-9a-f][0-9a-f]*\$" actual) |
| 43 | First, rewinding head to replay your work on top of it... |
| 44 | Applying: second commit |
| 45 | Applying: third commit |
| 46 | Applied autostash. |
| 47 | EOF |
| 48 | } |
| 49 | |
| 50 | create_expected_success_mergescip-shell shell cgit 1.2.3+git20250818.80.3346409+git2.51.0-1 create_expected_success_merge(). () { |
| 51 | q_to_cr >expected <<-EOF |
| 52 | $(grepscip-shell system . . grep. "^Created autostash: [0-9a-f][0-9a-f]*\$" actual) |
| 53 | Applied autostash. |
| 54 | Successfully rebased and updated refs/heads/rebased-feature-branch. |
| 55 | EOF |
| 56 | } |
| 57 | |
| 58 | create_expected_failure_applyscip-shell shell cgit 1.2.3+git20250818.80.3346409+git2.51.0-1 create_expected_failure_apply(). () { |
| 59 | catscip-shell system . . cat. >expected <<-EOF |
| 60 | $(grepscip-shell system . . grep. "^Created autostash: [0-9a-f][0-9a-f]*\$" actual) |
| 61 | First, rewinding head to replay your work on top of it... |
| 62 | Applying: second commit |
| 63 | Applying: third commit |
| 64 | Applying autostash resulted in conflicts. |
| 65 | Your changes are safe in the stash. |
| 66 | You can run "git stash pop" or "git stash drop" at any time. |
| 67 | EOF |
| 68 | } |
| 69 | |
| 70 | create_expected_failure_mergescip-shell shell cgit 1.2.3+git20250818.80.3346409+git2.51.0-1 create_expected_failure_merge(). () { |
| 71 | catscip-shell system . . cat. >expected <<-EOF |
| 72 | $(grepscip-shell system . . grep. "^Created autostash: [0-9a-f][0-9a-f]*\$" actual) |
| 73 | Applying autostash resulted in conflicts. |
| 74 | Your changes are safe in the stash. |
| 75 | You can run "git stash pop" or "git stash drop" at any time. |
| 76 | Successfully rebased and updated refs/heads/rebased-feature-branch. |
| 77 | EOF |
| 78 | } |
| 79 | |
| 80 | testrebasescip-shell shell cgit 1.2.3+git20250818.80.3346409+git2.51.0-1 testrebase(). () { |
| 81 | type=$1 |
| 82 | dotest=$2 |
| 83 | |
| 84 | test_expect_success "rebase$type: restore autostash when pre-rebase hook fails" ' |
| 85 | git checkout -f feature-branch && |
| 86 | test_hook pre-rebase <<-\EOF && |
| 87 | exit 1 |
| 88 | EOF |
| 89 | |
| 90 | echo changed >file0 && |
| 91 | test_must_fail git rebase $type --autostash -f HEAD^ && |
| 92 | test_must_fail git rebase --quit 2>err && |
| 93 | test_grep "no rebase in progress" err && |
| 94 | echo changed >expect && |
| 95 | test_cmp expect file0 |
| 96 | ' |
| 97 | |
| 98 | test_expect_success "rebase$type: restore autostash when checkout onto fails" ' |
| 99 | git checkout -f --detach feature-branch && |
| 100 | echo uncommitted-content >file0 && |
| 101 | echo untracked >file4 && |
| 102 | test_when_finished "rm file4" && |
| 103 | test_must_fail git rebase $type --autostash \ |
| 104 | unrelated-onto-branch && |
| 105 | test_must_fail git rebase --quit 2>err && |
| 106 | test_grep "no rebase in progress" err && |
| 107 | echo uncommitted-content >expect && |
| 108 | test_cmp expect file0 |
| 109 | ' |
| 110 | |
| 111 | test_expect_success "rebase$type: restore autostash when branch checkout fails" ' |
| 112 | git checkout -f unrelated-onto-branch^ && |
| 113 | echo uncommitted-content >file0 && |
| 114 | echo untracked >file4 && |
| 115 | test_when_finished "rm file4" && |
| 116 | test_must_fail git rebase $type --autostash HEAD \ |
| 117 | unrelated-onto-branch && |
| 118 | test_must_fail git rebase --quit 2>err && |
| 119 | test_grep "no rebase in progress" err && |
| 120 | echo uncommitted-content >expect && |
| 121 | test_cmp expect file0 |
| 122 | ' |
| 123 | |
| 124 | test_expect_success "rebase$type: dirty worktree, --no-autostash" ' |
| 125 | test_config rebase.autostash true && |
| 126 | git reset --hard && |
| 127 | git checkout -b rebased-feature-branch feature-branch && |
| 128 | test_when_finished git branch -D rebased-feature-branch && |
| 129 | test_when_finished git checkout feature-branch && |
| 130 | echo dirty >>file3 && |
| 131 | test_must_fail git rebase$type --no-autostash unrelated-onto-branch |
| 132 | ' |
| 133 | |
| 134 | test_expect_success "rebase$type: dirty worktree, non-conflicting rebase" ' |
| 135 | test_config rebase.autostash true && |
| 136 | git reset --hard && |
| 137 | git checkout -b rebased-feature-branch feature-branch && |
| 138 | echo dirty >>file3 && |
| 139 | git rebase$type unrelated-onto-branch >actual 2>&1 && |
| 140 | grep unrelated file4 && |
| 141 | grep dirty file3 && |
| 142 | git checkout feature-branch |
| 143 | ' |
| 144 | |
| 145 | test_expect_success "rebase$type --autostash: check output" ' |
| 146 | test_when_finished git branch -D rebased-feature-branch && |
| 147 | suffix=${type#\ --} && suffix=${suffix:-apply} && |
| 148 | if test ${suffix} = "interactive"; then |
| 149 | suffix=merge |
| 150 | fi && |
| 151 | create_expected_success_$suffix && |
| 152 | sed "$remove_progress_re" <actual >actual2 && |
| 153 | test_cmp expected actual2 |
| 154 | ' |
| 155 | |
| 156 | test_expect_success "rebase$type: dirty index, non-conflicting rebase" ' |
| 157 | test_config rebase.autostash true && |
| 158 | git reset --hard && |
| 159 | git checkout -b rebased-feature-branch feature-branch && |
| 160 | test_when_finished git branch -D rebased-feature-branch && |
| 161 | echo dirty >>file3 && |
| 162 | git add file3 && |
| 163 | git rebase$type unrelated-onto-branch && |
| 164 | grep unrelated file4 && |
| 165 | grep dirty file3 && |
| 166 | git checkout feature-branch |
| 167 | ' |
| 168 | |
| 169 | test_expect_success "rebase$type: conflicting rebase" ' |
| 170 | test_config rebase.autostash true && |
| 171 | git reset --hard && |
| 172 | git checkout -b rebased-feature-branch feature-branch && |
| 173 | test_when_finished git branch -D rebased-feature-branch && |
| 174 | echo dirty >>file3 && |
| 175 | test_must_fail git rebase$type related-onto-branch && |
| 176 | test_path_is_file $dotest/autostash && |
| 177 | test_path_is_missing file3 && |
| 178 | rm -rf $dotest && |
| 179 | git reset --hard && |
| 180 | git checkout feature-branch |
| 181 | ' |
| 182 | |
| 183 | test_expect_success "rebase$type: --continue" ' |
| 184 | test_config rebase.autostash true && |
| 185 | git reset --hard && |
| 186 | git checkout -b rebased-feature-branch feature-branch && |
| 187 | test_when_finished git branch -D rebased-feature-branch && |
| 188 | echo dirty >>file3 && |
| 189 | test_must_fail git rebase$type related-onto-branch && |
| 190 | test_path_is_file $dotest/autostash && |
| 191 | test_path_is_missing file3 && |
| 192 | echo "conflicting-plus-goodbye" >file2 && |
| 193 | git add file2 && |
| 194 | git rebase --continue && |
| 195 | test_path_is_missing $dotest/autostash && |
| 196 | grep dirty file3 && |
| 197 | git checkout feature-branch |
| 198 | ' |
| 199 | |
| 200 | test_expect_success "rebase$type: --skip" ' |
| 201 | test_config rebase.autostash true && |
| 202 | git reset --hard && |
| 203 | git checkout -b rebased-feature-branch feature-branch && |
| 204 | test_when_finished git branch -D rebased-feature-branch && |
| 205 | echo dirty >>file3 && |
| 206 | test_must_fail git rebase$type related-onto-branch && |
| 207 | test_path_is_file $dotest/autostash && |
| 208 | test_path_is_missing file3 && |
| 209 | git rebase --skip && |
| 210 | test_path_is_missing $dotest/autostash && |
| 211 | grep dirty file3 && |
| 212 | git checkout feature-branch |
| 213 | ' |
| 214 | |
| 215 | test_expect_success "rebase$type: --abort" ' |
| 216 | test_config rebase.autostash true && |
| 217 | git reset --hard && |
| 218 | git checkout -b rebased-feature-branch feature-branch && |
| 219 | test_when_finished git branch -D rebased-feature-branch && |
| 220 | echo dirty >>file3 && |
| 221 | test_must_fail git rebase$type related-onto-branch && |
| 222 | test_path_is_file $dotest/autostash && |
| 223 | test_path_is_missing file3 && |
| 224 | git rebase --abort && |
| 225 | test_path_is_missing $dotest/autostash && |
| 226 | grep dirty file3 && |
| 227 | git checkout feature-branch |
| 228 | ' |
| 229 | |
| 230 | test_expect_success "rebase$type: --quit" ' |
| 231 | test_config rebase.autostash true && |
| 232 | git reset --hard && |
| 233 | git checkout -b rebased-feature-branch feature-branch && |
| 234 | test_when_finished git branch -D rebased-feature-branch && |
| 235 | echo dirty >>file3 && |
| 236 | git diff >expect && |
| 237 | test_must_fail git rebase$type related-onto-branch && |
| 238 | test_path_is_file $dotest/autostash && |
| 239 | test_path_is_missing file3 && |
| 240 | git rebase --quit && |
| 241 | test_when_finished git stash drop && |
| 242 | test_path_is_missing $dotest/autostash && |
| 243 | ! grep dirty file3 && |
| 244 | git stash show -p >actual && |
| 245 | test_cmp expect actual && |
| 246 | git reset --hard && |
| 247 | git checkout feature-branch |
| 248 | ' |
| 249 | |
| 250 | test_expect_success "rebase$type: non-conflicting rebase, conflicting stash" ' |
| 251 | test_config rebase.autostash true && |
| 252 | git reset --hard && |
| 253 | git checkout -b rebased-feature-branch feature-branch && |
| 254 | echo dirty >file4 && |
| 255 | git add file4 && |
| 256 | git rebase$type unrelated-onto-branch >actual 2>&1 && |
| 257 | test_path_is_missing $dotest && |
| 258 | git reset --hard && |
| 259 | grep unrelated file4 && |
| 260 | ! grep dirty file4 && |
| 261 | git checkout feature-branch && |
| 262 | git stash pop && |
| 263 | grep dirty file4 |
| 264 | ' |
| 265 | |
| 266 | test_expect_success "rebase$type: check output with conflicting stash" ' |
| 267 | test_when_finished git branch -D rebased-feature-branch && |
| 268 | suffix=${type#\ --} && suffix=${suffix:-apply} && |
| 269 | if test ${suffix} = "interactive"; then |
| 270 | suffix=merge |
| 271 | fi && |
| 272 | create_expected_failure_$suffix && |
| 273 | sed "$remove_progress_re" <actual >actual2 && |
| 274 | test_cmp expected actual2 |
| 275 | ' |
| 276 | } |
| 277 | |
| 278 | test_expect_success "rebase: fast-forward rebase" ' |
| 279 | test_config rebase.autostash true && |
| 280 | git reset --hard && |
| 281 | git checkout -b behind-feature-branch feature-branch~1 && |
| 282 | test_when_finished git branch -D behind-feature-branch && |
| 283 | echo dirty >>file1 && |
| 284 | git rebase feature-branch && |
| 285 | grep dirty file1 && |
| 286 | git checkout feature-branch |
| 287 | ' |
| 288 | |
| 289 | test_expect_success "rebase: noop rebase" ' |
| 290 | test_config rebase.autostash true && |
| 291 | git reset --hard && |
| 292 | git checkout -b same-feature-branch feature-branch && |
| 293 | test_when_finished git branch -D same-feature-branch && |
| 294 | echo dirty >>file1 && |
| 295 | git rebase feature-branch && |
| 296 | grep dirty file1 && |
| 297 | git checkout feature-branch |
| 298 | ' |
| 299 | |
| 300 | testrebasescip-shell shell cgit 1.2.3+git20250818.80.3346409+git2.51.0-1 testrebase(). " --apply" .git/rebase-apply |
| 301 | testrebasescip-shell shell cgit 1.2.3+git20250818.80.3346409+git2.51.0-1 testrebase(). " --merge" .git/rebase-merge |
| 302 | testrebasescip-shell shell cgit 1.2.3+git20250818.80.3346409+git2.51.0-1 testrebase(). " --interactive" .git/rebase-merge |
| 303 | |
| 304 | test_expect_success 'abort rebase -i with --autostash' ' |
| 305 | test_when_finished "git reset --hard" && |
| 306 | echo uncommitted-content >file0 && |
| 307 | ( |
| 308 | write_script abort-editor.sh <<-\EOF && |
| 309 | echo >"$1" |
| 310 | EOF |
| 311 | test_set_editor "$(pwd)/abort-editor.sh" && |
| 312 | test_must_fail git rebase -i --autostash HEAD^ && |
| 313 | rm -f abort-editor.sh |
| 314 | ) && |
| 315 | echo uncommitted-content >expected && |
| 316 | test_cmp expected file0 |
| 317 | ' |
| 318 | |
| 319 | test_expect_success 'restore autostash on editor failure' ' |
| 320 | test_when_finished "git reset --hard" && |
| 321 | echo uncommitted-content >file0 && |
| 322 | ( |
| 323 | test_set_editor "false" && |
| 324 | test_must_fail git rebase -i --autostash HEAD^ |
| 325 | ) && |
| 326 | echo uncommitted-content >expected && |
| 327 | test_cmp expected file0 |
| 328 | ' |
| 329 | |
| 330 | test_expect_success 'autostash is saved on editor failure with conflict' ' |
| 331 | test_when_finished "git reset --hard" && |
| 332 | echo uncommitted-content >file0 && |
| 333 | ( |
| 334 | write_script abort-editor.sh <<-\EOF && |
| 335 | echo conflicting-content >file0 |
| 336 | exit 1 |
| 337 | EOF |
| 338 | test_set_editor "$(pwd)/abort-editor.sh" && |
| 339 | test_must_fail git rebase -i --autostash HEAD^ && |
| 340 | rm -f abort-editor.sh |
| 341 | ) && |
| 342 | echo conflicting-content >expected && |
| 343 | test_cmp expected file0 && |
| 344 | git checkout file0 && |
| 345 | git stash pop && |
| 346 | echo uncommitted-content >expected && |
| 347 | test_cmp expected file0 |
| 348 | ' |
| 349 | |
| 350 | test_expect_success 'autostash with dirty submodules' ' |
| 351 | test_when_finished "git reset --hard && git checkout main" && |
| 352 | git checkout -b with-submodule && |
| 353 | git -c protocol.file.allow=always submodule add ./ sub && |
| 354 | test_tick && |
| 355 | git commit -m add-submodule && |
| 356 | echo changed >sub/file0 && |
| 357 | git rebase -i --autostash HEAD |
| 358 | ' |
| 359 | |
| 360 | test_expect_success 'branch is left alone when possible' ' |
| 361 | git checkout -b unchanged-branch && |
| 362 | echo changed >file0 && |
| 363 | git rebase --autostash unchanged-branch && |
| 364 | test changed = "$(cat file0)" && |
| 365 | test unchanged-branch = "$(git rev-parse --abbrev-ref HEAD)" |
| 366 | ' |
| 367 | |
| 368 | test_expect_success 'never change active branch' ' |
| 369 | git checkout -b not-the-feature-branch unrelated-onto-branch && |
| 370 | test_when_finished "git reset --hard && git checkout main" && |
| 371 | echo changed >file0 && |
| 372 | git rebase --autostash not-the-feature-branch feature-branch && |
| 373 | test_cmp_rev not-the-feature-branch unrelated-onto-branch |
| 374 | ' |
| 375 | |
| 376 | test_expect_success 'autostash commit is marked as reachable' ' |
| 377 | echo changed >file0 && |
| 378 | git rebase --autostash --exec "git prune --expire=now" \ |
| 379 | feature-branch^ feature-branch && |
| 380 | # git rebase succeeds if the stash cannot be applied so we need to check |
| 381 | # the contents of file0 |
| 382 | echo changed >expect && |
| 383 | test_cmp expect file0 |
| 384 | ' |
| 385 | |
| 386 | test_done |