]> git.ipfire.org Git - thirdparty/git.git/commitdiff
config: teach repo_config to allow `repo` to be NULL
authorUsman Akinyemi <usmanakinyemi202@gmail.com>
Fri, 7 Mar 2025 23:35:00 +0000 (05:05 +0530)
committerJunio C Hamano <gitster@pobox.com>
Sat, 8 Mar 2025 00:52:00 +0000 (16:52 -0800)
The `repo` value can be NULL if a builtin command is run outside
any repository. The current implementation of `repo_config()` will
fail if `repo` is NULL.

If the `repo` is NULL the `repo_config()` can ignore the repository
configuration but it should read the other configuration sources like
the system-side configuration instead of failing.

Teach the `repo_config()` to allow `repo` to be NULL by calling the
`read_very_early_config()` which read config but only enumerate system
and global settings.

This will be useful in the following commits.

Suggested-by: Junio C Hamano <gitster@pobox.com>
Mentored-by: Christian Couder <chriscool@tuxfamily.org>
Signed-off-by: Usman Akinyemi <usmanakinyemi202@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
config.c
config.h

index dfd03b9421cdf94733224c118b54ea2318df5abe..f13a2870f16863d6052188eacc4cc79b927e940f 100644 (file)
--- a/config.c
+++ b/config.c
@@ -2521,6 +2521,10 @@ void repo_config_clear(struct repository *repo)
 
 void repo_config(struct repository *repo, config_fn_t fn, void *data)
 {
+       if (!repo) {
+               read_very_early_config(fn, data);
+               return;
+       }
        git_config_check_init(repo);
        configset_iter(repo->config, fn, data);
 }
index 5c730c4f899160285bb9d5a509542514d9279c67..29a027748375f1fedf31384ada7a3f70a435aa4c 100644 (file)
--- a/config.h
+++ b/config.h
@@ -219,6 +219,15 @@ void read_very_early_config(config_fn_t cb, void *data);
  * repo-specific one; by overwriting, the higher-priority repo-specific
  * value is left at the end).
  *
+ * In cases where the repository variable is NULL, repo_config() will
+ * skip the per-repository config but retain system and global configs
+ * by calling read_very_early_config() which also ignores one-time
+ * overrides like "git -c var=val". This is to support handling "git foo -h"
+ * (which lets git.c:run_builtin() to pass NULL and have the cmd_foo()
+ * call repo_config() before calling parse_options() to notice "-h", give
+ * help and exit) for a command that ordinarily require a repository
+ * so this limitation may be OK (but if needed you are welcome to fix it).
+ *
  * Unlike git_config_from_file(), this function respects includes.
  */
 void repo_config(struct repository *r, config_fn_t fn, void *);