3 test_description
='checkout'
5 TEST_CREATE_REPO_NO_TEMPLATE
=1
8 # Arguments: [!] <branch> <oid> [<checkout options>]
10 # Runs "git checkout" to switch to <branch>, testing that
12 # 1) we are on the specified branch, <branch>;
13 # 2) HEAD is <oid>; if <oid> is not specified, the old HEAD is used.
15 # If <checkout options> is not specified, "git checkout" is run with -b.
17 # If the first argument is `!`, "git checkout" is expected to fail when
27 exp_ref
="refs/heads/$exp_branch" &&
29 # if <oid> is not specified, use HEAD.
30 exp_oid
=${2:-$(git rev-parse --verify HEAD)} &&
32 # default options for git checkout: -b
40 if test -n "$should_fail"
42 test_must_fail git checkout
$opts $exp_branch $exp_oid
44 git checkout
$opts $exp_branch $exp_oid &&
45 echo "$exp_ref" >ref.expect
&&
46 git rev-parse
--symbolic-full-name HEAD
>ref.actual
&&
47 test_cmp ref.expect ref.actual
&&
48 echo "$exp_oid" >oid.expect
&&
49 git rev-parse
--verify HEAD
>oid.actual
&&
50 test_cmp oid.expect oid.actual
54 test_dirty_unmergeable
() {
55 test_expect_code
1 git
diff --exit-code
58 test_dirty_unmergeable_discards_changes
() {
62 setup_dirty_unmergeable
() {
66 test_dirty_mergeable
() {
67 test_expect_code
1 git
diff --cached --exit-code
70 test_dirty_mergeable_discards_changes
() {
71 git
diff --cached --exit-code
74 setup_dirty_mergeable
() {
79 test_expect_success
'setup' '
80 test_commit initial file1 &&
81 HEAD1=$(git rev-parse --verify HEAD) &&
83 test_commit change1 file1 &&
84 HEAD2=$(git rev-parse --verify HEAD) &&
89 test_expect_success
'checkout a branch without refs/heads/* prefix' '
90 git clone --no-tags . repo-odd-prefix &&
94 origin=$(git symbolic-ref refs/remotes/origin/HEAD) &&
95 git symbolic-ref refs/heads/a-branch "$origin" &&
97 git checkout -f a-branch &&
98 git checkout -f a-branch
102 test_expect_success
'checkout -b to a new branch, set to HEAD' '
104 git checkout branch1 &&
105 test_might_fail git branch -D branch2" &&
109 test_expect_success
'checkout -b to a merge base' '
111 git checkout branch1 &&
112 test_might_fail git branch -D branch2" &&
113 git checkout -b branch2 branch1...
116 test_expect_success
'checkout -b to a new branch, set to an explicit ref' '
118 git checkout branch1 &&
119 test_might_fail git branch -D branch2" &&
120 do_checkout branch2 $HEAD1
123 test_expect_success
'checkout -b to a new branch with unmergeable changes fails' '
124 setup_dirty_unmergeable &&
125 do_checkout ! branch2 $HEAD1 &&
126 test_dirty_unmergeable
129 test_expect_success
'checkout -f -b to a new branch with unmergeable changes discards changes' '
131 git checkout branch1 &&
132 test_might_fail git branch -D branch2" &&
134 # still dirty and on branch1
135 do_checkout branch2 $HEAD1 "-f -b" &&
136 test_dirty_unmergeable_discards_changes
139 test_expect_success
'checkout -b to a new branch preserves mergeable changes' '
142 git checkout branch1 &&
143 test_might_fail git branch -D branch2" &&
145 setup_dirty_mergeable &&
146 do_checkout branch2 $HEAD1 &&
150 test_expect_success
'checkout -f -b to a new branch with mergeable changes discards changes' '
151 test_when_finished git reset --hard HEAD &&
152 setup_dirty_mergeable &&
153 do_checkout branch2 $HEAD1 "-f -b" &&
154 test_dirty_mergeable_discards_changes
157 test_expect_success
'checkout -b to an existing branch fails' '
158 test_when_finished git reset --hard HEAD &&
159 do_checkout ! branch2 $HEAD2
162 test_expect_success
'checkout -b to @{-1} fails with the right branch name' '
163 git checkout branch1 &&
164 git checkout branch2 &&
165 echo >expect "fatal: a branch named '\''branch1'\'' already exists" &&
166 test_must_fail git checkout -b @{-1} 2>actual &&
167 test_cmp expect actual
170 test_expect_success
'checkout -B to an existing branch resets branch to HEAD' '
171 git checkout branch1 &&
173 do_checkout branch2 "" -B
176 test_expect_success
'checkout -B to a merge base' '
177 git checkout branch1 &&
179 git checkout -B branch2 branch1...
182 test_expect_success
'checkout -B to an existing branch from detached HEAD resets branch to HEAD' '
183 head=$(git rev-parse --verify HEAD) &&
184 git checkout "$head" &&
186 do_checkout branch2 "" -B
189 test_expect_success
'checkout -B to an existing branch with an explicit ref resets branch to that ref' '
190 git checkout branch1 &&
192 do_checkout branch2 $HEAD1 -B
195 test_expect_success
'checkout -B to an existing branch with unmergeable changes fails' '
196 git checkout branch1 &&
198 setup_dirty_unmergeable &&
199 do_checkout ! branch2 $HEAD1 -B &&
200 test_dirty_unmergeable
203 test_expect_success
'checkout -f -B to an existing branch with unmergeable changes discards changes' '
204 # still dirty and on branch1
205 do_checkout branch2 $HEAD1 "-f -B" &&
206 test_dirty_unmergeable_discards_changes
209 test_expect_success
'checkout -B to an existing branch preserves mergeable changes' '
210 test_when_finished git reset --hard &&
211 git checkout branch1 &&
213 setup_dirty_mergeable &&
214 do_checkout branch2 $HEAD1 -B &&
218 test_expect_success
'checkout -f -B to an existing branch with mergeable changes discards changes' '
219 git checkout branch1 &&
221 setup_dirty_mergeable &&
222 do_checkout branch2 $HEAD1 "-f -B" &&
223 test_dirty_mergeable_discards_changes
226 test_expect_success
'checkout -b <describe>' '
227 git tag -f -m "First commit" initial initial &&
228 git checkout -f change1 &&
229 name=$(git describe) &&
230 git checkout -b $name &&
231 git diff --exit-code change1 &&
232 echo "refs/heads/$name" >expect &&
233 git symbolic-ref HEAD >actual &&
234 test_cmp expect actual
237 test_expect_success
'checkout -B to the current branch works' '
238 git checkout branch1 &&
239 git checkout -B branch1-scratch &&
241 setup_dirty_mergeable &&
242 git checkout -B branch1-scratch initial &&
246 test_expect_success
'checkout -b after clone --no-checkout does a checkout of HEAD' '
248 test_commit -C src a &&
249 rev="$(git -C src rev-parse HEAD)" &&
250 git clone --no-checkout src dest &&
251 git -C dest checkout "$rev" -b branch &&
252 test_path_is_file dest/a.t
255 test_expect_success
'checkout -b to a new branch preserves mergeable changes despite sparse-checkout' '
258 git checkout branch1-scratch &&
259 test_might_fail git branch -D branch3 &&
260 git config core.sparseCheckout false &&
265 echo stuff >>file1 &&
267 echo file2 >.git/info/sparse-checkout &&
268 git config core.sparseCheckout true &&
270 CURHEAD=$(git rev-parse HEAD) &&
271 do_checkout branch3 $CURHEAD &&
273 echo file1 >expect &&
274 git diff --name-only >actual &&
275 test_cmp expect actual
278 test_expect_success
'checkout -b rejects an invalid start point' '
279 test_must_fail git checkout -b branch4 file1 2>err &&
280 test_i18ngrep "is not a commit" err
283 test_expect_success
'checkout -b rejects an extra path argument' '
284 test_must_fail git checkout -b branch5 branch1 file1 2>err &&
285 test_i18ngrep "Cannot update paths and switch to branch" err