repository: get worktree HEAD via `git_reference__read_head` The functions `git_repository_head_for_worktree` and `git_repository_detached_head_for_worktree` both implement their own logic to read the HEAD reference file. Use the new function `git_reference__read_head` instead to unify the code paths.
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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
diff --git a/src/repository.c b/src/repository.c
index f14eaf9..7eb5bed 100644
--- a/src/repository.c
+++ b/src/repository.c
@@ -2069,38 +2069,21 @@ static int get_worktree_file_path(git_buf *out, git_repository *repo, const char
return git_buf_printf(out, "%s/worktrees/%s/%s", repo->commondir, worktree, file);
}
-static int read_worktree_head(git_buf *out, git_repository *repo, const char *name)
-{
- git_buf path = GIT_BUF_INIT;
- int err;
-
- assert(out && repo && name);
-
- if ((err = get_worktree_file_path(&path, repo, name, "HEAD")) < 0 ||
- (err = git_futils_readbuffer(out, path.ptr)) < 0)
- goto out;
- git_buf_rtrim(out);
-
-out:
- git_buf_free(&path);
-
- return err;
-}
-
int git_repository_head_detached_for_worktree(git_repository *repo, const char *name)
{
- git_buf buf = GIT_BUF_INIT;
- int ret;
+ git_reference *ref = NULL;
+ int error;
assert(repo && name);
- if (read_worktree_head(&buf, repo, name) < 0)
- return -1;
+ if ((error = git_repository_head_for_worktree(&ref, repo, name)) < 0)
+ goto out;
- ret = git__strncmp(buf.ptr, GIT_SYMREF, strlen(GIT_SYMREF)) != 0;
- git_buf_free(&buf);
+ error = (git_reference_type(ref) != GIT_REF_SYMBOLIC);
+out:
+ git_reference_free(ref);
- return ret;
+ return error;
}
int git_repository_head(git_reference **head_out, git_repository *repo)
@@ -2124,44 +2107,34 @@ int git_repository_head(git_reference **head_out, git_repository *repo)
int git_repository_head_for_worktree(git_reference **out, git_repository *repo, const char *name)
{
- git_buf buf = GIT_BUF_INIT;
- git_reference *head;
- int err;
+ git_buf path = GIT_BUF_INIT;
+ git_reference *head = NULL;
+ int error;
assert(out && repo && name);
*out = NULL;
- if (git_repository_head_detached_for_worktree(repo, name))
- return -1;
- if ((err = read_worktree_head(&buf, repo, name)) < 0)
+ if ((error = get_worktree_file_path(&path, repo, name, GIT_HEAD_FILE)) < 0 ||
+ (error = git_reference__read_head(&head, repo, path.ptr)) < 0)
goto out;
- /* We can only resolve symbolic references */
- if (git__strncmp(buf.ptr, GIT_SYMREF, strlen(GIT_SYMREF)))
- {
- err = -1;
- goto out;
- }
- git_buf_consume(&buf, buf.ptr + strlen(GIT_SYMREF));
+ if (git_reference_type(head) != GIT_REF_OID) {
+ git_reference *resolved;
- if ((err = git_reference_lookup(&head, repo, buf.ptr)) < 0)
- goto out;
- if (git_reference_type(head) == GIT_REF_OID)
- {
- *out = head;
- err = 0;
- goto out;
+ error = git_reference_lookup_resolved(&resolved, repo, git_reference_symbolic_target(head), -1);
+ git_reference_free(head);
+ head = resolved;
}
- err = git_reference_lookup_resolved(
- out, repo, git_reference_symbolic_target(head), -1);
- git_reference_free(head);
+ *out = head;
out:
- git_buf_free(&buf);
+ if (error)
+ git_reference_free(head);
+ git_buf_clear(&path);
- return err;
+ return error;
}
int git_repository_head_unborn(git_repository *repo)