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

[PATCH] git-branch -D: make it work even when on a yet-to-be-born branch

From
Junio C Hamano <junkio@cox.net>
Date
Nov 25, 2006, 07:12 UTC
Message-ID
<7vr6vs579y.fsf_-_@assigned-by-dhcp.cox.net>
In-Reply-To
<7v1wns6q41.fsf@assigned-by-dhcp.cox.net>

This makes "git branch -D other_branch" work even when HEAD points at a yet-to-be-born branch.

Earlier, we checked the HEAD ref for the purpose of "subset" check even when the deletion was forced (i.e. not -d but -D). Because of this, you cannot delete a branch even with -D while on a yet-to-be-born branch.

With this change, the following sequence that now works:
	mkdir newdir && cd newdir
	git init-db
	git fetch -k $other_repo refs/heads/master:refs/heads/othre
	# oops, typo
	git branch other othre
	git branch -D othre
Signed-off-by: Junio C Hamano <junkio@cox.net>
---
  Junio C Hamano <junkio@cox.net> writes:
  > When forcing a deletion, we do not care about ancestry relation
  > between the HEAD and the branch being deleted, so we should not
  > even bother checking if HEAD is already valid.  The original
  > code before your patch shares the same problem.
  And here is a fix for that.  This is on top of your -v change
  which I've applied.
 builtin-branch.c |   12 ++++++++----
 1 files changed, 8 insertions(+), 4 deletions(-)
diff --git a/builtin-branch.c b/builtin-branch.c
index 69b7b55..3d5cb0e 100644
--- a/builtin-branch.c
+++ b/builtin-branch.c
@@ -38,12 +38,16 @@ static int in_merge_bases(const unsigned char *sha1,
 
 static void delete_branches(int argc, const char **argv, int force)
 {
-	struct commit *rev, *head_rev;
+	struct commit *rev, *head_rev = head_rev;
 	unsigned char sha1[20];
 	char *name;
 	int i;
 
-	head_rev = lookup_commit_reference(head_sha1);
+	if (!force) {
+		head_rev = lookup_commit_reference(head_sha1);
+		if (!head_rev)
+			die("Couldn't look up commit object for HEAD");
+	}
 	for (i = 0; i < argc; i++) {
 		if (!strcmp(head, argv[i]))
 			die("Cannot delete the branch you are currently on.");
@@ -53,8 +57,8 @@ static void delete_branches(int argc, const char **argv, int force)
 			die("Branch '%s' not found.", argv[i]);
 
 		rev = lookup_commit_reference(sha1);
-		if (!rev || !head_rev)
-			die("Couldn't look up commit objects.");
+		if (!rev)
+			die("Couldn't look up commit object for '%s'", name);
 
 		/* This checks whether the merge bases of branch and
 		 * HEAD contains branch -- which means that the HEAD
-- 
1.4.4.1.g61fba
Previous: Lars HjemliNext: Lars Hjemli
Message 9 of 13 in “[RFC] Teach git-branch howto rename a branch”
  1. Lars HjemliNov 24, 2006
  2. Junio C HamanoNov 25, 2006
  3. Shawn PearceNov 25, 2006
  4. Jakub NarebskiNov 25, 2006
  5. Shawn PearceNov 25, 2006
  6. Lars HjemliNov 25, 2006
  7. Junio C HamanoNov 25, 2006
  8. Lars HjemliNov 25, 2006
  9. git-branch -D: make it work even when on a yet-to-be-born branchJunio C Hamano, Nov 25, 2006
  10. Lars HjemliNov 25, 2006
  11. Fredrik KuivinenNov 25, 2006
  12. Lars HjemliNov 25, 2006
  13. Josef WeidendorferNov 26, 2006

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.