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

[PATCH v1 3/4] config: factor out global config file retrieval

From
Kristoffer Haugsbakk <code@khaugsbakk.name>
Date
Oct 18, 2023, 20:28 UTC
Message-ID
<147c767443c35b3b4a5516bf40557f41bb201078.1697660181.git.code@khaugsbakk.name>
In-Reply-To
<cover.1697660181.git.code@khaugsbakk.name>

Factor out code that retrieves the global config file so that we can use it in `gc.c` as well.

Use the old name from the previous commit since this function acts functionally the same as `git_system_config` but for “global”.

Signed-off-by: Kristoffer Haugsbakk <code@khaugsbakk.name>
---
 builtin/config.c | 25 ++-----------------------
 config.c         | 24 ++++++++++++++++++++++++
 config.h         |  1 +
 3 files changed, 27 insertions(+), 23 deletions(-)
diff --git a/builtin/config.c b/builtin/config.c
index 6fff2655816..df06b766fad 100644
--- a/builtin/config.c
+++ b/builtin/config.c
@@ -708,30 +708,9 @@ int cmd_config(int argc, const char **argv, const char *prefix)
 	}
 
 	if (use_global_config) {
-		char *user_config, *xdg_config;
-
-		git_global_config_paths(&user_config, &xdg_config);
-		if (!user_config)
-			/*
-			 * It is unknown if HOME/.gitconfig exists, so
-			 * we do not know if we should write to XDG
-			 * location; error out even if XDG_CONFIG_HOME
-			 * is set and points at a sane location.
-			 */
-			die(_("$HOME not set"));
-
+		given_config_source.file = git_global_config();
 		given_config_source.scope = CONFIG_SCOPE_GLOBAL;
-
-		if (access_or_warn(user_config, R_OK, 0) &&
-		    xdg_config && !access_or_warn(xdg_config, R_OK, 0)) {
-			given_config_source.file = xdg_config;
-			free(user_config);
-		} else {
-			given_config_source.file = user_config;
-			free(xdg_config);
-		}
-	}
-	else if (use_system_config) {
+	} else if (use_system_config) {
 		given_config_source.file = git_system_config();
 		given_config_source.scope = CONFIG_SCOPE_SYSTEM;
 	} else if (use_local_config) {
diff --git a/config.c b/config.c
index d2cdda96edd..2ff766c56ff 100644
--- a/config.c
+++ b/config.c
@@ -2111,6 +2111,30 @@ char *git_system_config(void)
 	return system_config;
 }
 
+char *git_global_config(void)
+{
+	char *user_config, *xdg_config;
+
+	git_global_config_paths(&user_config, &xdg_config);
+	if (!user_config)
+		/*
+		 * It is unknown if HOME/.gitconfig exists, so
+		 * we do not know if we should write to XDG
+		 * location; error out even if XDG_CONFIG_HOME
+		 * is set and points at a sane location.
+		 */
+		die(_("$HOME not set"));
+
+	if (access_or_warn(user_config, R_OK, 0) && xdg_config &&
+	    !access_or_warn(xdg_config, R_OK, 0)) {
+		free(user_config);
+		return xdg_config;
+	} else {
+		free(xdg_config);
+		return user_config;
+	}
+}
+
 void git_global_config_paths(char **user_out, char **xdg_out)
 {
 	char *user_config = xstrdup_or_null(getenv("GIT_CONFIG_GLOBAL"));
diff --git a/config.h b/config.h
index 9f04de8ee3e..5cf961b548d 100644
--- a/config.h
+++ b/config.h
@@ -394,6 +394,7 @@ int config_error_nonbool(const char *);
 #endif
 
 char *git_system_config(void);
+char *git_global_config(void);
 void git_global_config_paths(char **user, char **xdg);
 
 int git_config_parse_parameter(const char *, config_fn_t fn, void *data);
-- 
2.42.0.2.g879ad04204
Previous: Kristoffer HaugsbakkNext: Patrick Steinhardt
Message 4 of 33 in “maintenance: use XDG config if it exists”
  1. 0/4 maintenance: use XDG config if it existsKristoffer Haugsbakk, Oct 18, 2023
  2. 1/4 config: format newlinesKristoffer Haugsbakk, Oct 18, 2023
  3. 2/4 config: rename global config functionKristoffer Haugsbakk, Oct 18, 2023
  4. 3/4 config: factor out global config file retrievalKristoffer Haugsbakk, Oct 18, 2023
  5. Patrick SteinhardtOct 23, 2023
  6. Taylor BlauOct 23, 2023
  7. Kristoffer HaugsbakkOct 24, 2023
  8. Patrick SteinhardtOct 25, 2023
  9. Kristoffer HaugsbakkOct 25, 2023
  10. Patrick SteinhardtOct 25, 2023
  11. Junio C HamanoOct 27, 2023
  12. 4/4 maintenance: use XDG config if it existsKristoffer Haugsbakk, Oct 18, 2023
  13. Patrick SteinhardtOct 23, 2023
  14. Eric SunshineOct 23, 2023
  15. 0/4 maintenance: use XDG config if it existsKristoffer Haugsbakk, Jan 14, 2024
  16. 1/4 config: format newlinesKristoffer Haugsbakk, Jan 14, 2024
  17. 2/4 config: rename global config functionKristoffer Haugsbakk, Jan 14, 2024
  18. 3/4 config: factor out global config file retrievalKristoffer Haugsbakk, Jan 14, 2024
  19. Junio C HamanoJan 16, 2024
  20. Kristoffer HaugsbakkJan 16, 2024
  21. Patrick SteinhardtJan 19, 2024
  22. Kristoffer HaugsbakkJan 19, 2024
  23. Patrick SteinhardtJan 19, 2024
  24. Junio C HamanoJan 19, 2024
  25. Junio C HamanoJan 19, 2024
  26. rsbecker@nexbridge.comJan 19, 2024
  27. 4/4 maintenance: use XDG config if it existsKristoffer Haugsbakk, Jan 14, 2024
  28. Junio C HamanoJan 16, 2024
  29. 0/4 maintenance: use XDG config if it existsKristoffer Haugsbakk, Jan 18, 2024
  30. 1/4 config: format newlinesKristoffer Haugsbakk, Jan 18, 2024
  31. 2/4 config: rename global config functionKristoffer Haugsbakk, Jan 18, 2024
  32. 3/4 config: factor out global config file retrievalKristoffer Haugsbakk, Jan 18, 2024
  33. 4/4 maintenance: use XDG config if it existsKristoffer Haugsbakk, Jan 18, 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.