From 987f565917ed92094184384eb61c38d17e9e47f5 Mon Sep 17 00:00:00 2001 From: Patrick Steinhardt Date: Tue, 4 Apr 2017 17:12:22 +0200 Subject: [PATCH] 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. --- src/repository.c | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/src/repository.c b/src/repository.c index 900a15dc7..f14eaf96e 100644 --- a/src/repository.c +++ b/src/repository.c @@ -2063,6 +2063,12 @@ int git_repository_head_detached(git_repository *repo) return exists; } +static int get_worktree_file_path(git_buf *out, git_repository *repo, const char *worktree, const char *file) +{ + git_buf_clear(out); + 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; @@ -2070,17 +2076,8 @@ static int read_worktree_head(git_buf *out, git_repository *repo, const char *na assert(out && repo && name); - git_buf_clear(out); - - if ((err = git_buf_printf(&path, "%s/worktrees/%s/HEAD", repo->commondir, name)) < 0) - goto out; - if (!git_path_exists(path.ptr)) - { - err = -1; - goto out; - } - - if ((err = git_futils_readbuffer(out, path.ptr)) < 0) + 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);