Skip to content

Commit 0a2c7ee

Browse files
deskinspearce
authored andcommitted
git init: --bare/--shared overrides system/global config
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. core.bare is ignored in all situations, while core.sharedRepository will only be used if --shared is not specified to git init. Also includes testcases which use a specified global config file override, demonstrating the former failure scenario. Signed-off-by: Deskin Miller <[email protected]> Signed-off-by: Shawn O. Pearce <[email protected]>
1 parent bf07cc5 commit 0a2c7ee

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

builtin-init-db.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
#define TEST_FILEMODE 1
1818
#endif
1919

20+
static int init_is_bare_repository = 0;
21+
static int init_shared_repository = -1;
22+
2023
static void safe_create_dir(const char *dir, int share)
2124
{
2225
if (mkdir(dir, 0777) < 0) {
@@ -191,6 +194,9 @@ static int create_default_files(const char *template_path)
191194
copy_templates(template_path);
192195

193196
git_config(git_default_config, NULL);
197+
is_bare_repository_cfg = init_is_bare_repository;
198+
if (init_shared_repository != -1)
199+
shared_repository = init_shared_repository;
194200

195201
/*
196202
* We would have created the above under user's umask -- under
@@ -277,6 +283,8 @@ int init_db(const char *template_dir, unsigned int flags)
277283

278284
safe_create_dir(get_git_dir(), 0);
279285

286+
init_is_bare_repository = is_bare_repository();
287+
280288
/* Check to see if the repository version is right.
281289
* Note that a newly created repository does not have
282290
* config file, so this will not fail. What we are catching
@@ -381,9 +389,9 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)
381389
setenv(GIT_DIR_ENVIRONMENT, getcwd(git_dir,
382390
sizeof(git_dir)), 0);
383391
} else if (!strcmp(arg, "--shared"))
384-
shared_repository = PERM_GROUP;
392+
init_shared_repository = PERM_GROUP;
385393
else if (!prefixcmp(arg, "--shared="))
386-
shared_repository = git_config_perm("arg", arg+9);
394+
init_shared_repository = git_config_perm("arg", arg+9);
387395
else if (!strcmp(arg, "-q") || !strcmp(arg, "--quiet"))
388396
flags |= INIT_DB_QUIET;
389397
else

t/t0001-init.sh

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,4 +167,36 @@ test_expect_success 'init with --template (blank)' '
167167
! test -f template-blank/.git/info/exclude
168168
'
169169

170+
test_expect_success 'init --bare/--shared overrides system/global config' '
171+
(
172+
HOME="`pwd`" &&
173+
export HOME &&
174+
test_config="$HOME"/.gitconfig &&
175+
unset GIT_CONFIG_NOGLOBAL &&
176+
git config -f "$test_config" core.bare false &&
177+
git config -f "$test_config" core.sharedRepository 0640 &&
178+
mkdir init-bare-shared-override &&
179+
cd init-bare-shared-override &&
180+
git init --bare --shared=0666
181+
) &&
182+
check_config init-bare-shared-override true unset &&
183+
test x0666 = \
184+
x`git config -f init-bare-shared-override/config core.sharedRepository`
185+
'
186+
187+
test_expect_success 'init honors global core.sharedRepository' '
188+
(
189+
HOME="`pwd`" &&
190+
export HOME &&
191+
test_config="$HOME"/.gitconfig &&
192+
unset GIT_CONFIG_NOGLOBAL &&
193+
git config -f "$test_config" core.sharedRepository 0666 &&
194+
mkdir shared-honor-global &&
195+
cd shared-honor-global &&
196+
git init
197+
) &&
198+
test x0666 = \
199+
x`git config -f shared-honor-global/.git/config core.sharedRepository`
200+
'
201+
170202
test_done

0 commit comments

Comments
 (0)