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

[PATCH] branch: gracefully handle '-d' on detached HEAD

From
Taylor Blau <me@ttaylorr.com>
Date
Nov 1, 2022, 20:32 UTC
Message-ID
<c68f4b140f2495a35c5f30bec4e2e56c246160f4.1667334672.git.me@ttaylorr.com>
In-Reply-To
<Y2F9lkCWf/2rjT2E@nand.local>
From: Jeff King <peff@peff.net>

Since 67affd5173 (git-branch -D: make it work even when on a yet-to-be-born branch, 2006-11-24), 'git branch -d' refuses to work when orphaned, since there is no HEAD to resolve.

But since 67affd5173, there have been other checks, like 99c419c915 (branch -d: base the "already-merged" safety on the branch it merges with, 2009-12-29), which makes it OK to delete a branch if it is merged to HEAD or its upstream.

99c419c915 makes the check in 67affd5173 wrong, since it's OK to delete a branch if it is merged to its upstream.

Since the code in delete_branches() tolerates a NULL head_rev perfectly fine, make it non-fatal to fail to resolve a commit object at HEAD.

Signed-off-by: Jeff King <peff@peff.net>
Signed-off-by: Taylor Blau <me@ttaylorr.com>
---
OK, here's that patch. Since Peff wrote the main portion of it, he gets
authorship credit. My version comes with a test, which we should
consider picking up.

Even though it doesn't resolve Martin's original scenario (i.e., he'd still have to use -D or -f to actually delete 'main' there), I still think the bugfix is worth pursuing in its own right.

 builtin/branch.c  | 5 +----
 t/t3200-branch.sh | 7 +++++++
 2 files changed, 8 insertions(+), 4 deletions(-)
diff --git a/builtin/branch.c b/builtin/branch.c
index 15be0c03ef..f6ff9084c8 100644
--- a/builtin/branch.c
+++ b/builtin/branch.c
@@ -235,11 +235,8 @@ static int delete_branches(int argc, const char **argv, int force, int kinds,
 	}
 	branch_name_pos = strcspn(fmt, "%");

-	if (!force) {
+	if (!force)
 		head_rev = lookup_commit_reference(the_repository, &head_oid);
-		if (!head_rev)
-			die(_("Couldn't look up commit object for HEAD"));
-	}

 	for (i = 0; i < argc; i++, strbuf_reset(&bname)) {
 		char *target = NULL;
diff --git a/t/t3200-branch.sh b/t/t3200-branch.sh
index 7f605f865b..464d3f610b 100755
--- a/t/t3200-branch.sh
+++ b/t/t3200-branch.sh
@@ -279,6 +279,13 @@ test_expect_success 'git branch -M and -C fail on detached HEAD' '
 	test_cmp expect err
 '

+test_expect_success 'git branch -d on detached HEAD' '
+	test_when_finished git checkout main &&
+	git checkout --orphan orphan &&
+	test_must_fail git branch -d main 2>err &&
+	grep "not fully merged" err
+'
+
 test_expect_success 'git branch -v -d t should work' '
 	git branch t &&
 	git rev-parse --verify refs/heads/t &&
--
2.38.0.16.g393fd4c6db
Previous: Taylor BlauNext: Jeff King
Message 8 of 12 in “Bug in `git branch --delete main` when on other orphan branch”
  1. Martin von ZweigbergkOct 29, 2022
  2. Jeff KingNov 1, 2022
  3. Martin von ZweigbergkNov 1, 2022
  4. Taylor BlauNov 1, 2022
  5. Taylor BlauNov 1, 2022
  6. Jeff KingNov 1, 2022
  7. Taylor BlauNov 2, 2022
  8. branch: gracefully handle '-d' on detached HEADTaylor Blau, Nov 1, 2022
  9. branch: gracefully handle '-d' on orphan HEADJeff King, Nov 2, 2022
  10. Rubén JustoNov 4, 2022
  11. Jeff KingNov 4, 2022
  12. Rubén JustoNov 6, 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.