gc: add `gc.repackFilter` config option
[git.git] / t / t2018-checkout-branch.sh
blob8581ad3437907862e022e1bea8339182d68ef873
1 #!/bin/sh
3 test_description='checkout'
5 TEST_CREATE_REPO_NO_TEMPLATE=1
6 TEST_PASSES_SANITIZE_LEAK=true
7 . ./test-lib.sh
9 # Arguments: [!] <branch> <oid> [<checkout options>]
11 # Runs "git checkout" to switch to <branch>, testing that
13 # 1) we are on the specified branch, <branch>;
14 # 2) HEAD is <oid>; if <oid> is not specified, the old HEAD is used.
16 # If <checkout options> is not specified, "git checkout" is run with -b.
18 # If the first argument is `!`, "git checkout" is expected to fail when
19 # it is run.
20 do_checkout () {
21 should_fail= &&
22 if test "x$1" = "x!"
23 then
24 should_fail=yes &&
25 shift
26 fi &&
27 exp_branch=$1 &&
28 exp_ref="refs/heads/$exp_branch" &&
30 # if <oid> is not specified, use HEAD.
31 exp_oid=${2:-$(git rev-parse --verify HEAD)} &&
33 # default options for git checkout: -b
34 if test -z "$3"
35 then
36 opts="-b"
37 else
38 opts="$3"
41 if test -n "$should_fail"
42 then
43 test_must_fail git checkout $opts $exp_branch $exp_oid
44 else
45 git checkout $opts $exp_branch $exp_oid &&
46 echo "$exp_ref" >ref.expect &&
47 git rev-parse --symbolic-full-name HEAD >ref.actual &&
48 test_cmp ref.expect ref.actual &&
49 echo "$exp_oid" >oid.expect &&
50 git rev-parse --verify HEAD >oid.actual &&
51 test_cmp oid.expect oid.actual
55 test_dirty_unmergeable () {
56 test_expect_code 1 git diff --exit-code
59 test_dirty_unmergeable_discards_changes () {
60 git diff --exit-code
63 setup_dirty_unmergeable () {
64 echo >>file1 change2
67 test_dirty_mergeable () {
68 test_expect_code 1 git diff --cached --exit-code
71 test_dirty_mergeable_discards_changes () {
72 git diff --cached --exit-code
75 setup_dirty_mergeable () {
76 echo >file2 file2 &&
77 git add file2
80 test_expect_success 'setup' '
81 test_commit initial file1 &&
82 HEAD1=$(git rev-parse --verify HEAD) &&
84 test_commit change1 file1 &&
85 HEAD2=$(git rev-parse --verify HEAD) &&
87 git branch -m branch1
90 test_expect_success 'checkout a branch without refs/heads/* prefix' '
91 git clone --no-tags . repo-odd-prefix &&
93 cd repo-odd-prefix &&
95 origin=$(git symbolic-ref refs/remotes/origin/HEAD) &&
96 git symbolic-ref refs/heads/a-branch "$origin" &&
98 git checkout -f a-branch &&
99 git checkout -f a-branch
103 test_expect_success 'checkout -b to a new branch, set to HEAD' '
104 test_when_finished "
105 git checkout branch1 &&
106 test_might_fail git branch -D branch2" &&
107 do_checkout branch2
110 test_expect_success 'checkout -b to a merge base' '
111 test_when_finished "
112 git checkout branch1 &&
113 test_might_fail git branch -D branch2" &&
114 git checkout -b branch2 branch1...
117 test_expect_success 'checkout -b to a new branch, set to an explicit ref' '
118 test_when_finished "
119 git checkout branch1 &&
120 test_might_fail git branch -D branch2" &&
121 do_checkout branch2 $HEAD1
124 test_expect_success 'checkout -b to a new branch with unmergeable changes fails' '
125 setup_dirty_unmergeable &&
126 do_checkout ! branch2 $HEAD1 &&
127 test_dirty_unmergeable
130 test_expect_success 'checkout -f -b to a new branch with unmergeable changes discards changes' '
131 test_when_finished "
132 git checkout branch1 &&
133 test_might_fail git branch -D branch2" &&
135 # still dirty and on branch1
136 do_checkout branch2 $HEAD1 "-f -b" &&
137 test_dirty_unmergeable_discards_changes
140 test_expect_success 'checkout -b to a new branch preserves mergeable changes' '
141 test_when_finished "
142 git reset --hard &&
143 git checkout branch1 &&
144 test_might_fail git branch -D branch2" &&
146 setup_dirty_mergeable &&
147 do_checkout branch2 $HEAD1 &&
148 test_dirty_mergeable
151 test_expect_success 'checkout -f -b to a new branch with mergeable changes discards changes' '
152 test_when_finished git reset --hard HEAD &&
153 setup_dirty_mergeable &&
154 do_checkout branch2 $HEAD1 "-f -b" &&
155 test_dirty_mergeable_discards_changes
158 test_expect_success 'checkout -b to an existing branch fails' '
159 test_when_finished git reset --hard HEAD &&
160 do_checkout ! branch2 $HEAD2
163 test_expect_success 'checkout -b to @{-1} fails with the right branch name' '
164 git checkout branch1 &&
165 git checkout branch2 &&
166 echo >expect "fatal: a branch named '\''branch1'\'' already exists" &&
167 test_must_fail git checkout -b @{-1} 2>actual &&
168 test_cmp expect actual
171 test_expect_success 'checkout -B to an existing branch resets branch to HEAD' '
172 git checkout branch1 &&
174 do_checkout branch2 "" -B
177 test_expect_success 'checkout -B to a merge base' '
178 git checkout branch1 &&
180 git checkout -B branch2 branch1...
183 test_expect_success 'checkout -B to an existing branch from detached HEAD resets branch to HEAD' '
184 head=$(git rev-parse --verify HEAD) &&
185 git checkout "$head" &&
187 do_checkout branch2 "" -B
190 test_expect_success 'checkout -B to an existing branch with an explicit ref resets branch to that ref' '
191 git checkout branch1 &&
193 do_checkout branch2 $HEAD1 -B
196 test_expect_success 'checkout -B to an existing branch with unmergeable changes fails' '
197 git checkout branch1 &&
199 setup_dirty_unmergeable &&
200 do_checkout ! branch2 $HEAD1 -B &&
201 test_dirty_unmergeable
204 test_expect_success 'checkout -f -B to an existing branch with unmergeable changes discards changes' '
205 # still dirty and on branch1
206 do_checkout branch2 $HEAD1 "-f -B" &&
207 test_dirty_unmergeable_discards_changes
210 test_expect_success 'checkout -B to an existing branch preserves mergeable changes' '
211 test_when_finished git reset --hard &&
212 git checkout branch1 &&
214 setup_dirty_mergeable &&
215 do_checkout branch2 $HEAD1 -B &&
216 test_dirty_mergeable
219 test_expect_success 'checkout -f -B to an existing branch with mergeable changes discards changes' '
220 git checkout branch1 &&
222 setup_dirty_mergeable &&
223 do_checkout branch2 $HEAD1 "-f -B" &&
224 test_dirty_mergeable_discards_changes
227 test_expect_success 'checkout -b <describe>' '
228 git tag -f -m "First commit" initial initial &&
229 git checkout -f change1 &&
230 name=$(git describe) &&
231 git checkout -b $name &&
232 git diff --exit-code change1 &&
233 echo "refs/heads/$name" >expect &&
234 git symbolic-ref HEAD >actual &&
235 test_cmp expect actual
238 test_expect_success 'checkout -B to the current branch works' '
239 git checkout branch1 &&
240 git checkout -B branch1-scratch &&
242 setup_dirty_mergeable &&
243 git checkout -B branch1-scratch initial &&
244 test_dirty_mergeable
247 test_expect_success 'checkout -b after clone --no-checkout does a checkout of HEAD' '
248 git init src &&
249 test_commit -C src a &&
250 rev="$(git -C src rev-parse HEAD)" &&
251 git clone --no-checkout src dest &&
252 git -C dest checkout "$rev" -b branch &&
253 test_path_is_file dest/a.t
256 test_expect_success 'checkout -b to a new branch preserves mergeable changes despite sparse-checkout' '
257 test_when_finished "
258 git reset --hard &&
259 git checkout branch1-scratch &&
260 test_might_fail git branch -D branch3 &&
261 git config core.sparseCheckout false &&
262 rm -rf .git/info" &&
264 test_commit file2 &&
266 echo stuff >>file1 &&
267 mkdir .git/info &&
268 echo file2 >.git/info/sparse-checkout &&
269 git config core.sparseCheckout true &&
271 CURHEAD=$(git rev-parse HEAD) &&
272 do_checkout branch3 $CURHEAD &&
274 echo file1 >expect &&
275 git diff --name-only >actual &&
276 test_cmp expect actual
279 test_expect_success 'checkout -b rejects an invalid start point' '
280 test_must_fail git checkout -b branch4 file1 2>err &&
281 test_i18ngrep "is not a commit" err
284 test_expect_success 'checkout -b rejects an extra path argument' '
285 test_must_fail git checkout -b branch5 branch1 file1 2>err &&
286 test_i18ngrep "Cannot update paths and switch to branch" err
289 test_done