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

[PATCH 1/2] allow suppressing of global and system config

From
Jeff King <peff@peff.net>
Date
Feb 6, 2008, 10:11 UTC
Message-ID
<20080206101118.GA1123@coredump.intra.peff.net>
In-Reply-To
<20080206092753.GA32264@coredump.intra.peff.net>

The GIT_CONFIG_NOGLOBAL and GIT_CONFIG_NOSYSTEM environment variables are magic undocumented switches that can be used to ensure a totally clean environment. This is necessary for running reliable tests, since those config files may contain settings that change the outcome of tests.

Signed-off-by: Jeff King <peff@peff.net>
---
You can see that the config file lookup is more or less duplicated in
config.c and in builtin-config.c. I tried to keep the patch minimal, but
it's possible that those can be refactored into a single lookup point.
 builtin-config.c |    5 +++--
 cache.h          |    3 +++
 config.c         |   20 ++++++++++++++++++--
 3 files changed, 24 insertions(+), 4 deletions(-)
diff --git a/builtin-config.c b/builtin-config.c
index e4a12e3..404bb44 100644
--- a/builtin-config.c
+++ b/builtin-config.c
@@ -79,9 +79,10 @@ static int get_value(const char* key_, const char* regex_)
 		local = getenv(CONFIG_LOCAL_ENVIRONMENT);
 		if (!local)
 			local = repo_config = xstrdup(git_path("config"));
-		if (home)
+		if (git_config_global() && home)
 			global = xstrdup(mkpath("%s/.gitconfig", home));
-		system_wide = git_etc_gitconfig();
+		if (git_config_system())
+			system_wide = git_etc_gitconfig();
 	}
 
 	key = xstrdup(key_);
diff --git a/cache.h b/cache.h
index 549f4bb..af74e20 100644
--- a/cache.h
+++ b/cache.h
@@ -589,6 +589,9 @@ extern int git_config_set_multivar(const char *, const char *, const char *, int
 extern int git_config_rename_section(const char *, const char *);
 extern const char *git_etc_gitconfig(void);
 extern int check_repository_format_version(const char *var, const char *value);
+extern int git_env_bool(const char *, int);
+extern int git_config_system(void);
+extern int git_config_global(void);
 
 #define MAX_GITNAME (1000)
 extern char git_default_email[MAX_GITNAME];
diff --git a/config.c b/config.c
index 498259e..2f85e9d 100644
--- a/config.c
+++ b/config.c
@@ -492,6 +492,22 @@ const char *git_etc_gitconfig(void)
 	return system_wide;
 }
 
+int git_env_bool(const char *k, int def)
+{
+	const char *v = getenv(k);
+	return v ? git_config_bool(k, v) : def;
+}
+
+int git_config_system(void)
+{
+	return !git_env_bool("GIT_CONFIG_NOSYSTEM", 0);
+}
+
+int git_config_global(void)
+{
+	return !git_env_bool("GIT_CONFIG_NOGLOBAL", 0);
+}
+
 int git_config(config_fn_t fn)
 {
 	int ret = 0;
@@ -504,7 +520,7 @@ int git_config(config_fn_t fn)
 	 * config file otherwise. */
 	filename = getenv(CONFIG_ENVIRONMENT);
 	if (!filename) {
-		if (!access(git_etc_gitconfig(), R_OK))
+		if (git_config_system() && !access(git_etc_gitconfig(), R_OK))
 			ret += git_config_from_file(fn, git_etc_gitconfig());
 		home = getenv("HOME");
 		filename = getenv(CONFIG_LOCAL_ENVIRONMENT);
@@ -512,7 +528,7 @@ int git_config(config_fn_t fn)
 			filename = repo_config = xstrdup(git_path("config"));
 	}
 
-	if (home) {
+	if (git_config_global() && home) {
 		char *user_config = xstrdup(mkpath("%s/.gitconfig", home));
 		if (!access(user_config, R_OK))
 			ret = git_config_from_file(fn, user_config);
-- 
1.5.4.25.g251c56-dirty
Previous: Jeff KingNext: Jeff King
Message 7 of 8 in “Re: [PATCH] Make tests independent of global config files”
  1. Jeff KingFeb 6, 2008
  2. Jeff KingFeb 6, 2008
  3. 1/2 allow GIT_CONFIG_ETC to override compile-time settingJeff King, Feb 6, 2008
  4. 2/2 fix config reading in testsJeff King, Feb 6, 2008
  5. Junio C HamanoFeb 6, 2008
  6. Jeff KingFeb 6, 2008
  7. 1/2 allow suppressing of global and system configJeff King, Feb 6, 2008
  8. 2/2 fix config reading in testsJeff King, Feb 6, 2008

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.