From: Justin Tobler <jltobler@gmail.com>
To: Patrick Steinhardt <ps@pks.im>
Cc: git@vger.kernel.org
Subject: Re: [PATCH 2/7] builtin/init: simplify logic to configure worktree
Date: Wed, 10 Jun 2026 16:29:17 -0500 [thread overview]
Message-ID: <ainT3CvCCM26-gb_@denethor> (raw)
In-Reply-To: <20260610-b4-pks-setup-drop-global-state-v1-2-5dff3eec8f06@pks.im>
On 26/06/10 08:56AM, Patrick Steinhardt wrote:
> 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.
> - 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.
> +
> + 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
next prev parent reply other threads:[~2026-06-10 21:29 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-06-10 6:56 [PATCH 0/7] setup: drop global state Patrick Steinhardt
2026-06-10 6:56 ` [PATCH 1/7] builtin/init: stop modifying global `git_work_tree_cfg` variable Patrick Steinhardt
2026-06-10 21:15 ` Justin Tobler
2026-06-10 6:56 ` [PATCH 2/7] builtin/init: simplify logic to configure worktree Patrick Steinhardt
2026-06-10 21:29 ` Justin Tobler [this message]
2026-06-10 6:56 ` [PATCH 3/7] setup: remove global `git_work_tree_cfg` variable Patrick Steinhardt
2026-06-10 21:52 ` Justin Tobler
2026-06-10 6:56 ` [PATCH 4/7] builtin/init: stop modifying `is_bare_repository_cfg` Patrick Steinhardt
2026-06-10 6:56 ` [PATCH 5/7] environment: split up concerns of `is_bare_repository_cfg` Patrick Steinhardt
2026-06-10 22:22 ` Justin Tobler
2026-06-10 6:56 ` [PATCH 6/7] environment: stop using `the_repository` in `is_bare_repository()` Patrick Steinhardt
2026-06-10 6:56 ` [PATCH 7/7] treewide: drop USE_THE_REPOSITORY_VARIABLE Patrick Steinhardt
2026-06-10 22:26 ` Justin Tobler
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=ainT3CvCCM26-gb_@denethor \
--to=jltobler@gmail.com \
--cc=git@vger.kernel.org \
--cc=ps@pks.im \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox