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

[PATCH 08/21] config: make dependency on repo in `read_early_config()` explicit

From
Patrick Steinhardt <ps@pks.im>
Date
Aug 29, 2024, 09:38 UTC
Message-ID
<85e40696cc96c0956de4c9221e7ae49b1789163b.1724923648.git.ps@pks.im>
In-Reply-To
<cover.1724923648.git.ps@pks.im>

The `read_early_config()` function can be used to read configuration where a repository has not yet been set up. As such, it is optional whether or not `the_repository` has already been initialized. If it was initialized we use its commondir and gitdir. If not, the function will try to detect the Git directories by itself and, if found, also parse their config files.

This means that we implicitly rely on `the_repository`. Make this dependency explicit by passing a `struct repository`. This allows us to again drop the `USE_THE_REPOSITORY_VARIABLE` define in "config.c".

Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
 alias.c                |  6 ++++--
 config.c               | 10 ++++------
 config.h               |  2 +-
 help.c                 |  2 +-
 pager.c                |  7 +++++--
 t/helper/test-config.c |  3 ++-
 trace2/tr2_cfg.c       |  4 +++-
 7 files changed, 20 insertions(+), 14 deletions(-)
diff --git a/alias.c b/alias.c
index 4daafd9bdae..1a1a141a0ae 100644
--- a/alias.c
+++ b/alias.c
@@ -1,3 +1,5 @@
+#define USE_THE_REPOSITORY_VARIABLE
+
 #include "git-compat-util.h"
 #include "alias.h"
 #include "config.h"
@@ -37,7 +39,7 @@ char *alias_lookup(const char *alias)
 {
 	struct config_alias_data data = { alias, NULL };
 
-	read_early_config(config_alias_cb, &data);
+	read_early_config(the_repository, config_alias_cb, &data);
 
 	return data.v;
 }
@@ -46,7 +48,7 @@ void list_aliases(struct string_list *list)
 {
 	struct config_alias_data data = { NULL, NULL, list };
 
-	read_early_config(config_alias_cb, &data);
+	read_early_config(the_repository, config_alias_cb, &data);
 }
 
 void quote_cmdline(struct strbuf *buf, const char **argv)
diff --git a/config.c b/config.c
index a8357ea9544..043e1c8a078 100644
--- a/config.c
+++ b/config.c
@@ -6,8 +6,6 @@
  *
  */
 
-#define USE_THE_REPOSITORY_VARIABLE
-
 #include "git-compat-util.h"
 #include "abspath.h"
 #include "advice.h"
@@ -2204,7 +2202,7 @@ static void configset_iter(struct config_set *set, config_fn_t fn, void *data)
 	}
 }
 
-void read_early_config(config_fn_t cb, void *data)
+void read_early_config(struct repository *repo, config_fn_t cb, void *data)
 {
 	struct config_options opts = {0};
 	struct strbuf commondir = STRBUF_INIT;
@@ -2212,9 +2210,9 @@ void read_early_config(config_fn_t cb, void *data)
 
 	opts.respect_includes = 1;
 
-	if (have_git_dir()) {
-		opts.commondir = repo_get_common_dir(the_repository);
-		opts.git_dir = repo_get_git_dir(the_repository);
+	if (repo && repo->gitdir) {
+		opts.commondir = repo_get_common_dir(repo);
+		opts.git_dir = repo_get_git_dir(repo);
 	/*
 	 * When setup_git_directory() was not yet asked to discover the
 	 * GIT_DIR, we ask discover_git_directory() to figure out whether there
diff --git a/config.h b/config.h
index f5fa833cb98..5c730c4f899 100644
--- a/config.h
+++ b/config.h
@@ -198,7 +198,7 @@ int git_config_from_parameters(config_fn_t fn, void *data);
  * `the_repository` has not yet been set up, try to discover the Git
  * directory to read the configuration from.
  */
-void read_early_config(config_fn_t cb, void *data);
+void read_early_config(struct repository *repo, config_fn_t cb, void *data);
 
 /*
  * Read config but only enumerate system and global settings.
diff --git a/help.c b/help.c
index c03863f2265..413c93edaea 100644
--- a/help.c
+++ b/help.c
@@ -618,7 +618,7 @@ const char *help_unknown_cmd(const char *cmd)
 	memset(&other_cmds, 0, sizeof(other_cmds));
 	memset(&aliases, 0, sizeof(aliases));
 
-	read_early_config(git_unknown_cmd_config, NULL);
+	read_early_config(the_repository, git_unknown_cmd_config, NULL);
 
 	/*
 	 * Disable autocorrection prompt in a non-interactive session
diff --git a/pager.c b/pager.c
index 9c24ce62633..40b664f893c 100644
--- a/pager.c
+++ b/pager.c
@@ -1,3 +1,5 @@
+#define USE_THE_REPOSITORY_VARIABLE
+
 #include "git-compat-util.h"
 #include "config.h"
 #include "editor.h"
@@ -92,7 +94,8 @@ const char *git_pager(int stdout_is_tty)
 	pager = getenv("GIT_PAGER");
 	if (!pager) {
 		if (!pager_program)
-			read_early_config(core_pager_config, NULL);
+			read_early_config(the_repository,
+					  core_pager_config, NULL);
 		pager = pager_program;
 	}
 	if (!pager)
@@ -298,7 +301,7 @@ int check_pager_config(const char *cmd)
 	data.want = -1;
 	data.value = NULL;
 
-	read_early_config(pager_command_config, &data);
+	read_early_config(the_repository, pager_command_config, &data);
 
 	if (data.value)
 		pager_program = data.value;
diff --git a/t/helper/test-config.c b/t/helper/test-config.c
index e193079ed54..33247f0e92e 100644
--- a/t/helper/test-config.c
+++ b/t/helper/test-config.c
@@ -96,7 +96,8 @@ int cmd__config(int argc, const char **argv)
 	struct config_set cs;
 
 	if (argc == 3 && !strcmp(argv[1], "read_early_config")) {
-		read_early_config(early_config_cb, (void *)argv[2]);
+		read_early_config(the_repository, early_config_cb,
+				  (void *)argv[2]);
 		return 0;
 	}
 
diff --git a/trace2/tr2_cfg.c b/trace2/tr2_cfg.c
index d96d908bb9d..22a99a0682a 100644
--- a/trace2/tr2_cfg.c
+++ b/trace2/tr2_cfg.c
@@ -1,3 +1,5 @@
+#define USE_THE_REPOSITORY_VARIABLE
+
 #include "git-compat-util.h"
 #include "config.h"
 #include "strbuf.h"
@@ -124,7 +126,7 @@ void tr2_cfg_list_config_fl(const char *file, int line)
 	struct tr2_cfg_data data = { file, line };
 
 	if (tr2_cfg_load_patterns() > 0)
-		read_early_config(tr2_cfg_cb, &data);
+		read_early_config(the_repository, tr2_cfg_cb, &data);
 }
 
 void tr2_list_env_vars_fl(const char *file, int line)
-- 
2.46.0.421.g159f2d50e7.dirty
Previous: Patrick SteinhardtNext: Patrick Steinhardt
Message 11 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.