public inbox for git@vger.kernel.org 
 help / color / mirror / Atom feed
From: Junio C Hamano <gitster@pobox•com>
To: Duy Nguyen <pclouds@gmail•com>
Cc: Ximin Luo <infinity0@gmx•com>, git@vger•kernel.org
Subject: Re: [BUG] git-init does not respect existing separate-git-dir
Date: Thu, 29 Aug 2013 10:12:44 -0700	[thread overview]
Message-ID: <xmqq38ps775f.fsf@gitster.dls.corp.google.com> (raw)
In-Reply-To: <20130829130450.GA9323@lanh> (Duy Nguyen's message of "Thu, 29 Aug 2013 20:04:50 +0700")

Duy Nguyen <pclouds@gmail•com> 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);

  reply	other threads:[~2013-08-29 17:12 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-08-29 12:39 [BUG] git-init does not respect existing separate-git-dir Ximin Luo
2013-08-29 13:04 ` Duy Nguyen
2013-08-29 17:12   ` Junio C Hamano [this message]
2013-08-31  1:04     ` [PATCH] Make setup_git_env() resolve .git file when $GIT_DIR is not specified Nguyễn Thái Ngọc Duy
2013-09-03 18:14       ` Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=xmqq38ps775f.fsf@gitster.dls.corp.google.com \
    --to=gitster@pobox$(echo .)com \
    --cc=git@vger$(echo .)kernel.org \
    --cc=infinity0@gmx$(echo .)com \
    --cc=pclouds@gmail$(echo .)com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox