refs/files: skip creation of "refs/{heads,tags}" for worktrees
[git.git] / t / t2060-switch.sh
blobe247a4735bbc26b01fecd600ed3e529bea16f464
1 #!/bin/sh
3 test_description='switch basic functionality'
5 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
6 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
8 . ./test-lib.sh
10 test_expect_success 'setup' '
11 test_commit first &&
12 git branch first-branch &&
13 test_commit second &&
14 test_commit third &&
15 git remote add origin nohost:/nopath &&
16 git update-ref refs/remotes/origin/foo first-branch
19 test_expect_success 'switch branch no arguments' '
20 test_must_fail git switch
23 test_expect_success 'switch branch' '
24 git switch first-branch &&
25 test_path_is_missing second.t
28 test_expect_success 'switch and detach' '
29 test_when_finished git switch main &&
30 test_must_fail git switch main^{commit} &&
31 git switch --detach main^{commit} &&
32 test_must_fail git symbolic-ref HEAD
35 test_expect_success 'suggestion to detach' '
36 test_must_fail git switch main^{commit} 2>stderr &&
37 grep "try again with the --detach option" stderr
40 test_expect_success 'suggestion to detach is suppressed with advice.suggestDetachingHead=false' '
41 test_config advice.suggestDetachingHead false &&
42 test_must_fail git switch main^{commit} 2>stderr &&
43 ! grep "try again with the --detach option" stderr
46 test_expect_success 'switch and detach current branch' '
47 test_when_finished git switch main &&
48 git switch main &&
49 git switch --detach &&
50 test_must_fail git symbolic-ref HEAD
53 test_expect_success 'switch and create branch' '
54 test_when_finished git switch main &&
55 git switch -c temp main^ &&
56 test_cmp_rev main^ refs/heads/temp &&
57 echo refs/heads/temp >expected-branch &&
58 git symbolic-ref HEAD >actual-branch &&
59 test_cmp expected-branch actual-branch
62 test_expect_success 'force create branch from HEAD' '
63 test_when_finished git switch main &&
64 git switch --detach main &&
65 test_must_fail git switch -c temp &&
66 git switch -C temp &&
67 test_cmp_rev main refs/heads/temp &&
68 echo refs/heads/temp >expected-branch &&
69 git symbolic-ref HEAD >actual-branch &&
70 test_cmp expected-branch actual-branch
73 test_expect_success 'new orphan branch from empty' '
74 test_when_finished git switch main &&
75 test_must_fail git switch --orphan new-orphan HEAD &&
76 git switch --orphan new-orphan &&
77 test_commit orphan &&
78 git cat-file commit refs/heads/new-orphan >commit &&
79 ! grep ^parent commit &&
80 git ls-files >tracked-files &&
81 echo orphan.t >expected &&
82 test_cmp expected tracked-files
85 test_expect_success 'orphan branch works with --discard-changes' '
86 test_when_finished git switch main &&
87 echo foo >foo.txt &&
88 git switch --discard-changes --orphan new-orphan2 &&
89 git ls-files >tracked-files &&
90 test_must_be_empty tracked-files
93 test_expect_success 'switching ignores file of same branch name' '
94 test_when_finished git switch main &&
95 : >first-branch &&
96 git switch first-branch &&
97 echo refs/heads/first-branch >expected &&
98 git symbolic-ref HEAD >actual &&
99 test_cmp expected actual
102 test_expect_success 'guess and create branch' '
103 test_when_finished git switch main &&
104 test_must_fail git switch --no-guess foo &&
105 test_config checkout.guess false &&
106 test_must_fail git switch foo &&
107 test_config checkout.guess true &&
108 git switch foo &&
109 echo refs/heads/foo >expected &&
110 git symbolic-ref HEAD >actual &&
111 test_cmp expected actual
114 test_expect_success 'not switching when something is in progress' '
115 test_when_finished rm -f .git/MERGE_HEAD &&
116 # fake a merge-in-progress
117 cp .git/HEAD .git/MERGE_HEAD &&
118 test_must_fail git switch -d @^
121 test_expect_success 'tracking info copied with autoSetupMerge=inherit' '
122 # default config does not copy tracking info
123 git switch -c foo-no-inherit foo &&
124 test_cmp_config "" --default "" branch.foo-no-inherit.remote &&
125 test_cmp_config "" --default "" branch.foo-no-inherit.merge &&
126 # with --track=inherit, we copy tracking info from foo
127 git switch --track=inherit -c foo2 foo &&
128 test_cmp_config origin branch.foo2.remote &&
129 test_cmp_config refs/heads/foo branch.foo2.merge &&
130 # with autoSetupMerge=inherit, we do the same
131 test_config branch.autoSetupMerge inherit &&
132 git switch -c foo3 foo &&
133 test_cmp_config origin branch.foo3.remote &&
134 test_cmp_config refs/heads/foo branch.foo3.merge &&
135 # with --track, we override autoSetupMerge
136 git switch --track -c foo4 foo &&
137 test_cmp_config . branch.foo4.remote &&
138 test_cmp_config refs/heads/foo branch.foo4.merge &&
139 # and --track=direct does as well
140 git switch --track=direct -c foo5 foo &&
141 test_cmp_config . branch.foo5.remote &&
142 test_cmp_config refs/heads/foo branch.foo5.merge &&
143 # no tracking info to inherit from main
144 git switch -c main2 main &&
145 test_cmp_config "" --default "" branch.main2.remote &&
146 test_cmp_config "" --default "" branch.main2.merge
149 test_expect_success 'switch back when temporarily detached and checked out elsewhere ' '
150 test_when_finished "
151 git worktree remove wt1 ||:
152 git worktree remove wt2 ||:
153 git checkout - ||:
154 git branch -D shared ||:
155 " &&
156 git checkout -b shared &&
157 test_commit shared-first &&
158 HASH1=$(git rev-parse --verify HEAD) &&
159 test_commit shared-second &&
160 test_commit shared-third &&
161 HASH2=$(git rev-parse --verify HEAD) &&
162 git worktree add wt1 -f shared &&
163 git -C wt1 bisect start &&
164 git -C wt1 bisect good $HASH1 &&
165 git -C wt1 bisect bad $HASH2 &&
166 git worktree add wt2 -f shared &&
167 git -C wt2 bisect start &&
168 git -C wt2 bisect good $HASH1 &&
169 git -C wt2 bisect bad $HASH2 &&
170 # we test in both worktrees to ensure that works
171 # as expected with "first" and "next" worktrees
172 test_must_fail git -C wt1 switch shared &&
173 git -C wt1 switch --ignore-other-worktrees shared &&
174 test_must_fail git -C wt2 switch shared &&
175 git -C wt2 switch --ignore-other-worktrees shared
178 test_done