git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH 1/7] builtin/init: stop modifying global `git_work_tree_cfg` variable

From
Justin Tobler <jltobler@gmail.com>
Date
Jun 10, 2026, 21:15 UTC
Message-ID
<ainKs-8_0w_2HhOf@denethor>
In-Reply-To
<20260610-b4-pks-setup-drop-global-state-v1-1-5dff3eec8f06@pks.im>
On 26/06/10 08:56AM, Patrick Steinhardt wrote:
Show 10 quoted lines
> When executing git-init(1) we need to figure out the final location of
> the worktree. This location can be configured in a couple of ways: via
> an environment variable, via the preexisting "core.worktree" config in
> case we're reinitializing, or implicitly when reinitializing a non-bare
> repository.
> 
> When checking for the worktree location in "builtin/init-db.c" we
> populate any potentially-discovered value both by setting the global
> `git_work_tree_cfg` variable and via `set_git_work_tree()`, which
> ultimately ends up modifying `struct repository::worktree`.

Does `git_work_tree_cfg` have any bearing on the worktree value that ends up in `struct repository`? Or is it just `set_git_work_tree()`?

> Modifying `git_work_tree_cfg` is unnecessary though: we configure the
> worktree in `create_default_files()`, and that function derives the
> worktree location via `repo_get_work_tree()`. Consequently, propagating
> the worktree via `set_git_work_tree()` is sufficient.

Ok, so IIUC there is really no reason to globally store the `git_work_tree_cfg` value derived during `cmd_init_db()`. We only care about the worktree written to `struct repository`.

> Stop munging `git_work_tree_cfg` and make it file-local to "setup.c" and
> function-local to `cmd_init_db()`.
Makes sense.
Show 17 quoted lines
> Signed-off-by: Patrick Steinhardt <ps@pks.im>
> ---
>  builtin/init-db.c | 4 ++++
>  environment.c     | 3 ---
>  environment.h     | 1 -
>  setup.c           | 3 +++
>  4 files changed, 7 insertions(+), 4 deletions(-)
> 
> diff --git a/builtin/init-db.c b/builtin/init-db.c
> index c55517ad94..01bc27904e 100644
> --- a/builtin/init-db.c
> +++ b/builtin/init-db.c
> @@ -229,6 +229,8 @@ 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;

We figure out the worktree locally and continue to set it via `set_git_work_tree()` without updating a global worktree config value.

Show 49 quoted lines
> +
>  		if (git_dir_parent) {
>  			char *rel = xstrndup(git_dir, git_dir_parent - git_dir);
>  			git_work_tree_cfg = real_pathdup(rel, 1);
> @@ -243,6 +245,8 @@ int cmd_init_db(int argc,
>  		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)
> diff --git a/environment.c b/environment.c
> index fc3ed8bb1c..4e86335f25 100644
> --- a/environment.c
> +++ b/environment.c
> @@ -100,9 +100,6 @@ int auto_comment_line_char;
>  bool warn_on_auto_comment_char;
>  #endif /* !WITH_BREAKING_CHANGES */
>  
> -/* This is set by setup_git_directory_gently() and/or git_default_config() */
> -char *git_work_tree_cfg;
> -
>  /*
>   * Repository-local GIT_* environment variables; see environment.h for details.
>   */
> diff --git a/environment.h b/environment.h
> index ccfcf37bfb..5d6e4e6c1b 100644
> --- a/environment.h
> +++ b/environment.h
> @@ -149,7 +149,6 @@ int have_git_dir(void);
>  
>  extern int is_bare_repository_cfg;
>  int is_bare_repository(void);
> -extern char *git_work_tree_cfg;
>  
>  /* Environment bits from configuration mechanism */
>  extern int trust_executable_bit;
> diff --git a/setup.c b/setup.c
> index b4652651df..52228b42a1 100644
> --- a/setup.c
> +++ b/setup.c
> @@ -31,6 +31,9 @@ enum allowed_bare_repo {
>  	ALLOWED_BARE_REPO_ALL,
>  };
>  
> +/* This is set by setup_git_directory_gently() and/or git_default_config() */
> +static char *git_work_tree_cfg;
Ok, now `git_work_tree_cfg` is only referenced from "setup.c".
-Justin
Previous: Patrick SteinhardtNext: Patrick Steinhardt
Message 3 of 33 in “setup: drop global state”
  1. 0/7 setup: drop global statePatrick Steinhardt, Jun 10, 2026
  2. 1/7 builtin/init: stop modifying global `git_work_tree_cfg` variablePatrick Steinhardt, Jun 10, 2026
  3. Justin ToblerJun 10, 2026
  4. 2/7 builtin/init: simplify logic to configure worktreePatrick Steinhardt, Jun 10, 2026
  5. Justin ToblerJun 10, 2026
  6. 3/7 setup: remove global `git_work_tree_cfg` variablePatrick Steinhardt, Jun 10, 2026
  7. Justin ToblerJun 10, 2026
  8. Patrick SteinhardtJun 11, 2026
  9. 4/7 builtin/init: stop modifying `is_bare_repository_cfg`Patrick Steinhardt, Jun 10, 2026
  10. 5/7 environment: split up concerns of `is_bare_repository_cfg`Patrick Steinhardt, Jun 10, 2026
  11. Justin ToblerJun 10, 2026
  12. Patrick SteinhardtJun 11, 2026
  13. Justin ToblerJun 11, 2026
  14. 6/7 environment: stop using `the_repository` in `is_bare_repository()`Patrick Steinhardt, Jun 10, 2026
  15. 7/7 treewide: drop USE_THE_REPOSITORY_VARIABLEPatrick Steinhardt, Jun 10, 2026
  16. Justin ToblerJun 10, 2026
  17. Toon ClaesJun 12, 2026
  18. 0/7 setup: drop global statePatrick Steinhardt, Jun 11, 2026
  19. 1/7 builtin/init: stop modifying global `git_work_tree_cfg` variablePatrick Steinhardt, Jun 11, 2026
  20. Toon ClaesJun 12, 2026
  21. Patrick SteinhardtJun 12, 2026
  22. 2/7 builtin/init: simplify logic to configure worktreePatrick Steinhardt, Jun 11, 2026
  23. 3/7 setup: remove global `git_work_tree_cfg` variablePatrick Steinhardt, Jun 11, 2026
  24. 4/7 builtin/init: stop modifying `is_bare_repository_cfg`Patrick Steinhardt, Jun 11, 2026
  25. 5/7 environment: split up concerns of `is_bare_repository_cfg`Patrick Steinhardt, Jun 11, 2026
  26. Toon ClaesJun 12, 2026
  27. Patrick SteinhardtJun 12, 2026
  28. 6/7 environment: stop using `the_repository` in `is_bare_repository()`Patrick Steinhardt, Jun 11, 2026
  29. 7/7 treewide: drop USE_THE_REPOSITORY_VARIABLEPatrick Steinhardt, Jun 11, 2026
  30. Justin ToblerJun 11, 2026
  31. Toon ClaesJun 12, 2026
  32. Patrick SteinhardtJun 12, 2026
  33. Toon ClaesJun 16, 2026

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.