Skip to contentSkip to conversation

branch: report kind of checkout when rejecting delete

PATCH In review René Scharfe 4 participants 10 messages Jul 18 – Jul 29

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 branch: report kind of checkout when rejecting delete

git branch refuses to delete branches that are currently checked out with a message like this: "error: cannot delete branch 'foo' used by worktree at '/path/of/worktree'". This can be confusing with internal checkouts, e.g. if one tries to delete a branch associated with an active bisect run.

Mention the kind of internal checkout, if any, to spare the user from remembering that they might have forgotten a bisect or rebase. To do that, register the checkout reason in a strintmap alongside the existing strmap that stores the worktree path.

---
Original message:
https://lore.kernel.org/git/[email protected]/

 branch.c          | 48 ++++++++++++++++++++++++++---------------------
 branch.h          | 15 +++++++++++++++
 builtin/branch.c  | 31 +++++++++++++++++++++++++++---
 t/t3200-branch.sh |  4 ++--
 4 files changed, 72 insertions(+), 26 deletions(-)
branch.c +27 -21
index 243db7d0fc..aaa54f1b62 100644
@@ -384,6 +384,16 @@ int validate_branchname(const char *name, struct strbuf *ref)
 
 static int initialized_checked_out_branches;
 static struct strmap current_checked_out_branches = STRMAP_INIT;
+static struct strintmap current_checked_out_branch_kinds = STRINTMAP_INIT;
+
+static void register_checked_out_branch(const char *refname, const char *path,
+					enum branch_checkout_kind kind)
+{
+	char *old = strmap_put(&current_checked_out_branches, refname,
+			       xstrdup(path));
+	free(old);
+	strintmap_set(&current_checked_out_branch_kinds, refname, kind);
+}
 
 static void prepare_checked_out_branches(void)
 {
@@ -397,7 +407,7 @@ static void prepare_checked_out_branches(void)
 	worktrees = get_worktrees();
 
 	while (worktrees[i]) {
-		char *old, *wt_gitdir;
+		char *wt_gitdir;
 		struct wt_status_state state = { 0 };
 		struct worktree *wt = worktrees[i++];
 		struct string_list update_refs = STRING_LIST_INIT_DUP;
@@ -405,22 +415,17 @@ static void prepare_checked_out_branches(void)
 		if (wt->is_bare)
 			continue;
 
-		if (wt->head_ref) {
-			old = strmap_put(&current_checked_out_branches,
-					 wt->head_ref,
-					 xstrdup(wt->path));
-			free(old);
-		}
+		if (wt->head_ref)
+			register_checked_out_branch(wt->head_ref, wt->path,
+				BRANCH_CHECKOUT_KIND_CHECKOUT);
 
 		if (wt_status_check_rebase(wt, &state) &&
 		    (state.rebase_in_progress || state.rebase_interactive_in_progress) &&
 		    state.branch) {
 			struct strbuf ref = STRBUF_INIT;
 			strbuf_addf(&ref, "refs/heads/%s", state.branch);
-			old = strmap_put(&current_checked_out_branches,
-					 ref.buf,
-					 xstrdup(wt->path));
-			free(old);
+			register_checked_out_branch(ref.buf, wt->path,
+				BRANCH_CHECKOUT_KIND_REBASE);
 			strbuf_release(&ref);
 		}
 		wt_status_state_free_buffers(&state);
@@ -429,10 +434,8 @@ static void prepare_checked_out_branches(void)
 		    state.bisecting_from) {
 			struct strbuf ref = STRBUF_INIT;
 			strbuf_addf(&ref, "refs/heads/%s", state.bisecting_from);
-			old = strmap_put(&current_checked_out_branches,
-					 ref.buf,
-					 xstrdup(wt->path));
-			free(old);
+			register_checked_out_branch(ref.buf, wt->path,
+				BRANCH_CHECKOUT_KIND_BISECT);
 			strbuf_release(&ref);
 		}
 		wt_status_state_free_buffers(&state);
@@ -441,12 +444,9 @@ static void prepare_checked_out_branches(void)
 		if (!sequencer_get_update_refs_state(wt_gitdir,
 						     &update_refs)) {
 			struct string_list_item *item;
-			for_each_string_list_item(item, &update_refs) {
-				old = strmap_put(&current_checked_out_branches,
-						 item->string,
-						 xstrdup(wt->path));
-				free(old);
-			}
+			for_each_string_list_item(item, &update_refs)
+				register_checked_out_branch(item->string, wt->path,
+					BRANCH_CHECKOUT_KIND_UPDATE_REF);
 			string_list_clear(&update_refs, 1);
 		}
 
@@ -462,6 +462,12 @@ const char *branch_checked_out(const char *refname)
 	return strmap_get(&current_checked_out_branches, refname);
 }
 
