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

Re: [PATCH v2 10/21] environment: make `get_git_namespace()` self-contained

From
Justin Tobler <jltobler@gmail.com>
Date
Sep 11, 2024, 16:21 UTC
Message-ID
<otvkxgywps6smoib32x4gqiwc4klbjl6qbh66bh6wwwfvb2b2p@22gqczmx3p3e>
In-Reply-To
<f0d3794dfc44cd4393fd79fab9f60b73cf33db89.1725008898.git.ps@pks.im>
On 24/08/30 11:09AM, Patrick Steinhardt wrote:
Show 11 quoted lines
> The logic to set up and retrieve `git_namespace` is distributed across
> different functions which communicate with each other via a global
> environment variable. This is rather pointless though, as the value is
> always derived from an environment variable, and this environment
> variable does not change after we have parsed global options.
> 
> Convert the function to be fully self-contained such that it lazily
> populates once called.
> 
> Signed-off-by: Patrick Steinhardt <ps@pks.im>
> ---
[snip]
Show 15 quoted lines
> @@ -229,9 +204,35 @@ int have_git_dir(void)
>  
>  const char *get_git_namespace(void)
>  {
> -	if (!git_namespace)
> -		BUG("git environment hasn't been setup");
> -	return git_namespace;
> +	static const char *namespace;
> +
> +	struct strbuf buf = STRBUF_INIT;
> +	struct strbuf **components, **c;
> +	const char *raw_namespace;
> +
> +	if (namespace)
> +		return namespace;

We only initialize `namespace` once and do it lazily when `get_git_namespace()` is invoked for the first time. This is nice because we can simplify and remove the `git_namespace` global.

Makes sense to me.
Show 27 quoted lines
> +
> +	raw_namespace = getenv(GIT_NAMESPACE_ENVIRONMENT);
> +	if (!raw_namespace || !*raw_namespace) {
> +		namespace = "";
> +		return namespace;
> +	}
> +
> +	strbuf_addstr(&buf, raw_namespace);
> +	components = strbuf_split(&buf, '/');
> +	strbuf_reset(&buf);
> +	for (c = components; *c; c++)
> +		if (strcmp((*c)->buf, "/") != 0)
> +			strbuf_addf(&buf, "refs/namespaces/%s", (*c)->buf);
> +	strbuf_list_free(components);
> +	if (check_refname_format(buf.buf, 0))
> +		die(_("bad git namespace path \"%s\""), raw_namespace);
> +	strbuf_addch(&buf, '/');
> +
> +	namespace = strbuf_detach(&buf, NULL);
> +
> +	return namespace;
>  }
>  
>  const char *strip_namespace(const char *namespaced_ref)
> -- 
> 2.46.0.421.g159f2d50e7.dirty
> 
Previous: Patrick SteinhardtNext: Patrick Steinhardt
Message 50 of 92 in “environment: guard reliance on `the_repository`”
  1. 00/21 environment: guard reliance on `the_repository`Patrick Steinhardt, Aug 29, 2024
  2. 01/21 environment: make `get_git_dir()` accept a repositoryPatrick Steinhardt, Aug 29, 2024
  3. Justin ToblerAug 29, 2024
  4. Patrick SteinhardtAug 30, 2024
  5. 02/21 environment: make `get_git_common_dir()` accept a repositoryPatrick Steinhardt, Aug 29, 2024
  6. 03/21 environment: make `get_object_directory()` accept a repositoryPatrick Steinhardt, Aug 29, 2024
  7. 04/21 environment: make `get_index_file()` accept a repositoryPatrick Steinhardt, Aug 29, 2024
  8. 05/21 environment: make `get_graft_file()` accept a repositoryPatrick Steinhardt, Aug 29, 2024
  9. 06/21 environment: make `get_git_work_tree()` accept a repositoryPatrick Steinhardt, Aug 29, 2024
  10. 07/21 config: document `read_early_config()` and `read_very_early_config()`Patrick Steinhardt, Aug 29, 2024
  11. 08/21 config: make dependency on repo in `read_early_config()` explicitPatrick Steinhardt, Aug 29, 2024
  12. 09/21 environment: move `odb_mkstemp()` into object layerPatrick Steinhardt, Aug 29, 2024
  13. 10/21 environment: make `get_git_namespace()` self-containedPatrick Steinhardt, Aug 29, 2024
  14. 11/21 environment: move `set_git_dir()` and related into setup layerPatrick Steinhardt, Aug 29, 2024
  15. 12/21 environment: reorder header to split out `the_repository`-free sectionPatrick Steinhardt, Aug 29, 2024
  16. 13/21 environment: guard state depending on a repositoryPatrick Steinhardt, Aug 29, 2024
  17. 14/21 repo-settings: split out declarations into a standalone headerPatrick Steinhardt, Aug 29, 2024
  18. 15/21 branch: stop modifying `log_all_ref_updates` variablePatrick Steinhardt, Aug 29, 2024
  19. 16/21 refs: stop modifying global `log_all_ref_updates` variablePatrick Steinhardt, Aug 29, 2024
  20. 17/21 repo-settings: track defaults close to `struct repo_settings`Patrick Steinhardt, Aug 29, 2024
  21. 18/21 environment: stop storing "core.logAllRefUpdates" globallyPatrick Steinhardt, Aug 29, 2024
  22. 19/21 environment: stop storing "core.preferSymlinkRefs" globallyPatrick Steinhardt, Aug 29, 2024
  23. 20/21 environment: stop storing "core.warnAmbiguousRefs" globallyPatrick Steinhardt, Aug 29, 2024
  24. 21/21 environment: stop storing "core.notesRef" globallyPatrick Steinhardt, Aug 29, 2024
  25. Junio C HamanoAug 29, 2024
  26. Patrick SteinhardtAug 30, 2024
  27. Junio C HamanoAug 30, 2024
  28. Patrick SteinhardtSep 2, 2024
  29. 00/21 environment: guard reliance on `the_repository`Patrick Steinhardt, Aug 30, 2024
  30. 01/21 environment: make `get_git_dir()` accept a repositoryPatrick Steinhardt, Aug 30, 2024
  31. karthik nayakSep 11, 2024
  32. Patrick SteinhardtSep 12, 2024
  33. 02/21 environment: make `get_git_common_dir()` accept a repositoryPatrick Steinhardt, Aug 30, 2024
  34. 03/21 environment: make `get_object_directory()` accept a repositoryPatrick Steinhardt, Aug 30, 2024
  35. 04/21 environment: make `get_index_file()` accept a repositoryPatrick Steinhardt, Aug 30, 2024
  36. 05/21 environment: make `get_graft_file()` accept a repositoryPatrick Steinhardt, Aug 30, 2024
  37. 06/21 environment: make `get_git_work_tree()` accept a repositoryPatrick Steinhardt, Aug 30, 2024
  38. Justin ToblerSep 11, 2024
  39. Patrick SteinhardtSep 12, 2024
  40. 07/21 config: document `read_early_config()` and `read_very_early_config()`Patrick Steinhardt, Aug 30, 2024
  41. Justin ToblerSep 11, 2024
  42. Patrick SteinhardtSep 12, 2024
  43. 08/21 config: make dependency on repo in `read_early_config()` explicitPatrick Steinhardt, Aug 30, 2024
  44. James LiuSep 4, 2024
  45. Patrick SteinhardtSep 4, 2024
  46. 09/21 environment: move `odb_mkstemp()` into object layerPatrick Steinhardt, Aug 30, 2024
  47. karthik nayakSep 11, 2024
  48. Patrick SteinhardtSep 12, 2024
  49. 10/21 environment: make `get_git_namespace()` self-containedPatrick Steinhardt, Aug 30, 2024
  50. Justin ToblerSep 11, 2024
  51. 11/21 environment: move `set_git_dir()` and related into setup layerPatrick Steinhardt, Aug 30, 2024
  52. 12/21 environment: reorder header to split out `the_repository`-free sectionPatrick Steinhardt, Aug 30, 2024
  53. 13/21 environment: guard state depending on a repositoryPatrick Steinhardt, Aug 30, 2024
  54. 14/21 repo-settings: split out declarations into a standalone headerPatrick Steinhardt, Aug 30, 2024
  55. 15/21 repo-settings: track defaults close to `struct repo_settings`Patrick Steinhardt, Aug 30, 2024
  56. 16/21 branch: stop modifying `log_all_ref_updates` variablePatrick Steinhardt, Aug 30, 2024
  57. Justin ToblerSep 11, 2024
  58. Patrick SteinhardtSep 12, 2024
  59. 17/21 refs: stop modifying global `log_all_ref_updates` variablePatrick Steinhardt, Aug 30, 2024
  60. 18/21 environment: stop storing "core.logAllRefUpdates" globallyPatrick Steinhardt, Aug 30, 2024
  61. karthik nayakSep 12, 2024
  62. 19/21 environment: stop storing "core.preferSymlinkRefs" globallyPatrick Steinhardt, Aug 30, 2024
  63. 20/21 environment: stop storing "core.warnAmbiguousRefs" globallyPatrick Steinhardt, Aug 30, 2024
  64. James LiuSep 4, 2024
  65. 21/21 environment: stop storing "core.notesRef" globallyPatrick Steinhardt, Aug 30, 2024
  66. James LiuSep 4, 2024
  67. Patrick SteinhardtSep 4, 2024
  68. karthik nayakSep 12, 2024
  69. Patrick SteinhardtSep 12, 2024
  70. 00/21 environment: guard reliance on `the_repository`Patrick Steinhardt, Sep 12, 2024
  71. 01/21 environment: make `get_git_dir()` accept a repositoryPatrick Steinhardt, Sep 12, 2024
  72. 02/21 environment: make `get_git_common_dir()` accept a repositoryPatrick Steinhardt, Sep 12, 2024
  73. 03/21 environment: make `get_object_directory()` accept a repositoryPatrick Steinhardt, Sep 12, 2024
  74. 04/21 environment: make `get_index_file()` accept a repositoryPatrick Steinhardt, Sep 12, 2024
  75. 05/21 environment: make `get_graft_file()` accept a repositoryPatrick Steinhardt, Sep 12, 2024
  76. 06/21 environment: make `get_git_work_tree()` accept a repositoryPatrick Steinhardt, Sep 12, 2024
  77. 07/21 config: document `read_early_config()` and `read_very_early_config()`Patrick Steinhardt, Sep 12, 2024
  78. 08/21 config: make dependency on repo in `read_early_config()` explicitPatrick Steinhardt, Sep 12, 2024
  79. 09/21 environment: move object database functions into object layerPatrick Steinhardt, Sep 12, 2024
  80. 10/21 environment: make `get_git_namespace()` self-containedPatrick Steinhardt, Sep 12, 2024
  81. 11/21 environment: move `set_git_dir()` and related into setup layerPatrick Steinhardt, Sep 12, 2024
  82. 12/21 environment: reorder header to split out `the_repository`-free sectionPatrick Steinhardt, Sep 12, 2024
  83. 13/21 environment: guard state depending on a repositoryPatrick Steinhardt, Sep 12, 2024
  84. 14/21 repo-settings: split out declarations into a standalone headerPatrick Steinhardt, Sep 12, 2024
  85. 15/21 repo-settings: track defaults close to `struct repo_settings`Patrick Steinhardt, Sep 12, 2024
  86. 16/21 branch: stop modifying `log_all_ref_updates` variablePatrick Steinhardt, Sep 12, 2024
  87. 17/21 refs: stop modifying global `log_all_ref_updates` variablePatrick Steinhardt, Sep 12, 2024
  88. 18/21 environment: stop storing "core.logAllRefUpdates" globallyPatrick Steinhardt, Sep 12, 2024
  89. 19/21 environment: stop storing "core.preferSymlinkRefs" globallyPatrick Steinhardt, Sep 12, 2024
  90. 20/21 environment: stop storing "core.warnAmbiguousRefs" globallyPatrick Steinhardt, Sep 12, 2024
  91. 21/21 environment: stop storing "core.notesRef" globallyPatrick Steinhardt, Sep 12, 2024
  92. Junio C HamanoSep 12, 2024

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.