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

Re: [PATCH v2 2/3] config: unify code paths to get global config paths

From
Jeff King <peff@peff.net>
Date
Apr 9, 2021, 15:21 UTC
Message-ID
<YHBw7D/fEUdCckc8@coredump.intra.peff.net>
In-Reply-To
<dddc85bcf54e9b19f1612cf2a5be928dcb2bad7d.1617975637.git.ps@pks.im>
On Fri, Apr 09, 2021 at 03:43:25PM +0200, Patrick Steinhardt wrote:
Show 9 quoted lines
> There's two callsites which assemble global config paths, once in the
> config loading code and once in the git-config(1) builtin. We're about
> to implement a way to override global config paths via an environment
> variable which would require us to adjust both sites.
> 
> Unify both code paths into a single `git_global_config()` function which
> returns both paths for `~/.gitconfig` and the XDG config file. This will
> make the subsequent patch which introduces the new envvar easier to
> implement.

Seems like a good step forward. There is one minor issue with the implementation, though.

Show 13 quoted lines
> diff --git a/builtin/config.c b/builtin/config.c
> index 02ed0b3fe7..604a0973a5 100644
> --- a/builtin/config.c
> +++ b/builtin/config.c
> @@ -671,9 +671,9 @@ int cmd_config(int argc, const char **argv, const char *prefix)
>  	}
>  
>  	if (use_global_config) {
> -		char *user_config = expand_user_path("~/.gitconfig", 0);
> -		char *xdg_config = xdg_config_home("config");
> +		const char *user_config, *xdg_config;
>  
> +		git_global_config(&user_config, &xdg_config);

The pointer out-parameters make sense here, since we need to return two values. I notice they became const, so the function will hold on to ownership of the memory.

Show 9 quoted lines
> @@ -688,10 +688,8 @@ int cmd_config(int argc, const char **argv, const char *prefix)
>  		if (access_or_warn(user_config, R_OK, 0) &&
>  		    xdg_config && !access_or_warn(xdg_config, R_OK, 0)) {
>  			given_config_source.file = xdg_config;
> -			free(user_config);
>  		} else {
>  			given_config_source.file = user_config;
> -			free(xdg_config);
>  		}
...which is why we drop these free() calls. So far so good.
Show 12 quoted lines
> +void git_global_config(const char **user, const char **xdg)
> +{
> +	static const char *user_config, *xdg_config;
> +
> +	if (!user_config) {
> +		user_config = expand_user_path("~/.gitconfig", 0);
> +		xdg_config = xdg_config_home("config");
> +	}
> +
> +	*user = user_config;
> +	*xdg = xdg_config;
> +}
And here in the implementation we hold on to the static values forever.

I think your "did we initialize already" check isn't robust, though. expand_user_path() can return NULL, in which case every call would trigger a re-initialization (even leaking xdg_config if it was set in the last round).

So I think you'd need a separate "static int initialized" variable.

That said, I wonder if we should just pass ownership of the memory to the caller. It is a minor inconvenience that they will have to free() the result, but we're already doing that. And it removes any possibility of thread unsafety.

I guess it doesn't match git_system_config() as well, then. But arguably it should also just pass ownership (it also has only a handful of callers, and freeing the result would not be a big deal).

I'm OK with either solution, though.
-Peff
Previous: Patrick SteinhardtNext: Patrick Steinhardt
Message 15 of 52 in “config: Introduce GIT_CONFIG_NOGLOBAL”
  1. config: Introduce GIT_CONFIG_NOGLOBALPatrick Steinhardt, Apr 8, 2021
  2. Eric SunshineApr 8, 2021
  3. Junio C HamanoApr 8, 2021
  4. Jeff KingApr 8, 2021
  5. Junio C HamanoApr 8, 2021
  6. Jeff KingApr 9, 2021
  7. Ævar Arnfjörð BjarmasonApr 8, 2021
  8. Ævar Arnfjörð BjarmasonApr 8, 2021
  9. Ævar Arnfjörð BjarmasonApr 8, 2021
  10. Junio C HamanoApr 8, 2021
  11. 0/3 config: allow overriding global/system configPatrick Steinhardt, Apr 9, 2021
  12. 1/3 config: rename `git_etc_config()`Patrick Steinhardt, Apr 9, 2021
  13. Jeff KingApr 9, 2021
  14. 2/3 config: unify code paths to get global config pathsPatrick Steinhardt, Apr 9, 2021
  15. Jeff KingApr 9, 2021
  16. 3/3 config: allow overriding of global and system configurationPatrick Steinhardt, Apr 9, 2021
  17. Jeff KingApr 9, 2021
  18. Patrick SteinhardtApr 12, 2021
  19. Junio C HamanoApr 9, 2021
  20. Jeff KingApr 9, 2021
  21. 0/3 config: allow overriding global/system configPatrick Steinhardt, Apr 12, 2021
  22. 1/3 config: rename `git_etc_config()`Patrick Steinhardt, Apr 12, 2021
  23. 2/3 config: unify code paths to get global config pathsPatrick Steinhardt, Apr 12, 2021
  24. 3/3 config: allow overriding of global and system configurationPatrick Steinhardt, Apr 12, 2021
  25. Junio C HamanoApr 12, 2021
  26. 0/3 config: allow overriding global/system configPatrick Steinhardt, Apr 13, 2021
  27. 1/3 config: rename `git_etc_config()`Patrick Steinhardt, Apr 13, 2021
  28. Jeff KingApr 13, 2021
  29. SZEDER GáborApr 16, 2021
  30. Jeff KingApr 17, 2021
  31. Junio C HamanoApr 17, 2021
  32. Jeff KingApr 18, 2021
  33. Patrick SteinhardtApr 19, 2021
  34. Jeff KingApr 23, 2021
  35. 2/3 config: unify code paths to get global config pathsPatrick Steinhardt, Apr 13, 2021
  36. 3/3 config: allow overriding of global and system configurationPatrick Steinhardt, Apr 13, 2021
  37. Jeff KingApr 13, 2021
  38. Patrick SteinhardtApr 13, 2021
  39. Jeff KingApr 13, 2021
  40. Junio C HamanoApr 13, 2021
  41. Patrick SteinhardtApr 14, 2021
  42. 0/3 config: allow overriding global/system configPatrick Steinhardt, Apr 19, 2021
  43. 1/3 config: rename `git_etc_config()`Patrick Steinhardt, Apr 19, 2021
  44. 2/3 config: unify code paths to get global config pathsPatrick Steinhardt, Apr 19, 2021
  45. 3/3 config: allow overriding of global and system configurationPatrick Steinhardt, Apr 19, 2021
  46. SZEDER GáborApr 21, 2021
  47. SZEDER GáborApr 21, 2021
  48. Patrick SteinhardtApr 22, 2021
  49. t1300: fix unset of GIT_CONFIG_NOSYSTEM leaking into subsequent testsPatrick Steinhardt, Apr 23, 2021
  50. Junio C HamanoApr 19, 2021
  51. Jeff KingApr 23, 2021
  52. config: allow overriding of global and system configurationPatrick Steinhardt, Apr 12, 2021

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.