summaryrefslogtreecommitdiff
path: root/src/refs.c
diff options
context:
space:
mode:
authorPatrick Steinhardt <ps@pks.im>2017-04-05 10:43:18 +0200
committerPatrick Steinhardt <ps@pks.im>2017-04-05 13:49:31 +0200
commit5b65ac25780cc4c18c965e3202e2e882bd25d941 (patch)
tree76c239117cc35d15d10e6389064df8b9b3ec7cdc /src/refs.c
parent602972560a9c8700b1819430f409ce0cb8fbd58f (diff)
downloadlibgit2-5b65ac25780cc4c18c965e3202e2e882bd25d941.tar.gz
refs: implement function to read references from file
Currently, we only provide functions to read references directly from a repository's reference store via e.g. `git_reference_lookup`. But in some cases, we may want to read files not connected to the current repository, e.g. when looking up HEAD of connected work trees. This commit implements `git_reference__read_head`, which will read out and allocate a reference at an arbitrary path.
Diffstat (limited to 'src/refs.c')
-rw-r--r--src/refs.c34
1 files changed, 34 insertions, 0 deletions
diff --git a/src/refs.c b/src/refs.c
index 0837dc4a8..a53d094fc 100644
--- a/src/refs.c
+++ b/src/refs.c
@@ -249,6 +249,40 @@ int git_reference_lookup_resolved(
return 0;
}
+int git_reference__read_head(
+ git_reference **out,
+ git_repository *repo,
+ const char *path)
+{
+ git_buf reference = GIT_BUF_INIT;
+ char *name = NULL;
+ int error;
+
+ if ((error = git_futils_readbuffer(&reference, path)) < 0)
+ goto out;
+ git_buf_rtrim(&reference);
+
+ if (git__strncmp(reference.ptr, GIT_SYMREF, strlen(GIT_SYMREF)) == 0) {
+ git_buf_consume(&reference, reference.ptr + strlen(GIT_SYMREF));
+
+ name = git_path_basename(path);
+
+ if ((*out = git_reference__alloc_symbolic(name, reference.ptr)) == NULL) {
+ error = -1;
+ goto out;
+ }
+ } else {
+ if ((error = git_reference_lookup(out, repo, reference.ptr)) < 0)
+ goto out;
+ }
+
+out:
+ free(name);
+ git_buf_clear(&reference);
+
+ return error;
+}
+
int git_reference_dwim(git_reference **out, git_repository *repo, const char *refname)
{
int error = 0, i;