git/list[1] front-page[2] threads[3] people[4] search[5] about
 

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

From
Junio C Hamano <gitster@pobox.com>
Date
Aug 29, 2013, 17:12 UTC
Message-ID
<xmqq38ps775f.fsf@gitster.dls.corp.google.com>
In-Reply-To
<20130829130450.GA9323@lanh>
Duy Nguyen <pclouds@gmail.com> writes:
Show 27 quoted lines
> 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);
Previous: Duy NguyenNext: Nguyễn Thái Ngọc Duy
Message 3 of 5 in “[BUG] git-init does not respect existing separate-git-dir”
  1. Ximin LuoAug 29, 2013
  2. Duy NguyenAug 29, 2013
  3. Junio C HamanoAug 29, 2013
  4. Make setup_git_env() resolve .git file when $GIT_DIR is not specifiedNguyễn Thái Ngọc Duy, Aug 31, 2013
  5. Junio C HamanoSep 3, 2013

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.