@matheusbsbs / matheus-bagliano / commits / 67affd5173
Fork 0

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

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 committed Nov 24, 2006 at 23:10 UTC 67affd5173da059ca60aab7896985331acacd9b4
1 file changed +8 -4
builtin-branch.c
+8 -4
@@ -38,12 +38,16 @@ static int in_merge_bases(const unsigned char *sha1,
38
39 static void delete_branches(int argc, const char **argv, int force)
40 {
41 - struct commit *rev, *head_rev;
41 + struct commit *rev, *head_rev = head_rev;
42 unsigned char sha1[20];
43 char *name;
44 int i;
45
46 - head_rev = lookup_commit_reference(head_sha1);
46 + if (!force) {
47 + head_rev = lookup_commit_reference(head_sha1);
48 + if (!head_rev)
49 + die("Couldn't look up commit object for HEAD");
50 + }
51 for (i = 0; i < argc; i++) {
52 if (!strcmp(head, argv[i]))
53 die("Cannot delete the branch you are currently on.");
@@ -53,8 +57,8 @@ static void delete_branches(int argc, const char **argv, int force)
57 die("Branch '%s' not found.", argv[i]);
58
59 rev = lookup_commit_reference(sha1);
56 - if (!rev || !head_rev)
57 - die("Couldn't look up commit objects.");
60 + if (!rev)
61 + die("Couldn't look up commit object for '%s'", name);
62
63 /* This checks whether the merge bases of branch and
64 * HEAD contains branch -- which means that the HEAD