init: do not set unnecessary core.worktree
authorNguyễn Thái Ngọc Duy <pclouds@gmail.com>
Sun, 25 Sep 2016 03:14:39 +0000 (10:14 +0700)
committerJunio C Hamano <gitster@pobox.com>
Sun, 25 Sep 2016 23:32:35 +0000 (16:32 -0700)
The function needs_work_tree_config() that is called from
create_default_files() is supposed to be fed the path to ".git" that
looks as if it is at the top of the working tree, and decide if that
location matches the actual worktree being used. This comparison allows
"git init" to decide if core.worktree needs to be recorded in the
working tree.

In the current code, however, we feed the return value from
get_git_dir(), which can be totally different from what the function
expects when "gitdir" file is involved. Instead of giving the path to
the ".git" at the top of the working tree, we end up feeding the actual
path that the file points at.

This original location of ".git" however is only known to init_db().
Make init_db() save it and have it passed to create_default_files() as a
new parameter, which passes the correct location down to
needs_work_tree_config() to fix this.

Noticed-by: Max Nordlund <max.nordlund@sqore.com>
Helped-by: Michael J Gruber <git@drmicha.warpmail.net>
Helped-by: Junio C Hamano <gitster@pobox.com>
Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/init-db.c
t/t0001-init.sh
index 68c1ae330cda61ac188973aa926105cb1b9ee8cb..8069cd2e6263762cf947e70964c936b0f2c586e8 100644 (file)
@@ -171,7 +171,8 @@ static int needs_work_tree_config(const char *git_dir, const char *work_tree)
        return 1;
 }
 
        return 1;
 }
 
-static int create_default_files(const char *template_path)
+static int create_default_files(const char *template_path,
+                               const char *original_git_dir)
 {
        struct stat st1;
        struct strbuf buf = STRBUF_INIT;
 {
        struct stat st1;
        struct strbuf buf = STRBUF_INIT;
@@ -263,7 +264,7 @@ static int create_default_files(const char *template_path)
                /* allow template config file to override the default */
                if (log_all_ref_updates == -1)
                        git_config_set("core.logallrefupdates", "true");
                /* allow template config file to override the default */
                if (log_all_ref_updates == -1)
                        git_config_set("core.logallrefupdates", "true");
-               if (needs_work_tree_config(get_git_dir(), work_tree))
+               if (needs_work_tree_config(original_git_dir, work_tree))
                        git_config_set("core.worktree", work_tree);
        }
 
                        git_config_set("core.worktree", work_tree);
        }
 
@@ -337,6 +338,7 @@ int init_db(const char *git_dir, const char *real_git_dir,
 {
        int reinit;
        int exist_ok = flags & INIT_DB_EXIST_OK;
 {
        int reinit;
        int exist_ok = flags & INIT_DB_EXIST_OK;
+       char *original_git_dir = xstrdup(real_path(git_dir));
 
        if (real_git_dir) {
                struct stat st;
 
        if (real_git_dir) {
                struct stat st;
@@ -375,7 +377,7 @@ int init_db(const char *git_dir, const char *real_git_dir,
         */
        check_repository_format();
 
         */
        check_repository_format();
 
-       reinit = create_default_files(template_dir);
+       reinit = create_default_files(template_dir, original_git_dir);
 
        create_object_directory();
 
 
        create_object_directory();
 
@@ -412,6 +414,7 @@ int init_db(const char *git_dir, const char *real_git_dir,
                       git_dir, len && git_dir[len-1] != '/' ? "/" : "");
        }
 
                       git_dir, len && git_dir[len-1] != '/' ? "/" : "");
        }
 
+       free(original_git_dir);
        return 0;
 }
 
        return 0;
 }
 
index 488564b311e9eca45a96f1d5fd5a59b5a5666f4b..b8fc588b1922760ade8502fda3dc465f6ebf2887 100755 (executable)
@@ -400,9 +400,11 @@ test_expect_success 're-init from a linked worktree' '
                test_commit first &&
                git worktree add ../linked-worktree &&
                mv .git/info/exclude expected-exclude &&
                test_commit first &&
                git worktree add ../linked-worktree &&
                mv .git/info/exclude expected-exclude &&
+               cp .git/config expected-config &&
                find .git/worktrees -print | sort >expected &&
                git -C ../linked-worktree init &&
                test_cmp expected-exclude .git/info/exclude &&
                find .git/worktrees -print | sort >expected &&
                git -C ../linked-worktree init &&
                test_cmp expected-exclude .git/info/exclude &&
+               test_cmp expected-config .git/config &&
                find .git/worktrees -print | sort >actual &&
                test_cmp expected actual
        )
                find .git/worktrees -print | sort >actual &&
                test_cmp expected actual
        )