init: correct re-initialization from a linked worktree
authorNguyễn Thái Ngọc Duy <pclouds@gmail.com>
Sun, 25 Sep 2016 03:14:36 +0000 (10:14 +0700)
committerJunio C Hamano <gitster@pobox.com>
Sun, 25 Sep 2016 23:32:35 +0000 (16:32 -0700)
When 'git init' is called from a linked worktree, we treat '.git'
dir (which is $GIT_COMMON_DIR/worktrees/something) as the main
'.git' (i.e. $GIT_COMMON_DIR) and populate the whole repository skeleton
in there. It does not harm anything (*) but it is still wrong.

Since 'git init' calls set_git_dir() at preparation time, which
indirectly calls get_common_dir() and correctly detects multiple
worktree setup, all git_path_buf() calls in create_default_files() will
return correct paths in both single and multiple worktree setups. The
only thing left is copy_templates(), which targets $GIT_DIR, not
$GIT_COMMON_DIR.

Fix that with get_git_common_dir(). This function will return $GIT_DIR
in single-worktree setup, so we don't have to make a special case for
multiple-worktree here.

(*) It does in fact, thanks to another bug. More on that later.

Noticed-by: Max Nordlund <max.nordlund@sqore.com>
Helped-by: Michael J Gruber <git@drmicha.warpmail.net>
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 cc09fca81bb61dd6539c4a9e596699b7275b9a08..d5d7558c8a615815056e1a450535cea0243d84a2 100644 (file)
@@ -138,7 +138,7 @@ static void copy_templates(const char *template_dir)
                goto close_free_return;
        }
 
-       strbuf_addstr(&path, get_git_dir());
+       strbuf_addstr(&path, get_git_common_dir());
        strbuf_complete(&path, '/');
        copy_templates_1(&path, &template_path, dir);
 close_free_return:
index 8ffbbea4d65dbd8328563da53ef2959dd39ce796..488564b311e9eca45a96f1d5fd5a59b5a5666f4b 100755 (executable)
@@ -393,4 +393,19 @@ test_expect_success 'remote init from does not use config from cwd' '
        test_cmp expect actual
 '
 
+test_expect_success 're-init from a linked worktree' '
+       git init main-worktree &&
+       (
+               cd main-worktree &&
+               test_commit first &&
+               git worktree add ../linked-worktree &&
+               mv .git/info/exclude expected-exclude &&
+               find .git/worktrees -print | sort >expected &&
+               git -C ../linked-worktree init &&
+               test_cmp expected-exclude .git/info/exclude &&
+               find .git/worktrees -print | sort >actual &&
+               test_cmp expected actual
+       )
+'
+
 test_done