Skip to content

Commit 3e84aa5

Browse files
committed
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 parent 987f565 commit 3e84aa5

File tree

1 file changed

+23
-50
lines changed

1 file changed

+23
-50
lines changed

src/repository.c

Lines changed: 23 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2069,38 +2069,21 @@ static int get_worktree_file_path(git_buf *out, git_repository *repo, const char
20692069
return git_buf_printf(out, "%s/worktrees/%s/%s", repo->commondir, worktree, file);
20702070
}
20712071

2072-
static int read_worktree_head(git_buf *out, git_repository *repo, const char *name)
2073-
{
2074-
git_buf path = GIT_BUF_INIT;
2075-
int err;
2076-
2077-
assert(out && repo && name);
2078-
2079-
if ((err = get_worktree_file_path(&path, repo, name, "HEAD")) < 0 ||
2080-
(err = git_futils_readbuffer(out, path.ptr)) < 0)
2081-
goto out;
2082-
git_buf_rtrim(out);
2083-
2084-
out:
2085-
git_buf_free(&path);
2086-
2087-
return err;
2088-
}
2089-
20902072
int git_repository_head_detached_for_worktree(git_repository *repo, const char *name)
20912073
{
2092-
git_buf buf = GIT_BUF_INIT;
2093-
int ret;
2074+
git_reference *ref = NULL;
2075+
int error;
20942076

20952077
assert(repo && name);
20962078

2097-
if (read_worktree_head(&buf, repo, name) < 0)
2098-
return -1;
2079+
if ((error = git_repository_head_for_worktree(&ref, repo, name)) < 0)
2080+
goto out;
20992081

2100-
ret = git__strncmp(buf.ptr, GIT_SYMREF, strlen(GIT_SYMREF)) != 0;
2101-
git_buf_free(&buf);
2082+
error = (git_reference_type(ref) != GIT_REF_SYMBOLIC);
2083+
out:
2084+
git_reference_free(ref);
21022085

2103-
return ret;
2086+
return error;
21042087
}
21052088

21062089
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)
21242107

21252108
int git_repository_head_for_worktree(git_reference **out, git_repository *repo, const char *name)
21262109
{
2127-
git_buf buf = GIT_BUF_INIT;
2128-
git_reference *head;
2129-
int err;
2110+
git_buf path = GIT_BUF_INIT;
2111+
git_reference *head = NULL;
2112+
int error;
21302113

21312114
assert(out && repo && name);
21322115

21332116
*out = NULL;
21342117

2135-
if (git_repository_head_detached_for_worktree(repo, name))
2136-
return -1;
2137-
if ((err = read_worktree_head(&buf, repo, name)) < 0)
2118+
if ((error = get_worktree_file_path(&path, repo, name, GIT_HEAD_FILE)) < 0 ||
2119+
(error = git_reference__read_head(&head, repo, path.ptr)) < 0)
21382120
goto out;
21392121

2140-
/* We can only resolve symbolic references */
2141-
if (git__strncmp(buf.ptr, GIT_SYMREF, strlen(GIT_SYMREF)))
2142-
{
2143-
err = -1;
2144-
goto out;
2145-
}
2146-
git_buf_consume(&buf, buf.ptr + strlen(GIT_SYMREF));
2122+
if (git_reference_type(head) != GIT_REF_OID) {
2123+
git_reference *resolved;
21472124

2148-
if ((err = git_reference_lookup(&head, repo, buf.ptr)) < 0)
2149-
goto out;
2150-
if (git_reference_type(head) == GIT_REF_OID)
2151-
{
2152-
*out = head;
2153-
err = 0;
2154-
goto out;
2125+
error = git_reference_lookup_resolved(&resolved, repo, git_reference_symbolic_target(head), -1);
2126+
git_reference_free(head);
2127+
head = resolved;
21552128
}
21562129

2157-
err = git_reference_lookup_resolved(
2158-
out, repo, git_reference_symbolic_target(head), -1);
2159-
git_reference_free(head);
2130+
*out = head;
21602131

21612132
out:
2162-
git_buf_free(&buf);
2133+
if (error)
2134+
git_reference_free(head);
2135+
git_buf_clear(&path);
21632136

2164-
return err;
2137+
return error;
21652138
}
21662139

21672140
int git_repository_head_unborn(git_repository *repo)

0 commit comments

Comments
 (0)