]>
git.ipfire.org Git - thirdparty/git.git/blob - t/t2018-checkout-branch.sh
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.
17 exp_ref
="refs/heads/$exp_branch" &&
19 # if <sha> is not specified, use HEAD.
20 exp_sha
=${2:-$(git rev-parse --verify HEAD)} &&
22 # default options for git checkout: -b
29 git checkout
$opts $exp_branch $exp_sha &&
31 test $exp_ref = $
(git rev-parse
--symbolic-full-name HEAD
) &&
32 test $exp_sha = $
(git rev-parse
--verify HEAD
)
35 test_dirty_unmergeable
() {
36 ! git
diff --exit-code >/dev
/null
39 setup_dirty_unmergeable
() {
43 test_dirty_mergeable
() {
44 ! git
diff --cached --exit-code >/dev
/null
47 setup_dirty_mergeable
() {
52 test_expect_success
'setup' '
53 test_commit initial file1 &&
54 HEAD1=$(git rev-parse --verify HEAD) &&
56 test_commit change1 file1 &&
57 HEAD2=$(git rev-parse --verify HEAD) &&
62 test_expect_success
'checkout -b to a new branch, set to HEAD' '
64 git checkout branch1 &&
65 test_might_fail git branch -D branch2" &&
69 test_expect_success
'checkout -b to a merge base' '
71 git checkout branch1 &&
72 test_might_fail git branch -D branch2" &&
73 git checkout -b branch2 branch1...
76 test_expect_success
'checkout -b to a new branch, set to an explicit ref' '
78 git checkout branch1 &&
79 test_might_fail git branch -D branch2" &&
80 do_checkout branch2 $HEAD1
83 test_expect_success
'checkout -b to a new branch with unmergeable changes fails' '
84 setup_dirty_unmergeable &&
85 test_must_fail do_checkout branch2 $HEAD1 &&
86 test_dirty_unmergeable
89 test_expect_success
'checkout -f -b to a new branch with unmergeable changes discards changes' '
91 git checkout branch1 &&
92 test_might_fail git branch -D branch2" &&
94 # still dirty and on branch1
95 do_checkout branch2 $HEAD1 "-f -b" &&
96 test_must_fail test_dirty_unmergeable
99 test_expect_success
'checkout -b to a new branch preserves mergeable changes' '
102 git checkout branch1 &&
103 test_might_fail git branch -D branch2" &&
105 setup_dirty_mergeable &&
106 do_checkout branch2 $HEAD1 &&
110 test_expect_success
'checkout -f -b to a new branch with mergeable changes discards changes' '
111 test_when_finished git reset --hard HEAD &&
112 setup_dirty_mergeable &&
113 do_checkout branch2 $HEAD1 "-f -b" &&
114 test_must_fail test_dirty_mergeable
117 test_expect_success
'checkout -b to an existing branch fails' '
118 test_when_finished git reset --hard HEAD &&
119 test_must_fail do_checkout branch2 $HEAD2
122 test_expect_success
'checkout -b to @{-1} fails with the right branch name' '
123 git checkout branch1 &&
124 git checkout branch2 &&
125 echo >expect "fatal: A branch named '\''branch1'\'' already exists." &&
126 test_must_fail git checkout -b @{-1} 2>actual &&
127 test_i18ncmp expect actual
130 test_expect_success
'checkout -B to an existing branch resets branch to HEAD' '
131 git checkout branch1 &&
133 do_checkout branch2 "" -B
136 test_expect_success
'checkout -B to a merge base' '
137 git checkout branch1 &&
139 git checkout -B branch2 branch1...
142 test_expect_success
'checkout -B to an existing branch from detached HEAD resets branch to HEAD' '
143 git checkout $(git rev-parse --verify HEAD) &&
145 do_checkout branch2 "" -B
148 test_expect_success
'checkout -B to an existing branch with an explicit ref resets branch to that ref' '
149 git checkout branch1 &&
151 do_checkout branch2 $HEAD1 -B
154 test_expect_success
'checkout -B to an existing branch with unmergeable changes fails' '
155 git checkout branch1 &&
157 setup_dirty_unmergeable &&
158 test_must_fail do_checkout branch2 $HEAD1 -B &&
159 test_dirty_unmergeable
162 test_expect_success
'checkout -f -B to an existing branch with unmergeable changes discards changes' '
163 # still dirty and on branch1
164 do_checkout branch2 $HEAD1 "-f -B" &&
165 test_must_fail test_dirty_unmergeable
168 test_expect_success
'checkout -B to an existing branch preserves mergeable changes' '
169 test_when_finished git reset --hard &&
170 git checkout branch1 &&
172 setup_dirty_mergeable &&
173 do_checkout branch2 $HEAD1 -B &&
177 test_expect_success
'checkout -f -B to an existing branch with mergeable changes discards changes' '
178 git checkout branch1 &&
180 setup_dirty_mergeable &&
181 do_checkout branch2 $HEAD1 "-f -B" &&
182 test_must_fail test_dirty_mergeable
185 test_expect_success
'checkout -b <describe>' '
186 git tag -f -m "First commit" initial initial &&
187 git checkout -f change1 &&
188 name=$(git describe) &&
189 git checkout -b $name &&
190 git diff --exit-code change1 &&
191 echo "refs/heads/$name" >expect &&
192 git symbolic-ref HEAD >actual &&
193 test_cmp expect actual
196 test_expect_success
'checkout -B to the current branch works' '
197 git checkout branch1 &&
198 git checkout -B branch1-scratch &&
200 setup_dirty_mergeable &&
201 git checkout -B branch1-scratch initial &&
205 test_expect_success
'checkout -b after clone --no-checkout does a checkout of HEAD' '
207 test_commit -C src a &&
208 rev="$(git -C src rev-parse HEAD)" &&
209 git clone --no-checkout src dest &&
210 git -C dest checkout "$rev" -b branch &&
211 test_path_is_file dest/a.t