]> git.ipfire.org Git - thirdparty/git.git/blame - t/t2024-checkout-dwim.sh
git-prompt: change == to = for zsh's sake
[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 26status_uno_is_clean () {
c8cbf20c 27 git status -uno --porcelain >status.actual &&
9f4bcf81 28 test_must_be_empty status.actual
c8cbf20c
ÆAB
29}
30
399e4a1c 31test_expect_success 'setup' '
ec2764ee 32 test_commit my_master &&
399e4a1c
JH
33 git init repo_a &&
34 (
35 cd repo_a &&
36 test_commit a_master &&
37 git checkout -b foo &&
38 test_commit a_foo &&
39 git checkout -b bar &&
fa74180d
AM
40 test_commit a_bar &&
41 git checkout -b ambiguous_branch_and_file &&
42 test_commit a_ambiguous_branch_and_file
399e4a1c
JH
43 ) &&
44 git init repo_b &&
45 (
46 cd repo_b &&
47 test_commit b_master &&
48 git checkout -b foo &&
49 test_commit b_foo &&
50 git checkout -b baz &&
fa74180d
AM
51 test_commit b_baz &&
52 git checkout -b ambiguous_branch_and_file &&
53 test_commit b_ambiguous_branch_and_file
399e4a1c
JH
54 ) &&
55 git remote add repo_a repo_a &&
56 git remote add repo_b repo_b &&
57 git config remote.repo_b.fetch \
58 "+refs/heads/*:refs/remotes/other_b/*" &&
59 git fetch --all
60'
61
62test_expect_success 'checkout of non-existing branch fails' '
63 git checkout -B master &&
64 test_might_fail git branch -D xyzzy &&
65
66 test_must_fail git checkout xyzzy &&
c8cbf20c 67 status_uno_is_clean &&
399e4a1c
JH
68 test_must_fail git rev-parse --verify refs/heads/xyzzy &&
69 test_branch master
70'
71
ec2764ee 72test_expect_success 'checkout of branch from multiple remotes fails #1' '
399e4a1c
JH
73 git checkout -B master &&
74 test_might_fail git branch -D foo &&
75
76 test_must_fail git checkout foo &&
c8cbf20c 77 status_uno_is_clean &&
399e4a1c
JH
78 test_must_fail git rev-parse --verify refs/heads/foo &&
79 test_branch master
80'
81
fa74180d
AM
82test_expect_success 'when arg matches multiple remotes, do not fallback to interpreting as pathspec' '
83 # create a file with name matching remote branch name
84 git checkout -b t_ambiguous_branch_and_file &&
85 >ambiguous_branch_and_file &&
86 git add ambiguous_branch_and_file &&
87 git commit -m "ambiguous_branch_and_file" &&
88
89 # modify file to verify that it will not be touched by checkout
90 test_when_finished "git checkout -- ambiguous_branch_and_file" &&
91 echo "file contents" >ambiguous_branch_and_file &&
92 cp ambiguous_branch_and_file expect &&
93
94 test_must_fail git checkout ambiguous_branch_and_file 2>err &&
95
96 test_i18ngrep "matched multiple (2) remote tracking branches" err &&
97
98 # file must not be altered
99 test_cmp expect ambiguous_branch_and_file
100'
101
ad8d5104
ÆAB
102test_expect_success 'checkout of branch from multiple remotes fails with advice' '
103 git checkout -B master &&
104 test_might_fail git branch -D foo &&
105 test_must_fail git checkout foo 2>stderr &&
106 test_branch master &&
107 status_uno_is_clean &&
108 test_i18ngrep "^hint: " stderr &&
109 test_must_fail git -c advice.checkoutAmbiguousRemoteBranchName=false \
110 checkout foo 2>stderr &&
111 test_branch master &&
112 status_uno_is_clean &&
3338e995
ÆAB
113 test_i18ngrep ! "^hint: " stderr
114'
115
116test_expect_success PERL 'checkout -p with multiple remotes does not print advice' '
117 git checkout -B master &&
118 test_might_fail git branch -D foo &&
119
8d7b558b
ÆAB
120 git checkout -p foo 2>stderr &&
121 test_i18ngrep ! "^hint: " stderr &&
122 status_uno_is_clean
123'
124
125test_expect_success 'checkout of branch from multiple remotes succeeds with checkout.defaultRemote #1' '
126 git checkout -B master &&
127 status_uno_is_clean &&
128 test_might_fail git branch -D foo &&
129
130 git -c checkout.defaultRemote=repo_a checkout foo &&
131 status_uno_is_clean &&
132 test_branch foo &&
133 test_cmp_rev remotes/repo_a/foo HEAD &&
134 test_branch_upstream foo repo_a foo
ad8d5104
ÆAB
135'
136
399e4a1c
JH
137test_expect_success 'checkout of branch from a single remote succeeds #1' '
138 git checkout -B master &&
139 test_might_fail git branch -D bar &&
140
141 git checkout bar &&
c8cbf20c 142 status_uno_is_clean &&
399e4a1c
JH
143 test_branch bar &&
144 test_cmp_rev remotes/repo_a/bar HEAD &&
145 test_branch_upstream bar repo_a bar
146'
147
148test_expect_success 'checkout of branch from a single remote succeeds #2' '
149 git checkout -B master &&
150 test_might_fail git branch -D baz &&
151
152 git checkout baz &&
c8cbf20c 153 status_uno_is_clean &&
399e4a1c
JH
154 test_branch baz &&
155 test_cmp_rev remotes/other_b/baz HEAD &&
156 test_branch_upstream baz repo_b baz
157'
158
159test_expect_success '--no-guess suppresses branch auto-vivification' '
160 git checkout -B master &&
c8cbf20c 161 status_uno_is_clean &&
399e4a1c
JH
162 test_might_fail git branch -D bar &&
163
164 test_must_fail git checkout --no-guess bar &&
165 test_must_fail git rev-parse --verify refs/heads/bar &&
166 test_branch master
167'
168
ec2764ee
JH
169test_expect_success 'setup more remotes with unconventional refspecs' '
170 git checkout -B master &&
c8cbf20c 171 status_uno_is_clean &&
ec2764ee
JH
172 git init repo_c &&
173 (
174 cd repo_c &&
175 test_commit c_master &&
176 git checkout -b bar &&
5a517b1c 177 test_commit c_bar &&
ec2764ee
JH
178 git checkout -b spam &&
179 test_commit c_spam
180 ) &&
181 git init repo_d &&
182 (
183 cd repo_d &&
184 test_commit d_master &&
185 git checkout -b baz &&
5a517b1c 186 test_commit d_baz &&
ec2764ee 187 git checkout -b eggs &&
5a517b1c 188 test_commit d_eggs
ec2764ee
JH
189 ) &&
190 git remote add repo_c repo_c &&
191 git config remote.repo_c.fetch \
192 "+refs/heads/*:refs/remotes/extra_dir/repo_c/extra_dir/*" &&
193 git remote add repo_d repo_d &&
194 git config remote.repo_d.fetch \
195 "+refs/heads/*:refs/repo_d/*" &&
196 git fetch --all
197'
198
fa83a33b 199test_expect_success 'checkout of branch from multiple remotes fails #2' '
ec2764ee 200 git checkout -B master &&
c8cbf20c 201 status_uno_is_clean &&
ec2764ee
JH
202 test_might_fail git branch -D bar &&
203
204 test_must_fail git checkout bar &&
c8cbf20c 205 status_uno_is_clean &&
ec2764ee
JH
206 test_must_fail git rev-parse --verify refs/heads/bar &&
207 test_branch master
208'
209
fa83a33b 210test_expect_success 'checkout of branch from multiple remotes fails #3' '
ec2764ee 211 git checkout -B master &&
c8cbf20c 212 status_uno_is_clean &&
ec2764ee
JH
213 test_might_fail git branch -D baz &&
214
215 test_must_fail git checkout baz &&
c8cbf20c 216 status_uno_is_clean &&
ec2764ee
JH
217 test_must_fail git rev-parse --verify refs/heads/baz &&
218 test_branch master
219'
220
fa83a33b 221test_expect_success 'checkout of branch from a single remote succeeds #3' '
ec2764ee 222 git checkout -B master &&
c8cbf20c 223 status_uno_is_clean &&
ec2764ee
JH
224 test_might_fail git branch -D spam &&
225
226 git checkout spam &&
c8cbf20c 227 status_uno_is_clean &&
ec2764ee
JH
228 test_branch spam &&
229 test_cmp_rev refs/remotes/extra_dir/repo_c/extra_dir/spam HEAD &&
230 test_branch_upstream spam repo_c spam
231'
232
41c21f22 233test_expect_success 'checkout of branch from a single remote succeeds #4' '
ec2764ee 234 git checkout -B master &&
c8cbf20c 235 status_uno_is_clean &&
ec2764ee
JH
236 test_might_fail git branch -D eggs &&
237
238 git checkout eggs &&
c8cbf20c 239 status_uno_is_clean &&
ec2764ee
JH
240 test_branch eggs &&
241 test_cmp_rev refs/repo_d/eggs HEAD &&
242 test_branch_upstream eggs repo_d eggs
243'
244
a047fafc
MM
245test_expect_success 'checkout of branch with a file having the same name fails' '
246 git checkout -B master &&
c8cbf20c 247 status_uno_is_clean &&
a047fafc
MM
248 test_might_fail git branch -D spam &&
249
250 >spam &&
251 test_must_fail git checkout spam &&
c8cbf20c 252 status_uno_is_clean &&
a047fafc
MM
253 test_must_fail git rev-parse --verify refs/heads/spam &&
254 test_branch master
255'
256
b829b943
NTND
257test_expect_success 'checkout of branch with a file in subdir having the same name fails' '
258 git checkout -B master &&
c8cbf20c 259 status_uno_is_clean &&
b829b943
NTND
260 test_might_fail git branch -D spam &&
261
262 >spam &&
263 mkdir sub &&
264 mv spam sub/spam &&
265 test_must_fail git -C sub checkout spam &&
c8cbf20c 266 status_uno_is_clean &&
b829b943
NTND
267 test_must_fail git rev-parse --verify refs/heads/spam &&
268 test_branch master
269'
270
a047fafc
MM
271test_expect_success 'checkout <branch> -- succeeds, even if a file with the same name exists' '
272 git checkout -B master &&
c8cbf20c 273 status_uno_is_clean &&
a047fafc
MM
274 test_might_fail git branch -D spam &&
275
276 >spam &&
277 git checkout spam -- &&
c8cbf20c 278 status_uno_is_clean &&
a047fafc
MM
279 test_branch spam &&
280 test_cmp_rev refs/remotes/extra_dir/repo_c/extra_dir/spam HEAD &&
281 test_branch_upstream spam repo_c spam
282'
283
05e73682
JH
284test_expect_success 'loosely defined local base branch is reported correctly' '
285
286 git checkout master &&
c8cbf20c 287 status_uno_is_clean &&
05e73682
JH
288 git branch strict &&
289 git branch loose &&
290 git commit --allow-empty -m "a bit more" &&
291
292 test_config branch.strict.remote . &&
293 test_config branch.loose.remote . &&
294 test_config branch.strict.merge refs/heads/master &&
295 test_config branch.loose.merge master &&
296
297 git checkout strict | sed -e "s/strict/BRANCHNAME/g" >expect &&
c8cbf20c 298 status_uno_is_clean &&
05e73682 299 git checkout loose | sed -e "s/loose/BRANCHNAME/g" >actual &&
c8cbf20c 300 status_uno_is_clean &&
05e73682
JH
301
302 test_cmp expect actual
303'
304
be4908f1
NTND
305test_expect_success 'reject when arg could be part of dwim branch' '
306 git remote add foo file://non-existent-place &&
307 git update-ref refs/remotes/foo/dwim-arg HEAD &&
308 echo foo >dwim-arg &&
309 git add dwim-arg &&
310 echo bar >dwim-arg &&
311 test_must_fail git checkout dwim-arg &&
312 test_must_fail git rev-parse refs/heads/dwim-arg -- &&
313 grep bar dwim-arg
314'
315
316test_expect_success 'disambiguate dwim branch and checkout path (1)' '
317 git update-ref refs/remotes/foo/dwim-arg1 HEAD &&
318 echo foo >dwim-arg1 &&
319 git add dwim-arg1 &&
320 echo bar >dwim-arg1 &&
321 git checkout -- dwim-arg1 &&
322 test_must_fail git rev-parse refs/heads/dwim-arg1 -- &&
323 grep foo dwim-arg1
324'
325
326test_expect_success 'disambiguate dwim branch and checkout path (2)' '
327 git update-ref refs/remotes/foo/dwim-arg2 HEAD &&
328 echo foo >dwim-arg2 &&
329 git add dwim-arg2 &&
330 echo bar >dwim-arg2 &&
331 git checkout dwim-arg2 -- &&
332 git rev-parse refs/heads/dwim-arg2 -- &&
333 grep bar dwim-arg2
334'
335
399e4a1c 336test_done