]> git.ipfire.org Git - thirdparty/git.git/commitdiff
config: make dependency on repo in `read_early_config()` explicit
authorPatrick Steinhardt <ps@pks.im>
Thu, 12 Sep 2024 11:29:45 +0000 (13:29 +0200)
committerJunio C Hamano <gitster@pobox.com>
Thu, 12 Sep 2024 17:15:40 +0000 (10:15 -0700)
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>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
alias.c
config.c
config.h
help.c
pager.c
t/helper/test-config.c
trace2/tr2_cfg.c

diff --git a/alias.c b/alias.c
index 4daafd9bdae66ef9bd8cdf147699e4057cde89f7..1a1a141a0aebbb6b00e976f83def4a5694fd06c3 100644 (file)
--- 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)
index a8357ea9544ad978be02a9016be403523c7e64f8..043e1c8a078c1e78affc721b1227dc56cb85d3f7 100644 (file)
--- 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
index f5fa833cb98521f6accb81a457ec78a1d0141009..5c730c4f899160285bb9d5a509542514d9279c67 100644 (file)
--- 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 c03863f2265f7bbaba72fc26b377a677683ea58d..413c93edaea0be0dd21edc3a8c205b4d91a1a405 100644 (file)
--- 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 9c24ce6263385eb4af306453c1eb2668edd3af1e..40b664f893c8ec61af0e6d00d58956f45b247d65 100644 (file)
--- 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;
index e193079ed54a7a1b1617607420b6d75e116ee643..33247f0e92e01ddf24e219894fea6b3aa66de26e 100644 (file)
@@ -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;
        }
 
index d96d908bb9df6fddb22e4dbecfa07882049bdf78..22a99a0682a4985d43cfb2bc4ae26c8954e09de4 100644 (file)
@@ -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)