@matheusbsbs / matheus-bagliano / commits / 4ef346482d
Fork 0

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 <johannes.schindelin@gmx.de> Signed-off-by: Hariom Verma <hariom18599@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Hariom Verma committed Feb 23, 2020 at 18:57 UTC 4ef346482d6d5748861c1aa9d56712e847369b40
3 files changed +41 -17
builtin/receive-pack.c
+19 -17
@@ -27,6 +27,7 @@
27 #include "object-store.h"
28 #include "protocol.h"
29 #include "commit-reach.h"
30 +#include "worktree.h"
31
32 static const char * const receive_pack_usage[] = {
33 N_("git receive-pack <git-dir>"),
@@ -816,16 +817,6 @@ static int run_update_hook(struct command *cmd)
817 return finish_command(&proc);
818 }
819
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 -
820 static char *refuse_unconfigured_deny_msg =
821 N_("By default, updating the current branch in a non-bare repository\n"
822 "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,
988 return NULL;
989 }
990
1000 -static const char *update_worktree(unsigned char *sha1)
991 +static const char *update_worktree(unsigned char *sha1, const struct worktree *worktree)
992 {
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;
994 struct argv_array env = ARGV_ARRAY_INIT;
995
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 +
1003 if (is_bare_repository())
1004 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();
1009
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));
1011
1012 if (!find_hook(push_to_checkout_hook))
1013 retval = push_to_deploy(sha1, &env, work_tree);
@@ -1026,6 +1027,7 @@ static const char *update(struct command *cmd, struct shallow_info *si)
1027 struct object_id *old_oid = &cmd->old_oid;
1028 struct object_id *new_oid = &cmd->new_oid;
1029 int do_update_worktree = 0;
1030 + const struct worktree *worktree = is_bare_repository() ? NULL : find_shared_symref("HEAD", name);
1031
1032 /* only refs/... are allowed */
1033 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)
1039 free(namespaced_name);
1040 namespaced_name = strbuf_detach(&namespaced_name_buf, NULL);
1041
1040 - if (is_ref_checked_out(namespaced_name)) {
1042 + if (worktree) {
1043 switch (deny_current_branch) {
1044 case DENY_IGNORE:
1045 break;
@@ -1069,7 +1071,7 @@ static const char *update(struct command *cmd, struct shallow_info *si)
1071 return "deletion prohibited";
1072 }
1073
1072 - if (head_name && !strcmp(namespaced_name, head_name)) {
1074 + if (worktree || (head_name && !strcmp(namespaced_name, head_name))) {
1075 switch (deny_delete_current) {
1076 case DENY_IGNORE:
1077 break;
@@ -1118,7 +1120,7 @@ static const char *update(struct command *cmd, struct shallow_info *si)
1120 }
1121
1122 if (do_update_worktree) {
1121 - ret = update_worktree(new_oid->hash);
1123 + ret = update_worktree(new_oid->hash, find_shared_symref("HEAD", name));
1124 if (ret)
1125 return ret;
1126 }
t/t5509-fetch-push-namespaces.sh
+11
@@ -152,4 +152,15 @@ test_expect_success 'clone chooses correct HEAD (v2)' '
152 test_cmp expect actual
153 '
154
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 +
166 test_done
t/t5516-fetch-push.sh
+11
@@ -1712,4 +1712,15 @@ test_expect_success 'updateInstead with push-to-checkout hook' '
1712 )
1713 '
1714
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 +
1726 test_done