mirror of
https://git.proxmox.com/git/libgit2
synced 2025-05-05 20:33:41 +00:00
branch: use foreach_head
to see if a branch is checked out
Previously, we have extracted the logic to find and iterate over all HEADs of a repository. Use this function in `git_branch_is_checked_out`.
This commit is contained in:
parent
74511aa204
commit
38fc5ab0a2
67
src/branch.c
67
src/branch.c
@ -127,62 +127,29 @@ int git_branch_create_from_annotated(
|
|||||||
repository, branch_name, commit->commit, commit->description, force);
|
repository, branch_name, commit->commit, commit->description, force);
|
||||||
}
|
}
|
||||||
|
|
||||||
int git_branch_is_checked_out(
|
static int branch_equals(git_repository *repo, const char *path, void *payload)
|
||||||
const git_reference *branch)
|
|
||||||
{
|
{
|
||||||
git_buf path = GIT_BUF_INIT, buf = GIT_BUF_INIT;
|
git_reference *branch = (git_reference *) payload;
|
||||||
git_strarray worktrees;
|
git_reference *head;
|
||||||
git_reference *ref = NULL;
|
int equal;
|
||||||
git_repository *repo;
|
|
||||||
const char *worktree;
|
|
||||||
int found = false;
|
|
||||||
size_t i;
|
|
||||||
|
|
||||||
|
if (git_reference__read_head(&head, repo, path) < 0 ||
|
||||||
|
git_reference_type(head) != GIT_REF_SYMBOLIC)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
equal = !git__strcmp(head->target.symbolic, branch->name);
|
||||||
|
git_reference_free(head);
|
||||||
|
return equal;
|
||||||
|
}
|
||||||
|
|
||||||
|
int git_branch_is_checked_out(const git_reference *branch)
|
||||||
|
{
|
||||||
assert(branch && git_reference_is_branch(branch));
|
assert(branch && git_reference_is_branch(branch));
|
||||||
|
|
||||||
repo = git_reference_owner(branch);
|
return git_repository_foreach_head(git_reference_owner(branch),
|
||||||
|
branch_equals, (void *) branch) == 1;
|
||||||
if (git_worktree_list(&worktrees, repo) < 0)
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
for (i = 0; i < worktrees.count; i++) {
|
|
||||||
worktree = worktrees.strings[i];
|
|
||||||
|
|
||||||
if (git_repository_head_for_worktree(&ref, repo, worktree) < 0)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
if (git__strcmp(ref->name, branch->name) == 0) {
|
|
||||||
found = true;
|
|
||||||
git_reference_free(ref);
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
git_reference_free(ref);
|
|
||||||
}
|
|
||||||
git_strarray_free(&worktrees);
|
|
||||||
|
|
||||||
if (found)
|
|
||||||
return found;
|
|
||||||
|
|
||||||
/* Check HEAD of parent */
|
|
||||||
if (git_buf_joinpath(&path, repo->commondir, GIT_HEAD_FILE) < 0)
|
|
||||||
goto out;
|
|
||||||
if (git_futils_readbuffer(&buf, path.ptr) < 0)
|
|
||||||
goto out;
|
|
||||||
if (git__prefixcmp(buf.ptr, "ref: ") == 0)
|
|
||||||
git_buf_consume(&buf, buf.ptr + strlen("ref: "));
|
|
||||||
git_buf_rtrim(&buf);
|
|
||||||
|
|
||||||
found = git__strcmp(buf.ptr, branch->name) == 0;
|
|
||||||
|
|
||||||
out:
|
|
||||||
git_buf_free(&buf);
|
|
||||||
git_buf_free(&path);
|
|
||||||
|
|
||||||
return found;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
int git_branch_delete(git_reference *branch)
|
int git_branch_delete(git_reference *branch)
|
||||||
{
|
{
|
||||||
int is_head;
|
int is_head;
|
||||||
|
Loading…
Reference in New Issue
Block a user