repo: add head_commit helper

Provide a helper method to provide the HEAD's commit, much like our
existing `head_tree` helper.
This commit is contained in:
Edward Thomson
2024-01-14 14:39:17 +00:00
parent 25e2b9d8c2
commit 048421a404
2 changed files with 20 additions and 0 deletions

View File

@@ -3314,6 +3314,25 @@ int git_repository_set_bare(git_repository *repo)
return 0;
}
int git_repository_head_commit(git_commit **commit, git_repository *repo)
{
git_reference *head;
git_object *obj;
int error;
if ((error = git_repository_head(&head, repo)) < 0)
return error;
if ((error = git_reference_peel(&obj, head, GIT_OBJECT_COMMIT)) < 0)
goto cleanup;
*commit = (git_commit *)obj;
cleanup:
git_reference_free(head);
return error;
}
int git_repository_head_tree(git_tree **tree, git_repository *repo)
{
git_reference *head;

View File

@@ -173,6 +173,7 @@ GIT_INLINE(git_attr_cache *) git_repository_attr_cache(git_repository *repo)
return repo->attrcache;
}
int git_repository_head_commit(git_commit **commit, git_repository *repo);
int git_repository_head_tree(git_tree **tree, git_repository *repo);
int git_repository_create_head(const char *git_dir, const char *ref_name);