1#!/bin/sh
23
test_description='checkout <branch>
45
Ensures that checkout on an unborn branch does what the user expects'
67
. ./test-lib.sh
89
# Is the current branch "refs/heads/$1"?
10test_branch () {
11printf "%s\n" "refs/heads/$1" >expect.HEAD &&
12git symbolic-ref HEAD >actual.HEAD &&
13test_cmp expect.HEAD actual.HEAD
14}
1516
# Is branch "refs/heads/$1" set to pull from "$2/$3"?
17test_branch_upstream () {
18printf "%s\n" "$2" "refs/heads/$3" >expect.upstream &&
19{
20git config "branch.$1.remote" &&
21git config "branch.$1.merge"
22} >actual.upstream &&
23test_cmp expect.upstream actual.upstream
24}
2526
test_expect_success 'setup' '
27test_commit my_master &&
28git init repo_a &&
29(
30cd repo_a &&
31test_commit a_master &&
32git checkout -b foo &&
33test_commit a_foo &&
34git checkout -b bar &&
35test_commit a_bar
36) &&
37git init repo_b &&
38(
39cd repo_b &&
40test_commit b_master &&
41git checkout -b foo &&
42test_commit b_foo &&
43git checkout -b baz &&
44test_commit b_baz
45) &&
46git remote add repo_a repo_a &&
47git remote add repo_b repo_b &&
48git config remote.repo_b.fetch \
49"+refs/heads/*:refs/remotes/other_b/*" &&
50git fetch --all
51'
5253
test_expect_success 'checkout of non-existing branch fails' '
54git checkout -B master &&
55test_might_fail git branch -D xyzzy &&
5657
test_must_fail git checkout xyzzy &&
58test_must_fail git rev-parse --verify refs/heads/xyzzy &&
59test_branch master
60'
6162
test_expect_success 'checkout of branch from multiple remotes fails #1' '
63git checkout -B master &&
64test_might_fail git branch -D foo &&
6566
test_must_fail git checkout foo &&
67test_must_fail git rev-parse --verify refs/heads/foo &&
68test_branch master
69'
7071
test_expect_success 'checkout of branch from a single remote succeeds #1' '
72git checkout -B master &&
73test_might_fail git branch -D bar &&
7475
git checkout bar &&
76test_branch bar &&
77test_cmp_rev remotes/repo_a/bar HEAD &&
78test_branch_upstream bar repo_a bar
79'
8081
test_expect_success 'checkout of branch from a single remote succeeds #2' '
82git checkout -B master &&
83test_might_fail git branch -D baz &&
8485
git checkout baz &&
86test_branch baz &&
87test_cmp_rev remotes/other_b/baz HEAD &&
88test_branch_upstream baz repo_b baz
89'
9091
test_expect_success '--no-guess suppresses branch auto-vivification' '
92git checkout -B master &&
93test_might_fail git branch -D bar &&
9495
test_must_fail git checkout --no-guess bar &&
96test_must_fail git rev-parse --verify refs/heads/bar &&
97test_branch master
98'
99100
test_expect_success 'setup more remotes with unconventional refspecs' '
101git checkout -B master &&
102git init repo_c &&
103(
104cd repo_c &&
105test_commit c_master &&
106git checkout -b bar &&
107test_commit c_bar &&
108git checkout -b spam &&
109test_commit c_spam
110) &&
111git init repo_d &&
112(
113cd repo_d &&
114test_commit d_master &&
115git checkout -b baz &&
116test_commit d_baz &&
117git checkout -b eggs &&
118test_commit d_eggs
119) &&
120git remote add repo_c repo_c &&
121git config remote.repo_c.fetch \
122"+refs/heads/*:refs/remotes/extra_dir/repo_c/extra_dir/*" &&
123git remote add repo_d repo_d &&
124git config remote.repo_d.fetch \
125"+refs/heads/*:refs/repo_d/*" &&
126git fetch --all
127'
128129
test_expect_success 'checkout of branch from multiple remotes fails #2' '
130git checkout -B master &&
131test_might_fail git branch -D bar &&
132133
test_must_fail git checkout bar &&
134test_must_fail git rev-parse --verify refs/heads/bar &&
135test_branch master
136'
137138
test_expect_success 'checkout of branch from multiple remotes fails #3' '
139git checkout -B master &&
140test_might_fail git branch -D baz &&
141142
test_must_fail git checkout baz &&
143test_must_fail git rev-parse --verify refs/heads/baz &&
144test_branch master
145'
146147
test_expect_success 'checkout of branch from a single remote succeeds #3' '
148git checkout -B master &&
149test_might_fail git branch -D spam &&
150151
git checkout spam &&
152test_branch spam &&
153test_cmp_rev refs/remotes/extra_dir/repo_c/extra_dir/spam HEAD &&
154test_branch_upstream spam repo_c spam
155'
156157
test_expect_success 'checkout of branch from a single remote succeeds #4' '
158git checkout -B master &&
159test_might_fail git branch -D eggs &&
160161
git checkout eggs &&
162test_branch eggs &&
163test_cmp_rev refs/repo_d/eggs HEAD &&
164test_branch_upstream eggs repo_d eggs
165'
166167
test_expect_success 'checkout of branch with a file having the same name fails' '
168git checkout -B master &&
169test_might_fail git branch -D spam &&
170171
>spam &&
172test_must_fail git checkout spam &&
173test_must_fail git rev-parse --verify refs/heads/spam &&
174test_branch master
175'
176177
test_expect_success 'checkout of branch with a file in subdir having the same name fails' '
178git checkout -B master &&
179test_might_fail git branch -D spam &&
180181
>spam &&
182mkdir sub &&
183mv spam sub/spam &&
184test_must_fail git -C sub checkout spam &&
185test_must_fail git rev-parse --verify refs/heads/spam &&
186test_branch master
187'
188189
test_expect_success 'checkout <branch> -- succeeds, even if a file with the same name exists' '
190git checkout -B master &&
191test_might_fail git branch -D spam &&
192193
>spam &&
194git checkout spam -- &&
195test_branch spam &&
196test_cmp_rev refs/remotes/extra_dir/repo_c/extra_dir/spam HEAD &&
197test_branch_upstream spam repo_c spam
198'
199200
test_expect_success 'loosely defined local base branch is reported correctly' '
201202
git checkout master &&
203git branch strict &&
204git branch loose &&
205git commit --allow-empty -m "a bit more" &&
206207
test_config branch.strict.remote . &&
208test_config branch.loose.remote . &&
209test_config branch.strict.merge refs/heads/master &&
210test_config branch.loose.merge master &&
211212
git checkout strict | sed -e "s/strict/BRANCHNAME/g" >expect &&
213git checkout loose | sed -e "s/loose/BRANCHNAME/g" >actual &&
214215
test_cmp expect actual
216'
217218
test_done