repository: Add git_repository_head()
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 51 52 53 54 55 56 57 58 59 60 61 62
diff --git a/include/git2/repository.h b/include/git2/repository.h
index e432643..fb9b09f 100644
--- a/include/git2/repository.h
+++ b/include/git2/repository.h
@@ -219,6 +219,16 @@ GIT_EXTERN(void) git_repository_free(git_repository *repo);
GIT_EXTERN(int) git_repository_init(git_repository **repo_out, const char *path, unsigned is_bare);
/**
+ * Retrieve and resolve the reference pointed at by HEAD.
+ *
+ * @param head_out pointer to the reference which will be retrieved
+ * @param repo a repository object
+ *
+ * @return 0 on success; error code otherwise
+ */
+GIT_EXTERN(int) git_repository_head(git_reference **head_out, git_repository *repo);
+
+/**
* Check if a repository's HEAD is detached
*
* A repository's HEAD is detached when it points directly to a commit
diff --git a/src/repository.c b/src/repository.c
index 1b06c4f..cbd73fe 100644
--- a/src/repository.c
+++ b/src/repository.c
@@ -729,19 +729,31 @@ int git_repository_head_detached(git_repository *repo)
return 1;
}
-int git_repository_head_orphan(git_repository *repo)
+int git_repository_head(git_reference **head_out, git_repository *repo)
{
git_reference *ref;
int error;
+ *head_out = NULL;
+
error = git_reference_lookup(&ref, repo, GIT_HEAD_FILE);
if (error < GIT_SUCCESS)
- return error;
-
- if (git_reference_type(ref) == GIT_REF_OID)
- return 0;
+ return git__rethrow(GIT_ENOTAREPO, "Failed to locate the HEAD");
error = git_reference_resolve(&ref, ref);
+ if (error < GIT_SUCCESS)
+ return git__rethrow(error, "Failed to resolve the HEAD");
+
+ *head_out = ref;
+ return GIT_SUCCESS;
+}
+
+int git_repository_head_orphan(git_repository *repo)
+{
+ git_reference *ref;
+ int error;
+
+ error = git_repository_head(&ref, repo);
return error == GIT_ENOTFOUND ? 1 : error;
}