Subject: [PATCH 1/4] config: allow customizing /etc/gitconfig location

From: Junio C Hamano <[email-masked]>
Date: Wed, 28 Sep 2016 16:30:44 -0700

With a new environment variable GIT_ETC_GITCONFIG, the users can
specify a file that is used instead of /etc/gitconfig to read (and
write) the system-wide configuration.

Earlier, we introduced GIT_CONFIG_NOSYSTEM environment variable
ab88c363 ("allow suppressing of global and system config",
2008-02-06), primarily to protect our tests from random set of
configuration variables the system administrators would put in their
/etc/gitconfig file.  We can replace the use of this mechanism in
our tests by pointing GIT_ETC_GITCONFIG at our own instead.

Signed-off-by: Junio C Hamano <[email-masked]>
---

 * The next step is to add "[core]abbrev=7" to this file and update
   default_abbrev to 12 in environment.c and see what breaks.  I
   suspect that "git worktree list" would break without my recent
   patch.  I also know some tests expect "git config -l" to show
   only values they set to their local configuration, which would
   need to be corrected.  We'll see them in next steps.

 cache.h                |  1 +
 config.c               |  2 ++
 t/gitconfig-for-test   |  6 ++++++
 t/t1300-repo-config.sh | 15 +++++++++++++++
 t/test-lib.sh          |  4 ++--
 5 files changed, 26 insertions(+), 2 deletions(-)
 create mode 100644 t/gitconfig-for-test

diff --git a/cache.h b/cache.h
index b0dae4b..81a07bf 100644
--- a/cache.h
+++ b/cache.h
@@ -408,6 +408,7 @@ static inline enum object_type object_type(unsigned int mode)
 #define GIT_NAMESPACE_ENVIRONMENT "GIT_NAMESPACE"
 #define GIT_WORK_TREE_ENVIRONMENT "GIT_WORK_TREE"
 #define GIT_PREFIX_ENVIRONMENT "GIT_PREFIX"
+#define GIT_ETC_GITCONFIG_ENVIRONMENT "GIT_ETC_GITCONFIG"
 #define DEFAULT_GIT_DIR_ENVIRONMENT ".git"
 #define DB_ENVIRONMENT "GIT_OBJECT_DIRECTORY"
 #define INDEX_ENVIRONMENT "GIT_INDEX_FILE"
diff --git a/config.c b/config.c
index 0dfed68..124699b 100644
--- a/config.c
+++ b/config.c
@@ -1253,6 +1253,8 @@ const char *git_etc_gitconfig(void)
 {
 	static const char *system_wide;
 	if (!system_wide)
+		system_wide = getenv(GIT_ETC_GITCONFIG_ENVIRONMENT);
+	if (!system_wide)
 		system_wide = system_path(ETC_GITCONFIG);
 	return system_wide;
 }
diff --git a/t/gitconfig-for-test b/t/gitconfig-for-test
new file mode 100644
index 0000000..4598885
--- /dev/null
+++ b/t/gitconfig-for-test
@@ -0,0 +1,6 @@
+;; This file is used as if it were /etc/gitconfig while running the
+;; test scripts in this directory.
+;;
+;; [user]
+;;	name = A U Thor
+;;	email = [email-masked]
diff --git a/t/t1300-repo-config.sh b/t/t1300-repo-config.sh
index 923bfc5..1184f43 100755
--- a/t/t1300-repo-config.sh
+++ b/t/t1300-repo-config.sh
@@ -1372,4 +1372,19 @@ test_expect_success !MINGW '--show-origin blob ref' '
 	test_cmp expect output
 '
 
+test_expect_success 'system-wide configuration' '
+	system="$TRASH_DIRECTORY/system-wide" &&
+	>"$system" &&
+	git config -f "$system" --add frotz.nitfol xyzzy &&
+
+	git config -f "$system" frotz.nitfol >expect &&
+	GIT_ETC_GITCONFIG="$system" \
+	git config --system frotz.nitfol >actual &&
+
+	GIT_ETC_GITCONFIG="$system" \
+	git config --system --replace-all frotz.nitfol blorb &&
+	echo blorb >expect &&
+	GIT_ETC_GITCONFIG="$system" git config --system frotz.nitfol >actual
+'
+
 test_done
diff --git a/t/test-lib.sh b/t/test-lib.sh
index ac56512..6803212 100644
--- a/t/test-lib.sh
+++ b/t/test-lib.sh
@@ -851,9 +851,9 @@ else # normal case, use ../bin-wrappers only unless $with_dashes:
 	fi
 fi
 GIT_TEMPLATE_DIR="$GIT_BUILD_DIR"/templates/blt
-GIT_CONFIG_NOSYSTEM=1
+GIT_ETC_GITCONFIG="$GIT_BUILD_DIR/t/gitconfig-for-test"
 GIT_ATTR_NOSYSTEM=1
-export PATH GIT_EXEC_PATH GIT_TEMPLATE_DIR GIT_CONFIG_NOSYSTEM GIT_ATTR_NOSYSTEM
+export PATH GIT_EXEC_PATH GIT_TEMPLATE_DIR GIT_ETC_GITCONFIG GIT_ATTR_NOSYSTEM
 
 if test -z "$GIT_TEST_CMP"
 then
-- 
2.10.0-584-gc9e068c