Re: [PATCH 2/7] builtin/init: simplify logic to configure worktree
- From
Justin Tobler <jltobler@gmail.com>
- Date
- Jun 10, 2026, 21:29 UTC
- Message-ID
- <ainT3CvCCM26-gb_@denethor>
- In-Reply-To
- <20260610-b4-pks-setup-drop-global-state-v1-2-5dff3eec8f06@pks.im>
On 26/06/10 08:56AM, Patrick Steinhardt wrote:
Show 34 quoted lines
> 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>
> ---
> builtin/init-db.c | 31 ++++++++++++++++++-------------
> 1 file changed, 18 insertions(+), 13 deletions(-)
>
> diff --git a/builtin/init-db.c b/builtin/init-db.c
> index 01bc27904e..b4343c2804 100644
> --- a/builtin/init-db.c
> +++ b/builtin/init-db.c
> @@ -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);Ok, if the worktree is already set via the GIT_WORK_TREE environment variable, we can just apply it skip everything else.
Show 13 quoted lines
> - 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();When the environment variable is not set, we try to derive the worktree from the parent directory of the gitdir. If that doesn't work we fallback to the current working directory.
This matches previous the previous behavior.
Show 5 quoted lines
> + > + set_git_work_tree(the_repository, work_tree_cfg); > + > + free(work_tree_cfg); > + }
Good cleanup. I think this is a bit easier to read too.
-Justin