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