branch: allow deletion of branch when HEAD's missing
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
diff --git a/src/branch.c b/src/branch.c
index d0bd1c4..d9fa9eb 100644
--- a/src/branch.c
+++ b/src/branch.c
@@ -265,13 +265,19 @@ int git_branch_is_head(
{
git_reference *head;
bool is_same = false;
+ int error;
assert(branch);
if (!git_reference_is_branch(branch))
return false;
- if (git_repository_head(&head, git_reference_owner(branch)) < 0)
+ error = git_repository_head(&head, git_reference_owner(branch));
+
+ if (error == GIT_ENOTFOUND)
+ return false;
+
+ if (error < 0)
return -1;
is_same = strcmp(
diff --git a/tests-clar/refs/branches/delete.c b/tests-clar/refs/branches/delete.c
index b261240..1a97dc8 100644
--- a/tests-clar/refs/branches/delete.c
+++ b/tests-clar/refs/branches/delete.c
@@ -38,17 +38,16 @@ void test_refs_branches_delete__can_not_delete_a_branch_pointed_at_by_HEAD(void)
git_reference_free(branch);
}
-void test_refs_branches_delete__can_not_delete_a_branch_if_HEAD_is_missing(void)
+void test_refs_branches_delete__can_delete_a_branch_even_if_HEAD_is_missing(void)
{
git_reference *head;
- git_reference *branch = NULL;
+ git_reference *branch;
cl_git_pass(git_reference_lookup(&head, repo, GIT_HEAD_FILE));
git_reference_delete(head);
cl_git_pass(git_branch_lookup(&branch, repo, "br2", GIT_BRANCH_LOCAL));
- cl_git_fail(git_branch_delete(branch));
- git_reference_free(branch);
+ cl_git_pass(git_branch_delete(branch));
}
void test_refs_branches_delete__can_delete_a_branch_pointed_at_by_detached_HEAD(void)