server_supports(): parse feature list more carefully
[git/jnareb-git.git] / t / t2018-checkout-branch.sh
blob2741262369e40a05cdc6732e4c9e6f04acb63bba
1 #!/bin/sh
3 test_description='checkout '
5 . ./test-lib.sh
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.
15 do_checkout() {
16 exp_branch=$1 &&
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
23 if [ -z "$3" ]; then
24 opts="-b"
25 else
26 opts="$3"
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() {
40 echo >>file1 change2
43 test_dirty_mergeable() {
44 ! git diff --cached --exit-code >/dev/null
47 setup_dirty_mergeable() {
48 echo >file2 file2 &&
49 git add file2
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) &&
59 git branch -m branch1
62 test_expect_success 'checkout -b to a new branch, set to HEAD' '
63 do_checkout branch2
66 test_expect_success 'checkout -b to a new branch, set to an explicit ref' '
67 git checkout branch1 &&
68 git branch -D branch2 &&
70 do_checkout branch2 $HEAD1
73 test_expect_success 'checkout -b to a new branch with unmergeable changes fails' '
74 git checkout branch1 &&
76 # clean up from previous test
77 git branch -D branch2 &&
79 setup_dirty_unmergeable &&
80 test_must_fail do_checkout branch2 $HEAD1 &&
81 test_dirty_unmergeable
84 test_expect_success 'checkout -f -b to a new branch with unmergeable changes discards changes' '
85 # still dirty and on branch1
86 do_checkout branch2 $HEAD1 "-f -b" &&
87 test_must_fail test_dirty_unmergeable
90 test_expect_success 'checkout -b to a new branch preserves mergeable changes' '
91 git checkout branch1 &&
93 # clean up from previous test
94 git branch -D branch2 &&
96 setup_dirty_mergeable &&
97 do_checkout branch2 $HEAD1 &&
98 test_dirty_mergeable
101 test_expect_success 'checkout -f -b to a new branch with mergeable changes discards changes' '
102 # clean up from previous test
103 git reset --hard &&
105 git checkout branch1 &&
107 # clean up from previous test
108 git branch -D branch2 &&
110 setup_dirty_mergeable &&
111 do_checkout branch2 $HEAD1 "-f -b" &&
112 test_must_fail test_dirty_mergeable
115 test_expect_success 'checkout -b to an existing branch fails' '
116 git reset --hard HEAD &&
118 test_must_fail do_checkout branch2 $HEAD2
121 test_expect_success 'checkout -b to @{-1} fails with the right branch name' '
122 git reset --hard HEAD &&
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_cmp 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 an existing branch from detached HEAD resets branch to HEAD' '
137 git checkout $(git rev-parse --verify HEAD) &&
139 do_checkout branch2 "" -B
142 test_expect_success 'checkout -B to an existing branch with an explicit ref resets branch to that ref' '
143 git checkout branch1 &&
145 do_checkout branch2 $HEAD1 -B
148 test_expect_success 'checkout -B to an existing branch with unmergeable changes fails' '
149 git checkout branch1 &&
151 setup_dirty_unmergeable &&
152 test_must_fail do_checkout branch2 $HEAD1 -B &&
153 test_dirty_unmergeable
156 test_expect_success 'checkout -f -B to an existing branch with unmergeable changes discards changes' '
157 # still dirty and on branch1
158 do_checkout branch2 $HEAD1 "-f -B" &&
159 test_must_fail test_dirty_unmergeable
162 test_expect_success 'checkout -B to an existing branch preserves mergeable changes' '
163 git checkout branch1 &&
165 setup_dirty_mergeable &&
166 do_checkout branch2 $HEAD1 -B &&
167 test_dirty_mergeable
170 test_expect_success 'checkout -f -B to an existing branch with mergeable changes discards changes' '
171 # clean up from previous test
172 git reset --hard &&
174 git checkout branch1 &&
176 setup_dirty_mergeable &&
177 do_checkout branch2 $HEAD1 "-f -B" &&
178 test_must_fail test_dirty_mergeable
181 test_expect_success 'checkout -b <describe>' '
182 git tag -f -m "First commit" initial initial &&
183 git checkout -f change1 &&
184 name=$(git describe) &&
185 git checkout -b $name &&
186 git diff --exit-code change1 &&
187 echo "refs/heads/$name" >expect &&
188 git symbolic-ref HEAD >actual &&
189 test_cmp expect actual
192 test_expect_success 'checkout -B to the current branch works' '
193 git checkout branch1 &&
194 git checkout -B branch1-scratch &&
196 setup_dirty_mergeable &&
197 git checkout -B branch1-scratch initial &&
198 test_dirty_mergeable
201 test_done