git-init does not respect existing separate-git-dir
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
One 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.
(Please CC me as I am not subscribed.)
Signature · 2 lines
GPG: 4096R/1318EFAC5FBBDBCE git://github.com/infinity0/pubkeys.git
Quoted 8 lines · Ximin Luo
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< --
builtin/init-db.c +9 -1
@@ -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); +memcpy (path, git_dir, len); + } +if (len && path[len-1 ] !='/' ) path[len++] ='/' ; -- 8< --
Quoted 4 lines
One 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.
--
Duy
Quoted 18 lines · Duy Nguyen
Quoted 8 lines · Ximin Luo
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 1This patch seems to work. Lightly tested.
-- 8< --
builtin/init-db.c +0
-0
builtin/init-db.c static int create_default_files(const char *template_path) · line 200
Quoted 9 lines
builtin/init-db.c +6
-0@@ -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(-)
environment.c +7 -5
@@ -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 );
PATCH 1/1 Make setup_git_env() resolve .git file when $GIT_DIR is not specified
This makes reinitializing on a .git file repository work.
This is probably the only case that setup_git_env() (via set_git_dir()) is called on a .git file. Other cases in setup_git_dir_gently() and enter_repo() both cover .git file case explicitly because they need to verify the target repo is valid.
---
Slight change in the patch to xstrdup(gitfile) because read_gitfile returns a static buffer.
environment.c | 9 ++++----- t/t0001-init.sh | 4 ++++ 2 files changed, 8 insertions(+), 5 deletions(-)
environment.c +4 -5
@@ -123,14 +123,13 @@ 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); + git_dir =xstrdup (gitfile ? gitfile : git_dir); git_object_dir =getenv (DB_ENVIRONMENT );if (!git_object_dir) { git_object_dir =xmalloc (strlen (git_dir) +9 );
t/t0001-init.sh +4 -0
@@ -379,6 +379,10 @@ test_expect_success 'init with separate gitdir' ' test -d realgitdir/refs ' +test_expect_success 're-init on .git file' ' + ( cd newdir && git init ) +' + test_expect_success 're-init to update git link' ' ( cd newdir &&
Signature · 1 line
1.8.2.83.gc99314b
Re: [PATCH] Make setup_git_env() resolve .git file when $GIT_DIR is not specified
Quoted 7 lines · Nguyễn Thái Ngọc Duy
This makes reinitializing on a .git file repository work.
This is probably the only case that
setup_git_env()(viaset_git_dir()) is called on a .git file. Other cases insetup_git_dir_gently()andenter_repo()both cover .git file case explicitly because they need to verify the target repo is valid.
Quoted 6 lines
Signed-off-by Junio C Hamano <[email protected]>Signed-off-by Nguyễn Thái Ngọc Duy <[email protected]>Reported-by Ximin Luo <[email protected]>---
Slight change in the patch to xstrdup(gitfile) becauseread_gitfilereturns a static buffer.
Thanks for fixing it up. Will queue.
Quoted 43 lines
environment.c | 9 ++++----- t/t0001-init.sh | 4 ++++ 2 files changed, 8 insertions(+), 5 deletions(-)environment.c +4
-5@@ -123,14 +123,13 @@ 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); + git_dir =xstrdup (gitfile ? gitfile : git_dir); git_object_dir =getenv (DB_ENVIRONMENT );if (!git_object_dir) { git_object_dir =xmalloc (strlen (git_dir) +9 );t/t0001-init.sh +4
-0@@ -379,6 +379,10 @@ test_expect_success 'init with separate gitdir' ' test -d realgitdir/refs ' +test_expect_success 're-init on .git file' ' + ( cd newdir && git init ) +' + test_expect_success 're-init to update git link' ' ( cd newdir &&