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