git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v7 7/8] receive-pack: protect current branch for bare repository worktree

From
Anders Kaseorg <andersk@mit.edu>
Date
Dec 1, 2021, 22:15 UTC
Message-ID
<20211201221547.1796213-8-andersk@mit.edu>
In-Reply-To
<20211201221547.1796213-1-andersk@mit.edu>

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 <andersk@mit.edu>
---
 builtin/receive-pack.c |  8 +++-----
 t/t5516-fetch-push.sh  | 14 ++++++++++++++
 2 files changed, 17 insertions(+), 5 deletions(-)
diff --git a/builtin/receive-pack.c b/builtin/receive-pack.c
index 29dc0e56f7..26ecaa5cd5 100644
--- a/builtin/receive-pack.c
+++ b/builtin/receive-pack.c
@@ -1455,7 +1455,7 @@ static const char *update_worktree(unsigned char *sha1, const struct worktree *w
 	if (!worktree || !worktree->path)
 		BUG("worktree->path must be non-NULL");
 
-	if (is_bare_repository())
+	if (worktree->is_bare)
 		return "denyCurrentBranch = updateInstead needs a worktree";
 	git_dir = get_worktree_git_dir(worktree);
 
@@ -1481,9 +1481,7 @@ static const char *update(struct command *cmd, struct shallow_info *si)
 	int do_update_worktree = 0;
 	struct worktree **worktrees = get_worktrees();
 	const struct worktree *worktree =
-		is_bare_repository() ?
-			NULL :
-			find_shared_symref(worktrees, "HEAD", name);
+		find_shared_symref(worktrees, "HEAD", name);
 
 	/* only refs/... are allowed */
 	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)
 	free(namespaced_name);
 	namespaced_name = strbuf_detach(&namespaced_name_buf, NULL);
 
-	if (worktree) {
+	if (worktree && !worktree->is_bare) {
 		switch (deny_current_branch) {
 		case DENY_IGNORE:
 			break;
diff --git a/t/t5516-fetch-push.sh b/t/t5516-fetch-push.sh
index f07e32126f..cd2e144c37 100755
--- a/t/t5516-fetch-push.sh
+++ b/t/t5516-fetch-push.sh
@@ -1769,9 +1769,23 @@ test_expect_success 'denyCurrentBranch and worktrees' '
 	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_path_exists new-wt/first.t &&
 	test_must_fail git -C cloned push --delete origin new-wt
 '
 
+test_expect_success 'denyCurrentBranch and bare repository worktrees' '
+	test_when_finished "rm -fr bare.git" &&
+	git clone --bare . bare.git &&
+	git -C bare.git worktree add wt &&
+	test_commit grape &&
+	git -C bare.git config receive.denyCurrentBranch refuse &&
+	test_must_fail git push bare.git HEAD:wt &&
+	git -C bare.git config receive.denyCurrentBranch updateInstead &&
+	git push bare.git HEAD:wt &&
+	test_path_exists bare.git/wt/grape.t &&
+	test_must_fail git push --delete bare.git wt
+'
+
 test_expect_success 'refuse fetch to current branch of worktree' '
 	test_when_finished "git worktree remove --force wt && git branch -D wt" &&
 	git worktree add wt &&
-- 
2.34.1
Previous: Anders KaseorgNext: Anders Kaseorg
Message 16 of 20 in “protect branches checked out in all worktrees”
  1. 0/8 protect branches checked out in all worktreesAnders Kaseorg, Dec 1, 2021
  2. 1/8 fetch: lowercase error messagesAnders Kaseorg, Dec 1, 2021
  3. 3/8 branch: lowercase error messagesAnders Kaseorg, Dec 1, 2021
  4. 4/8 worktree: simplify find_shared_symref() memory ownership modelAnders Kaseorg, Dec 1, 2021
  5. Eric SunshineDec 1, 2021
  6. Anders KaseorgDec 1, 2021
  7. Eric SunshineDec 2, 2021
  8. Eric SunshineDec 2, 2021
  9. Anders KaseorgDec 2, 2021
  10. Eric SunshineDec 2, 2021
  11. 2/8 receive-pack: lowercase error messagesAnders Kaseorg, Dec 1, 2021
  12. 5/8 fetch: protect branches checked out in all worktreesAnders Kaseorg, Dec 1, 2021
  13. Eric SunshineDec 2, 2021
  14. Anders KaseorgDec 2, 2021
  15. 6/8 receive-pack: clean dead code from update_worktree()Anders Kaseorg, Dec 1, 2021
  16. 7/8 receive-pack: protect current branch for bare repository worktreeAnders Kaseorg, Dec 1, 2021
  17. 8/8 branch: protect branches checked out in all worktreesAnders Kaseorg, Dec 1, 2021
  18. Jiang XinDec 23, 2021
  19. Jiang XinJan 12, 2022
  20. Junio C HamanoJan 12, 2022

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.