2010-06-23 21:28:58 +02:00
|
|
|
#!/bin/sh
|
|
|
|
|
2019-12-27 14:47:10 +01:00
|
|
|
test_description='checkout'
|
2010-06-23 21:28:58 +02:00
|
|
|
|
|
|
|
. ./test-lib.sh
|
|
|
|
|
2020-01-07 05:53:04 +01:00
|
|
|
# Arguments: [!] <branch> <oid> [<checkout options>]
|
2010-06-23 21:28:58 +02:00
|
|
|
#
|
|
|
|
# Runs "git checkout" to switch to <branch>, testing that
|
|
|
|
#
|
|
|
|
# 1) we are on the specified branch, <branch>;
|
2020-01-07 05:53:04 +01:00
|
|
|
# 2) HEAD is <oid>; if <oid> is not specified, the old HEAD is used.
|
2010-06-23 21:28:58 +02:00
|
|
|
#
|
|
|
|
# If <checkout options> is not specified, "git checkout" is run with -b.
|
2020-01-07 05:53:02 +01:00
|
|
|
#
|
|
|
|
# If the first argument is `!`, "git checkout" is expected to fail when
|
|
|
|
# it is run.
|
2020-01-07 05:52:59 +01:00
|
|
|
do_checkout () {
|
2020-01-07 05:53:02 +01:00
|
|
|
should_fail= &&
|
|
|
|
if test "x$1" = "x!"
|
|
|
|
then
|
|
|
|
should_fail=yes &&
|
|
|
|
shift
|
|
|
|
fi &&
|
2010-06-23 21:28:58 +02:00
|
|
|
exp_branch=$1 &&
|
|
|
|
exp_ref="refs/heads/$exp_branch" &&
|
|
|
|
|
2020-01-07 05:53:04 +01:00
|
|
|
# if <oid> is not specified, use HEAD.
|
|
|
|
exp_oid=${2:-$(git rev-parse --verify HEAD)} &&
|
2010-06-23 21:28:58 +02:00
|
|
|
|
|
|
|
# default options for git checkout: -b
|
2020-01-07 05:53:00 +01:00
|
|
|
if test -z "$3"
|
|
|
|
then
|
2010-06-23 21:28:58 +02:00
|
|
|
opts="-b"
|
|
|
|
else
|
|
|
|
opts="$3"
|
|
|
|
fi
|
|
|
|
|
2020-01-07 05:53:02 +01:00
|
|
|
if test -n "$should_fail"
|
|
|
|
then
|
2020-01-07 05:53:04 +01:00
|
|
|
test_must_fail git checkout $opts $exp_branch $exp_oid
|
2020-01-07 05:53:02 +01:00
|
|
|
else
|
2020-01-07 05:53:04 +01:00
|
|
|
git checkout $opts $exp_branch $exp_oid &&
|
2020-01-07 05:53:03 +01:00
|
|
|
echo "$exp_ref" >ref.expect &&
|
|
|
|
git rev-parse --symbolic-full-name HEAD >ref.actual &&
|
|
|
|
test_cmp ref.expect ref.actual &&
|
2020-01-07 05:53:04 +01:00
|
|
|
echo "$exp_oid" >oid.expect &&
|
|
|
|
git rev-parse --verify HEAD >oid.actual &&
|
|
|
|
test_cmp oid.expect oid.actual
|
2020-01-07 05:53:02 +01:00
|
|
|
fi
|
2010-06-23 21:28:58 +02:00
|
|
|
}
|
|
|
|
|
2020-01-07 05:52:59 +01:00
|
|
|
test_dirty_unmergeable () {
|
2020-01-26 21:23:05 +01:00
|
|
|
test_expect_code 1 git diff --exit-code
|
|
|
|
}
|
|
|
|
|
|
|
|
test_dirty_unmergeable_discards_changes () {
|
|
|
|
git diff --exit-code
|
2010-06-23 21:28:58 +02:00
|
|
|
}
|
|
|
|
|
2020-01-07 05:52:59 +01:00
|
|
|
setup_dirty_unmergeable () {
|
2010-06-23 21:28:58 +02:00
|
|
|
echo >>file1 change2
|
|
|
|
}
|
|
|
|
|
2020-01-07 05:52:59 +01:00
|
|
|
test_dirty_mergeable () {
|
2020-01-26 21:23:05 +01:00
|
|
|
test_expect_code 1 git diff --cached --exit-code
|
|
|
|
}
|
|
|
|
|
|
|
|
test_dirty_mergeable_discards_changes () {
|
|
|
|
git diff --cached --exit-code
|
2010-06-23 21:28:58 +02:00
|
|
|
}
|
|
|
|
|
2020-01-07 05:52:59 +01:00
|
|
|
setup_dirty_mergeable () {
|
2010-06-23 21:28:58 +02:00
|
|
|
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' '
|
2019-04-27 14:02:19 +02:00
|
|
|
test_when_finished "
|
|
|
|
git checkout branch1 &&
|
|
|
|
test_might_fail git branch -D branch2" &&
|
2010-06-23 21:28:58 +02:00
|
|
|
do_checkout branch2
|
|
|
|
'
|
|
|
|
|
2019-04-27 14:02:22 +02:00
|
|
|
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...
|
|
|
|
'
|
|
|
|
|
2010-06-23 21:28:58 +02:00
|
|
|
test_expect_success 'checkout -b to a new branch, set to an explicit ref' '
|
2019-04-27 14:02:19 +02:00
|
|
|
test_when_finished "
|
|
|
|
git checkout branch1 &&
|
|
|
|
test_might_fail git branch -D branch2" &&
|
2010-06-23 21:28:58 +02:00
|
|
|
do_checkout branch2 $HEAD1
|
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success 'checkout -b to a new branch with unmergeable changes fails' '
|
|
|
|
setup_dirty_unmergeable &&
|
2020-01-07 05:53:02 +01:00
|
|
|
do_checkout ! branch2 $HEAD1 &&
|
2010-06-23 21:28:58 +02:00
|
|
|
test_dirty_unmergeable
|
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success 'checkout -f -b to a new branch with unmergeable changes discards changes' '
|
2019-04-27 14:02:19 +02:00
|
|
|
test_when_finished "
|
|
|
|
git checkout branch1 &&
|
|
|
|
test_might_fail git branch -D branch2" &&
|
|
|
|
|
2010-06-23 21:28:58 +02:00
|
|
|
# still dirty and on branch1
|
|
|
|
do_checkout branch2 $HEAD1 "-f -b" &&
|
2020-01-26 21:23:05 +01:00
|
|
|
test_dirty_unmergeable_discards_changes
|
2010-06-23 21:28:58 +02:00
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success 'checkout -b to a new branch preserves mergeable changes' '
|
2019-04-27 14:02:19 +02:00
|
|
|
test_when_finished "
|
|
|
|
git reset --hard &&
|
|
|
|
git checkout branch1 &&
|
|
|
|
test_might_fail git branch -D branch2" &&
|
2010-06-23 21:28:58 +02:00
|
|
|
|
|
|
|
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' '
|
2019-04-27 14:02:19 +02:00
|
|
|
test_when_finished git reset --hard HEAD &&
|
2010-06-23 21:28:58 +02:00
|
|
|
setup_dirty_mergeable &&
|
|
|
|
do_checkout branch2 $HEAD1 "-f -b" &&
|
2020-01-26 21:23:05 +01:00
|
|
|
test_dirty_mergeable_discards_changes
|
2010-06-23 21:28:58 +02:00
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success 'checkout -b to an existing branch fails' '
|
2019-04-27 14:02:19 +02:00
|
|
|
test_when_finished git reset --hard HEAD &&
|
2020-01-07 05:53:02 +01:00
|
|
|
do_checkout ! branch2 $HEAD2
|
2010-06-23 21:28:58 +02:00
|
|
|
'
|
|
|
|
|
2011-08-20 23:49:49 +02:00
|
|
|
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 &&
|
2021-02-11 02:53:53 +01:00
|
|
|
test_cmp expect actual
|
2011-08-20 23:49:49 +02:00
|
|
|
'
|
|
|
|
|
2010-06-23 21:29:00 +02:00
|
|
|
test_expect_success 'checkout -B to an existing branch resets branch to HEAD' '
|
|
|
|
git checkout branch1 &&
|
|
|
|
|
|
|
|
do_checkout branch2 "" -B
|
|
|
|
'
|
|
|
|
|
2019-04-27 14:02:22 +02:00
|
|
|
test_expect_success 'checkout -B to a merge base' '
|
|
|
|
git checkout branch1 &&
|
|
|
|
|
|
|
|
git checkout -B branch2 branch1...
|
|
|
|
'
|
|
|
|
|
2010-08-09 18:52:26 +02:00
|
|
|
test_expect_success 'checkout -B to an existing branch from detached HEAD resets branch to HEAD' '
|
2020-01-07 05:53:03 +01:00
|
|
|
head=$(git rev-parse --verify HEAD) &&
|
|
|
|
git checkout "$head" &&
|
2010-08-09 18:52:26 +02:00
|
|
|
|
|
|
|
do_checkout branch2 "" -B
|
|
|
|
'
|
|
|
|
|
2010-06-23 21:29:00 +02:00
|
|
|
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 &&
|
2020-01-07 05:53:02 +01:00
|
|
|
do_checkout ! branch2 $HEAD1 -B &&
|
2010-06-23 21:29:00 +02:00
|
|
|
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" &&
|
2020-01-26 21:23:05 +01:00
|
|
|
test_dirty_unmergeable_discards_changes
|
2010-06-23 21:29:00 +02:00
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success 'checkout -B to an existing branch preserves mergeable changes' '
|
2019-04-27 14:02:19 +02:00
|
|
|
test_when_finished git reset --hard &&
|
2010-06-23 21:29:00 +02:00
|
|
|
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" &&
|
2020-01-26 21:23:05 +01:00
|
|
|
test_dirty_mergeable_discards_changes
|
2010-06-23 21:29:00 +02:00
|
|
|
'
|
|
|
|
|
2011-06-06 07:17:04 +02:00
|
|
|
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
|
|
|
|
'
|
|
|
|
|
2011-11-26 09:54:55 +01:00
|
|
|
test_expect_success 'checkout -B to the current branch works' '
|
2011-08-20 23:49:48 +02:00
|
|
|
git checkout branch1 &&
|
2011-11-26 09:54:55 +01:00
|
|
|
git checkout -B branch1-scratch &&
|
|
|
|
|
2011-08-20 23:49:48 +02:00
|
|
|
setup_dirty_mergeable &&
|
2011-11-26 09:54:55 +01:00
|
|
|
git checkout -B branch1-scratch initial &&
|
|
|
|
test_dirty_mergeable
|
2011-08-20 23:49:48 +02:00
|
|
|
'
|
|
|
|
|
2019-01-23 21:02:01 +01:00
|
|
|
test_expect_success 'checkout -b after clone --no-checkout does a checkout of HEAD' '
|
2019-01-23 21:02:00 +01:00
|
|
|
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
|
|
|
|
'
|
|
|
|
|
unpack-trees: failure to set SKIP_WORKTREE bits always just a warning
Setting and clearing of the SKIP_WORKTREE bit is not only done when
users run 'sparse-checkout'; other commands such as 'checkout' also run
through unpack_trees() which has logic for handling this special bit.
As such, we need to consider how they handle special cases. A couple
comparison points should help explain the rationale for changing how
unpack_trees() handles these bits:
Ignoring sparse checkouts for a moment, if you are switching
branches and have dirty changes, it is only considered an error that
will prevent the branch switching from being successful if the dirty
file happens to be one of the paths with different contents.
SKIP_WORKTREE has always been considered advisory; for example, if
rebase or merge need or even want to materialize a path as part of
their work, they have always been allowed to do so regardless of the
SKIP_WORKTREE setting. This has been used for unmerged paths, but
it was often used for paths it wasn't needed just because it made
the code simpler. It was a best-effort consideration, and when it
materialized paths contrary to the SKIP_WORKTREE setting, it was
never required to even print a warning message.
In the past if you trying to run e.g. 'git checkout' and:
1) you had a path that was materialized and had some dirty changes
2) the path was listed in $GITDIR/info/sparse-checkout
3) this path did not different between the current and target branches
then despite the comparison points above, the inability to set
SKIP_WORKTREE was treated as a *hard* error that would abort the
checkout operation. This is completely inconsistent with how
SKIP_WORKTREE is handled elsewhere, and rather annoying for users as
leaving the paths materialized in the working copy (with a simple
warning) should present no problem at all.
Downgrade any errors from inability to toggle the SKIP_WORKTREE bit to a
warning and allow the operations to continue.
Reviewed-by: Derrick Stolee <dstolee@microsoft.com>
Signed-off-by: Elijah Newren <newren@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2020-03-27 01:49:00 +01:00
|
|
|
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 .git/info/sparse-checkout" &&
|
|
|
|
|
|
|
|
test_commit file2 &&
|
|
|
|
|
|
|
|
echo stuff >>file1 &&
|
|
|
|
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
|
|
|
|
'
|
|
|
|
|
2020-05-24 09:22:51 +02:00
|
|
|
test_expect_success 'checkout -b rejects an invalid start point' '
|
|
|
|
test_must_fail git checkout -b branch4 file1 2>err &&
|
|
|
|
test_i18ngrep "is not a commit" err
|
|
|
|
'
|
|
|
|
|
2020-05-24 09:23:00 +02:00
|
|
|
test_expect_success 'checkout -b rejects an extra path argument' '
|
2020-05-24 09:22:51 +02:00
|
|
|
test_must_fail git checkout -b branch5 branch1 file1 2>err &&
|
|
|
|
test_i18ngrep "Cannot update paths and switch to branch" err
|
|
|
|
'
|
|
|
|
|
2010-06-23 21:28:58 +02:00
|
|
|
test_done
|