git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* git init --bare versus git --bare init
@ 2010-05-08 10:38 Oliver Hoffmann
  2010-05-10  9:42 ` [PATCH] handle "git --bare init <dir>" properly Jeff King
  0 siblings, 1 reply; 2+ messages in thread
From: Oliver Hoffmann @ 2010-05-08 10:38 UTC (permalink / raw)
  To: git

Hi Git Folk,

If one accidentally writes "git --bare init <dir>" instead of "git init --bare 
<dir>" an appended directory argument is ignored and $CUR_DIR or the working 
directory will be used:

> pwd
/tmp/foo
> git --bare init /tmp/bar
Initialized empty Git repository in /tmp/foo/
> git init --bare /tmp/bar
Initialized empty Git repository in /tmp/bar/

... but both should probably be initialized in /tmp/bar/

Greetings,
Oliver Hoffmann

^ permalink raw reply	[flat|nested] 2+ messages in thread

* [PATCH] handle "git --bare init <dir>" properly
  2010-05-08 10:38 git init --bare versus git --bare init Oliver Hoffmann
@ 2010-05-10  9:42 ` Jeff King
  0 siblings, 0 replies; 2+ messages in thread
From: Jeff King @ 2010-05-10  9:42 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: Oliver Hoffmann, git

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>
---
Thanks for the bug report, Oliver.

 builtin/init-db.c |    2 +-
 t/t0001-init.sh   |   14 ++++++++++++++
 2 files changed, 15 insertions(+), 1 deletions(-)

diff --git a/builtin/init-db.c b/builtin/init-db.c
index edc40ff..0271285 100644
--- a/builtin/init-db.c
+++ b/builtin/init-db.c
@@ -463,7 +463,7 @@ int cmd_init_db(int argc, const char **argv, const char *prefix)
 		static char git_dir[PATH_MAX+1];
 
 		setenv(GIT_DIR_ENVIRONMENT,
-			getcwd(git_dir, sizeof(git_dir)), 0);
+			getcwd(git_dir, sizeof(git_dir)), argc > 0);
 	}
 
 	if (init_shared_repository != -1)
diff --git a/t/t0001-init.sh b/t/t0001-init.sh
index 6757734..7c0a698 100755
--- a/t/t0001-init.sh
+++ b/t/t0001-init.sh
@@ -310,4 +310,18 @@ test_expect_success POSIXPERM 'init notices EPERM' '
 	)
 '
 
+test_expect_success 'init creates a new bare directory with global --bare' '
+	rm -rf newdir &&
+	git --bare init newdir &&
+	test -d newdir/refs
+'
+
+test_expect_success 'init prefers command line to GIT_DIR' '
+	rm -rf newdir &&
+	mkdir otherdir &&
+	GIT_DIR=otherdir git --bare init newdir &&
+	test -d newdir/refs &&
+	! test -d otherdir/refs
+'
+
 test_done
-- 
1.7.1.248.gf52fc.dirty

^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2010-05-10  9:42 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-05-08 10:38 git init --bare versus git --bare init Oliver Hoffmann
2010-05-10  9:42 ` [PATCH] handle "git --bare init <dir>" properly Jeff King

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).