git
[Ported from github.com/git/git] Sync with Git 2.56.1
Brought by drakestapleton
git / t / t2027-checkout-track.sh
| 1 | #!/bin/sh |
| 2 | |
| 3 | test_description='tests for git branch --track' |
| 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_expect_success 'setup' ' |
| 11 | test_commit one && |
| 12 | test_commit two |
| 13 | ' |
| 14 | |
| 15 | test_expect_success 'checkout --track -b creates a new tracking branch' ' |
| 16 | git checkout --track -b branch1 main && |
| 17 | test $(git rev-parse --abbrev-ref HEAD) = branch1 && |
| 18 | test $(git config --get branch.branch1.remote) = . && |
| 19 | test $(git config --get branch.branch1.merge) = refs/heads/main |
| 20 | ' |
| 21 | |
| 22 | test_expect_success 'checkout --track -b rejects an extra path argument' ' |
| 23 | test_must_fail git checkout --track -b branch2 main one.t 2>err && |
| 24 | test_grep "cannot be used with updating paths" err |
| 25 | ' |
| 26 | |
| 27 | test_expect_success 'checkout --track -b overrides autoSetupMerge=inherit' ' |
| 28 | # Set up tracking config on main |
| 29 | test_config branch.main.remote origin && |
| 30 | test_config branch.main.merge refs/heads/some-branch && |
| 31 | test_config branch.autoSetupMerge inherit && |
| 32 | # With --track=inherit, we copy the tracking config from main |
| 33 | git checkout --track=inherit -b b1 main && |
| 34 | test_cmp_config origin branch.b1.remote && |
| 35 | test_cmp_config refs/heads/some-branch branch.b1.merge && |
| 36 | # With branch.autoSetupMerge=inherit, we do the same |
| 37 | git checkout -b b2 main && |
| 38 | test_cmp_config origin branch.b2.remote && |
| 39 | test_cmp_config refs/heads/some-branch branch.b2.merge && |
| 40 | # But --track overrides this |
| 41 | git checkout --track -b b3 main && |
| 42 | test_cmp_config . branch.b3.remote && |
| 43 | test_cmp_config refs/heads/main branch.b3.merge && |
| 44 | # And --track=direct does as well |
| 45 | git checkout --track=direct -b b4 main && |
| 46 | test_cmp_config . branch.b4.remote && |
| 47 | test_cmp_config refs/heads/main branch.b4.merge |
| 48 | ' |
| 49 | |
| 50 | test_expect_success 'checkout -b completes when tracking config cannot be written' ' |
| 51 | test_when_finished "git update-ref -d refs/remotes/lockremote/lockbranch" && |
| 52 | test_when_finished "git branch -D lock-new" && |
| 53 | test_when_finished "git checkout main" && |
| 54 | test_config remote.lockremote.url . && |
| 55 | test_config remote.lockremote.fetch "+refs/heads/*:refs/remotes/lockremote/*" && |
| 56 | git update-ref refs/remotes/lockremote/lockbranch one && |
| 57 | test_when_finished "rm -f .git/config.lock" && |
| 58 | >.git/config.lock && |
| 59 | git -c core.configLockTimeout=0 \ |
| 60 | checkout -b lock-new lockremote/lockbranch 2>err && |
| 61 | test_grep "unable to write upstream branch configuration" err && |
| 62 | echo refs/heads/lock-new >expect && |
| 63 | git symbolic-ref HEAD >actual && |
| 64 | test_cmp expect actual && |
| 65 | git diff --cached --exit-code && |
| 66 | rm -f .git/config.lock && |
| 67 | test_must_fail git config branch.lock-new.remote |
| 68 | ' |
| 69 | |
| 70 | test_expect_success 'ambiguous tracking info' ' |
| 71 | # Set up a few remote repositories |
| 72 | git init --bare --initial-branch=trunk src1 && |
| 73 | git init --bare --initial-branch=trunk src2 && |
| 74 | git push src1 one:refs/heads/trunk && |
| 75 | git push src2 two:refs/heads/trunk && |
| 76 | |
| 77 | git remote add -f src1 "file://$PWD/src1" && |
| 78 | git remote add -f src2 "file://$PWD/src2" && |
| 79 | |
| 80 | # DWIM |
| 81 | test_must_fail git checkout trunk 2>hint.checkout && |
| 82 | test_grep "hint: *git checkout --track" hint.checkout && |
| 83 | |
| 84 | test_must_fail git switch trunk 2>hint.switch && |
| 85 | test_grep "hint: *git switch --track" hint.switch |
| 86 | ' |
| 87 | |
| 88 | test_done |