mirror of
https://github.com/git/git
synced 2024-09-12 21:04:12 +00:00
c17b229454
When create a new branch, we fed "refs/heads/<proposed name>" as a string
to get_sha1() and expected it to fail when a branch already exists.
The right way to check if a ref exists is to check with resolve_ref().
A naïve solution that might appear attractive but does not work is to
forbid slashes in get_describe_name() but that will not work. A describe
name is is in the form of "ANYTHING-g<short sha1>", and that ANYTHING part
comes from a original tag name used in the repository the user ran the
describe command. A sick user could have a confusing hierarchical tag
whose name is "refs/heads/foobar" (stored as refs/tags/refs/heads/foobar")
to generate a describe name "refs/heads/foobar-6-g02ac983", and we should
be able to use that name to refer to the object whose name is 02ac983
.
Signed-off-by: Junio C Hamano <gitster@pobox.com>
178 lines
4.1 KiB
Bash
Executable file
178 lines
4.1 KiB
Bash
Executable file
#!/bin/sh
|
|
|
|
test_description='checkout '
|
|
|
|
. ./test-lib.sh
|
|
|
|
# Arguments: <branch> <sha> [<checkout options>]
|
|
#
|
|
# Runs "git checkout" to switch to <branch>, testing that
|
|
#
|
|
# 1) we are on the specified branch, <branch>;
|
|
# 2) HEAD is <sha>; if <sha> is not specified, the old HEAD is used.
|
|
#
|
|
# If <checkout options> is not specified, "git checkout" is run with -b.
|
|
do_checkout() {
|
|
exp_branch=$1 &&
|
|
exp_ref="refs/heads/$exp_branch" &&
|
|
|
|
# if <sha> is not specified, use HEAD.
|
|
exp_sha=${2:-$(git rev-parse --verify HEAD)} &&
|
|
|
|
# default options for git checkout: -b
|
|
if [ -z "$3" ]; then
|
|
opts="-b"
|
|
else
|
|
opts="$3"
|
|
fi
|
|
|
|
git checkout $opts $exp_branch $exp_sha &&
|
|
|
|
test $exp_ref = $(git rev-parse --symbolic-full-name HEAD) &&
|
|
test $exp_sha = $(git rev-parse --verify HEAD)
|
|
}
|
|
|
|
test_dirty_unmergeable() {
|
|
! git diff --exit-code >/dev/null
|
|
}
|
|
|
|
setup_dirty_unmergeable() {
|
|
echo >>file1 change2
|
|
}
|
|
|
|
test_dirty_mergeable() {
|
|
! git diff --cached --exit-code >/dev/null
|
|
}
|
|
|
|
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 -b to a new branch, set to HEAD' '
|
|
do_checkout branch2
|
|
'
|
|
|
|
test_expect_success 'checkout -b to a new branch, set to an explicit ref' '
|
|
git checkout branch1 &&
|
|
git branch -D branch2 &&
|
|
|
|
do_checkout branch2 $HEAD1
|
|
'
|
|
|
|
test_expect_success 'checkout -b to a new branch with unmergeable changes fails' '
|
|
git checkout branch1 &&
|
|
|
|
# clean up from previous test
|
|
git branch -D branch2 &&
|
|
|
|
setup_dirty_unmergeable &&
|
|
test_must_fail do_checkout branch2 $HEAD1 &&
|
|
test_dirty_unmergeable
|
|
'
|
|
|
|
test_expect_success 'checkout -f -b to a new branch with unmergeable changes discards changes' '
|
|
# still dirty and on branch1
|
|
do_checkout branch2 $HEAD1 "-f -b" &&
|
|
test_must_fail test_dirty_unmergeable
|
|
'
|
|
|
|
test_expect_success 'checkout -b to a new branch preserves mergeable changes' '
|
|
git checkout branch1 &&
|
|
|
|
# clean up from previous test
|
|
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' '
|
|
# clean up from previous test
|
|
git reset --hard &&
|
|
|
|
git checkout branch1 &&
|
|
|
|
# clean up from previous test
|
|
git branch -D branch2 &&
|
|
|
|
setup_dirty_mergeable &&
|
|
do_checkout branch2 $HEAD1 "-f -b" &&
|
|
test_must_fail test_dirty_mergeable
|
|
'
|
|
|
|
test_expect_success 'checkout -b to an existing branch fails' '
|
|
git reset --hard HEAD &&
|
|
|
|
test_must_fail do_checkout branch2 $HEAD2
|
|
'
|
|
|
|
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 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 &&
|
|
test_must_fail 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_must_fail test_dirty_unmergeable
|
|
'
|
|
|
|
test_expect_success 'checkout -B to an existing branch preserves mergeable changes' '
|
|
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' '
|
|
# clean up from previous test
|
|
git reset --hard &&
|
|
|
|
git checkout branch1 &&
|
|
|
|
setup_dirty_mergeable &&
|
|
do_checkout branch2 $HEAD1 "-f -B" &&
|
|
test_must_fail test_dirty_mergeable
|
|
'
|
|
|
|
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_done
|