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

[PATCH] git init: --bare/--shared overrides system/global config

From
Deskin Miller <deskinm@umich.edu>
Date
Oct 5, 2008, 19:44 UTC
Message-ID
<20081005194412.GE3052@riemann.deskinm.fdns.net>
>From b6144562983703079a1eba8cdac3506c18d751a3 Mon Sep 17 00:00:00 2001
From: Deskin Miller <deskinm@umich.edu>
Date: Sat, 4 Oct 2008 20:07:44 -0400

If core.bare or core.sharedRepository are set in /etc/gitconfig or ~/.gitconfig, then 'git init' will read the values when constructing a new config file; reading them, however, will override the values specified on the command line. In the case of --bare, this ends up causing a segfault, without the repository being properly initialised; in the case of --shared, the permissions are set according to the existing config settings, not what was specified on the command line.

This fix saves any specified values for --bare and --shared prior to reading existing config settings, and restores them after reading but before writing the new config file.

Also includes a testcase which has a specified global config file override, demonstrating the former failure scenario.

Signed-off-by: Deskin Miller <deskinm@umich.edu>
---
I'm not a great fan of the method I took to save and restore the values
specified to init, but it works.  Also, I think the testcase is nice (but I'm
biased, seeing how I wrote it): in general I'd argue for more testcases which
deal with issues caused by the user's complete installation.
I based this off of maint, because I think it should be applied there, but it
applies cleanly to master if you feel that's better.
 builtin-init-db.c |    8 ++++++++
 t/t0001-init.sh   |   17 +++++++++++++++++
 2 files changed, 25 insertions(+), 0 deletions(-)
diff --git a/builtin-init-db.c b/builtin-init-db.c
index 8140c12..38e282c 100644
--- a/builtin-init-db.c
+++ b/builtin-init-db.c
@@ -17,6 +17,9 @@
 #define TEST_FILEMODE 1
 #endif
 
+static int init_is_bare_repository = 0;
+static int init_shared_repository = PERM_UMASK;
+
 static void safe_create_dir(const char *dir, int share)
 {
 	if (mkdir(dir, 0777) < 0) {
@@ -191,6 +194,8 @@ static int create_default_files(const char *template_path)
 	copy_templates(template_path);
 
 	git_config(git_default_config, NULL);
+	is_bare_repository_cfg = init_is_bare_repository;
+	shared_repository = init_shared_repository;
 
 	/*
 	 * We would have created the above under user's umask -- under
@@ -277,6 +282,9 @@ int init_db(const char *template_dir, unsigned int flags)
 
 	safe_create_dir(get_git_dir(), 0);
 
+	init_is_bare_repository = is_bare_repository();
+	init_shared_repository = shared_repository;
+
 	/* Check to see if the repository version is right.
 	 * Note that a newly created repository does not have
 	 * config file, so this will not fail.  What we are catching
diff --git a/t/t0001-init.sh b/t/t0001-init.sh
index 620da5b..6a6bca0 100755
--- a/t/t0001-init.sh
+++ b/t/t0001-init.sh
@@ -167,4 +167,21 @@ test_expect_success 'init with --template (blank)' '
 	! test -f template-blank/.git/info/exclude
 '
 
+test_expect_success 'init --bare/--shared overrides system/global config' '
+	(
+		HOME="`pwd`" &&
+		export HOME &&
+		test_config="$HOME"/.gitconfig &&
+		unset GIT_CONFIG_NOGLOBAL &&
+		git config -f "$test_config" core.bare false &&
+		git config -f "$test_config" core.sharedRepository 0640 &&
+		mkdir init-bare-shared-override &&
+		cd init-bare-shared-override &&
+		git init --bare --shared=0666
+	) &&
+	check_config init-bare-shared-override true unset &&
+	test 0666 = \
+	`git config -f init-bare-shared-override/config core.sharedRepository`
+'
+
 test_done
-- 
1.6.0.2.307.gc427
Next: Shawn O. Pearce
Message 1 of 3 in “git init: --bare/--shared overrides system/global config”
  1. git init: --bare/--shared overrides system/global configDeskin Miller, Oct 5, 2008
  2. Shawn O. PearceOct 6, 2008
  3. Deskin MillerOct 7, 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.