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

[PATCH 1/9] config: teach repo_config to allow `repo` to be NULL

From
Usman Akinyemi <usmanakinyemi202@gmail.com>
Date
Mar 18, 2025, 11:58 UTC
Message-ID
<20250318115912.2978992-2-usmanakinyemi202@gmail.com>
In-Reply-To
<20250318115912.2978992-1-usmanakinyemi202@gmail.com>

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>
---
 config.c | 4 ++++
 config.h | 9 +++++++++
 2 files changed, 13 insertions(+)
diff --git a/config.c b/config.c
index 658569af08..e127afaa8f 100644
--- 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);
 }
diff --git a/config.h b/config.h
index 5c730c4f89..29a0277483 100644
--- 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 *);
-- 
2.48.1
Previous: Usman AkinyemiNext: Patrick Steinhardt
Message 2 of 17 in “remove unnecessary if statement”
  1. 0/9 remove unnecessary if statementUsman Akinyemi, Mar 18, 2025
  2. 1/9 config: teach repo_config to allow `repo` to be NULLUsman Akinyemi, Mar 18, 2025
  3. Patrick SteinhardtMar 20, 2025
  4. Usman AkinyemiMar 21, 2025
  5. 2/9 builtin/verify-tag: stop using `the_repository`Usman Akinyemi, Mar 18, 2025
  6. Patrick SteinhardtMar 20, 2025
  7. 3/9 builtin/verify-commit: stop using `the_repository`Usman Akinyemi, Mar 18, 2025
  8. 4/9 builtin/send-pack: stop using `the_repository`Usman Akinyemi, Mar 18, 2025
  9. 5/9 builtin/pack-refs: stop using `the_repository`Usman Akinyemi, Mar 18, 2025
  10. 6/9 builtin/ls-files: stop using `the_repository`Usman Akinyemi, Mar 18, 2025
  11. 7/9 builtin/for-each-ref: stop using `the_repository`Usman Akinyemi, Mar 18, 2025
  12. 8/9 builtin/checkout-index: stop using `the_repository`Usman Akinyemi, Mar 18, 2025
  13. 9/9 builtin/update-server-info: remove unnecessary if statementUsman Akinyemi, Mar 18, 2025
  14. Patrick SteinhardtMar 20, 2025
  15. Usman AkinyemiMar 21, 2025
  16. Junio C HamanoMar 18, 2025
  17. Usman AkinyemiMar 19, 2025

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.