3 test_description
='checkout'
7 # Arguments: [!] <branch> <sha> [<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 <sha>; if <sha> 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 <sha> is not specified, use HEAD.
29 exp_sha
=${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_sha
43 git checkout
$opts $exp_branch $exp_sha &&
44 test $exp_ref = $
(git rev-parse
--symbolic-full-name HEAD
) &&
45 test $exp_sha = $
(git rev-parse
--verify HEAD
)
49 test_dirty_unmergeable
() {
50 test_expect_code
1 git
diff --exit-code
53 test_dirty_unmergeable_discards_changes
() {
57 setup_dirty_unmergeable
() {
61 test_dirty_mergeable
() {
62 test_expect_code
1 git
diff --cached --exit-code
65 test_dirty_mergeable_discards_changes
() {
66 git
diff --cached --exit-code
69 setup_dirty_mergeable
() {
74 test_expect_success
'setup' '
75 test_commit initial file1 &&
76 HEAD1=$(git rev-parse --verify HEAD) &&
78 test_commit change1 file1 &&
79 HEAD2=$(git rev-parse --verify HEAD) &&
84 test_expect_success
'checkout -b to a new branch, set to HEAD' '
86 git checkout branch1 &&
87 test_might_fail git branch -D branch2" &&
91 test_expect_success
'checkout -b to a merge base' '
93 git checkout branch1 &&
94 test_might_fail git branch -D branch2" &&
95 git checkout -b branch2 branch1...
98 test_expect_success
'checkout -b to a new branch, set to an explicit ref' '
100 git checkout branch1 &&
101 test_might_fail git branch -D branch2" &&
102 do_checkout branch2 $HEAD1
105 test_expect_success
'checkout -b to a new branch with unmergeable changes fails' '
106 setup_dirty_unmergeable &&
107 do_checkout ! branch2 $HEAD1 &&
108 test_dirty_unmergeable
111 test_expect_success
'checkout -f -b to a new branch with unmergeable changes discards changes' '
113 git checkout branch1 &&
114 test_might_fail git branch -D branch2" &&
116 # still dirty and on branch1
117 do_checkout branch2 $HEAD1 "-f -b" &&
118 test_dirty_unmergeable_discards_changes
121 test_expect_success
'checkout -b to a new branch preserves mergeable changes' '
124 git checkout branch1 &&
125 test_might_fail git branch -D branch2" &&
127 setup_dirty_mergeable &&
128 do_checkout branch2 $HEAD1 &&
132 test_expect_success
'checkout -f -b to a new branch with mergeable changes discards changes' '
133 test_when_finished git reset --hard HEAD &&
134 setup_dirty_mergeable &&
135 do_checkout branch2 $HEAD1 "-f -b" &&
136 test_dirty_mergeable_discards_changes
139 test_expect_success
'checkout -b to an existing branch fails' '
140 test_when_finished git reset --hard HEAD &&
141 do_checkout ! branch2 $HEAD2
144 test_expect_success
'checkout -b to @{-1} fails with the right branch name' '
145 git checkout branch1 &&
146 git checkout branch2 &&
147 echo >expect "fatal: A branch named '\''branch1'\'' already exists." &&
148 test_must_fail git checkout -b @{-1} 2>actual &&
149 test_i18ncmp expect actual
152 test_expect_success
'checkout -B to an existing branch resets branch to HEAD' '
153 git checkout branch1 &&
155 do_checkout branch2 "" -B
158 test_expect_success
'checkout -B to a merge base' '
159 git checkout branch1 &&
161 git checkout -B branch2 branch1...
164 test_expect_success
'checkout -B to an existing branch from detached HEAD resets branch to HEAD' '
165 git checkout $(git rev-parse --verify HEAD) &&
167 do_checkout branch2 "" -B
170 test_expect_success
'checkout -B to an existing branch with an explicit ref resets branch to that ref' '
171 git checkout branch1 &&
173 do_checkout branch2 $HEAD1 -B
176 test_expect_success
'checkout -B to an existing branch with unmergeable changes fails' '
177 git checkout branch1 &&
179 setup_dirty_unmergeable &&
180 do_checkout ! branch2 $HEAD1 -B &&
181 test_dirty_unmergeable
184 test_expect_success
'checkout -f -B to an existing branch with unmergeable changes discards changes' '
185 # still dirty and on branch1
186 do_checkout branch2 $HEAD1 "-f -B" &&
187 test_dirty_unmergeable_discards_changes
190 test_expect_success
'checkout -B to an existing branch preserves mergeable changes' '
191 test_when_finished git reset --hard &&
192 git checkout branch1 &&
194 setup_dirty_mergeable &&
195 do_checkout branch2 $HEAD1 -B &&
199 test_expect_success
'checkout -f -B to an existing branch with mergeable changes discards changes' '
200 git checkout branch1 &&
202 setup_dirty_mergeable &&
203 do_checkout branch2 $HEAD1 "-f -B" &&
204 test_dirty_mergeable_discards_changes
207 test_expect_success
'checkout -b <describe>' '
208 git tag -f -m "First commit" initial initial &&
209 git checkout -f change1 &&
210 name=$(git describe) &&
211 git checkout -b $name &&
212 git diff --exit-code change1 &&
213 echo "refs/heads/$name" >expect &&
214 git symbolic-ref HEAD >actual &&
215 test_cmp expect actual
218 test_expect_success
'checkout -B to the current branch works' '
219 git checkout branch1 &&
220 git checkout -B branch1-scratch &&
222 setup_dirty_mergeable &&
223 git checkout -B branch1-scratch initial &&
227 test_expect_success
'checkout -b after clone --no-checkout does a checkout of HEAD' '
229 test_commit -C src a &&
230 rev="$(git -C src rev-parse HEAD)" &&
231 git clone --no-checkout src dest &&
232 git -C dest checkout "$rev" -b branch &&
233 test_path_is_file dest/a.t