Skip to content

Commit bf013fc

Browse files
committed
branch: fix branch_is_checked_out with bare repos
In a bare repository, HEAD usually points to the branch that is considered the "default" branch. As the current implementation for `git_branch_is_checked_out` only does a comparison of HEAD with the branch that is to be checked, it will say that the branch pointed to by HEAD in such a bare repo is checked out. Fix this by skipping the main repo's HEAD when it is bare.
1 parent efb2082 commit bf013fc

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

src/branch.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,13 +153,20 @@ static int branch_equals(git_repository *repo, const char *path, void *payload)
153153

154154
int git_branch_is_checked_out(const git_reference *branch)
155155
{
156+
git_repository *repo;
157+
int flags = 0;
158+
156159
assert(branch);
157160

158161
if (!git_reference_is_branch(branch))
159162
return 0;
160163

161-
return git_repository_foreach_head(git_reference_owner(branch),
162-
branch_equals, 0, (void *) branch) == 1;
164+
repo = git_reference_owner(branch);
165+
166+
if (git_repository_is_bare(repo))
167+
flags |= GIT_REPOSITORY_FOREACH_HEAD_SKIP_REPO;
168+
169+
return git_repository_foreach_head(repo, branch_equals, flags, (void *) branch) == 1;
163170
}
164171

165172
int git_branch_delete(git_reference *branch)

tests/refs/branches/checkedout.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,10 @@ void test_refs_branches_checkedout__head_is_not_checked_out(void)
4444
assert_checked_out(repo, "HEAD", 0);
4545
cl_git_sandbox_cleanup();
4646
}
47+
48+
void test_refs_branches_checkedout__master_in_bare_repo_is_not_checked_out(void)
49+
{
50+
repo = cl_git_sandbox_init("testrepo.git");
51+
assert_checked_out(repo, "refs/heads/master", 0);
52+
cl_git_sandbox_cleanup();
53+
}

0 commit comments

Comments
 (0)