3 test_description
='checkout <branch>
5 Ensures that checkout on an unborn branch does what the user expects'
7 TEST_PASSES_SANITIZE_LEAK
=true
10 # Is the current branch "refs/heads/$1"?
12 printf "%s\n" "refs/heads/$1" >expect.HEAD
&&
13 git symbolic-ref HEAD
>actual.HEAD
&&
14 test_cmp expect.HEAD actual.HEAD
17 # Is branch "refs/heads/$1" set to pull from "$2/$3"?
18 test_branch_upstream
() {
19 printf "%s\n" "$2" "refs/heads/$3" >expect.upstream
&&
21 git config
"branch.$1.remote" &&
22 git config
"branch.$1.merge"
24 test_cmp expect.upstream actual.upstream
27 status_uno_is_clean
() {
28 git status
-uno --porcelain >status.actual
&&
29 test_must_be_empty status.actual
32 test_expect_success
'setup' '
33 test_commit my_main &&
38 git checkout -b foo &&
40 git checkout -b bar &&
42 git checkout -b ambiguous_branch_and_file &&
43 test_commit a_ambiguous_branch_and_file
49 git checkout -b foo &&
51 git checkout -b baz &&
53 git checkout -b ambiguous_branch_and_file &&
54 test_commit b_ambiguous_branch_and_file
56 git remote add repo_a repo_a &&
57 git remote add repo_b repo_b &&
58 git config remote.repo_b.fetch \
59 "+refs/heads/*:refs/remotes/other_b/*" &&
63 test_expect_success
'checkout of non-existing branch fails' '
64 git checkout -B main &&
65 test_might_fail git branch -D xyzzy &&
67 test_must_fail git checkout xyzzy &&
68 status_uno_is_clean &&
69 test_must_fail git rev-parse --verify refs/heads/xyzzy &&
73 test_expect_success
'checkout of branch from multiple remotes fails #1' '
74 git checkout -B main &&
75 test_might_fail git branch -D foo &&
77 test_must_fail git checkout foo &&
78 status_uno_is_clean &&
79 test_must_fail git rev-parse --verify refs/heads/foo &&
83 test_expect_success
'when arg matches multiple remotes, do not fallback to interpreting as pathspec' '
84 # create a file with name matching remote branch name
85 git checkout -b t_ambiguous_branch_and_file &&
86 >ambiguous_branch_and_file &&
87 git add ambiguous_branch_and_file &&
88 git commit -m "ambiguous_branch_and_file" &&
90 # modify file to verify that it will not be touched by checkout
91 test_when_finished "git checkout -- ambiguous_branch_and_file" &&
92 echo "file contents" >ambiguous_branch_and_file &&
93 cp ambiguous_branch_and_file expect &&
95 test_must_fail git checkout ambiguous_branch_and_file 2>err &&
97 test_grep "matched multiple (2) remote tracking branches" err &&
99 # file must not be altered
100 test_cmp expect ambiguous_branch_and_file
103 test_expect_success
'checkout of branch from multiple remotes fails with advice' '
104 git checkout -B main &&
105 test_might_fail git branch -D foo &&
106 test_must_fail git checkout foo 2>stderr &&
108 status_uno_is_clean &&
109 test_grep "^hint: " stderr &&
110 test_must_fail git -c advice.checkoutAmbiguousRemoteBranchName=false \
111 checkout foo 2>stderr &&
113 status_uno_is_clean &&
114 test_grep ! "^hint: " stderr
117 test_expect_success
'checkout -p with multiple remotes does not print advice' '
118 git checkout -B main &&
119 test_might_fail git branch -D foo &&
121 git checkout -p foo 2>stderr &&
122 test_grep ! "^hint: " stderr &&
126 test_expect_success
'checkout of branch from multiple remotes succeeds with checkout.defaultRemote #1' '
127 git checkout -B main &&
128 status_uno_is_clean &&
129 test_might_fail git branch -D foo &&
131 git -c checkout.defaultRemote=repo_a checkout foo &&
132 status_uno_is_clean &&
134 test_cmp_rev remotes/repo_a/foo HEAD &&
135 test_branch_upstream foo repo_a foo
138 test_expect_success
'checkout of branch from a single remote succeeds #1' '
139 git checkout -B main &&
140 test_might_fail git branch -D bar &&
143 status_uno_is_clean &&
145 test_cmp_rev remotes/repo_a/bar HEAD &&
146 test_branch_upstream bar repo_a bar
149 test_expect_success
'checkout of branch from a single remote succeeds #2' '
150 git checkout -B main &&
151 test_might_fail git branch -D baz &&
154 status_uno_is_clean &&
156 test_cmp_rev remotes/other_b/baz HEAD &&
157 test_branch_upstream baz repo_b baz
160 test_expect_success
'--no-guess suppresses branch auto-vivification' '
161 git checkout -B main &&
162 status_uno_is_clean &&
163 test_might_fail git branch -D bar &&
165 test_must_fail git checkout --no-guess bar &&
166 test_must_fail git rev-parse --verify refs/heads/bar &&
170 test_expect_success
'checkout.guess = false suppresses branch auto-vivification' '
171 git checkout -B main &&
172 status_uno_is_clean &&
173 test_might_fail git branch -D bar &&
175 test_config checkout.guess false &&
176 test_must_fail git checkout bar &&
177 test_must_fail git rev-parse --verify refs/heads/bar &&
181 test_expect_success
'setup more remotes with unconventional refspecs' '
182 git checkout -B main &&
183 status_uno_is_clean &&
187 test_commit c_main &&
188 git checkout -b bar &&
190 git checkout -b spam &&
196 test_commit d_main &&
197 git checkout -b baz &&
199 git checkout -b eggs &&
202 git remote add repo_c repo_c &&
203 git config remote.repo_c.fetch \
204 "+refs/heads/*:refs/remotes/extra_dir/repo_c/extra_dir/*" &&
205 git remote add repo_d repo_d &&
206 git config remote.repo_d.fetch \
207 "+refs/heads/*:refs/repo_d/*" &&
211 test_expect_success
'checkout of branch from multiple remotes fails #2' '
212 git checkout -B main &&
213 status_uno_is_clean &&
214 test_might_fail git branch -D bar &&
216 test_must_fail git checkout bar &&
217 status_uno_is_clean &&
218 test_must_fail git rev-parse --verify refs/heads/bar &&
222 test_expect_success
'checkout of branch from multiple remotes fails #3' '
223 git checkout -B main &&
224 status_uno_is_clean &&
225 test_might_fail git branch -D baz &&
227 test_must_fail git checkout baz &&
228 status_uno_is_clean &&
229 test_must_fail git rev-parse --verify refs/heads/baz &&
233 test_expect_success
'checkout of branch from a single remote succeeds #3' '
234 git checkout -B main &&
235 status_uno_is_clean &&
236 test_might_fail git branch -D spam &&
239 status_uno_is_clean &&
241 test_cmp_rev refs/remotes/extra_dir/repo_c/extra_dir/spam HEAD &&
242 test_branch_upstream spam repo_c spam
245 test_expect_success
'checkout of branch from a single remote succeeds #4' '
246 git checkout -B main &&
247 status_uno_is_clean &&
248 test_might_fail git branch -D eggs &&
251 status_uno_is_clean &&
253 test_cmp_rev refs/repo_d/eggs HEAD &&
254 test_branch_upstream eggs repo_d eggs
257 test_expect_success
'checkout of branch with a file having the same name fails' '
258 git checkout -B main &&
259 status_uno_is_clean &&
260 test_might_fail git branch -D spam &&
263 test_must_fail git checkout spam &&
264 status_uno_is_clean &&
265 test_must_fail git rev-parse --verify refs/heads/spam &&
269 test_expect_success
'checkout of branch with a file in subdir having the same name fails' '
270 git checkout -B main &&
271 status_uno_is_clean &&
272 test_might_fail git branch -D spam &&
277 test_must_fail git -C sub checkout spam &&
278 status_uno_is_clean &&
279 test_must_fail git rev-parse --verify refs/heads/spam &&
283 test_expect_success
'checkout <branch> -- succeeds, even if a file with the same name exists' '
284 git checkout -B main &&
285 status_uno_is_clean &&
286 test_might_fail git branch -D spam &&
289 git checkout spam -- &&
290 status_uno_is_clean &&
292 test_cmp_rev refs/remotes/extra_dir/repo_c/extra_dir/spam HEAD &&
293 test_branch_upstream spam repo_c spam
296 test_expect_success
'loosely defined local base branch is reported correctly' '
299 status_uno_is_clean &&
302 git commit --allow-empty -m "a bit more" &&
304 test_config branch.strict.remote . &&
305 test_config branch.loose.remote . &&
306 test_config branch.strict.merge refs/heads/main &&
307 test_config branch.loose.merge main &&
309 git checkout strict >expect.raw 2>&1 &&
310 sed -e "s/strict/BRANCHNAME/g" <expect.raw >expect &&
311 status_uno_is_clean &&
312 git checkout loose >actual.raw 2>&1 &&
313 sed -e "s/loose/BRANCHNAME/g" <actual.raw >actual &&
314 status_uno_is_clean &&
315 grep BRANCHNAME actual &&
317 test_cmp expect actual
320 test_expect_success
'reject when arg could be part of dwim branch' '
321 git remote add foo file://non-existent-place &&
322 git update-ref refs/remotes/foo/dwim-arg HEAD &&
323 echo foo >dwim-arg &&
325 echo bar >dwim-arg &&
326 test_must_fail git checkout dwim-arg &&
327 test_must_fail git rev-parse refs/heads/dwim-arg -- &&
331 test_expect_success
'disambiguate dwim branch and checkout path (1)' '
332 git update-ref refs/remotes/foo/dwim-arg1 HEAD &&
333 echo foo >dwim-arg1 &&
335 echo bar >dwim-arg1 &&
336 git checkout -- dwim-arg1 &&
337 test_must_fail git rev-parse refs/heads/dwim-arg1 -- &&
341 test_expect_success
'disambiguate dwim branch and checkout path (2)' '
342 git update-ref refs/remotes/foo/dwim-arg2 HEAD &&
343 echo foo >dwim-arg2 &&
345 echo bar >dwim-arg2 &&
346 git checkout dwim-arg2 -- &&
347 git rev-parse refs/heads/dwim-arg2 -- &&