Skip to content

Commit 9fdf4f1

Browse files
anderskgitster
authored andcommitted
receive-pack: protect current branch for bare repository worktree
A bare repository won’t have a working tree at "..", but it may still have separate working trees created with git worktree. We should protect the current branch of such working trees from being updated or deleted, according to receive.denyCurrentBranch. Signed-off-by: Anders Kaseorg <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 38baae6 commit 9fdf4f1

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

builtin/receive-pack.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1455,7 +1455,7 @@ static const char *update_worktree(unsigned char *sha1, const struct worktree *w
14551455
if (!worktree || !worktree->path)
14561456
BUG("worktree->path must be non-NULL");
14571457

1458-
if (is_bare_repository())
1458+
if (worktree->is_bare)
14591459
return "denyCurrentBranch = updateInstead needs a worktree";
14601460
git_dir = get_worktree_git_dir(worktree);
14611461

@@ -1481,9 +1481,7 @@ static const char *update(struct command *cmd, struct shallow_info *si)
14811481
int do_update_worktree = 0;
14821482
struct worktree **worktrees = get_worktrees();
14831483
const struct worktree *worktree =
1484-
is_bare_repository() ?
1485-
NULL :
1486-
find_shared_symref(worktrees, "HEAD", name);
1484+
find_shared_symref(worktrees, "HEAD", name);
14871485

14881486
/* only refs/... are allowed */
14891487
if (!starts_with(name, "refs/") || check_refname_format(name + 5, 0)) {
@@ -1496,7 +1494,7 @@ static const char *update(struct command *cmd, struct shallow_info *si)
14961494
free(namespaced_name);
14971495
namespaced_name = strbuf_detach(&namespaced_name_buf, NULL);
14981496

1499-
if (worktree) {
1497+
if (worktree && !worktree->is_bare) {
15001498
switch (deny_current_branch) {
15011499
case DENY_IGNORE:
15021500
break;

t/t5516-fetch-push.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1769,9 +1769,23 @@ test_expect_success 'denyCurrentBranch and worktrees' '
17691769
test_must_fail git -C cloned push origin HEAD:new-wt &&
17701770
test_config receive.denyCurrentBranch updateInstead &&
17711771
git -C cloned push origin HEAD:new-wt &&
1772+
test_path_exists new-wt/first.t &&
17721773
test_must_fail git -C cloned push --delete origin new-wt
17731774
'
17741775

1776+
test_expect_success 'denyCurrentBranch and bare repository worktrees' '
1777+
test_when_finished "rm -fr bare.git" &&
1778+
git clone --bare . bare.git &&
1779+
git -C bare.git worktree add wt &&
1780+
test_commit grape &&
1781+
git -C bare.git config receive.denyCurrentBranch refuse &&
1782+
test_must_fail git push bare.git HEAD:wt &&
1783+
git -C bare.git config receive.denyCurrentBranch updateInstead &&
1784+
git push bare.git HEAD:wt &&
1785+
test_path_exists bare.git/wt/grape.t &&
1786+
test_must_fail git push --delete bare.git wt
1787+
'
1788+
17751789
test_expect_success 'refuse fetch to current branch of worktree' '
17761790
test_when_finished "git worktree remove --force wt && git branch -D wt" &&
17771791
git worktree add wt &&

0 commit comments

Comments
 (0)