git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: "Hariom Verma via GitGitGadget" <gitgitgadget@gmail.com>
To: git@vger.kernel.org
Cc: Hariom Verma <hariom18599@gmail.com>,
	Hariom Verma <hariom18599@gmail.com>
Subject: [PATCH 3/3] receive.denyCurrentBranch: respect all worktrees
Date: Thu, 13 Feb 2020 18:59:10 +0000	[thread overview]
Message-ID: <3352c0bffc19f17518b292ad36c38f902801b06a.1581620351.git.gitgitgadget@gmail.com> (raw)
In-Reply-To: <pull.535.git.1581620351.gitgitgadget@gmail.com>

From: Hariom Verma <hariom18599@gmail.com>

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.  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.

Helped-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Hariom Verma <hariom18599@gmail.com>
---
 builtin/receive-pack.c | 37 ++++++++++++++++++++-----------------
 t/t5516-fetch-push.sh  | 11 +++++++++++
 2 files changed, 31 insertions(+), 17 deletions(-)

diff --git a/builtin/receive-pack.c b/builtin/receive-pack.c
index 411e0b4d99..b5ca3123b7 100644
--- a/builtin/receive-pack.c
+++ b/builtin/receive-pack.c
@@ -27,6 +27,7 @@
 #include "object-store.h"
 #include "protocol.h"
 #include "commit-reach.h"
+#include "worktree.h"
 
 static const char * const receive_pack_usage[] = {
 	N_("git receive-pack <git-dir>"),
@@ -816,16 +817,6 @@ static int run_update_hook(struct command *cmd)
 	return finish_command(&proc);
 }
 
