Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/git/blob/master/t/t2018-checkout-branch.sh # git [Ported from github.com/git/git] Sync with Git 2.56.1 Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `master` Branches Tags History `git / t / t2018-checkout-branch.sh` 288 lines ยท 7384 bytes Raw Blame History 1 #!/bin/sh 2 3 test_description='checkout' 4 5 TEST_CREATE_REPO_NO_TEMPLATE=1 6 . ./test-lib.sh 7 8 # Arguments: [!] [] 9 # 10 # Runs "git checkout" to switch to , testing that 11 # 12 # 1) we are on the specified branch, ; 13 # 2) HEAD is ; if is not specified, the old HEAD is used. 14 # 15 # If is not specified, "git checkout" is run with -b. 16 # 17 # If the first argument is `!`, "git checkout" is expected to fail when 18 # it is run. 19 do_checkout () { 20 should_fail= && 21 if test "x$1" = "x!" 22 then 23 should_fail=yes && 24 shift 25 fi && 26 exp_branch=$1 && 27 exp_ref="refs/heads/$exp_branch" && 28 29 # if is not specified, use HEAD. 30 exp_oid=${2:-$(git rev-parse --verify HEAD)} && 31 32 # default options for git checkout: -b 33 if test -z "$3" 34 then 35 opts="-b" 36 else 37 opts="$3" 38 fi 39 40 if test -n "$should_fail" 41 then 42 test_must_fail git checkout $opts $exp_branch $exp_oid 43 else 44 git checkout $opts $exp_branch $exp_oid && 45 echo "$exp_ref" >ref.expect && 46 git rev-parse --symbolic-full-name HEAD >ref.actual && 47 test_cmp ref.expect ref.actual && 48 echo "$exp_oid" >oid.expect && 49 git rev-parse --verify HEAD >oid.actual && 50 test_cmp oid.expect oid.actual 51 fi 52 } 53 54 test_dirty_unmergeable () { 55 test_expect_code 1 git diff --exit-code 56 } 57 58 test_dirty_unmergeable_discards_changes () { 59 git diff --exit-code 60 } 61 62 setup_dirty_unmergeable () { 63 echo >>file1 change2 64 } 65 66 test_dirty_mergeable () { 67 test_expect_code 1 git diff --cached --exit-code 68 } 69 70 test_dirty_mergeable_discards_changes () { 71 git diff --cached --exit-code 72 } 73 74 setup_dirty_mergeable () { 75 echo >file2 file2 && 76 git add file2 77 } 78 79 test_expect_success 'setup' ' 80 test_commit initial file1 && 81 HEAD1=$(git rev-parse --verify HEAD) && 82 83 test_commit change1 file1 && 84 HEAD2=$(git rev-parse --verify HEAD) && 85 86 git branch -m branch1 87 ' 88 89 test_expect_success 'checkout a branch without refs/heads/* prefix' ' 90 git clone --no-tags . repo-odd-prefix && 91 ( 92 cd repo-odd-prefix && 93 94 origin=$(git symbolic-ref refs/remotes/origin/HEAD) && 95 git symbolic-ref refs/heads/a-branch "$origin" && 96 97 git checkout -f a-branch && 98 git checkout -f a-branch 99 ) 100 ' 101 102 test_expect_success 'checkout -b to a new branch, set to HEAD' ' 103 test_when_finished " 104 git checkout branch1 && 105 test_might_fail git branch -D branch2" && 106 do_checkout branch2 107 ' 108 109 test_expect_success 'checkout -b to a merge base' ' 110 test_when_finished " 111 git checkout branch1 && 112 test_might_fail git branch -D branch2" && 113 git checkout -b branch2 branch1... 114 ' 115 116 test_expect_success 'checkout -b to a new branch, set to an explicit ref' ' 117 test_when_finished " 118 git checkout branch1 && 119 test_might_fail git branch -D branch2" && 120 do_checkout branch2 $HEAD1 121 ' 122 123 test_expect_success 'checkout -b to a new branch with unmergeable changes fails' ' 124 setup_dirty_unmergeable && 125 do_checkout ! branch2 $HEAD1 && 126 test_dirty_unmergeable 127 ' 128 129 test_expect_success 'checkout -f -b to a new branch with unmergeable changes discards changes' ' 130 test_when_finished " 131 git checkout branch1 && 132 test_might_fail git branch -D branch2" && 133 134 # still dirty and on branch1 135 do_checkout branch2 $HEAD1 "-f -b" && 136 test_dirty_unmergeable_discards_changes 137 ' 138 139 test_expect_success 'checkout -b to a new branch preserves mergeable changes' ' 140 test_when_finished " 141 git reset --hard && 142 git checkout branch1 && 143 test_might_fail git branch -D branch2" && 144 145 setup_dirty_mergeable && 146 do_checkout branch2 $HEAD1 && 147 test_dirty_mergeable 148 ' 149 150 test_expect_success 'checkout -f -b to a new branch with mergeable changes discards changes' ' 151 test_when_finished git reset --hard HEAD && 152 setup_dirty_mergeable && 153 do_checkout branch2 $HEAD1 "-f -b" && 154 test_dirty_mergeable_discards_changes 155 ' 156 157 test_expect_success 'checkout -b to an existing branch fails' ' 158 test_when_finished git reset --hard HEAD && 159 do_checkout ! branch2 $HEAD2 160 ' 161 162 test_expect_success 'checkout -b to @{-1} fails with the right branch name' ' 163 git checkout branch1 && 164 git checkout branch2 && 165 echo >expect "fatal: a branch named '\''branch1'\'' already exists" && 166 test_must_fail git checkout -b @{-1} 2>actual && 167 test_cmp expect actual 168 ' 169 170 test_expect_success 'checkout -B to an existing branch resets branch to HEAD' ' 171 git checkout branch1 && 172 173 do_checkout branch2 "" -B 174 ' 175 176 test_expect_success 'checkout -B to a merge base' ' 177 git checkout branch1 && 178 179 git checkout -B branch2 branch1... 180 ' 181 182 test_expect_success 'checkout -B to an existing branch from detached HEAD resets branch to HEAD' ' 183 head=$(git rev-parse --verify HEAD) && 184 git checkout "$head" && 185 186 do_checkout branch2 "" -B 187 ' 188 189 test_expect_success 'checkout -B to an existing branch with an explicit ref resets branch to that ref' ' 190 git checkout branch1 && 191 192 do_checkout branch2 $HEAD1 -B 193 ' 194 195 test_expect_success 'checkout -B to an existing branch with unmergeable changes fails' ' 196 git checkout branch1 && 197 198 setup_dirty_unmergeable && 199 do_checkout ! branch2 $HEAD1 -B && 200 test_dirty_unmergeable 201 ' 202 203 test_expect_success 'checkout -f -B to an existing branch with unmergeable changes discards changes' ' 204 # still dirty and on branch1 205 do_checkout branch2 $HEAD1 "-f -B" && 206 test_dirty_unmergeable_discards_changes 207 ' 208 209 test_expect_success 'checkout -B to an existing branch preserves mergeable changes' ' 210 test_when_finished git reset --hard && 211 git checkout branch1 && 212 213 setup_dirty_mergeable && 214 do_checkout branch2 $HEAD1 -B && 215 test_dirty_mergeable 216 ' 217 218 test_expect_success 'checkout -f -B to an existing branch with mergeable changes discards changes' ' 219 git checkout branch1 && 220 221 setup_dirty_mergeable && 222 do_checkout branch2 $HEAD1 "-f -B" && 223 test_dirty_mergeable_discards_changes 224 ' 225 226 test_expect_success 'checkout -b ' ' 227 git tag -f -m "First commit" initial initial && 228 git checkout -f change1 && 229 name=$(git describe) && 230 git checkout -b $name && 231 git diff --exit-code change1 && 232 echo "refs/heads/$name" >expect && 233 git symbolic-ref HEAD >actual && 234 test_cmp expect actual 235 ' 236 237 test_expect_success 'checkout -B to the current branch works' ' 238 git checkout branch1 && 239 git checkout -B branch1-scratch && 240 241 setup_dirty_mergeable && 242 git checkout -B branch1-scratch initial && 243 test_dirty_mergeable 244 ' 245 246 test_expect_success 'checkout -b after clone --no-checkout does a checkout of HEAD' ' 247 git init src && 248 test_commit -C src a && 249 rev="$(git -C src rev-parse HEAD)" && 250 git clone --no-checkout src dest && 251 git -C dest checkout "$rev" -b branch && 252 test_path_is_file dest/a.t 253 ' 254 255 test_expect_success 'checkout -b to a new branch preserves mergeable changes despite sparse-checkout' ' 256 test_when_finished " 257 git reset --hard && 258 git checkout branch1-scratch && 259 test_might_fail git branch -D branch3 && 260 git config core.sparseCheckout false && 261 rm -rf .git/info" && 262 263 test_commit file2 && 264 265 echo stuff >>file1 && 266 mkdir .git/info && 267 echo file2 >.git/info/sparse-checkout && 268 git config core.sparseCheckout true && 269 270 CURHEAD=$(git rev-parse HEAD) && 271 do_checkout branch3 $CURHEAD && 272 273 echo file1 >expect && 274 git diff --name-only >actual && 275 test_cmp expect actual 276 ' 277 278 test_expect_success 'checkout -b rejects an invalid start point' ' 279 test_must_fail git checkout -b branch4 file1 2>err && 280 test_grep "is not a commit" err 281 ' 282 283 test_expect_success 'checkout -b rejects an extra path argument' ' 284 test_must_fail git checkout -b branch5 branch1 file1 2>err && 285 test_grep "Cannot update paths and switch to branch" err 286 ' 287 288 test_done