+enum branch_checkout_kind branch_checkout_kind(const char *refname)
+{
+	prepare_checked_out_branches();
+	return strintmap_get(&current_checked_out_branch_kinds, refname);
+}
+
 /*
  * Check if a branch 'name' can be created as a new branch; die otherwise.
  * 'force' can be used when it is OK for the named branch already exists.
branch.h +15 -0
index 3dc6e2a0ff..d1073fe1cd 100644
@@ -15,6 +15,14 @@ enum branch_track {
 	BRANCH_TRACK_SIMPLE,
 };
 
+enum branch_checkout_kind {
+	BRANCH_CHECKOUT_KIND_UNSPECIFIED = 0,
+	BRANCH_CHECKOUT_KIND_CHECKOUT,
+	BRANCH_CHECKOUT_KIND_REBASE,
+	BRANCH_CHECKOUT_KIND_BISECT,
+	BRANCH_CHECKOUT_KIND_UPDATE_REF,
+};
+
 /* Functions for acting on the information about branches. */
 
 /**
@@ -106,6 +114,13 @@ void create_branches_recursively(struct repository *r, const char *name,
  */
 const char *branch_checked_out(const char *refname);
 
+/*
+ * If the branch at 'refname' is currently checked out in a worktree,
+ * then return the kind of checkout, i.e. whether it was done by an
+ * actual checkout or a rebase etc.
+ */
+enum branch_checkout_kind branch_checkout_kind(const char *refname);
+
 /*
  * Check if 'name' can be a valid name for a branch; die otherwise.
  * Return 1 if the named branch already exists; return 0 otherwise.
builtin/branch.c +28 -3
index dede60d27b..3223347129 100644
@@ -266,9 +266,34 @@ static int delete_branches(int argc, const char **argv, int force, int kinds,
 		if (kinds == FILTER_REFS_BRANCHES) {
 			const char *path;
 			if ((path = branch_checked_out(name))) {
-				error(_("cannot delete branch '%s' "
-					"used by worktree at '%s'"),
-				      bname.buf, path);
+				int kind = branch_checkout_kind(name);
+				switch (kind) {
+				case BRANCH_CHECKOUT_KIND_CHECKOUT:
+					error(_("cannot delete branch '%s' "
+						"used by worktree at '%s'"),
+					      bname.buf, path);
+					break;
+				case BRANCH_CHECKOUT_KIND_REBASE:
+					error(_("cannot delete branch '%s' "
+						"used by worktree at '%s' "
+						"for rebase"),
+					      bname.buf, path);
+					break;
+				case BRANCH_CHECKOUT_KIND_BISECT:
+					error(_("cannot delete branch '%s' "
+						"used by worktree at '%s' "
+						"for bisect"),
+					      bname.buf, path);
+					break;
+				case BRANCH_CHECKOUT_KIND_UPDATE_REF:
+					error(_("cannot delete branch '%s' "
+						"used by worktree at '%s' "
+						"for update-ref"),
+					      bname.buf, path);
+					break;
+				default:
+					BUG("invalid checkout kind %d", kind);
+				}
 				ret = 1;
 				continue;
 			}
t/t3200-branch.sh +2 -2
index e2682a83a0..e5df493b66 100755
@@ -930,7 +930,7 @@ test_expect_success 'deleting currently checked out branch fails' '
 	git worktree add -b my7 my7 &&
 	test_must_fail git -C my7 branch -d my7 &&
 	test_must_fail git branch -d my7 2>actual &&
-	grep "^error: cannot delete branch .my7. used by worktree at " actual &&
+	test_grep "^error: cannot delete branch '"'"'my7'"'"' used by worktree at '"'.*'\$"'" actual &&
 	rm -r my7 &&
 	git worktree prune
 '
@@ -941,7 +941,7 @@ test_expect_success 'deleting in-use branch fails' '
 	git -C my7 bisect start HEAD HEAD~2 &&
 	test_must_fail git -C my7 branch -d my7 &&
 	test_must_fail git branch -d my7 2>actual &&
-	grep "^error: cannot delete branch .my7. used by worktree at " actual &&
+	test_grep "^error: cannot delete branch '"'"'my7'"'"' used by worktree at '"'.*' for bisect\$"'" actual &&
 	rm -r my7 &&
 	git worktree prune
 '
Signature · 1 line
2.55.0
#1 lore raw
Quoted 11 lines · René Scharfe

git branch refuses to delete branches that are currently checked out with a message like this: "error: cannot delete branch 'foo' used by worktree at '/path/of/worktree'". This can be confusing with internal checkouts, e.g. if one tries to delete a branch associated with an active bisect run.

Mention the kind of internal checkout, if any, to spare the user from remembering that they might have forgotten a bisect or rebase. To do that, register the checkout reason in a strintmap alongside the existing strmap that stores the worktree path.

Quoted 5 lines

---
Original message:
https://lore.kernel.org/git/[email protected]/

This reminds me of another recent discussion on rewriting a branch that is checked out elsewhere, where the "git history" command forgot to apply the same safety check:

https://lore.kernel.org/git/[email protected]/

We definitely need an easy-to-use API to determine consistently which branches are in use, and to teach all commands that repoint branch tips to use it to offer the same safety to users. The framework that this patch introduces might be a good starting point for that effort.

Quoted 13 lines
branch.h +5 -0
index 3dc6e2a0ff..d1073fe1cd 100644
@@ -15,6 +15,14 @@ enum branch_track {
 	BRANCH_TRACK_SIMPLE,
 };
 
+enum branch_checkout_kind {
+	BRANCH_CHECKOUT_KIND_UNSPECIFIED = 0,
+	BRANCH_CHECKOUT_KIND_CHECKOUT,
+	BRANCH_CHECKOUT_KIND_REBASE,
+	BRANCH_CHECKOUT_KIND_BISECT,

branch.h enum branch_track {

Quoted 10 lines
branch.h +9 -0
+	BRANCH_CHECKOUT_KIND_UPDATE_REF,
+};
+
...
+/*
+ * If the branch at 'refname' is currently checked out in a worktree,
+ * then return the kind of checkout, i.e. whether it was done by an
+ * actual checkout or a rebase etc.
+ */
+enum branch_checkout_kind branch_checkout_kind(const char *refname);

