Skip to content

Commit 4ef3464

Browse files
harry-hovgitster
authored andcommitted
receive.denyCurrentBranch: respect all worktrees
The receive.denyCurrentBranch config option controls what happens if you push to a branch that is checked out into a non-bare repository. By default, it rejects it. It can be disabled via `ignore` or `warn`. Another yet trickier option is `updateInstead`. However, this setting was forgotten when the git worktree command was introduced: only the main worktree's current branch is respected. With this change, all worktrees are respected. That change also leads to revealing another bug, i.e. `receive.denyCurrentBranch = true` was ignored when pushing into a non-bare repository's unborn current branch using ref namespaces. As `is_ref_checked_out()` returns 0 which means `receive-pack` does not get into conditional statement to switch `deny_current_branch` accordingly (ignore, warn, refuse, unconfigured, updateInstead). receive.denyCurrentBranch uses the function `refs_resolve_ref_unsafe()` (called via `resolve_refdup()`) to resolve the symbolic ref HEAD, but that function fails when HEAD does not point at a valid commit. As we replace the call to `refs_resolve_ref_unsafe()` with `find_shared_symref()`, which has no problem finding the worktree for a given branch even if it is unborn yet, this bug is fixed at the same time: receive.denyCurrentBranch now also handles worktrees with unborn branches as intended even while using ref namespaces. Helped-by: Johannes Schindelin <[email protected]> Signed-off-by: Hariom Verma <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent f869211 commit 4ef3464

File tree

3 files changed

+41
-17
lines changed

3 files changed

+41
-17
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/t5509-fetch-push-namespaces.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

0 commit comments

Comments
 (0)