2022-06-14 21:27:29 +02:00
|
|
|
#!/bin/sh
|
|
|
|
|
|
|
|
test_description='test operations trying to overwrite refs at worktree HEAD'
|
|
|
|
|
|
|
|
TEST_PASSES_SANITIZE_LEAK=true
|
|
|
|
. ./test-lib.sh
|
|
|
|
|
|
|
|
test_expect_success 'setup' '
|
|
|
|
test_commit init &&
|
|
|
|
git branch -f fake-1 &&
|
|
|
|
git branch -f fake-2 &&
|
|
|
|
|
|
|
|
for i in 1 2 3 4
|
|
|
|
do
|
|
|
|
test_commit $i &&
|
|
|
|
git branch wt-$i &&
|
|
|
|
git worktree add wt-$i wt-$i || return 1
|
fetch: use new branch_checked_out() and add tests
When fetching refs from a remote, it is possible that the refspec will
cause use to overwrite a ref that is checked out in a worktree. The
existing logic in builtin/fetch.c uses a possibly-slow mechanism. Update
those sections to use the new, more efficient branch_checked_out()
helper.
These uses were not previously tested, so add a test case that can be
used for these kinds of collisions. There is only one test now, but more
tests will be added as other consumers of branch_checked_out() are
added.
Note that there are two uses in builtin/fetch.c, but only one of the
messages is tested. This is because the tested check is run before
completing the fetch, and the untested check is not reachable without
concurrent updates to the filesystem. Thus, it is beneficial to keep
that extra check for the sake of defense-in-depth. However, we should
not attempt to test the check, as the effort required is too
complicated to be worth the effort. This use in update_local_ref()
also requires a change in the error message because we no longer have
access to the worktree struct, only the path of the worktree. This error
is so rare that making a distinction between the two is not critical.
Signed-off-by: Derrick Stolee <derrickstolee@github.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2022-06-14 21:27:31 +02:00
|
|
|
done &&
|
|
|
|
|
|
|
|
# Create a server that updates each branch by one commit
|
|
|
|
git init server &&
|
|
|
|
test_commit -C server initial &&
|
|
|
|
git remote add server ./server &&
|
|
|
|
for i in 1 2 3 4
|
|
|
|
do
|
|
|
|
git -C server checkout -b wt-$i &&
|
|
|
|
test_commit -C server A-$i || return 1
|
|
|
|
done &&
|
|
|
|
for i in 1 2
|
|
|
|
do
|
|
|
|
git -C server checkout -b fake-$i &&
|
|
|
|
test_commit -C server f-$i || return 1
|
2022-06-14 21:27:29 +02:00
|
|
|
done
|
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success 'refuse to overwrite: checked out in worktree' '
|
|
|
|
for i in 1 2 3 4
|
|
|
|
do
|
|
|
|
test_must_fail git branch -f wt-$i HEAD 2>err
|
2022-06-14 21:27:32 +02:00
|
|
|
grep "cannot force update the branch" err &&
|
|
|
|
|
|
|
|
test_must_fail git branch -D wt-$i 2>err
|
|
|
|
grep "Cannot delete branch" err || return 1
|
2022-06-14 21:27:29 +02:00
|
|
|
done
|
|
|
|
'
|
|
|
|
|
2022-06-14 21:27:30 +02:00
|
|
|
test_expect_success 'refuse to overwrite: worktree in bisect' '
|
|
|
|
test_when_finished rm -rf .git/worktrees/wt-*/BISECT_* &&
|
|
|
|
|
|
|
|
touch .git/worktrees/wt-4/BISECT_LOG &&
|
|
|
|
echo refs/heads/fake-2 >.git/worktrees/wt-4/BISECT_START &&
|
|
|
|
|
|
|
|
test_must_fail git branch -f fake-2 HEAD 2>err &&
|
|
|
|
grep "cannot force update the branch '\''fake-2'\'' checked out at.*wt-4" err
|
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success 'refuse to overwrite: worktree in rebase' '
|
|
|
|
test_when_finished rm -rf .git/worktrees/wt-*/rebase-merge &&
|
|
|
|
|
|
|
|
mkdir -p .git/worktrees/wt-3/rebase-merge &&
|
|
|
|
touch .git/worktrees/wt-3/rebase-merge/interactive &&
|
|
|
|
echo refs/heads/fake-1 >.git/worktrees/wt-3/rebase-merge/head-name &&
|
|
|
|
echo refs/heads/fake-2 >.git/worktrees/wt-3/rebase-merge/onto &&
|
|
|
|
|
|
|
|
test_must_fail git branch -f fake-1 HEAD 2>err &&
|
|
|
|
grep "cannot force update the branch '\''fake-1'\'' checked out at.*wt-3" err
|
|
|
|
'
|
|
|
|
|
fetch: use new branch_checked_out() and add tests
When fetching refs from a remote, it is possible that the refspec will
cause use to overwrite a ref that is checked out in a worktree. The
existing logic in builtin/fetch.c uses a possibly-slow mechanism. Update
those sections to use the new, more efficient branch_checked_out()
helper.
These uses were not previously tested, so add a test case that can be
used for these kinds of collisions. There is only one test now, but more
tests will be added as other consumers of branch_checked_out() are
added.
Note that there are two uses in builtin/fetch.c, but only one of the
messages is tested. This is because the tested check is run before
completing the fetch, and the untested check is not reachable without
concurrent updates to the filesystem. Thus, it is beneficial to keep
that extra check for the sake of defense-in-depth. However, we should
not attempt to test the check, as the effort required is too
complicated to be worth the effort. This use in update_local_ref()
also requires a change in the error message because we no longer have
access to the worktree struct, only the path of the worktree. This error
is so rare that making a distinction between the two is not critical.
Signed-off-by: Derrick Stolee <derrickstolee@github.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
2022-06-14 21:27:31 +02:00
|
|
|
test_expect_success !SANITIZE_LEAK 'refuse to fetch over ref: checked out' '
|
|
|
|
test_must_fail git fetch server +refs/heads/wt-3:refs/heads/wt-3 2>err &&
|
|
|
|
grep "refusing to fetch into branch '\''refs/heads/wt-3'\''" err &&
|
|
|
|
|
|
|
|
# General fetch into refs/heads/ will fail on first ref,
|
|
|
|
# so use a generic error message check.
|
|
|
|
test_must_fail git fetch server +refs/heads/*:refs/heads/* 2>err &&
|
|
|
|
grep "refusing to fetch into branch" err
|
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success !SANITIZE_LEAK 'refuse to fetch over ref: worktree in bisect' '
|
|
|
|
test_when_finished rm -rf .git/worktrees/wt-*/BISECT_* &&
|
|
|
|
|
|
|
|
touch .git/worktrees/wt-4/BISECT_LOG &&
|
|
|
|
echo refs/heads/fake-2 >.git/worktrees/wt-4/BISECT_START &&
|
|
|
|
|
|
|
|
test_must_fail git fetch server +refs/heads/fake-2:refs/heads/fake-2 2>err &&
|
|
|
|
grep "refusing to fetch into branch" err
|
|
|
|
'
|
|
|
|
|
|
|
|
test_expect_success !SANITIZE_LEAK 'refuse to fetch over ref: worktree in rebase' '
|
|
|
|
test_when_finished rm -rf .git/worktrees/wt-*/rebase-merge &&
|
|
|
|
|
|
|
|
mkdir -p .git/worktrees/wt-4/rebase-merge &&
|
|
|
|
touch .git/worktrees/wt-4/rebase-merge/interactive &&
|
|
|
|
echo refs/heads/fake-1 >.git/worktrees/wt-4/rebase-merge/head-name &&
|
|
|
|
echo refs/heads/fake-2 >.git/worktrees/wt-4/rebase-merge/onto &&
|
|
|
|
|
|
|
|
test_must_fail git fetch server +refs/heads/fake-1:refs/heads/fake-1 2>err &&
|
|
|
|
grep "refusing to fetch into branch" err
|
|
|
|
'
|
|
|
|
|
2022-06-14 21:27:33 +02:00
|
|
|
test_expect_success 'refuse to overwrite when in error states' '
|
|
|
|
test_when_finished rm -rf .git/worktrees/wt-*/rebase-merge &&
|
|
|
|
test_when_finished rm -rf .git/worktrees/wt-*/BISECT_* &&
|
|
|
|
|
|
|
|
# Both branches are currently under rebase.
|
|
|
|
mkdir -p .git/worktrees/wt-3/rebase-merge &&
|
|
|
|
touch .git/worktrees/wt-3/rebase-merge/interactive &&
|
|
|
|
echo refs/heads/fake-1 >.git/worktrees/wt-3/rebase-merge/head-name &&
|
|
|
|
echo refs/heads/fake-2 >.git/worktrees/wt-3/rebase-merge/onto &&
|
|
|
|
mkdir -p .git/worktrees/wt-4/rebase-merge &&
|
|
|
|
touch .git/worktrees/wt-4/rebase-merge/interactive &&
|
|
|
|
echo refs/heads/fake-2 >.git/worktrees/wt-4/rebase-merge/head-name &&
|
|
|
|
echo refs/heads/fake-1 >.git/worktrees/wt-4/rebase-merge/onto &&
|
|
|
|
|
|
|
|
# Both branches are currently under bisect.
|
|
|
|
touch .git/worktrees/wt-4/BISECT_LOG &&
|
|
|
|
echo refs/heads/fake-2 >.git/worktrees/wt-4/BISECT_START &&
|
|
|
|
touch .git/worktrees/wt-1/BISECT_LOG &&
|
|
|
|
echo refs/heads/fake-1 >.git/worktrees/wt-1/BISECT_START &&
|
|
|
|
|
|
|
|
for i in 1 2
|
|
|
|
do
|
|
|
|
test_must_fail git branch -f fake-$i HEAD 2>err &&
|
|
|
|
grep "cannot force update the branch '\''fake-$i'\'' checked out at" err ||
|
|
|
|
return 1
|
|
|
|
done
|
|
|
|
'
|
|
|
|
|
2022-06-14 21:27:29 +02:00
|
|
|
test_done
|