Commit b1a3a70e by nulltoken

repository: Refine repository_head() error report

parent ef887151
......@@ -273,7 +273,7 @@ GIT_EXTERN(int) git_repository_init_ext(
* @param repo a repository object
*
* @return 0 on success, GIT_EORPHANEDHEAD when HEAD points to a non existing
* branch, an error code otherwise
* branch, GIT_ENOTFOUND when HEAD is missing; an error code otherwise
*/
GIT_EXTERN(int) git_repository_head(git_reference **head_out, git_repository *repo);
......
......@@ -317,7 +317,7 @@ int git_branch_is_head(
error = git_repository_head(&head, git_reference_owner(branch));
if (error == GIT_EORPHANEDHEAD)
if (error == GIT_EORPHANEDHEAD || error == GIT_ENOTFOUND)
return false;
if (error < 0)
......
......@@ -1207,9 +1207,19 @@ int git_repository_head_detached(git_repository *repo)
int git_repository_head(git_reference **head_out, git_repository *repo)
{
git_reference *head;
int error;
error = git_reference_lookup_resolved(head_out, repo, GIT_HEAD_FILE, -1);
if ((error = git_reference_lookup(&head, repo, GIT_HEAD_FILE)) < 0)
return error;
if (git_reference_type(head) == GIT_REF_OID) {
*head_out = head;
return 0;
}
error = git_reference_lookup_resolved(head_out, repo, git_reference_target(head), -1);
git_reference_free(head);
return error == GIT_ENOTFOUND ? GIT_EORPHANEDHEAD : error;
}
......
......@@ -39,6 +39,22 @@ void test_refs_branches_ishead__can_properly_handle_orphaned_HEAD(void)
repo = NULL;
}
void test_refs_branches_ishead__can_properly_handle_missing_HEAD(void)
{
git_repository_free(repo);
repo = cl_git_sandbox_init("testrepo.git");
delete_head(repo);
cl_git_pass(git_reference_lookup(&branch, repo, "refs/heads/master"));
cl_assert_equal_i(false, git_branch_is_head(branch));
cl_git_sandbox_cleanup();
repo = NULL;
}
void test_refs_branches_ishead__can_tell_if_a_branch_is_not_pointed_at_by_HEAD(void)
{
cl_git_pass(git_reference_lookup(&branch, repo, "refs/heads/br2"));
......
#include "clar_libgit2.h"
#include "refs.h"
#include "repo_helpers.h"
#include "posix.h"
git_repository *repo;
......@@ -178,6 +179,15 @@ void test_repo_head__retrieving_an_orphaned_head_returns_GIT_EORPHANEDHEAD(void)
cl_assert_equal_i(GIT_EORPHANEDHEAD, git_repository_head(&head, repo));
}
void test_repo_head__retrieving_a_missing_head_returns_GIT_ENOTFOUND(void)
{
git_reference *head;
delete_head(repo);
cl_assert_equal_i(GIT_ENOTFOUND, git_repository_head(&head, repo));
}
void test_repo_head__can_tell_if_an_orphaned_head_is_detached(void)
{
make_head_orphaned(repo, NON_EXISTING_HEAD);
......
#include "clar_libgit2.h"
#include "refs.h"
#include "repo_helpers.h"
#include "posix.h"
void make_head_orphaned(git_repository* repo, const char *target)
{
......@@ -9,3 +10,13 @@ void make_head_orphaned(git_repository* repo, const char *target)
cl_git_pass(git_reference_create_symbolic(&head, repo, GIT_HEAD_FILE, target, 1));
git_reference_free(head);
}
void delete_head(git_repository* repo)
{
git_buf head_path = GIT_BUF_INIT;
cl_git_pass(git_buf_joinpath(&head_path, git_repository_path(repo), GIT_HEAD_FILE));
cl_git_pass(p_unlink(git_buf_cstr(&head_path)));
git_buf_free(&head_path);
}
......@@ -3,3 +3,4 @@
#define NON_EXISTING_HEAD "refs/heads/hide/and/seek"
extern void make_head_orphaned(git_repository* repo, const char *target);
extern void delete_head(git_repository* repo);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment