submodule--helper: fix initialization of warn_if_uninitialized
[git.git] / t / t2018-checkout-branch.sh
blob52e51b0726f39c3e7ffbedad5a77484b8bec6070
1 #!/bin/sh
3 test_description='checkout'
5 . ./test-lib.sh
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
17 # it is run.
18 do_checkout () {
19 should_fail= &&
20 if test "x$1" = "x!"
21 then
22 should_fail=yes &&
23 shift
24 fi &&
25 exp_branch=$1 &&
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
32 if test -z "$3"
33 then
34 opts="-b"
35 else
36 opts="$3"
39 if test -n "$should_fail"
40 then
41 test_must_fail git checkout $opts $exp_branch $exp_oid
42 else
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 () {
58 git diff --exit-code
61 setup_dirty_unmergeable () {
62 echo >>file1 change2
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 () {
74 echo >file2 file2 &&
75 git add file2
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) &&
85 git branch -m branch1
88 test_expect_success 'checkout a branch without refs/heads/* prefix' '
89 git clone --no-tags . repo-odd-prefix &&
91 cd 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' '
102 test_when_finished "
103 git checkout branch1 &&
104 test_might_fail git branch -D branch2" &&
105 do_checkout branch2
108 test_expect_success 'checkout -b to a merge base' '
109 test_when_finished "
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' '
116 test_when_finished "
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' '
129 test_when_finished "
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' '
139 test_when_finished "
140 git reset --hard &&
141 git checkout branch1 &&
142 test_might_fail git branch -D branch2" &&
144 setup_dirty_mergeable &&
145 do_checkout branch2 $HEAD1 &&
146 test_dirty_mergeable
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 &&
214 test_dirty_mergeable
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 &&
242 test_dirty_mergeable
245 test_expect_success 'checkout -b after clone --no-checkout does a checkout of HEAD' '
246 git init src &&
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' '
255 test_when_finished "
256 git reset --hard &&
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" &&
262 test_commit file2 &&
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
286 test_done