Skip to content

Commit 987f565

Browse files
committed
repository: extract function to get path to a file in a work tree
The function `read_worktree_head` has the logic embedded to construct the path to `HEAD` in the work tree's git directory, which is quite useful for other callers. Extract the logic into its own function to make it reusable by others.
1 parent 8242cc1 commit 987f565

File tree

1 file changed

+8
-11
lines changed

1 file changed

+8
-11
lines changed

src/repository.c

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2063,24 +2063,21 @@ int git_repository_head_detached(git_repository *repo)
20632063
return exists;
20642064
}
20652065

2066+
static int get_worktree_file_path(git_buf *out, git_repository *repo, const char *worktree, const char *file)
2067+
{
2068+
git_buf_clear(out);
2069+
return git_buf_printf(out, "%s/worktrees/%s/%s", repo->commondir, worktree, file);
2070+
}
2071+
20662072
static int read_worktree_head(git_buf *out, git_repository *repo, const char *name)
20672073
{
20682074
git_buf path = GIT_BUF_INIT;
20692075
int err;
20702076

20712077
assert(out && repo && name);
20722078

2073-
git_buf_clear(out);
2074-
2075-
if ((err = git_buf_printf(&path, "%s/worktrees/%s/HEAD", repo->commondir, name)) < 0)
2076-
goto out;
2077-
if (!git_path_exists(path.ptr))
2078-
{
2079-
err = -1;
2080-
goto out;
2081-
}
2082-
2083-
if ((err = git_futils_readbuffer(out, path.ptr)) < 0)
2079+
if ((err = get_worktree_file_path(&path, repo, name, "HEAD")) < 0 ||
2080+
(err = git_futils_readbuffer(out, path.ptr)) < 0)
20842081
goto out;
20852082
git_buf_rtrim(out);
20862083

0 commit comments

Comments
 (0)