@matheusbsbs / matheus-bagliano / commits / 87a074df24
Fork 0

handle "git --bare init <dir>" properly

If we know we are creating a bare repository, we use setenv to set the GIT_DIR directory to the current directory (either where we already were, or one we created and chdir'd into with "git init --bare <dir>"). However, with "git --bare init <dir>" (note the --bare as a git wrapper option), the setup code actually sets GIT_DIR for us, but it uses the wrong, original cwd when a directory is given. Because our setenv does not use the overwrite flag, it is ignored. We need to set the overwrite flag, but only when we are given a directory on the command line. That still allows: GIT_DIR=foo.git git init --bare to work. The behavior is changed for: GIT_DIR=foo.git git init --bare bar.git which used to create the repository in foo.git, but now will use bar.git. This is more sane, as command line options should generally override the environment. Noticed by Oliver Hoffmann. Signed-off-by: Jeff King <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Jeff King committed May 10, 2010 at 05:42 UTC 87a074df2455f305df8facc22e85dac1cfeb0d6b
2 files changed +15 -1
builtin/init-db.c
+1 -1
@@ -463,7 +463,7 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)
463 static char git_dir[PATH_MAX+1];
464
465 setenv(GIT_DIR_ENVIRONMENT,
466 - getcwd(git_dir, sizeof(git_dir)), 0);
466 + getcwd(git_dir, sizeof(git_dir)), argc > 0);
467 }
468
469 if (init_shared_repository != -1)
t/t0001-init.sh
+14
@@ -310,4 +310,18 @@ test_expect_success POSIXPERM 'init notices EPERM' '
310 )
311 '
312
313 +test_expect_success 'init creates a new bare directory with global --bare' '
314 + rm -rf newdir &&
315 + git --bare init newdir &&
316 + test -d newdir/refs
317 +'
318 +
319 +test_expect_success 'init prefers command line to GIT_DIR' '
320 + rm -rf newdir &&
321 + mkdir otherdir &&
322 + GIT_DIR=otherdir git --bare init newdir &&
323 + test -d newdir/refs &&
324 + ! test -d otherdir/refs
325 +'
326 +
327 test_done