config: teach repo_config to allow `repo` to be NULL

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>

Usman Akinyemi committed Mar 8, 2025 at 05:05 UTC f29f1990b5605f8dbed65324d8e92b67d4dd1713
2 files changed +13
config.c
+4
@@ -2521,6 +2521,10 @@ void repo_config_clear(struct repository *repo)
2521
2522 void repo_config(struct repository *repo, config_fn_t fn, void *data)
2523 {
2524 + if (!repo) {
2525 + read_very_early_config(fn, data);
2526 + return;
2527 + }
2528 git_config_check_init(repo);
2529 configset_iter(repo->config, fn, data);
2530 }
config.h
+9
@@ -219,6 +219,15 @@ void read_very_early_config(config_fn_t cb, void *data);
219 * repo-specific one; by overwriting, the higher-priority repo-specific
220 * value is left at the end).
221 *
222 + * In cases where the repository variable is NULL, repo_config() will
223 + * skip the per-repository config but retain system and global configs
224 + * by calling read_very_early_config() which also ignores one-time
225 + * overrides like "git -c var=val". This is to support handling "git foo -h"
226 + * (which lets git.c:run_builtin() to pass NULL and have the cmd_foo()
227 + * call repo_config() before calling parse_options() to notice "-h", give
228 + * help and exit) for a command that ordinarily require a repository
229 + * so this limitation may be OK (but if needed you are welcome to fix it).
230 + *
231 * Unlike git_config_from_file(), this function respects includes.
232 */
233 void repo_config(struct repository *r, config_fn_t fn, void *);