Subject: Re: [BUG] git-init does not respect existing separate-git-dir

From: Junio C Hamano <[email-masked]>
Date: Thu, 29 Aug 2013 10:12:44 -0700

Duy Nguyen <[email-masked]> writes:

> On Thu, Aug 29, 2013 at 01:39:02PM +0100, Ximin Luo wrote:
>> It should not be necessary to re-specify --separate-git-dir when re-initialising a git repo.
>> 
>> $ git init --separate-git-dir ../repo
>> Initialized empty Git repository in /home/infinity0/tmp/repo/
>> 
>> $ git init
>> /home/infinity0/tmp/wtree/.git/refs: Not a directory
>> 1
>
> This patch seems to work. Lightly tested.
>
> -- 8< --
> diff --git a/builtin/init-db.c b/builtin/init-db.c
> index 78aa387..d0e5b2e 100644
> --- a/builtin/init-db.c
> +++ b/builtin/init-db.c
> @@ -192,6 +192,15 @@ static int create_default_files(const char *template_path)
>  		die(_("insane git directory %s"), git_dir);
>  	memcpy(path, git_dir, len);
>  
> +	if (!lstat(path, &st1) && S_ISREG(st1.st_mode)) {
> +		git_dir = read_gitfile(git_dir);
> +		len = strlen(git_dir);
> +		if (len > sizeof(path)-50)
> +			die(_("insane git directory %s"), git_dir);
> +		set_git_dir(git_dir);

This repetition from the pre-context of the patch makes me wonder if
it may be a better solution to make sure we have already resolved
the gitfile way before we get here, so that get_git_dir() gives the
real location.

The codepaths in init and clone are both special in that they may be
dealing with a new repository and because of that, they may need to
call set_git_dir() themselves, but in the codeflow for normal (read:
those who deal with an existing repositories) programs, discovery of
the real ".git" directory is done by environment.c::get_git_dir(),
which to calls setup_git_env(), which in turn read_gitfile()'s on
".git" when using the default ".git", like so:

    static void setup_git_env(void)
    {
            git_dir = getenv(GIT_DIR_ENVIRONMENT);
            git_dir = git_dir ? xstrdup(git_dir) : NULL;
            if (!git_dir) {
                    git_dir = read_gitfile(DEFAULT_GIT_DIR_ENVIRONMENT);
                    git_dir = git_dir ? xstrdup(git_dir) : NULL;
            }
            if (!git_dir)
                    git_dir = DEFAULT_GIT_DIR_ENVIRONMENT;

And after this sequence, git_object_dir and git_index_file are
computed off of git_dir, unless they are specified to use an
alternate location via environment variables.

I wonder if the last "use DEFAULT_GIT_DIR_ENVIRONMENT" (which is
".git") should also do the read_gitfile() thing, perhaps like this
(totally untested):

 environment.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)

diff --git a/environment.c b/environment.c
index 5398c36..944e10e 100644
--- a/environment.c
+++ b/environment.c
@@ -123,14 +123,16 @@ static char *expand_namespace(const char *raw_namespace)
 
 static void setup_git_env(void)
 {
+	const char *gitfile;
+
 	git_dir = getenv(GIT_DIR_ENVIRONMENT);
-	git_dir = git_dir ? xstrdup(git_dir) : NULL;
-	if (!git_dir) {
-		git_dir = read_gitfile(DEFAULT_GIT_DIR_ENVIRONMENT);
-		git_dir = git_dir ? xstrdup(git_dir) : NULL;
-	}
 	if (!git_dir)
 		git_dir = DEFAULT_GIT_DIR_ENVIRONMENT;
+	gitfile = read_gitfile(git_dir);
+	if (!gitfile)
+		git_dir = xstrdup(git_dir);
+	else
+		git_dir = gitfile;
 	git_object_dir = getenv(DB_ENVIRONMENT);
 	if (!git_object_dir) {
 		git_object_dir = xmalloc(strlen(git_dir) + 9);