Skip to content

Commit 4a2e91d

Browse files
committed
Merge branch 'hv/receive-denycurrent-everywhere'
"git push" should stop from updating a branch that is checked out when receive.denyCurrentBranch configuration is set, but it failed to pay attention to checkouts in secondary worktrees. This has been corrected. * hv/receive-denycurrent-everywhere: t2402: test worktree path when called in .git directory receive.denyCurrentBranch: respect all worktrees t5509: use a bare repository for test push target get_main_worktree(): allow it to be called in the Git directory
2 parents 49e5043 + 4d86489 commit 4a2e91d

File tree

5 files changed

+49
-18
lines changed

5 files changed

+49
-18
lines changed

builtin/receive-pack.c

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "object-store.h"
2828
#include "protocol.h"
2929
#include "commit-reach.h"
30+
#include "worktree.h"
3031

3132
static const char * const receive_pack_usage[] = {
3233
N_("git receive-pack <git-dir>"),
@@ -816,16 +817,6 @@ static int run_update_hook(struct command *cmd)
816817
return finish_command(&proc);
817818
}
818819

819-
static int is_ref_checked_out(const char *ref)
820-
{
821-
if (is_bare_repository())
822-
return 0;
823-
824-
if (!head_name)
825-
return 0;
826-
return !strcmp(head_name, ref);
827-
}
828-
829820
static char *refuse_unconfigured_deny_msg =
830821
N_("By default, updating the current branch in a non-bare repository\n"
831822
"is denied, because it will make the index and work tree inconsistent\n"
@@ -997,16 +988,26 @@ static const char *push_to_checkout(unsigned char *hash,
997988
return NULL;
998989
}
999990

1000-
static const char *update_worktree(unsigned char *sha1)
991+
static const char *update_worktree(unsigned char *sha1, const struct worktree *worktree)
1001992
{
1002-
const char *retval;
1003-
const char *work_tree = git_work_tree_cfg ? git_work_tree_cfg : "..";
993+
const char *retval, *work_tree, *git_dir = NULL;
1004994
struct argv_array env = ARGV_ARRAY_INIT;
1005995

996+
if (worktree && worktree->path)
997+
work_tree = worktree->path;
998+
else if (git_work_tree_cfg)
999+
work_tree = git_work_tree_cfg;
1000+
else
1001+
work_tree = "..";
1002+
10061003
if (is_bare_repository())
10071004
return "denyCurrentBranch = updateInstead needs a worktree";
1005+
if (worktree)
1006+
git_dir = get_worktree_git_dir(worktree);
1007+
if (!git_dir)
1008+
git_dir = get_git_dir();
10081009

1009-
argv_array_pushf(&env, "GIT_DIR=%s", absolute_path(get_git_dir()));
1010+
argv_array_pushf(&env, "GIT_DIR=%s", absolute_path(git_dir));
10101011

10111012
if (!find_hook(push_to_checkout_hook))
10121013
retval = push_to_deploy(sha1, &env, work_tree);
@@ -1026,6 +1027,7 @@ static const char *update(struct command *cmd, struct shallow_info *si)
10261027
struct object_id *old_oid = &cmd->old_oid;
10271028
struct object_id *new_oid = &cmd->new_oid;
10281029
int do_update_worktree = 0;
1030+
const struct worktree *worktree = is_bare_repository() ? NULL : find_shared_symref("HEAD", name);
10291031

10301032
/* only refs/... are allowed */
10311033
if (!starts_with(name, "refs/") || check_refname_format(name + 5, 0)) {
@@ -1037,7 +1039,7 @@ static const char *update(struct command *cmd, struct shallow_info *si)
10371039
free(namespaced_name);
10381040
namespaced_name = strbuf_detach(&namespaced_name_buf, NULL);
10391041

1040-
if (is_ref_checked_out(namespaced_name)) {
1042+
if (worktree) {
10411043
switch (deny_current_branch) {
10421044
case DENY_IGNORE:
10431045
break;
@@ -1069,7 +1071,7 @@ static const char *update(struct command *cmd, struct shallow_info *si)
10691071
return "deletion prohibited";
10701072
}
10711073

1072-
if (head_name && !strcmp(namespaced_name, head_name)) {
1074+
if (worktree || (head_name && !strcmp(namespaced_name, head_name))) {
10731075
switch (deny_delete_current) {
10741076
case DENY_IGNORE:
10751077
break;
@@ -1118,7 +1120,7 @@ static const char *update(struct command *cmd, struct shallow_info *si)
11181120
}
11191121

11201122
if (do_update_worktree) {
1121-
ret = update_worktree(new_oid->hash);
1123+
ret = update_worktree(new_oid->hash, find_shared_symref("HEAD", name));
11221124
if (ret)
11231125
return ret;
11241126
}

t/t2402-worktree-list.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,4 +151,10 @@ test_expect_success 'linked worktrees are sorted' '
151151
test_cmp expected sorted/main/actual
152152
'
153153

154+
test_expect_success 'worktree path when called in .git directory' '
155+
git worktree list >list1&&
156+
git -C .git worktree list >list2 &&
157+
test_cmp list1 list2
158+
'
159+
154160
test_done

t/t5509-fetch-push-namespaces.sh

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ test_expect_success setup '
2020
) &&
2121
commit0=$(cd original && git rev-parse HEAD^) &&
2222
commit1=$(cd original && git rev-parse HEAD) &&
23-
git init pushee &&
23+
git init --bare pushee &&
2424
git init puller
2525
'
2626

@@ -152,4 +152,15 @@ test_expect_success 'clone chooses correct HEAD (v2)' '
152152
test_cmp expect actual
153153
'
154154

155+
test_expect_success 'denyCurrentBranch and unborn branch with ref namespace' '
156+
(
157+
cd original &&
158+
git init unborn &&
159+
git remote add unborn-namespaced "ext::git --namespace=namespace %s unborn" &&
160+
test_must_fail git push unborn-namespaced HEAD:master &&
161+
git -C unborn config receive.denyCurrentBranch updateInstead &&
162+
git push unborn-namespaced HEAD:master
163+
)
164+
'
165+
155166
test_done

t/t5516-fetch-push.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1712,4 +1712,15 @@ test_expect_success 'updateInstead with push-to-checkout hook' '
17121712
)
17131713
'
17141714

1715+
test_expect_success 'denyCurrentBranch and worktrees' '
1716+
git worktree add new-wt &&
1717+
git clone . cloned &&
1718+
test_commit -C cloned first &&
1719+
test_config receive.denyCurrentBranch refuse &&
1720+
test_must_fail git -C cloned push origin HEAD:new-wt &&
1721+
test_config receive.denyCurrentBranch updateInstead &&
1722+
git -C cloned push origin HEAD:new-wt &&
1723+
test_must_fail git -C cloned push --delete origin new-wt
1724+
'
1725+
17151726
test_done

worktree.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ static struct worktree *get_main_worktree(void)
5050
struct strbuf worktree_path = STRBUF_INIT;
5151

5252
strbuf_add_absolute_path(&worktree_path, get_git_common_dir());
53+
strbuf_strip_suffix(&worktree_path, "/.");
5354
if (!strbuf_strip_suffix(&worktree_path, "/.git"))
5455
strbuf_strip_suffix(&worktree_path, "/.");
5556

0 commit comments

Comments
 (0)