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

[PATCH 4/4] branch: use branch_checked_out() when deleting refs

From
Derrick Stolee via GitGitGadget <gitgitgadget@gmail.com>
Date
Jun 8, 2022, 20:09 UTC
Message-ID
<af645b43032e8b138da3aae1732047208b3b2890.1654718942.git.gitgitgadget@gmail.com>
In-Reply-To
<pull.1254.git.1654718942.gitgitgadget@gmail.com>
From: Derrick Stolee <derrickstolee@github.com>

This is the last current use of find_shared_symref() that can easily be replaced by branch_checked_out(). The benefit of this switch is that the code is a bit simpler, but also it is faster on repeated calls.

The remaining uses of find_shared_symref() are non-trivial to remove, so we probably should not continue in that direction:

* builtin/notes.c uses find_shared_symref() with "NOTES_MERGE_REF"
  instead of "HEAD", so it doesn't have an immediate analogue with
  branch_checked_out(). Perhaps we should consider extending it to
  include that symref in addition to HEAD, BISECT_HEAD, and
  REBASE_HEAD.
* receive-pack.c checks to see if a worktree has a checkout for the ref
  that is being updated. The tricky part is that it can actually decide
  to update the worktree directly instead of just skipping the update.
  This all depends on the receive.denyCurrentBranch config option. The
  implementation currenty cares about receiving the worktree in the
  result, so the current branch_checked_out() prototype is insufficient
  currently. This is something to investigate later, though, since a
  large number of refs could be updated at the same time and using the
  strmap implementation of branch_checked_out() could be beneficial.
Signed-off-by: Derrick Stolee <derrickstolee@github.com>
---
 builtin/branch.c          | 8 ++++----
 t/t2407-worktree-heads.sh | 5 ++++-
 2 files changed, 8 insertions(+), 5 deletions(-)
diff --git a/builtin/branch.c b/builtin/branch.c
index 5d00d0b8d32..8e11e433840 100644
--- a/builtin/branch.c
+++ b/builtin/branch.c
@@ -253,12 +253,12 @@ static int delete_branches(int argc, const char **argv, int force, int kinds,
 		name = mkpathdup(fmt, bname.buf);
 
 		if (kinds == FILTER_REFS_BRANCHES) {
-			const struct worktree *wt =
-				find_shared_symref(worktrees, "HEAD", name);
-			if (wt) {
+			char *path;
+			if (branch_checked_out(name, &path)) {
 				error(_("Cannot delete branch '%s' "
 					"checked out at '%s'"),
-				      bname.buf, wt->path);
+				      bname.buf, path);
+				free(path);
 				ret = 1;
 				continue;
 			}
diff --git a/t/t2407-worktree-heads.sh b/t/t2407-worktree-heads.sh
index f3f8b0b2b79..6dcc0d39a2d 100755
--- a/t/t2407-worktree-heads.sh
+++ b/t/t2407-worktree-heads.sh
@@ -26,7 +26,10 @@ test_expect_success 'refuse to overwrite: checked out in worktree' '
 	for i in 1 2 3 4
 	do
 		test_must_fail git branch -f wt-$i HEAD 2>err
-		grep "cannot force update the branch" err || return 1
+		grep "cannot force update the branch" err &&
+
+		test_must_fail git branch -D wt-$i 2>err
+		grep "Cannot delete branch" err || return 1
 	done
 '
 
-- 
gitgitgadget
Previous: Derrick StoleeNext: Derrick Stolee
Message 16 of 28 in “Create branch_checked_out() helper”
  1. 0/4 Create branch_checked_out() helperDerrick Stolee via GitGitGadget, Jun 8, 2022
  2. 1/4 branch: add branch_checked_out() helperDerrick Stolee via GitGitGadget, Jun 8, 2022
  3. Junio C HamanoJun 9, 2022
  4. Ævar Arnfjörð BjarmasonJun 13, 2022
  5. Derrick StoleeJun 14, 2022
  6. Ævar Arnfjörð BjarmasonJun 14, 2022
  7. Phillip WoodJun 14, 2022
  8. Phillip WoodJun 14, 2022
  9. Derrick StoleeJun 14, 2022
  10. 2/4 branch: check for bisects and rebasesDerrick Stolee via GitGitGadget, Jun 8, 2022
  11. Junio C HamanoJun 8, 2022
  12. 3/4 fetch: use new branch_checked_out() and add testsDerrick Stolee via GitGitGadget, Jun 8, 2022
  13. Ævar Arnfjörð BjarmasonJun 14, 2022
  14. Phillip WoodJun 14, 2022
  15. Derrick StoleeJun 14, 2022
  16. 4/4 branch: use branch_checked_out() when deleting refsDerrick Stolee via GitGitGadget, Jun 8, 2022
  17. 5/5 branch: fix branch_checked_out() leaksDerrick Stolee, Jun 13, 2022
  18. Junio C HamanoJun 13, 2022
  19. Ævar Arnfjörð BjarmasonJun 14, 2022
  20. Derrick StoleeJun 14, 2022
  21. Ævar Arnfjörð BjarmasonJun 14, 2022
  22. 0/5 Create branch_checked_out() helperDerrick Stolee via GitGitGadget, Jun 14, 2022
  23. 1/5 branch: add branch_checked_out() helperDerrick Stolee via GitGitGadget, Jun 14, 2022
  24. 2/5 branch: check for bisects and rebasesDerrick Stolee via GitGitGadget, Jun 14, 2022
  25. 3/5 fetch: use new branch_checked_out() and add testsDerrick Stolee via GitGitGadget, Jun 14, 2022
  26. 4/5 branch: use branch_checked_out() when deleting refsDerrick Stolee via GitGitGadget, Jun 14, 2022
  27. 5/5 branch: fix branch_checked_out() leaksDerrick Stolee via GitGitGadget, Jun 14, 2022
  28. Phillip WoodJun 19, 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.