3 test_description
='checkout'
7 # Arguments: [!] <branch> <oid> [<checkout options>]
9 # Runs "git checkout" to switch to <branch>, testing that
11 # 1) we are on the specified branch, <branch>;
12 # 2) HEAD is <oid>; if <oid> is not specified, the old HEAD is used.
14 # If <checkout options> is not specified, "git checkout" is run with -b.
16 # If the first argument is `!`, "git checkout" is expected to fail when
26 exp_ref
="refs/heads/$exp_branch" &&
28 # if <oid> is not specified, use HEAD.
29 exp_oid
=${2:-$(git rev-parse --verify HEAD)} &&
31 # default options for git checkout: -b
39 if test -n "$should_fail"
41 test_must_fail git checkout
$opts $exp_branch $exp_oid
43 git checkout
$opts $exp_branch $exp_oid &&
44 echo "$exp_ref" >ref.expect
&&
45 git rev-parse
--symbolic-full-name HEAD
>ref.actual
&&
46 test_cmp ref.expect ref.actual
&&
47 echo "$exp_oid" >oid.expect
&&
48 git rev-parse
--verify HEAD
>oid.actual
&&
49 test_cmp oid.expect oid.actual
53 test_dirty_unmergeable
() {
54 test_expect_code
1 git
diff --exit-code
57 test_dirty_unmergeable_discards_changes
() {
61 setup_dirty_unmergeable
() {
65 test_dirty_mergeable
() {
66 test_expect_code
1 git
diff --cached --exit-code
69 test_dirty_mergeable_discards_changes
() {
70 git
diff --cached --exit-code
73 setup_dirty_mergeable
() {
78 test_expect_success
'setup' '
79 test_commit initial file1 &&
80 HEAD1=$(git rev-parse --verify HEAD) &&
82 test_commit change1 file1 &&
83 HEAD2=$(git rev-parse --verify HEAD) &&
88 test_expect_success
'checkout a branch without refs/heads/* prefix' '
89 git clone --no-tags . repo-odd-prefix &&
93 origin=$(git symbolic-ref refs/remotes/origin/HEAD) &&
94 git symbolic-ref refs/heads/a-branch "$origin" &&
96 git checkout -f a-branch &&
97 git checkout -f a-branch
101 test_expect_success
'checkout -b to a new branch, set to HEAD' '
103 git checkout branch1 &&
104 test_might_fail git branch -D branch2" &&
108 test_expect_success
'checkout -b to a merge base' '
110 git checkout branch1 &&
111 test_might_fail git branch -D branch2" &&
112 git checkout -b branch2 branch1...
115 test_expect_success
'checkout -b to a new branch, set to an explicit ref' '
117 git checkout branch1 &&
118 test_might_fail git branch -D branch2" &&
119 do_checkout branch2 $HEAD1
122 test_expect_success
'checkout -b to a new branch with unmergeable changes fails' '
123 setup_dirty_unmergeable &&
124 do_checkout ! branch2 $HEAD1 &&
125 test_dirty_unmergeable
128 test_expect_success
'checkout -f -b to a new branch with unmergeable changes discards changes' '
130 git checkout branch1 &&
131 test_might_fail git branch -D branch2" &&
133 # still dirty and on branch1
134 do_checkout branch2 $HEAD1 "-f -b" &&
135 test_dirty_unmergeable_discards_changes
138 test_expect_success
'checkout -b to a new branch preserves mergeable changes' '
141 git checkout branch1 &&
142 test_might_fail git branch -D branch2" &&
144 setup_dirty_mergeable &&
145 do_checkout branch2 $HEAD1 &&
149 test_expect_success
'checkout -f -b to a new branch with mergeable changes discards changes' '
150 test_when_finished git reset --hard HEAD &&
151 setup_dirty_mergeable &&
152 do_checkout branch2 $HEAD1 "-f -b" &&
153 test_dirty_mergeable_discards_changes
156 test_expect_success
'checkout -b to an existing branch fails' '
157 test_when_finished git reset --hard HEAD &&
158 do_checkout ! branch2 $HEAD2
161 test_expect_success
'checkout -b to @{-1} fails with the right branch name' '
162 git checkout branch1 &&
163 git checkout branch2 &&
164 echo >expect "fatal: a branch named '\''branch1'\'' already exists" &&
165 test_must_fail git checkout -b @{-1} 2>actual &&
166 test_cmp expect actual
169 test_expect_success
'checkout -B to an existing branch resets branch to HEAD' '
170 git checkout branch1 &&
172 do_checkout branch2 "" -B
175 test_expect_success
'checkout -B to a merge base' '
176 git checkout branch1 &&
178 git checkout -B branch2 branch1...
181 test_expect_success
'checkout -B to an existing branch from detached HEAD resets branch to HEAD' '
182 head=$(git rev-parse --verify HEAD) &&
183 git checkout "$head" &&
185 do_checkout branch2 "" -B
188 test_expect_success
'checkout -B to an existing branch with an explicit ref resets branch to that ref' '
189 git checkout branch1 &&
191 do_checkout branch2 $HEAD1 -B
194 test_expect_success
'checkout -B to an existing branch with unmergeable changes fails' '
195 git checkout branch1 &&
197 setup_dirty_unmergeable &&
198 do_checkout ! branch2 $HEAD1 -B &&
199 test_dirty_unmergeable
202 test_expect_success
'checkout -f -B to an existing branch with unmergeable changes discards changes' '
203 # still dirty and on branch1
204 do_checkout branch2 $HEAD1 "-f -B" &&
205 test_dirty_unmergeable_discards_changes
208 test_expect_success
'checkout -B to an existing branch preserves mergeable changes' '
209 test_when_finished git reset --hard &&
210 git checkout branch1 &&
212 setup_dirty_mergeable &&
213 do_checkout branch2 $HEAD1 -B &&
217 test_expect_success
'checkout -f -B to an existing branch with mergeable changes discards changes' '
218 git checkout branch1 &&
220 setup_dirty_mergeable &&
221 do_checkout branch2 $HEAD1 "-f -B" &&
222 test_dirty_mergeable_discards_changes
225 test_expect_success
'checkout -b <describe>' '
226 git tag -f -m "First commit" initial initial &&
227 git checkout -f change1 &&
228 name=$(git describe) &&
229 git checkout -b $name &&
230 git diff --exit-code change1 &&
231 echo "refs/heads/$name" >expect &&
232 git symbolic-ref HEAD >actual &&
233 test_cmp expect actual
236 test_expect_success
'checkout -B to the current branch works' '
237 git checkout branch1 &&
238 git checkout -B branch1-scratch &&
240 setup_dirty_mergeable &&
241 git checkout -B branch1-scratch initial &&
245 test_expect_success
'checkout -b after clone --no-checkout does a checkout of HEAD' '
247 test_commit -C src a &&
248 rev="$(git -C src rev-parse HEAD)" &&
249 git clone --no-checkout src dest &&
250 git -C dest checkout "$rev" -b branch &&
251 test_path_is_file dest/a.t
254 test_expect_success
'checkout -b to a new branch preserves mergeable changes despite sparse-checkout' '
257 git checkout branch1-scratch &&
258 test_might_fail git branch -D branch3 &&
259 git config core.sparseCheckout false &&
260 rm .git/info/sparse-checkout" &&
264 echo stuff >>file1 &&
265 echo file2 >.git/info/sparse-checkout &&
266 git config core.sparseCheckout true &&
268 CURHEAD=$(git rev-parse HEAD) &&
269 do_checkout branch3 $CURHEAD &&
271 echo file1 >expect &&
272 git diff --name-only >actual &&
273 test_cmp expect actual
276 test_expect_success
'checkout -b rejects an invalid start point' '
277 test_must_fail git checkout -b branch4 file1 2>err &&
278 test_i18ngrep "is not a commit" err
281 test_expect_success
'checkout -b rejects an extra path argument' '
282 test_must_fail git checkout -b branch5 branch1 file1 2>err &&
283 test_i18ngrep "Cannot update paths and switch to branch" err