builtin/init: simplify logic to configure worktree

In the preceding commit we have stopped modifying the global
`git_work_tree_cfg` variable. With this change there's now some code
paths where we end up setting the local `git_work_tree_cfg` variable,
but without actually using the value for anything.

Refactor the code a bit so that we only set the worktree configuration
in case it's actually needed. Furthermore, reflow it a bit to make the
code easier to follow.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
main
Patrick Steinhardt 2026-06-11 08:44:40 +02:00 committed by Junio C Hamano
parent 9bef2cf331
commit 65eb5b989a
1 changed files with 18 additions and 13 deletions

View File

@ -229,24 +229,29 @@ int cmd_init_db(int argc,

if (!is_bare_repository_cfg) {
const char *git_dir_parent = strrchr(git_dir, '/');
char *git_work_tree_cfg = NULL;

if (git_dir_parent) {
char *rel = xstrndup(git_dir, git_dir_parent - git_dir);
git_work_tree_cfg = real_pathdup(rel, 1);
free(rel);
}
if (!git_work_tree_cfg)
git_work_tree_cfg = xgetcwd();
if (work_tree)
if (work_tree) {
set_git_work_tree(the_repository, work_tree);
else
set_git_work_tree(the_repository, git_work_tree_cfg);
} else {
char *work_tree_cfg = NULL;

if (git_dir_parent) {
char *rel = xstrndup(git_dir, git_dir_parent - git_dir);
work_tree_cfg = real_pathdup(rel, 1);
free(rel);
}

if (!work_tree_cfg)
work_tree_cfg = xgetcwd();

set_git_work_tree(the_repository, work_tree_cfg);

free(work_tree_cfg);
}

if (access(repo_get_work_tree(the_repository), X_OK))
die_errno (_("Cannot access work tree '%s'"),
repo_get_work_tree(the_repository));

free(git_work_tree_cfg);
}
else {
if (real_git_dir)