]> git.ipfire.org Git - thirdparty/git.git/blame - t/t2024-checkout-dwim.sh
Merge branch 'sb/object-store-lookup'
[thirdparty/git.git] / t / t2024-checkout-dwim.sh
CommitLineData
399e4a1c
JH
1#!/bin/sh
2
3test_description='checkout <branch>
4
5Ensures that checkout on an unborn branch does what the user expects'
6
7. ./test-lib.sh
8
9# Is the current branch "refs/heads/$1"?
10test_branch () {
11 printf "%s\n" "refs/heads/$1" >expect.HEAD &&
12 git symbolic-ref HEAD >actual.HEAD &&
13 test_cmp expect.HEAD actual.HEAD
14}
15
16# Is branch "refs/heads/$1" set to pull from "$2/$3"?
17test_branch_upstream () {
18 printf "%s\n" "$2" "refs/heads/$3" >expect.upstream &&
19 {
20 git config "branch.$1.remote" &&
21 git config "branch.$1.merge"
22 } >actual.upstream &&
23 test_cmp expect.upstream actual.upstream
24}
25
c8cbf20c
ÆAB
26status_uno_is_clean () {
27 >status.expect &&
28 git status -uno --porcelain >status.actual &&
29 test_cmp status.expect status.actual
30}
31
399e4a1c 32test_expect_success 'setup' '
ec2764ee 33 test_commit my_master &&
399e4a1c
JH
34 git init repo_a &&
35 (
36 cd repo_a &&
37 test_commit a_master &&
38 git checkout -b foo &&
39 test_commit a_foo &&
40 git checkout -b bar &&
41 test_commit a_bar
42 ) &&
43 git init repo_b &&
44 (
45 cd repo_b &&
46 test_commit b_master &&
47 git checkout -b foo &&
48 test_commit b_foo &&
49 git checkout -b baz &&
50 test_commit b_baz
51 ) &&
52 git remote add repo_a repo_a &&
53 git remote add repo_b repo_b &&
54 git config remote.repo_b.fetch \
55 "+refs/heads/*:refs/remotes/other_b/*" &&
56 git fetch --all
57'
58
59test_expect_success 'checkout of non-existing branch fails' '
60 git checkout -B master &&
61 test_might_fail git branch -D xyzzy &&
62
63 test_must_fail git checkout xyzzy &&
c8cbf20c 64 status_uno_is_clean &&
399e4a1c
JH
65 test_must_fail git rev-parse --verify refs/heads/xyzzy &&
66 test_branch master
67'
68
ec2764ee 69test_expect_success 'checkout of branch from multiple remotes fails #1' '
399e4a1c
JH
70 git checkout -B master &&
71 test_might_fail git branch -D foo &&
72
73 test_must_fail git checkout foo &&
c8cbf20c 74 status_uno_is_clean &&
399e4a1c
JH
75 test_must_fail git rev-parse --verify refs/heads/foo &&
76 test_branch master
77'
78
ad8d5104
ÆAB
79test_expect_success 'checkout of branch from multiple remotes fails with advice' '
80 git checkout -B master &&
81 test_might_fail git branch -D foo &&
82 test_must_fail git checkout foo 2>stderr &&
83 test_branch master &&
84 status_uno_is_clean &&
85 test_i18ngrep "^hint: " stderr &&
86 test_must_fail git -c advice.checkoutAmbiguousRemoteBranchName=false \
87 checkout foo 2>stderr &&
88 test_branch master &&
89 status_uno_is_clean &&
8d7b558b
ÆAB
90 test_i18ngrep ! "^hint: " stderr &&
91 # Make sure the likes of checkout -p do not print this hint
92 git checkout -p foo 2>stderr &&
93 test_i18ngrep ! "^hint: " stderr &&
94 status_uno_is_clean
95'
96
97test_expect_success 'checkout of branch from multiple remotes succeeds with checkout.defaultRemote #1' '
98 git checkout -B master &&
99 status_uno_is_clean &&
100 test_might_fail git branch -D foo &&
101
102 git -c checkout.defaultRemote=repo_a checkout foo &&
103 status_uno_is_clean &&
104 test_branch foo &&
105 test_cmp_rev remotes/repo_a/foo HEAD &&
106 test_branch_upstream foo repo_a foo
ad8d5104
ÆAB
107'
108
399e4a1c
JH
109test_expect_success 'checkout of branch from a single remote succeeds #1' '
110 git checkout -B master &&
111 test_might_fail git branch -D bar &&
112
113 git checkout bar &&
c8cbf20c 114 status_uno_is_clean &&
399e4a1c
JH
115 test_branch bar &&
116 test_cmp_rev remotes/repo_a/bar HEAD &&
117 test_branch_upstream bar repo_a bar
118'
119
120test_expect_success 'checkout of branch from a single remote succeeds #2' '
121 git checkout -B master &&
122 test_might_fail git branch -D baz &&
123
124 git checkout baz &&
c8cbf20c 125 status_uno_is_clean &&
399e4a1c
JH
126 test_branch baz &&
127 test_cmp_rev remotes/other_b/baz HEAD &&
128 test_branch_upstream baz repo_b baz
129'
130
131test_expect_success '--no-guess suppresses branch auto-vivification' '
132 git checkout -B master &&
c8cbf20c 133 status_uno_is_clean &&
399e4a1c
JH
134 test_might_fail git branch -D bar &&
135
136 test_must_fail git checkout --no-guess bar &&
137 test_must_fail git rev-parse --verify refs/heads/bar &&
138 test_branch master
139'
140
ec2764ee
JH
141test_expect_success 'setup more remotes with unconventional refspecs' '
142 git checkout -B master &&
c8cbf20c 143 status_uno_is_clean &&
ec2764ee
JH
144 git init repo_c &&
145 (
146 cd repo_c &&
147 test_commit c_master &&
148 git checkout -b bar &&
5a517b1c 149 test_commit c_bar &&
ec2764ee
JH
150 git checkout -b spam &&
151 test_commit c_spam
152 ) &&
153 git init repo_d &&
154 (
155 cd repo_d &&
156 test_commit d_master &&
157 git checkout -b baz &&
5a517b1c 158 test_commit d_baz &&
ec2764ee 159 git checkout -b eggs &&
5a517b1c 160 test_commit d_eggs
ec2764ee
JH
161 ) &&
162 git remote add repo_c repo_c &&
163 git config remote.repo_c.fetch \
164 "+refs/heads/*:refs/remotes/extra_dir/repo_c/extra_dir/*" &&
165 git remote add repo_d repo_d &&
166 git config remote.repo_d.fetch \
167 "+refs/heads/*:refs/repo_d/*" &&
168 git fetch --all
169'
170
fa83a33b 171test_expect_success 'checkout of branch from multiple remotes fails #2' '
ec2764ee 172 git checkout -B master &&
c8cbf20c 173 status_uno_is_clean &&
ec2764ee
JH
174 test_might_fail git branch -D bar &&
175
176 test_must_fail git checkout bar &&
c8cbf20c 177 status_uno_is_clean &&
ec2764ee
JH
178 test_must_fail git rev-parse --verify refs/heads/bar &&
179 test_branch master
180'
181
fa83a33b 182test_expect_success 'checkout of branch from multiple remotes fails #3' '
ec2764ee 183 git checkout -B master &&
c8cbf20c 184 status_uno_is_clean &&
ec2764ee
JH
185 test_might_fail git branch -D baz &&
186
187 test_must_fail git checkout baz &&
c8cbf20c 188 status_uno_is_clean &&
ec2764ee
JH
189 test_must_fail git rev-parse --verify refs/heads/baz &&
190 test_branch master
191'
192
fa83a33b 193test_expect_success 'checkout of branch from a single remote succeeds #3' '
ec2764ee 194 git checkout -B master &&
c8cbf20c 195 status_uno_is_clean &&
ec2764ee
JH
196 test_might_fail git branch -D spam &&
197
198 git checkout spam &&
c8cbf20c 199 status_uno_is_clean &&
ec2764ee
JH
200 test_branch spam &&
201 test_cmp_rev refs/remotes/extra_dir/repo_c/extra_dir/spam HEAD &&
202 test_branch_upstream spam repo_c spam
203'
204
41c21f22 205test_expect_success 'checkout of branch from a single remote succeeds #4' '
ec2764ee 206 git checkout -B master &&
c8cbf20c 207 status_uno_is_clean &&
ec2764ee
JH
208 test_might_fail git branch -D eggs &&
209
210 git checkout eggs &&
c8cbf20c 211 status_uno_is_clean &&
ec2764ee
JH
212 test_branch eggs &&
213 test_cmp_rev refs/repo_d/eggs HEAD &&
214 test_branch_upstream eggs repo_d eggs
215'
216
a047fafc
MM
217test_expect_success 'checkout of branch with a file having the same name fails' '
218 git checkout -B master &&
c8cbf20c 219 status_uno_is_clean &&
a047fafc
MM
220 test_might_fail git branch -D spam &&
221
222 >spam &&
223 test_must_fail git checkout spam &&
c8cbf20c 224 status_uno_is_clean &&
a047fafc
MM
225 test_must_fail git rev-parse --verify refs/heads/spam &&
226 test_branch master
227'
228
b829b943
NTND
229test_expect_success 'checkout of branch with a file in subdir having the same name fails' '
230 git checkout -B master &&
c8cbf20c 231 status_uno_is_clean &&
b829b943
NTND
232 test_might_fail git branch -D spam &&
233
234 >spam &&
235 mkdir sub &&
236 mv spam sub/spam &&
237 test_must_fail git -C sub checkout spam &&
c8cbf20c 238 status_uno_is_clean &&
b829b943
NTND
239 test_must_fail git rev-parse --verify refs/heads/spam &&
240 test_branch master
241'
242
a047fafc
MM
243test_expect_success 'checkout <branch> -- succeeds, even if a file with the same name exists' '
244 git checkout -B master &&
c8cbf20c 245 status_uno_is_clean &&
a047fafc
MM
246 test_might_fail git branch -D spam &&
247
248 >spam &&
249 git checkout spam -- &&
c8cbf20c 250 status_uno_is_clean &&
a047fafc
MM
251 test_branch spam &&
252 test_cmp_rev refs/remotes/extra_dir/repo_c/extra_dir/spam HEAD &&
253 test_branch_upstream spam repo_c spam
254'
255
05e73682
JH
256test_expect_success 'loosely defined local base branch is reported correctly' '
257
258 git checkout master &&
c8cbf20c 259 status_uno_is_clean &&
05e73682
JH
260 git branch strict &&
261 git branch loose &&
262 git commit --allow-empty -m "a bit more" &&
263
264 test_config branch.strict.remote . &&
265 test_config branch.loose.remote . &&
266 test_config branch.strict.merge refs/heads/master &&
267 test_config branch.loose.merge master &&
268
269 git checkout strict | sed -e "s/strict/BRANCHNAME/g" >expect &&
c8cbf20c 270 status_uno_is_clean &&
05e73682 271 git checkout loose | sed -e "s/loose/BRANCHNAME/g" >actual &&
c8cbf20c 272 status_uno_is_clean &&
05e73682
JH
273
274 test_cmp expect actual
275'
276
399e4a1c 277test_done