Skip to content

Commit

Permalink
git init: --bare/--shared overrides system/global config
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Deskin Miller authored and Shawn O. Pearce committed Oct 8, 2008
1 parent bf07cc5 commit 0a2c7ee
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
12 changes: 10 additions & 2 deletions builtin-init-db.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
#define TEST_FILEMODE 1
#endif

static int init_is_bare_repository = 0;
static int init_shared_repository = -1;

static void safe_create_dir(const char *dir, int share)
{
if (mkdir(dir, 0777) < 0) {
Expand Down Expand Up @@ -191,6 +194,9 @@ 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;
if (init_shared_repository != -1)
shared_repository = init_shared_repository;

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

safe_create_dir(get_git_dir(), 0);

init_is_bare_repository = is_bare_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
Expand Down Expand Up @@ -381,9 +389,9 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)
setenv(GIT_DIR_ENVIRONMENT, getcwd(git_dir,
sizeof(git_dir)), 0);
} else if (!strcmp(arg, "--shared"))
shared_repository = PERM_GROUP;
init_shared_repository = PERM_GROUP;
else if (!prefixcmp(arg, "--shared="))
shared_repository = git_config_perm("arg", arg+9);
init_shared_repository = git_config_perm("arg", arg+9);
else if (!strcmp(arg, "-q") || !strcmp(arg, "--quiet"))
flags |= INIT_DB_QUIET;
else
Expand Down
32 changes: 32 additions & 0 deletions t/t0001-init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -167,4 +167,36 @@ 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 x0666 = \
x`git config -f init-bare-shared-override/config core.sharedRepository`
'

test_expect_success 'init honors global core.sharedRepository' '
(
HOME="`pwd`" &&
export HOME &&
test_config="$HOME"/.gitconfig &&
unset GIT_CONFIG_NOGLOBAL &&
git config -f "$test_config" core.sharedRepository 0666 &&
mkdir shared-honor-global &&
cd shared-honor-global &&
git init
) &&
test x0666 = \
x`git config -f shared-honor-global/.git/config core.sharedRepository`
'

test_done

0 comments on commit 0a2c7ee

Please sign in to comment.