interplane.Get Omega
Read in your language

git

[Ported from github.com/git/git] Sync with Git 2.56.1

Brought by drakestapleton

branch master Branches Tags History

git / t / t2060-switch.sh

200 lines · 6640 bytes Raw Blame History
1#!/bin/sh
2
3test_description='switch basic functionality'
4
5GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
6export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
7
8. ./test-lib.sh
9
10test_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
17'
18
19test_expect_success 'switch branch no arguments' '
20 test_must_fail git switch
21'
22
23test_expect_success 'switch branch' '
24 git switch first-branch &&
25 test_path_is_missing second.t
26'
27
28test_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
33'
34
35test_expect_success 'suggestion to detach' '
36 test_must_fail git switch main^{commit} 2>stderr &&
37 test_grep "try again with the --detach option" stderr
38'
39
40test_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 test_grep ! "try again with the --detach option" stderr
44'
45
46test_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
51'
52
53test_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
60'
61
62test_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
71'
72
73test_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 test_grep ! ^parent commit &&
80 git ls-files >tracked-files &&
81 echo orphan.t >expected &&
82 test_cmp expected tracked-files
83'
84
85test_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
91'
92
93test_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
100'
101
102test_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
112'
113
114test_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 @^
119'
120
121test_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
147'
148
149test_expect_success 'switch -c completes when tracking config cannot be written' '
150 test_when_finished "git update-ref -d refs/remotes/lockremote/lockbranch" &&
151 test_when_finished "git branch -D lock-new" &&
152 test_when_finished "git switch main" &&
153 test_config remote.lockremote.url . &&
154 test_config remote.lockremote.fetch "+refs/heads/*:refs/remotes/lockremote/*" &&
155 git update-ref refs/remotes/lockremote/lockbranch first-branch &&
156 test_when_finished "rm -f .git/config.lock" &&
157 >.git/config.lock &&
158 git -c core.configLockTimeout=0 \
159 switch -c lock-new lockremote/lockbranch 2>err &&
160 test_grep "unable to write upstream branch configuration" err &&
161 echo refs/heads/lock-new >expect &&
162 git symbolic-ref HEAD >actual &&
163 test_cmp expect actual &&
164 git diff --cached --exit-code &&
165 rm -f .git/config.lock &&
166 test_must_fail git config branch.lock-new.remote
167'
168
169test_expect_success 'switch back when temporarily detached and checked out elsewhere ' '
170 test_when_finished "
171 git worktree remove wt1 ||:
172 git worktree remove wt2 ||:
173 git checkout - ||:
174 git branch -D shared ||:
175 " &&
176 git checkout -b shared &&
177 test_commit shared-first &&
178 HASH1=$(git rev-parse --verify HEAD) &&
179 test_commit shared-second &&
180 test_commit shared-third &&
181 HASH2=$(git rev-parse --verify HEAD) &&
182 git worktree add wt1 -f shared &&
183 git -C wt1 bisect start &&
184 git -C wt1 bisect good $HASH1 &&
185 git -C wt1 bisect bad $HASH2 &&
186 git worktree add wt2 -f shared &&
187 git -C wt2 bisect start &&
188 git -C wt2 bisect good $HASH1 &&
189 git -C wt2 bisect bad $HASH2 &&
190 # we test in both worktrees to ensure that works
191 # as expected with "first" and "next" worktrees
192 test_must_fail git -C wt1 switch shared &&
193 test_must_fail git -C wt1 switch -C shared &&
194 git -C wt1 switch --ignore-other-worktrees shared &&
195 test_must_fail git -C wt2 switch shared &&
196 test_must_fail git -C wt2 switch -C shared &&
197 git -C wt2 switch --ignore-other-worktrees shared
198'
199
200test_done