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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



From: Jeff King <peff@xxxxxxxx>

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@xxxxxxxx>
Signed-off-by: Taylor Blau <me@xxxxxxxxxxxx>
---
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



[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]

  Powered by Linux