Skip to content

Commit eb20e63

Browse files
peffttaylorr
authored andcommitted
branch: gracefully handle '-d' on orphan HEAD
When deleting a branch, "git branch -d" has a safety check that ensures the branch is merged to its upstream (if any), or to HEAD. To do that, naturally we try to resolve HEAD to a commit object. If we're on an orphan branch (i.e., HEAD points to a branch that does not yet exist), that will fail, and we'll bail with an error: $ git branch -d to-delete fatal: Couldn't look up commit object for HEAD This usually isn't that big of a deal. The deletion would fail anyway, since the branch isn't merged to HEAD, and you'd need to use "-D" (or "-f"). And doing so skips the HEAD resolution, courtesy of 67affd5 (git-branch -D: make it work even when on a yet-to-be-born branch, 2006-11-24). But there are still two problems: 1. The error message isn't very helpful. We should give the usual "not fully merged" message, which points the user at "branch -D". That was a problem even back in 67affd5. 2. Even without a HEAD, these days it's still possible for the deletion to succeed. After 67affd5, commit 99c419c (branch -d: base the "already-merged" safety on the branch it merges with, 2009-12-29) made it OK to delete a branch if it is merged to its upstream. We can fix both by removing the die() in delete_branches() completely, leaving head_rev NULL in this case. It's tempting to stop there, as it appears at first glance that the rest of the code does the right thing with a NULL. But sadly, it's not quite true. We end up feeding the NULL to repo_is_descendant_of(). In the traditional code path there, we call repo_in_merge_bases_many(). It feeds the NULL to repo_parse_commit(), which is smart enough to return an error, and we immediately return "no, it's not a descendant". But there's an alternate code path: if we have a commit graph with generation numbers, we end up in can_all_from_reach(), which does eventually try to set a flag on the NULL commit and segfaults. So instead, we'll teach the local branch_merged() helper to treat a NULL as "not merged". This would be a little more elegant in in_merge_bases() itself, but that function is called in a lot of places, and it's not clear that quietly returning "not merged" is the right thing everywhere (I'd expect in many cases, feeding a NULL is a sign of a bug). There are four tests here: a. The first one confirms that deletion succeeds with an orphaned HEAD when the branch is merged to its upstream. This is case (2) above. b. Same, but with commit graphs enabled. Even if it is merged to upstream, we still check head_rev so that we can say "deleting because it's merged to upstream, even though it's not merged to HEAD". Without the second hunk in branch_merged(), this test would segfault in can_all_from_reach(). c. The third one confirms that we correctly say "not merged to HEAD" when we can't resolve HEAD, and reject the deletion. d. Same, but with commit graphs enabled. Without the first hunk in branch_merged(), this one would segfault. Reported-by: Martin von Zweigbergk <[email protected]> Signed-off-by: Jeff King <[email protected]> Signed-off-by: Taylor Blau <[email protected]>
1 parent 319605f commit eb20e63

File tree

2 files changed

+39
-6
lines changed

2 files changed

+39
-6
lines changed

builtin/branch.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ static int branch_merged(int kind, const char *name,
150150
if (!reference_rev)
151151
reference_rev = head_rev;
152152

153-
merged = in_merge_bases(rev, reference_rev);
153+
merged = reference_rev ? in_merge_bases(rev, reference_rev) : 0;
154154

155155
/*
156156
* After the safety valve is fully redefined to "check with
@@ -160,7 +160,7 @@ static int branch_merged(int kind, const char *name,
160160
* a gentle reminder is in order.
161161
*/
162162
if ((head_rev != reference_rev) &&
163-
in_merge_bases(rev, head_rev) != merged) {
163+
(head_rev ? in_merge_bases(rev, head_rev) : 0) != merged) {
164164
if (merged)
165165
warning(_("deleting branch '%s' that has been merged to\n"
166166
" '%s', but not yet merged to HEAD."),
@@ -235,11 +235,8 @@ static int delete_branches(int argc, const char **argv, int force, int kinds,
235235
}
236236
branch_name_pos = strcspn(fmt, "%");
237237

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

244241
for (i = 0; i < argc; i++, strbuf_reset(&bname)) {
245242
char *target = NULL;

t/t3200-branch.sh

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,42 @@ test_expect_success 'git branch -M and -C fail on detached HEAD' '
279279
test_cmp expect err
280280
'
281281

282+
test_expect_success 'git branch -d on orphan HEAD (merged)' '
283+
test_when_finished git checkout main &&
284+
git checkout --orphan orphan &&
285+
test_when_finished "rm -rf .git/objects/commit-graph*" &&
286+
git commit-graph write --reachable &&
287+
git branch --track to-delete main &&
288+
git branch -d to-delete
289+
'
290+
291+
test_expect_success 'git branch -d on orphan HEAD (merged, graph)' '
292+
test_when_finished git checkout main &&
293+
git checkout --orphan orphan &&
294+
git branch --track to-delete main &&
295+
git branch -d to-delete
296+
'
297+
298+
test_expect_success 'git branch -d on orphan HEAD (unmerged)' '
299+
test_when_finished git checkout main &&
300+
git checkout --orphan orphan &&
301+
test_when_finished "git branch -D to-delete" &&
302+
git branch to-delete main &&
303+
test_must_fail git branch -d to-delete 2>err &&
304+
grep "not fully merged" err
305+
'
306+
307+
test_expect_success 'git branch -d on orphan HEAD (unmerged, graph)' '
308+
test_when_finished git checkout main &&
309+
git checkout --orphan orphan &&
310+
test_when_finished "git branch -D to-delete" &&
311+
git branch to-delete main &&
312+
test_when_finished "rm -rf .git/objects/commit-graph*" &&
313+
git commit-graph write --reachable &&
314+
test_must_fail git branch -d to-delete 2>err &&
315+
grep "not fully merged" err
316+
'
317+
282318
test_expect_success 'git branch -v -d t should work' '
283319
git branch t &&
284320
git rev-parse --verify refs/heads/t &&

0 commit comments

Comments
 (0)