@matheusbsbs / matheus-bagliano / commits / 0a2c7eea41
Fork 0

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 <deskinm@umich.edu> Signed-off-by: Shawn O. Pearce <spearce@spearce.org>

Deskin Miller committed Oct 7, 2008 at 01:37 UTC 0a2c7eea41867ead2b2fb1d5898494c3a72405e5
2 files changed +42 -2
builtin-init-db.c
+10 -2
@@ -17,6 +17,9 @@
17 #define TEST_FILEMODE 1
18 #endif
19
20 +static int init_is_bare_repository = 0;
21 +static int init_shared_repository = -1;
22 +
23 static void safe_create_dir(const char *dir, int share)
24 {
25 if (mkdir(dir, 0777) < 0) {
@@ -191,6 +194,9 @@ static int create_default_files(const char *template_path)
194 copy_templates(template_path);
195
196 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;
200
201 /*
202 * 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)
283
284 safe_create_dir(get_git_dir(), 0);
285
286 + init_is_bare_repository = is_bare_repository();
287 +
288 /* Check to see if the repository version is right.
289 * Note that a newly created repository does not have
290 * 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)
389 setenv(GIT_DIR_ENVIRONMENT, getcwd(git_dir,
390 sizeof(git_dir)), 0);
391 } else if (!strcmp(arg, "--shared"))
384 - shared_repository = PERM_GROUP;
392 + init_shared_repository = PERM_GROUP;
393 else if (!prefixcmp(arg, "--shared="))
386 - shared_repository = git_config_perm("arg", arg+9);
394 + init_shared_repository = git_config_perm("arg", arg+9);
395 else if (!strcmp(arg, "-q") || !strcmp(arg, "--quiet"))
396 flags |= INIT_DB_QUIET;
397 else
t/t0001-init.sh
+32
@@ -167,4 +167,36 @@ test_expect_success 'init with --template (blank)' '
167 ! test -f template-blank/.git/info/exclude
168 '
169
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 +
202 test_done