3 test_description
='checkout <branch>
5 Ensures that checkout on an unborn branch does what the user expects'
9 # Is the current branch "refs/heads/$1"?
11 printf "%s\n" "refs/heads/$1" >expect.HEAD
&&
12 git symbolic-ref HEAD
>actual.HEAD
&&
13 test_cmp expect.HEAD actual.HEAD
16 # Is branch "refs/heads/$1" set to pull from "$2/$3"?
17 test_branch_upstream
() {
18 printf "%s\n" "$2" "refs/heads/$3" >expect.upstream
&&
20 git config
"branch.$1.remote" &&
21 git config
"branch.$1.merge"
23 test_cmp expect.upstream actual.upstream
26 test_expect_success
'setup' '
27 test_commit my_master &&
31 test_commit a_master &&
32 git checkout -b foo &&
34 git checkout -b bar &&
40 test_commit b_master &&
41 git checkout -b foo &&
43 git checkout -b baz &&
46 git remote add repo_a repo_a &&
47 git remote add repo_b repo_b &&
48 git config remote.repo_b.fetch \
49 "+refs/heads/*:refs/remotes/other_b/*" &&
53 test_expect_success
'checkout of non-existing branch fails' '
54 git checkout -B master &&
55 test_might_fail git branch -D xyzzy &&
57 test_must_fail git checkout xyzzy &&
58 test_must_fail git rev-parse --verify refs/heads/xyzzy &&
62 test_expect_success
'checkout of branch from multiple remotes fails #1' '
63 git checkout -B master &&
64 test_might_fail git branch -D foo &&
66 test_must_fail git checkout foo &&
67 test_must_fail git rev-parse --verify refs/heads/foo &&
71 test_expect_success
'checkout of branch from a single remote succeeds #1' '
72 git checkout -B master &&
73 test_might_fail git branch -D bar &&
77 test_cmp_rev remotes/repo_a/bar HEAD &&
78 test_branch_upstream bar repo_a bar
81 test_expect_success
'checkout of branch from a single remote succeeds #2' '
82 git checkout -B master &&
83 test_might_fail git branch -D baz &&
87 test_cmp_rev remotes/other_b/baz HEAD &&
88 test_branch_upstream baz repo_b baz
91 test_expect_success
'--no-guess suppresses branch auto-vivification' '
92 git checkout -B master &&
93 test_might_fail git branch -D bar &&
95 test_must_fail git checkout --no-guess bar &&
96 test_must_fail git rev-parse --verify refs/heads/bar &&
100 test_expect_success
'setup more remotes with unconventional refspecs' '
101 git checkout -B master &&
105 test_commit c_master &&
106 git checkout -b bar &&
108 git checkout -b spam &&
114 test_commit d_master &&
115 git checkout -b baz &&
117 git checkout -b eggs &&
120 git remote add repo_c repo_c &&
121 git config remote.repo_c.fetch \
122 "+refs/heads/*:refs/remotes/extra_dir/repo_c/extra_dir/*" &&
123 git remote add repo_d repo_d &&
124 git config remote.repo_d.fetch \
125 "+refs/heads/*:refs/repo_d/*" &&
129 test_expect_success
'checkout of branch from multiple remotes fails #2' '
130 git checkout -B master &&
131 test_might_fail git branch -D bar &&
133 test_must_fail git checkout bar &&
134 test_must_fail git rev-parse --verify refs/heads/bar &&
138 test_expect_success
'checkout of branch from multiple remotes fails #3' '
139 git checkout -B master &&
140 test_might_fail git branch -D baz &&
142 test_must_fail git checkout baz &&
143 test_must_fail git rev-parse --verify refs/heads/baz &&
147 test_expect_success
'checkout of branch from a single remote succeeds #3' '
148 git checkout -B master &&
149 test_might_fail git branch -D spam &&
153 test_cmp_rev refs/remotes/extra_dir/repo_c/extra_dir/spam HEAD &&
154 test_branch_upstream spam repo_c spam
157 test_expect_success
'checkout of branch from a single remote succeeds #4' '
158 git checkout -B master &&
159 test_might_fail git branch -D eggs &&
163 test_cmp_rev refs/repo_d/eggs HEAD &&
164 test_branch_upstream eggs repo_d eggs
167 test_expect_success
'checkout of branch with a file having the same name fails' '
168 git checkout -B master &&
169 test_might_fail git branch -D spam &&
172 test_must_fail git checkout spam &&
173 test_must_fail git rev-parse --verify refs/heads/spam &&
177 test_expect_success
'checkout <branch> -- succeeds, even if a file with the same name exists' '
178 git checkout -B master &&
179 test_might_fail git branch -D spam &&
182 git checkout spam -- &&
184 test_cmp_rev refs/remotes/extra_dir/repo_c/extra_dir/spam HEAD &&
185 test_branch_upstream spam repo_c spam
188 test_expect_success
'loosely defined local base branch is reported correctly' '
190 git checkout master &&
193 git commit --allow-empty -m "a bit more" &&
195 test_config branch.strict.remote . &&
196 test_config branch.loose.remote . &&
197 test_config branch.strict.merge refs/heads/master &&
198 test_config branch.loose.merge master &&
200 git checkout strict | sed -e "s/strict/BRANCHNAME/g" >expect &&
201 git checkout loose | sed -e "s/loose/BRANCHNAME/g" >actual &&
203 test_cmp expect actual