-static int is_ref_checked_out(const char *ref)
-{
-	if (is_bare_repository())
-		return 0;
-
-	if (!head_name)
-		return 0;
-	return !strcmp(head_name, ref);
-}
-
 static char *refuse_unconfigured_deny_msg =
 	N_("By default, updating the current branch in a non-bare repository\n"
 	   "is denied, because it will make the index and work tree inconsistent\n"
@@ -997,16 +988,27 @@ static const char *push_to_checkout(unsigned char *hash,
 		return NULL;
 }
 
-static const char *update_worktree(unsigned char *sha1)
+static const char *update_worktree(unsigned char *sha1, const struct worktree *worktree)
 {
-	const char *retval;
-	const char *work_tree = git_work_tree_cfg ? git_work_tree_cfg : "..";
+	const char *retval, *work_tree, *git_dir = NULL;
 	struct argv_array env = ARGV_ARRAY_INIT;
 
+	if (worktree && worktree->path)
+		work_tree = worktree->path;
+	else if (git_work_tree_cfg)
+		work_tree = git_work_tree_cfg;
+	else
+		work_tree = "..";
+
 	if (is_bare_repository())
 		return "denyCurrentBranch = updateInstead needs a worktree";
+	
+	if (worktree)
+		git_dir = get_worktree_git_dir(worktree);
+	if (!git_dir)
+		git_dir = get_git_dir();
 
-	argv_array_pushf(&env, "GIT_DIR=%s", absolute_path(get_git_dir()));
+	argv_array_pushf(&env, "GIT_DIR=%s", absolute_path(git_dir));
 
 	if (!find_hook(push_to_checkout_hook))
 		retval = push_to_deploy(sha1, &env, work_tree);
@@ -1026,6 +1028,7 @@ static const char *update(struct command *cmd, struct shallow_info *si)
 	struct object_id *old_oid = &cmd->old_oid;
 	struct object_id *new_oid = &cmd->new_oid;
 	int do_update_worktree = 0;
+	const struct worktree *worktree = is_bare_repository() ? NULL : find_shared_symref("HEAD", name);
 
 	/* only refs/... are allowed */
 	if (!starts_with(name, "refs/") || check_refname_format(name + 5, 0)) {
@@ -1037,7 +1040,7 @@ static const char *update(struct command *cmd, struct shallow_info *si)
 	free(namespaced_name);
 	namespaced_name = strbuf_detach(&namespaced_name_buf, NULL);
 
-	if (is_ref_checked_out(namespaced_name)) {
+	if (worktree) {
 		switch (deny_current_branch) {
 		case DENY_IGNORE:
 			break;
@@ -1069,7 +1072,7 @@ static const char *update(struct command *cmd, struct shallow_info *si)
 			return "deletion prohibited";
 		}
 
-		if (head_name && !strcmp(namespaced_name, head_name)) {
+		if (worktree || (head_name && !strcmp(namespaced_name, head_name))) {
 			switch (deny_delete_current) {
 			case DENY_IGNORE:
 				break;
@@ -1118,7 +1121,7 @@ static const char *update(struct command *cmd, struct shallow_info *si)
 	}
 
 	if (do_update_worktree) {
-		ret = update_worktree(new_oid->hash);
+		ret = update_worktree(new_oid->hash, find_shared_symref("HEAD", name));
 		if (ret)
 			return ret;
 	}
diff --git a/t/t5516-fetch-push.sh b/t/t5516-fetch-push.sh
index c81ca360ac..6608e391f0 100755
--- a/t/t5516-fetch-push.sh
+++ b/t/t5516-fetch-push.sh
@@ -1712,4 +1712,15 @@ test_expect_success 'updateInstead with push-to-checkout hook' '
 	)
 '
 
+test_expect_success 'denyCurrentBranch and worktrees' '
+    git worktree add new-wt &&
+	git clone . cloned &&
+	test_commit -C cloned first &&
+	test_config receive.denyCurrentBranch refuse &&
+	test_must_fail git -C cloned push origin HEAD:new-wt &&
+	test_config receive.denyCurrentBranch updateInstead &&
+	git -C cloned push origin HEAD:new-wt &&
+	test_must_fail git -C cloned push --delete origin new-wt
+'
+
 test_done
-- 
gitgitgadget

  parent reply	other threads:[~2020-02-13 18:59 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-02-13 18:59 [PATCH 0/3] [GSoC] receive.denyCurrentBranch: respect all worktrees Hariom Verma via GitGitGadget
2020-02-13 18:59 ` [PATCH 1/3] get_main_worktree(): allow it to be called in the Git directory Hariom Verma via GitGitGadget
2020-02-13 18:59 ` [PATCH 2/3] t5509: initialized `pushee` as bare repository Hariom Verma via GitGitGadget
2020-02-13 20:14   ` Junio C Hamano
2020-02-13 20:34     ` Junio C Hamano
2020-02-14 11:59     ` Johannes Schindelin
2020-02-14 15:03       ` Junio C Hamano
2020-02-15 21:52         ` Hariom verma
2020-02-16 23:49           ` Junio C Hamano
2020-02-22 22:54             ` Hariom verma
2020-02-13 18:59 ` Hariom Verma via GitGitGadget [this message]
2020-02-22 22:35 ` [PATCH v2 0/4] [GSoC] receive.denyCurrentBranch: respect all worktrees Hariom Verma via GitGitGadget
2020-02-22 22:35   ` [PATCH v2 1/4] get_main_worktree(): allow it to be called in the Git directory Hariom Verma via GitGitGadget
2020-02-22 22:35   ` [PATCH v2 2/4] t5509: initialized `pushee` as bare repository Hariom Verma via GitGitGadget
2020-02-23  6:24     ` Junio C Hamano
2020-02-22 22:35   ` [PATCH v2 3/4] bug: denyCurrentBranch and unborn branch with ref namespace Hariom Verma via GitGitGadget
2020-02-23  6:10     ` Junio C Hamano
2020-02-22 22:35   ` [PATCH v2 4/4] receive.denyCurrentBranch: respect all worktrees Hariom Verma via GitGitGadget
2020-02-23  6:18     ` Junio C Hamano
2020-02-23 18:57   ` [PATCH v3 0/3] [GSoC] " Hariom Verma via GitGitGadget
2020-02-23 18:57     ` [PATCH v3 1/3] get_main_worktree(): allow it to be called in the Git directory Hariom Verma via GitGitGadget
2020-02-24  1:42       ` Eric Sunshine
2020-02-24 11:09         ` Hariom verma
2020-02-24 17:00           ` Eric Sunshine
2020-02-24 18:58           ` Johannes Schindelin
2020-02-24 22:27             ` Eric Sunshine
2020-02-27 15:58               ` Johannes Schindelin
2020-02-24 19:13           ` Junio C Hamano
2020-02-23 18:57     ` [PATCH v3 2/3] t5509: use a bare repository for test push target Hariom Verma via GitGitGadget
2020-02-23 18:57     ` [PATCH v3 3/3] receive.denyCurrentBranch: respect all worktrees Hariom Verma via GitGitGadget

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: http://vger.kernel.org/majordomo-info.html

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=3352c0bffc19f17518b292ad36c38f902801b06a.1581620351.git.gitgitgadget@gmail.com \
    --to=gitgitgadget@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=hariom18599@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).