Skip to content

Commit 9f9fd05

Browse files
committed
repository: factor out worktree check
The check whether a repository is a worktree or not is currently done inside of `git_repository_open_ext`. As we want to extend this function later on, pull it out into its own function `repo_is_worktree` to ease working on it.
1 parent 3284197 commit 9f9fd05

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

src/repository.c

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -758,13 +758,30 @@ static int _git_repository_open_ext_from_env(
758758
return error;
759759
}
760760

761+
static int repo_is_worktree(unsigned *out, const git_repository *repo)
762+
{
763+
git_buf gitdir_link = GIT_BUF_INIT;
764+
int error;
765+
766+
if ((error = git_buf_joinpath(&gitdir_link, repo->gitdir, "gitdir")) < 0)
767+
return -1;
768+
769+
/* A 'gitdir' file inside a git directory is currently
770+
* only used when the repository is a working tree. */
771+
*out = !!git_path_exists(gitdir_link.ptr);
772+
773+
git_buf_free(&gitdir_link);
774+
return error;
775+
}
776+
761777
int git_repository_open_ext(
762778
git_repository **repo_ptr,
763779
const char *start_path,
764780
unsigned int flags,
765781
const char *ceiling_dirs)
766782
{
767783
int error;
784+
unsigned is_worktree;
768785
git_buf gitdir = GIT_BUF_INIT, workdir = GIT_BUF_INIT,
769786
gitlink = GIT_BUF_INIT, commondir = GIT_BUF_INIT;
770787
git_repository *repo;
@@ -797,12 +814,9 @@ int git_repository_open_ext(
797814
GITERR_CHECK_ALLOC(repo->commondir);
798815
}
799816

800-
if ((error = git_buf_joinpath(&gitdir, repo->gitdir, "gitdir")) < 0)
817+
if ((error = repo_is_worktree(&is_worktree, repo)) < 0)
801818
goto cleanup;
802-
/* A 'gitdir' file inside a git directory is currently
803-
* only used when the repository is a working tree. */
804-
if (git_path_exists(gitdir.ptr))
805-
repo->is_worktree = 1;
819+
repo->is_worktree = is_worktree;
806820

807821
/*
808822
* We'd like to have the config, but git doesn't particularly

0 commit comments

Comments
 (0)