Merge branch 'rj/cygwin-has-dev-tty'
[git/gitster.git] / t / t2024-checkout-dwim.sh
blob2caada3d834ffc139c0af2e3f1b0b52f9eee9718
1 #!/bin/sh
3 test_description='checkout <branch>
5 Ensures that checkout on an unborn branch does what the user expects'
7 TEST_PASSES_SANITIZE_LEAK=true
8 . ./test-lib.sh
10 # Is the current branch "refs/heads/$1"?
11 test_branch () {
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"
23 } >actual.upstream &&
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 &&
34 git init repo_a &&
36 cd repo_a &&
37 test_commit a_main &&
38 git checkout -b foo &&
39 test_commit a_foo &&
40 git checkout -b bar &&
41 test_commit a_bar &&
42 git checkout -b ambiguous_branch_and_file &&
43 test_commit a_ambiguous_branch_and_file
44 ) &&
45 git init repo_b &&
47 cd repo_b &&
48 test_commit b_main &&
49 git checkout -b foo &&
50 test_commit b_foo &&
51 git checkout -b baz &&
52 test_commit b_baz &&
53 git checkout -b ambiguous_branch_and_file &&
54 test_commit b_ambiguous_branch_and_file
55 ) &&
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/*" &&
60 git fetch --all
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 &&
70 test_branch main
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 &&
80 test_branch main
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 &&
107 test_branch main &&
108 status_uno_is_clean &&
109 test_grep "^hint: " stderr &&
110 test_must_fail git -c advice.checkoutAmbiguousRemoteBranchName=false \
111 checkout foo 2>stderr &&
112 test_branch main &&
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 &&
123 status_uno_is_clean
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 &&
133 test_branch foo &&
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 &&
142 git checkout bar &&
143 status_uno_is_clean &&
144 test_branch bar &&
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 &&
153 git checkout baz &&
154 status_uno_is_clean &&
155 test_branch baz &&
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 &&
167 test_branch main
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 &&
178 test_branch main
181 test_expect_success 'setup more remotes with unconventional refspecs' '
182 git checkout -B main &&
183 status_uno_is_clean &&
184 git init repo_c &&
186 cd repo_c &&
187 test_commit c_main &&
188 git checkout -b bar &&
189 test_commit c_bar &&
190 git checkout -b spam &&
191 test_commit c_spam
192 ) &&
193 git init repo_d &&
195 cd repo_d &&
196 test_commit d_main &&
197 git checkout -b baz &&
198 test_commit d_baz &&
199 git checkout -b eggs &&
200 test_commit d_eggs
201 ) &&
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/*" &&
208 git fetch --all
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 &&
219 test_branch main
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 &&
230 test_branch main
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 &&
238 git checkout spam &&
239 status_uno_is_clean &&
240 test_branch spam &&
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 &&
250 git checkout eggs &&
251 status_uno_is_clean &&
252 test_branch eggs &&
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 &&
262 >spam &&
263 test_must_fail git checkout spam &&
264 status_uno_is_clean &&
265 test_must_fail git rev-parse --verify refs/heads/spam &&
266 test_branch main
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 &&
274 >spam &&
275 mkdir sub &&
276 mv spam sub/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 &&
280 test_branch main
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 &&
288 >spam &&
289 git checkout spam -- &&
290 status_uno_is_clean &&
291 test_branch spam &&
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' '
298 git checkout main &&
299 status_uno_is_clean &&
300 git branch strict &&
301 git branch loose &&
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 &&
324 git add 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 -- &&
328 grep bar 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 &&
334 git add dwim-arg1 &&
335 echo bar >dwim-arg1 &&
336 git checkout -- dwim-arg1 &&
337 test_must_fail git rev-parse refs/heads/dwim-arg1 -- &&
338 grep foo 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 &&
344 git add dwim-arg2 &&
345 echo bar >dwim-arg2 &&
346 git checkout dwim-arg2 -- &&
347 git rev-parse refs/heads/dwim-arg2 -- &&
348 grep bar dwim-arg2
351 test_done