{"thread":{"id":"34795","subject":"[BUG] git-init does not respect existing separate-git-dir","startedAt":"2013-08-29T12:39:02Z","lastAt":"2013-09-03T18:14:32Z","messageCount":5,"participants":["Ximin Luo","Duy Nguyen","Junio C Hamano","Nguyễn Thái Ngọc Duy"],"isPatch":false,"patchVersion":null,"patchTotal":null},"messages":[{"id":"226150","messageId":"521F40E6.4040102@gmx.com","threadId":"34795","inReplyTo":null,"subject":"[BUG] git-init does not respect existing separate-git-dir","fromName":"Ximin Luo","fromEmail":"infinity0@gmx.com","sentAt":"2013-08-29T12:39:02Z","receivedAt":"2013-08-29T12:39:02Z","isPatch":false,"sender":{"key":"infinity0@gmx.com","avatar":null},"body":"It should not be necessary to re-specify --separate-git-dir when re-initialising a git repo.\n\n$ git init --separate-git-dir ../repo\nInitialized empty Git repository in /home/infinity0/tmp/repo/\n\n$ git init\n/home/infinity0/tmp/wtree/.git/refs: Not a directory\n1\n\nOne big motivation is so \"git init\" can be a good \"fire-and-forget\" invocation that should work anywhere. Currently, one has to do \"git init --separate-git-dir $(git rev-parse --git-dir)\" which is a lot less elegant.\n\n(Please CC me as I am not subscribed.)\n\n-- \nGPG: 4096R/1318EFAC5FBBDBCE\ngit://github.com/infinity0/pubkeys.git\n\n"},{"id":"226151","messageId":"20130829130450.GA9323@lanh","threadId":"34795","inReplyTo":"521F40E6.4040102@gmx.com","subject":"Re: [BUG] git-init does not respect existing separate-git-dir","fromName":"Duy Nguyen","fromEmail":"pclouds@gmail.com","sentAt":"2013-08-29T13:04:50Z","receivedAt":"2013-08-29T13:04:50Z","isPatch":false,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"On Thu, Aug 29, 2013 at 01:39:02PM +0100, Ximin Luo wrote:\n> It should not be necessary to re-specify --separate-git-dir when re-initialising a git repo.\n> \n> $ git init --separate-git-dir ../repo\n> Initialized empty Git repository in /home/infinity0/tmp/repo/\n> \n> $ git init\n> /home/infinity0/tmp/wtree/.git/refs: Not a directory\n> 1\n\nThis patch seems to work. Lightly tested.\n\n-- 8< --\ndiff --git a/builtin/init-db.c b/builtin/init-db.c\nindex 78aa387..d0e5b2e 100644\n--- a/builtin/init-db.c\n+++ b/builtin/init-db.c\n@@ -192,6 +192,15 @@ static int create_default_files(const char *template_path)\n \t\tdie(_(\"insane git directory %s\"), git_dir);\n \tmemcpy(path, git_dir, len);\n \n+\tif (!lstat(path, &st1) && S_ISREG(st1.st_mode)) {\n+\t\tgit_dir = read_gitfile(git_dir);\n+\t\tlen = strlen(git_dir);\n+\t\tif (len > sizeof(path)-50)\n+\t\t\tdie(_(\"insane git directory %s\"), git_dir);\n+\t\tset_git_dir(git_dir);\n+\t\tmemcpy(path, git_dir, len);\n+\t}\n+\n \tif (len && path[len-1] != '/')\n \t\tpath[len++] = '/';\n-- 8< -- \n\n> One big motivation is so \"git init\" can be a good \"fire-and-forget\"\n> invocation that should work anywhere. Currently, one has to do \"git\n> init --separate-git-dir $(git rev-parse --git-dir)\" which is a lot\n> less elegant.\n--\nDuy\n"},{"id":"226187","messageId":"xmqq38ps775f.fsf@gitster.dls.corp.google.com","threadId":"34795","inReplyTo":"20130829130450.GA9323@lanh","subject":"Re: [BUG] git-init does not respect existing separate-git-dir","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2013-08-29T17:12:44Z","receivedAt":"2013-08-29T17:12:44Z","isPatch":false,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Duy Nguyen <pclouds@gmail.com> writes:\n\n> On Thu, Aug 29, 2013 at 01:39:02PM +0100, Ximin Luo wrote:\n>> It should not be necessary to re-specify --separate-git-dir when re-initialising a git repo.\n>> \n>> $ git init --separate-git-dir ../repo\n>> Initialized empty Git repository in /home/infinity0/tmp/repo/\n>> \n>> $ git init\n>> /home/infinity0/tmp/wtree/.git/refs: Not a directory\n>> 1\n>\n> This patch seems to work. Lightly tested.\n>\n> -- 8< --\n> diff --git a/builtin/init-db.c b/builtin/init-db.c\n> index 78aa387..d0e5b2e 100644\n> --- a/builtin/init-db.c\n> +++ b/builtin/init-db.c\n> @@ -192,6 +192,15 @@ static int create_default_files(const char *template_path)\n>  \t\tdie(_(\"insane git directory %s\"), git_dir);\n>  \tmemcpy(path, git_dir, len);\n>  \n> +\tif (!lstat(path, &st1) && S_ISREG(st1.st_mode)) {\n> +\t\tgit_dir = read_gitfile(git_dir);\n> +\t\tlen = strlen(git_dir);\n> +\t\tif (len > sizeof(path)-50)\n> +\t\t\tdie(_(\"insane git directory %s\"), git_dir);\n> +\t\tset_git_dir(git_dir);\n\nThis repetition from the pre-context of the patch makes me wonder if\nit may be a better solution to make sure we have already resolved\nthe gitfile way before we get here, so that get_git_dir() gives the\nreal location.\n\nThe codepaths in init and clone are both special in that they may be\ndealing with a new repository and because of that, they may need to\ncall set_git_dir() themselves, but in the codeflow for normal (read:\nthose who deal with an existing repositories) programs, discovery of\nthe real \".git\" directory is done by environment.c::get_git_dir(),\nwhich to calls setup_git_env(), which in turn read_gitfile()'s on\n\".git\" when using the default \".git\", like so:\n\n    static void setup_git_env(void)\n    {\n            git_dir = getenv(GIT_DIR_ENVIRONMENT);\n            git_dir = git_dir ? xstrdup(git_dir) : NULL;\n            if (!git_dir) {\n                    git_dir = read_gitfile(DEFAULT_GIT_DIR_ENVIRONMENT);\n                    git_dir = git_dir ? xstrdup(git_dir) : NULL;\n            }\n            if (!git_dir)\n                    git_dir = DEFAULT_GIT_DIR_ENVIRONMENT;\n\nAnd after this sequence, git_object_dir and git_index_file are\ncomputed off of git_dir, unless they are specified to use an\nalternate location via environment variables.\n\nI wonder if the last \"use DEFAULT_GIT_DIR_ENVIRONMENT\" (which is\n\".git\") should also do the read_gitfile() thing, perhaps like this\n(totally untested):\n\n environment.c | 12 +++++++-----\n 1 file changed, 7 insertions(+), 5 deletions(-)\n\ndiff --git a/environment.c b/environment.c\nindex 5398c36..944e10e 100644\n--- a/environment.c\n+++ b/environment.c\n@@ -123,14 +123,16 @@ static char *expand_namespace(const char *raw_namespace)\n \n static void setup_git_env(void)\n {\n+\tconst char *gitfile;\n+\n \tgit_dir = getenv(GIT_DIR_ENVIRONMENT);\n-\tgit_dir = git_dir ? xstrdup(git_dir) : NULL;\n-\tif (!git_dir) {\n-\t\tgit_dir = read_gitfile(DEFAULT_GIT_DIR_ENVIRONMENT);\n-\t\tgit_dir = git_dir ? xstrdup(git_dir) : NULL;\n-\t}\n \tif (!git_dir)\n \t\tgit_dir = DEFAULT_GIT_DIR_ENVIRONMENT;\n+\tgitfile = read_gitfile(git_dir);\n+\tif (!gitfile)\n+\t\tgit_dir = xstrdup(git_dir);\n+\telse\n+\t\tgit_dir = gitfile;\n \tgit_object_dir = getenv(DB_ENVIRONMENT);\n \tif (!git_object_dir) {\n \t\tgit_object_dir = xmalloc(strlen(git_dir) + 9);\n"},{"id":"226388","messageId":"1377911054-20086-1-git-send-email-pclouds@gmail.com","threadId":"34795","inReplyTo":"xmqq38ps775f.fsf@gitster.dls.corp.google.com","subject":"[PATCH] Make setup_git_env() resolve .git file when $GIT_DIR is not specified","fromName":"Nguyễn Thái Ngọc Duy","fromEmail":"pclouds@gmail.com","sentAt":"2013-08-31T01:04:14Z","receivedAt":"2013-08-31T01:04:14Z","isPatch":true,"sender":{"key":"pclouds@gmail.com","avatar":"https://avatars.githubusercontent.com/u/720?v=4"},"body":"This makes reinitializing on a .git file repository work.\n\nThis is probably the only case that setup_git_env() (via\nset_git_dir()) is called on a .git file. Other cases in\nsetup_git_dir_gently() and enter_repo() both cover .git file case\nexplicitly because they need to verify the target repo is valid.\n\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\nSigned-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\nReported-by: Ximin Luo <infinity0@gmx.com>\n---\n Slight change in the patch to xstrdup(gitfile) because read_gitfile\n returns a static buffer.\n\n environment.c   | 9 ++++-----\n t/t0001-init.sh | 4 ++++\n 2 files changed, 8 insertions(+), 5 deletions(-)\n\ndiff --git a/environment.c b/environment.c\nindex 5398c36..378254c 100644\n--- a/environment.c\n+++ b/environment.c\n@@ -123,14 +123,13 @@ static char *expand_namespace(const char *raw_namespace)\n \n static void setup_git_env(void)\n {\n+\tconst char *gitfile;\n+\n \tgit_dir = getenv(GIT_DIR_ENVIRONMENT);\n-\tgit_dir = git_dir ? xstrdup(git_dir) : NULL;\n-\tif (!git_dir) {\n-\t\tgit_dir = read_gitfile(DEFAULT_GIT_DIR_ENVIRONMENT);\n-\t\tgit_dir = git_dir ? xstrdup(git_dir) : NULL;\n-\t}\n \tif (!git_dir)\n \t\tgit_dir = DEFAULT_GIT_DIR_ENVIRONMENT;\n+\tgitfile = read_gitfile(git_dir);\n+\tgit_dir = xstrdup(gitfile ? gitfile : git_dir);\n \tgit_object_dir = getenv(DB_ENVIRONMENT);\n \tif (!git_object_dir) {\n \t\tgit_object_dir = xmalloc(strlen(git_dir) + 9);\ndiff --git a/t/t0001-init.sh b/t/t0001-init.sh\nindex ad66410..9fb582b 100755\n--- a/t/t0001-init.sh\n+++ b/t/t0001-init.sh\n@@ -379,6 +379,10 @@ test_expect_success 'init with separate gitdir' '\n \ttest -d realgitdir/refs\n '\n \n+test_expect_success 're-init on .git file' '\n+\t( cd newdir && git init )\n+'\n+\n test_expect_success 're-init to update git link' '\n \t(\n \tcd newdir &&\n-- \n1.8.2.83.gc99314b\n"},{"id":"226650","messageId":"xmqqr4d5srg7.fsf@gitster.dls.corp.google.com","threadId":"34795","inReplyTo":"1377911054-20086-1-git-send-email-pclouds@gmail.com","subject":"Re: [PATCH] Make setup_git_env() resolve .git file when $GIT_DIR is not specified","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2013-09-03T18:14:32Z","receivedAt":"2013-09-03T18:14:32Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Nguyễn Thái Ngọc Duy  <pclouds@gmail.com> writes:\n\n> This makes reinitializing on a .git file repository work.\n>\n> This is probably the only case that setup_git_env() (via\n> set_git_dir()) is called on a .git file. Other cases in\n> setup_git_dir_gently() and enter_repo() both cover .git file case\n> explicitly because they need to verify the target repo is valid.\n>\n> Signed-off-by: Junio C Hamano <gitster@pobox.com>\n> Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>\n> Reported-by: Ximin Luo <infinity0@gmx.com>\n> ---\n>  Slight change in the patch to xstrdup(gitfile) because read_gitfile\n>  returns a static buffer.\n\nThanks for fixing it up.  Will queue.\n\n>\n>  environment.c   | 9 ++++-----\n>  t/t0001-init.sh | 4 ++++\n>  2 files changed, 8 insertions(+), 5 deletions(-)\n>\n> diff --git a/environment.c b/environment.c\n> index 5398c36..378254c 100644\n> --- a/environment.c\n> +++ b/environment.c\n> @@ -123,14 +123,13 @@ static char *expand_namespace(const char *raw_namespace)\n>  \n>  static void setup_git_env(void)\n>  {\n> +\tconst char *gitfile;\n> +\n>  \tgit_dir = getenv(GIT_DIR_ENVIRONMENT);\n> -\tgit_dir = git_dir ? xstrdup(git_dir) : NULL;\n> -\tif (!git_dir) {\n> -\t\tgit_dir = read_gitfile(DEFAULT_GIT_DIR_ENVIRONMENT);\n> -\t\tgit_dir = git_dir ? xstrdup(git_dir) : NULL;\n> -\t}\n>  \tif (!git_dir)\n>  \t\tgit_dir = DEFAULT_GIT_DIR_ENVIRONMENT;\n> +\tgitfile = read_gitfile(git_dir);\n> +\tgit_dir = xstrdup(gitfile ? gitfile : git_dir);\n>  \tgit_object_dir = getenv(DB_ENVIRONMENT);\n>  \tif (!git_object_dir) {\n>  \t\tgit_object_dir = xmalloc(strlen(git_dir) + 9);\n> diff --git a/t/t0001-init.sh b/t/t0001-init.sh\n> index ad66410..9fb582b 100755\n> --- a/t/t0001-init.sh\n> +++ b/t/t0001-init.sh\n> @@ -379,6 +379,10 @@ test_expect_success 'init with separate gitdir' '\n>  \ttest -d realgitdir/refs\n>  '\n>  \n> +test_expect_success 're-init on .git file' '\n> +\t( cd newdir && git init )\n> +'\n> +\n>  test_expect_success 're-init to update git link' '\n>  \t(\n>  \tcd newdir &&\n"}]}