OK.

builtin/branch.c static int delete_branches(int argc, const char **argv, int force, int kinds, · line 269

Quoted 12 lines
builtin/branch.c +1 -3
index dede60d27b..3223347129 100644
@@ -266,9 +266,34 @@ static int delete_branches(int argc, const char **argv, int force, int kinds,
 		if (kinds == FILTER_REFS_BRANCHES) {
 			const char *path;
 			if ((path = branch_checked_out(name))) {
-				error(_("cannot delete branch '%s' "
-					"used by worktree at '%s'"),
-				      bname.buf, path);
+				int kind = branch_checkout_kind(name);

Not "enum branch_checkout_kind" but "int"?

builtin/branch.c static int delete_branches(int argc, const char **argv, int force, int kinds, · line 275

Quoted 6 lines
builtin/branch.c +6 -0
+				switch (kind) {
+				case BRANCH_CHECKOUT_KIND_CHECKOUT:
+					error(_("cannot delete branch '%s' "
+						"used by worktree at '%s'"),
+					      bname.buf, path);
+					break;

We may want to be more explicit and say "cannot delete branch 'frotz' checked out in worktree at '/tmp/nitfol'" instead. Unless this is a catch-all entry for states that are neither 'rebase', 'bisect', nor 'rebase-merges' but are somehow otherwise in use, that is.

builtin/branch.c static int delete_branches(int argc, const char **argv, int force, int kinds, · line 293

Quoted 6 lines
builtin/branch.c +6 -0
+				case BRANCH_CHECKOUT_KIND_UPDATE_REF:
+					error(_("cannot delete branch '%s' "
+						"used by worktree at '%s' "
+						"for update-ref"),
+					      bname.buf, path);
+					break;

I was quite lost when searching for cases where this 'update-ref' state might be encountered, and I still lack confidence. Can we make the diagnostic message a bit friendlier to our users?

For instance, something like: 'You are rebasing a history with merges in that other worktree, and the tip of this branch will be updated when that process completes, so you cannot delete it from here.' (Naturally, I may have misidentified the exact nature of the error, but this illustrates the level of detail and user-facing clarity I hope to see.)

Quoted 14 lines
t/t3200-branch.sh +1 -1
index e2682a83a0..e5df493b66 100755
@@ -930,7 +930,7 @@ test_expect_success 'deleting currently checked out branch fails' '
 	git worktree add -b my7 my7 &&
 	test_must_fail git -C my7 branch -d my7 &&
 	test_must_fail git branch -d my7 2>actual &&
-	grep "^error: cannot delete branch .my7. used by worktree at " actual &&
+	test_grep "^error: cannot delete branch '"'"'my7'"'"' used by worktree at '"'.*'\$"'" actual &&
 	rm -r my7 &&
 	git worktree prune
 '

t/t3200-branch.sh test_expect_success 'deleting in-use branch fails' ' · line 947

Quoted 8 lines
t/t3200-branch.sh +1 -1
@@ -941,7 +941,7 @@ test_expect_success 'deleting in-use branch fails' '
 	git -C my7 bisect start HEAD HEAD~2 &&
 	test_must_fail git -C my7 branch -d my7 &&
 	test_must_fail git branch -d my7 2>actual &&
-	grep "^error: cannot delete branch .my7. used by worktree at " actual &&
+	test_grep "^error: cannot delete branch '"'"'my7'"'"' used by worktree at '"'.*' for bisect\$"'" actual &&
 	rm -r my7 &&
 	git worktree prune
 '

We distinguish four kinds in the code but we test only two of them?

Thanks. I very much like the direction this is taking us.

#2 lore raw
Quoted 23 lines · Junio C Hamano
Quoted 11 lines · René Scharfe

git branch refuses to delete branches that are currently checked out with a message like this: "error: cannot delete branch 'foo' used by worktree at '/path/of/worktree'". This can be confusing with internal checkouts, e.g. if one tries to delete a branch associated with an active bisect run.

Mention the kind of internal checkout, if any, to spare the user from remembering that they might have forgotten a bisect or rebase. To do that, register the checkout reason in a strintmap alongside the existing strmap that stores the worktree path.

Quoted 5 lines

---
Original message:
https://lore.kernel.org/git/[email protected]/

This reminds me of another recent discussion on rewriting a branch that is checked out elsewhere, where the "git history" command forgot to apply the same safety check:

Quoted 7 lines

https://lore.kernel.org/git/[email protected]/

We definitely need an easy-to-use API to determine consistently which branches are in use, and to teach all commands that repoint branch tips to use it to offer the same safety to users. The framework that this patch introduces might be a good starting point for that effort.

branch_checked_out() already allows to check whether a branch is in use, but I guess git history needs to respond differently depending on the kind of use, e.g. leave active bisects and rebases untouched and update checked out branches. branch_checkout_kind() would allow that, but I now wonder if it suffices for cases where branches appear in multiple worktrees. So perhaps the query we need to enable are "is this branch used by a rebase/bisect" and not the more limited "tell me one use of this branch"?

Quoted 26 lines
Quoted 13 lines
branch.h +5 -0
index 3dc6e2a0ff..d1073fe1cd 100644
@@ -15,6 +15,14 @@ enum branch_track {
 	BRANCH_TRACK_SIMPLE,
 };
 
+enum branch_checkout_kind {
+	BRANCH_CHECKOUT_KIND_UNSPECIFIED = 0,
+	BRANCH_CHECKOUT_KIND_CHECKOUT,
+	BRANCH_CHECKOUT_KIND_REBASE,
+	BRANCH_CHECKOUT_KIND_BISECT,

branch.h enum branch_track {

Quoted 10 lines
branch.h +9 -0
+	BRANCH_CHECKOUT_KIND_UPDATE_REF,
+};
+
...
+/*
+ * If the branch at 'refname' is currently checked out in a worktree,
+ * then return the kind of checkout, i.e. whether it was done by an
+ * actual checkout or a rebase etc.
+ */
+enum branch_checkout_kind branch_checkout_kind(const char *refname);

OK.

Quoted 14 lines

builtin/branch.c static int delete_branches(int argc, const char **argv, int force, int kinds, · line 269

Quoted 12 lines
builtin/branch.c +1 -3
index dede60d27b..3223347129 100644
@@ -266,9 +266,34 @@ static int delete_branches(int argc, const char **argv, int force, int kinds,
 		if (kinds == FILTER_REFS_BRANCHES) {
 			const char *path;
 			if ((path = branch_checked_out(name))) {
-				error(_("cannot delete branch '%s' "
-					"used by worktree at '%s'"),
-				      bname.buf, path);
+				int kind = branch_checkout_kind(name);

Not "enum branch_checkout_kind" but "int"?

Yes, it doesn't matter for the switch and is easier to print.

Quoted 25 lines

builtin/branch.c static int delete_branches(int argc, const char **argv, int force, int kinds, · line 275

Quoted 7 lines
builtin/branch.c +6 -0
+				switch (kind) {
+				case BRANCH_CHECKOUT_KIND_CHECKOUT:
+					error(_("cannot delete branch '%s' "
+						"used by worktree at '%s'"),
+					      bname.buf, path);
+					break;

We may want to be more explicit and say "cannot delete branch 'frotz' checked out in worktree at '/tmp/nitfol'" instead. Unless this is a catch-all entry for states that are neither 'rebase', 'bisect', nor 'rebase-merges' but are somehow otherwise in use, that is.

builtin/branch.c static int delete_branches(int argc, const char **argv, int force, int kinds, · line 293

Quoted 6 lines
builtin/branch.c +6 -0
+				case BRANCH_CHECKOUT_KIND_UPDATE_REF:
+					error(_("cannot delete branch '%s' "
+						"used by worktree at '%s' "
+						"for update-ref"),
+					      bname.buf, path);
+					break;

I was quite lost when searching for cases where this 'update-ref' state might be encountered, and I still lack confidence. Can we make the diagnostic message a bit friendlier to our users?

Quoted 6 lines

For instance, something like: 'You are rebasing a history with merges in that other worktree, and the tip of this branch will be updated when that process completes, so you cannot delete it from here.' (Naturally, I may have misidentified the exact nature of the error, but this illustrates the level of detail and user-facing clarity I hope to see.)

That's quite long. Would it make sense to throw that update-ref case into the rebase bin, i.e. only distinguish between checkout, bisect and rebase?

Quoted 17 lines
Quoted 17 lines
t/t3200-branch.sh +1 -1
index e2682a83a0..e5df493b66 100755
@@ -930,7 +930,7 @@ test_expect_success 'deleting currently checked out branch fails' '
 	git worktree add -b my7 my7 &&
 	test_must_fail git -C my7 branch -d my7 &&
 	test_must_fail git branch -d my7 2>actual &&
-	grep "^error: cannot delete branch .my7. used by worktree at " actual &&
+	test_grep "^error: cannot delete branch '"'"'my7'"'"' used by worktree at '"'.*'\$"'" actual &&
 	rm -r my7 &&
 	git worktree prune
 '
@@ -941,7 +941,7 @@ test_expect_success 'deleting in-use branch fails' '
 	git -C my7 bisect start HEAD HEAD~2 &&
 	test_must_fail git -C my7 branch -d my7 &&
Quoted 8 lines

t/t3200-branch.sh test_expect_success 'deleting in-use branch fails' ' · line 947

Quoted 6 lines
t/t3200-branch.sh +1 -1
 	test_must_fail git branch -d my7 2>actual &&
-	grep "^error: cannot delete branch .my7. used by worktree at " actual &&
+	test_grep "^error: cannot delete branch '"'"'my7'"'"' used by worktree at '"'.*' for bisect\$"'" actual &&
 	rm -r my7 &&
 	git worktree prune
 '

We distinguish four kinds in the code but we test only two of them?

Laziness, and it was enough for the user requirement..

René

#3 lore raw
Quoted 5 lines · René Scharfe
Quoted 3 lines

builtin/branch.c static int delete_branches(int argc, const char **argv, int force, int kinds, · line 269

Quoted 1 line
builtin/branch.c +1 -0
+				int kind = branch_checkout_kind(name);

Not "enum branch_checkout_kind" but "int"?

Yes, it doesn't matter for the switch and is easier to print.

I do not understand the "print" part. I was probably in the last group of people who was forced to switch from CPP macros to enum and their argument was always "'print kind' in GDB gives symbolic output". As "enum" is an glorified "int", wouldn't

	int i_kind;
	enum branch_checkout_kind e_kind;
	BUG(_("we did not expect %d %d"), e_kind, i_kind);

do just what we expect?

Quoted 31 lines
Quoted 24 lines

builtin/branch.c static int delete_branches(int argc, const char **argv, int force, int kinds, · line 275

Quoted 6 lines
builtin/branch.c +6 -0
+				switch (kind) {
+				case BRANCH_CHECKOUT_KIND_CHECKOUT:
+					error(_("cannot delete branch '%s' "
+						"used by worktree at '%s'"),
+					      bname.buf, path);
+					break;

We may want to be more explicit and say "cannot delete branch 'frotz' checked out in worktree at '/tmp/nitfol'" instead. Unless this is a catch-all entry for states that are neither 'rebase', 'bisect', nor 'rebase-merges' but are somehow otherwise in use, that is.

builtin/branch.c static int delete_branches(int argc, const char **argv, int force, int kinds, · line 293

Quoted 6 lines
builtin/branch.c +6 -0
+				case BRANCH_CHECKOUT_KIND_UPDATE_REF:
+					error(_("cannot delete branch '%s' "
+						"used by worktree at '%s' "
+						"for update-ref"),
+					      bname.buf, path);
+					break;

I was quite lost when searching for cases where this 'update-ref' state might be encountered, and I still lack confidence. Can we make the diagnostic message a bit friendlier to our users?

Quoted 6 lines

For instance, something like: 'You are rebasing a history with merges in that other worktree, and the tip of this branch will be updated when that process completes, so you cannot delete it from here.' (Naturally, I may have misidentified the exact nature of the error, but this illustrates the level of detail and user-facing clarity I hope to see.)

Quoted 3 lines

That's quite long. Would it make sense to throw that update-ref case into the rebase bin, i.e. only distinguish between checkout, bisect and rebase?

Shortening a quite long expression down to digestable pieces is left as an exercise for those with this particular itch to scratch ;-). I do not personally mind if it ends up indistinguishable from other "rebase" case (or unified the "kind" enum into one), but others may have ideas to shorten the message to fit in the pattern we see above.

Thanks.

#4 lore raw
Quoted 13 lines · Junio C Hamano
Quoted 5 lines · René Scharfe
Quoted 3 lines

builtin/branch.c static int delete_branches(int argc, const char **argv, int force, int kinds, · line 269

Quoted 1 line
builtin/branch.c +1 -0
+				int kind = branch_checkout_kind(name);

Not "enum branch_checkout_kind" but "int"?

Yes, it doesn't matter for the switch and is easier to print.

I do not understand the "print" part. I was probably in the last group of people who was forced to switch from CPP macros to enum and their argument was always "'print kind' in GDB gives symbolic output". As "enum" is an glorified "int", wouldn't

Quoted 5 lines
	int i_kind;
	enum branch_checkout_kind e_kind;
	BUG(_("we did not expect %d %d"), e_kind, i_kind);

do just what we expect?

True, integer promotion makes this work regardless of the underlying type of the enum. It was easier for me to make that conversion explicit than to remember the conversion rule. Which isn't that complicated, though, admittedly.

René

#5 lore raw
In reply to Junio C Hamano
Quoted 14 lines · Junio C Hamano
Quoted 4 lines · René Scharfe
Quoted 4 lines
Quoted 4 lines
builtin/branch.c +4 -0
+				switch (kind) {
+				case BRANCH_CHECKOUT_KIND_CHECKOUT:
+					error(_("cannot delete branch '%s' "
+						"used by worktree at '%s'"),
Quoted 8 lines
Quoted 8 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+					      bname.buf, path);
+					break;

We may want to be more explicit and say "cannot delete branch 'frotz' checked out in worktree at '/tmp/nitfol'" instead. Unless this is a catch-all entry for states that are neither 'rebase', 'bisect', nor 'rebase-merges' but are somehow otherwise in use, that is.

That's a great suggestion, I don't think there are any other cases so it should be fine to say "checked out".

Quoted 18 lines
Quoted 18 lines
Quoted 17 lines

builtin/branch.c static int delete_branches(int argc, const char **argv, int force, int kinds, · line 293

Quoted 6 lines
builtin/branch.c +6 -0
+				case BRANCH_CHECKOUT_KIND_UPDATE_REF:
+					error(_("cannot delete branch '%s' "
+						"used by worktree at '%s' "
+						"for update-ref"),
+					      bname.buf, path);
+					break;

I was quite lost when searching for cases where this 'update-ref' state might be encountered, and I still lack confidence. Can we make the diagnostic message a bit friendlier to our users?

For instance, something like: 'You are rebasing a history with merges in that other worktree, and the tip of this branch will be updated when that process completes, so you cannot delete it from here.' (Naturally, I may have misidentified the exact nature of the error, but this illustrates the level of detail and user-facing clarity I hope to see.)

Quoted 3 lines
Quoted 3 lines

That's quite long. Would it make sense to throw that update-ref case into the rebase bin, i.e. only distinguish between checkout, bisect and rebase?

I also wondered whether we should fold this into the rebase case. My concern is that if the user sees

cannot delete branch 'feature' because it is being rebased in the worktree '../feature'

and then they do

cd ../feature
git status

they'll see a different branch name in the status output which is confusing. So I think we either need to improve the status output to show all the branches that are being rewritten (which to my mind is the better option, it is more work but shouldn't be too difficult as it already parses "rebase-merge/git-rebase-todo" and "rebase-merge/done"), or say something like

cannot delete branch 'feature' because it is being updated by a rebase running in '../feature' which is updating multiple branches.

for the update-refs case.

Thanks for working on this, it is a nice usability improvement.

Phillip

Quoted 8 lines

Shortening a quite long expression down to digestable pieces is left as an exercise for those with this particular itch to scratch ;-). I do not personally mind if it ends up indistinguishable from other "rebase" case (or unified the "kind" enum into one), but others may have ideas to shorten the message to fit in the pattern we see above.

Thanks.

#6 lore raw

PATCH v2 1/1 branch: report active bisect run when rejecting delete

In reply to René Scharfe

git branch refuses to delete branches that are currently checked out with a message like this: "error: cannot delete branch 'foo' used by worktree at '/path/of/worktree'". This can be confusing if it's an internal checkout for git bisect. Report a more specific error in that case to help users that might have forgotten their bisect run.

---
Changes since v2:
- Only report bisect runs as specific rejection reason for now.
- Leave rebasing unaddressed because I don't see how to formulate clear and readable messages for those scenarios, yet.
- Collect all checkout reasons of all branches in a simple array for easy use, e.g. to eventually address rebases or for git status.
- Rebased onto the landed test_grep conversion.

 branch.c          | 80 +++++++++++++++++++++++++++++++++--------------
 branch.h          |  6 ++++
 builtin/branch.c  |  7 +++++
 t/t3200-branch.sh |  4 +--
 4 files changed, 72 insertions(+), 25 deletions(-)
branch.c +57 -23
index 243db7d0fc..a9fc790818 100644
@@ -385,6 +385,39 @@ int validate_branchname(const char *name, struct strbuf *ref)
 static int initialized_checked_out_branches;
 static struct strmap current_checked_out_branches = STRMAP_INIT;
 
+enum branch_checkout_kind {
+	BRANCH_CHECKOUT_KIND_CHECKOUT,
+	BRANCH_CHECKOUT_KIND_REBASE,
+	BRANCH_CHECKOUT_KIND_BISECT,
+	BRANCH_CHECKOUT_KIND_UPDATE_REF,
+};
+
+struct checked_out_branch {
+	char *refname;
+	char *path;
+	enum branch_checkout_kind kind;
+};
+
+static struct checked_out_branch *checked_out_branches;
+static size_t checked_out_branches_alloc, checked_out_branches_nr;
+
+static void register_checked_out_branch(const char *prefix, const char *name,
+					const char *path,
+					enum branch_checkout_kind kind)
+{
+	char *refname = xstrfmt("%s%s", prefix, name);
+	char *path_copy = xstrdup(path);
+
+	ALLOC_GROW(checked_out_branches, checked_out_branches_nr + 1,
+		   checked_out_branches_alloc);
+	checked_out_branches[checked_out_branches_nr].refname = refname;
+	checked_out_branches[checked_out_branches_nr].path = path_copy;
+	checked_out_branches[checked_out_branches_nr].kind = kind;
+	checked_out_branches_nr++;
+
+	strmap_put(&current_checked_out_branches, refname, path_copy);
+}
+
 static void prepare_checked_out_branches(void)
 {
 	int i = 0;
@@ -397,7 +430,7 @@ static void prepare_checked_out_branches(void)
 	worktrees = get_worktrees();
 
 	while (worktrees[i]) {
-		char *old, *wt_gitdir;
+		char *wt_gitdir;
 		struct wt_status_state state = { 0 };
 		struct worktree *wt = worktrees[i++];
 		struct string_list update_refs = STRING_LIST_INIT_DUP;
@@ -406,34 +439,25 @@ static void prepare_checked_out_branches(void)
 			continue;
 
 		if (wt->head_ref) {
-			old = strmap_put(&current_checked_out_branches,
-					 wt->head_ref,
-					 xstrdup(wt->path));
-			free(old);
+			register_checked_out_branch("", wt->head_ref, wt->path,
+						    BRANCH_CHECKOUT_KIND_CHECKOUT);
 		}
 
 		if (wt_status_check_rebase(wt, &state) &&
 		    (state.rebase_in_progress || state.rebase_interactive_in_progress) &&
 		    state.branch) {
-			struct strbuf ref = STRBUF_INIT;
-			strbuf_addf(&ref, "refs/heads/%s", state.branch);
-			old = strmap_put(&current_checked_out_branches,
-					 ref.buf,
-					 xstrdup(wt->path));
-			free(old);
-			strbuf_release(&ref);
+			register_checked_out_branch("refs/heads/", state.branch,
+						    wt->path,
+						    BRANCH_CHECKOUT_KIND_REBASE);
 		}
 		wt_status_state_free_buffers(&state);
 
 		if (wt_status_check_bisect(wt, &state) &&
 		    state.bisecting_from) {
-			struct strbuf ref = STRBUF_INIT;
-			strbuf_addf(&ref, "refs/heads/%s", state.bisecting_from);
-			old = strmap_put(&current_checked_out_branches,
-					 ref.buf,
-					 xstrdup(wt->path));
-			free(old);
-			strbuf_release(&ref);
+			register_checked_out_branch("refs/heads/",
+						    state.bisecting_from,
+						    wt->path,
+						    BRANCH_CHECKOUT_KIND_BISECT);
 		}
 		wt_status_state_free_buffers(&state);
 
@@ -442,10 +466,9 @@ static void prepare_checked_out_branches(void)
 						     &update_refs)) {
 			struct string_list_item *item;
 			for_each_string_list_item(item, &update_refs) {
-				old = strmap_put(&current_checked_out_branches,
-						 item->string,
-						 xstrdup(wt->path));
-				free(old);
+				register_checked_out_branch("", item->string,
+							    wt->path,
+							    BRANCH_CHECKOUT_KIND_UPDATE_REF);
 			}
 			string_list_clear(&update_refs, 1);
 		}
@@ -462,6 +485,17 @@ const char *branch_checked_out(const char *refname)
 	return strmap_get(&current_checked_out_branches, refname);
 }
 
+const char *branch_bisecting(const char *refname)
+{
+	prepare_checked_out_branches();
+	for (size_t i = 0; i < checked_out_branches_nr; i++) {
+		if (!strcmp(refname, checked_out_branches[i].refname) &&
+		    checked_out_branches[i].kind == BRANCH_CHECKOUT_KIND_BISECT)
+			return checked_out_branches[i].path;
+	}
+	return NULL;
+}
+
 /*
  * Check if a branch 'name' can be created as a new branch; die otherwise.
  * 'force' can be used when it is OK for the named branch already exists.
branch.h +6 -0
index 3dc6e2a0ff..e9b1f7b37d 100644
@@ -106,6 +106,12 @@ void create_branches_recursively(struct repository *r, const char *name,
  */
 const char *branch_checked_out(const char *refname);
 
+/*
+ * If the branch at 'refname' is currently used for bisecting in a
+ * worktree, then return the path to that worktree.
+ */
+const char *branch_bisecting(const char *refname);
+
 /*
  * Check if 'name' can be a valid name for a branch; die otherwise.
  * Return 1 if the named branch already exists; return 0 otherwise.
builtin/branch.c +7 -0
index dede60d27b..29e4ec6c67 100644
@@ -265,6 +265,13 @@ static int delete_branches(int argc, const char **argv, int force, int kinds,
 
 		if (kinds == FILTER_REFS_BRANCHES) {
 			const char *path;
+			if ((path = branch_bisecting(name))) {
+				error(_("cannot delete branch '%s' "
+					"used by worktree at '%s' for bisect"),
+					      bname.buf, path);
+				ret = 1;
+				continue;
+			}
 			if ((path = branch_checked_out(name))) {
 				error(_("cannot delete branch '%s' "
 					"used by worktree at '%s'"),
t/t3200-branch.sh +2 -2
index 1ecbafbee1..051434d9c6 100755
@@ -930,7 +930,7 @@ test_expect_success 'deleting currently checked out branch fails' '
 	git worktree add -b my7 my7 &&
 	test_must_fail git -C my7 branch -d my7 &&
 	test_must_fail git branch -d my7 2>actual &&
-	test_grep "^error: cannot delete branch .my7. used by worktree at " actual &&
+	test_grep "^error: cannot delete branch '"'"'my7'"'"' used by worktree at '"'.*'\$"'" actual &&
 	rm -r my7 &&
 	git worktree prune
 '
@@ -941,7 +941,7 @@ test_expect_success 'deleting in-use branch fails' '
 	git -C my7 bisect start HEAD HEAD~2 &&
 	test_must_fail git -C my7 branch -d my7 &&
 	test_must_fail git branch -d my7 2>actual &&
-	test_grep "^error: cannot delete branch .my7. used by worktree at " actual &&
+	test_grep "^error: cannot delete branch '"'"'my7'"'"' used by worktree at '"'.*' for bisect\$"'" actual &&
 	rm -r my7 &&
 	git worktree prune
 '
Signature · 1 line
2.55.0
#7 lore raw

Re: [PATCH v2] branch: report active bisect run when rejecting delete

Quoted 8 lines · René Scharfe

git branch refuses to delete branches that are currently checked out with a message like this: "error: cannot delete branch 'foo' used by worktree at '/path/of/worktree'". This can be confusing if it's an internal checkout for git bisect. Report a more specific error in that case to help users that might have forgotten their bisect run.

Quoted 8 lines

---
Changes since v2:
- Only report bisect runs as specific rejection reason for now.
- Leave rebasing unaddressed because I don't see how to formulate clear and readable messages for those scenarios, yet.
- Collect all checkout reasons of all branches in a simple array for easy use, e.g. to eventually address rebases or for git status.
- Rebased onto the landed test_grep conversion.

The scope of this patch has shrunk a bit. While the 'in use by a rebase' state is still recognized internally, unlike in the previous round, we guard users only against the 'in use by a bisect' state and the 'in use by being checked out' state.

This is probably a good single step, so unless there are objections, let's mark the topic for 'next'.

My understanding of what is stopping us from taking the next step is that we do not know the exact phrasing to express the 'in use by a rebase' state concisely.

Thanks.

#8 lore raw

Re: [PATCH v2] branch: report active bisect run when rejecting delete

Quoted 8 lines · Junio C Hamano
Quoted 6 lines · René Scharfe

git branch refuses to delete branches that are currently checked out with a message like this: "error: cannot delete branch 'foo' used by worktree at '/path/of/worktree'". This can be confusing if it's an internal checkout for git bisect. Report a more specific error in that case to help users that might have forgotten their bisect run.

Quoted 5 lines
Quoted 5 lines

---
Changes since v2:
- Only report bisect runs as specific rejection reason for now.

Works for me.

Quoted 4 lines
Quoted 4 lines

- Leave rebasing unaddressed because I don't see how to formulate clear and readable messages for those scenarios, yet.
- Collect all checkout reasons of all branches in a simple array for easy use, e.g. to eventually address rebases or for git status.

That's handy for future use.

Quoted 9 lines
Quoted 1 line

- Rebased onto the landed test_grep conversion.

The scope of this patch has shrunk a bit. While the 'in use by a rebase' state is still recognized internally, unlike in the previous round, we guard users only against the 'in use by a bisect' state and the 'in use by being checked out' state.

This is probably a good single step, so unless there are objections, let's mark the topic for 'next'.

Yeah, I agree it's a step in the right direction.

I've reviewed the patch and looks good to me.

To answer your other question:

Quoted 11 lines

This reminds me of another recent discussion on rewriting a branch that is checked out elsewhere, where the "git history" command forgot to apply the same safety check:

https://lore.kernel.org/git/[email protected]/

We definitely need an easy-to-use API to determine consistently which branches are in use, and to teach all commands that repoint branch tips to use it to offer the same safety to users. The framework that this patch introduces might be a good starting point for that effort.

Yeah, there is definetly some overlap here. As far as I can tell this is a good step in the direction of an API the git-history(1) can use.

Signature · 2 lines
Cheers,
Toon
#9 lore raw

Re: [PATCH v2] branch: report active bisect run when rejecting delete

In reply to Junio C Hamano
Quoted 19 lines · Junio C Hamano
Quoted 8 lines · René Scharfe

git branch refuses to delete branches that are currently checked out with a message like this: "error: cannot delete branch 'foo' used by worktree at '/path/of/worktree'". This can be confusing if it's an internal checkout for git bisect. Report a more specific error in that case to help users that might have forgotten their bisect run.

Quoted 8 lines

---
Changes since v2:
- Only report bisect runs as specific rejection reason for now.
- Leave rebasing unaddressed because I don't see how to formulate clear and readable messages for those scenarios, yet.
- Collect all checkout reasons of all branches in a simple array for easy use, e.g. to eventually address rebases or for git status.
- Rebased onto the landed test_grep conversion.

Quoted 7 lines

The scope of this patch has shrunk a bit. While the 'in use by a rebase' state is still recognized internally, unlike in the previous round, we guard users only against the 'in use by a bisect' state and the 'in use by being checked out' state.

This is probably a good single step, so unless there are objections, let's mark the topic for 'next'.

I agree this is a useful improvement as it is.

Thanks

Phillip

Quoted 6 lines

My understanding of what is stopping us from taking the next step is that we do not know the exact phrasing to express the 'in use by a rebase' state concisely.

Thanks.