Skip to content

Commit fe9aa0b

Browse files
pcloudsgitster
authored andcommitted
init: correct re-initialization from a linked worktree
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 <[email protected]> Helped-by: Michael J Gruber <[email protected]> Signed-off-by: Nguyễn Thái Ngọc Duy <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 4d0efa1 commit fe9aa0b

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

builtin/init-db.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ static void copy_templates(const char *template_dir)
138138
goto close_free_return;
139139
}
140140

141-
strbuf_addstr(&path, get_git_dir());
141+
strbuf_addstr(&path, get_git_common_dir());
142142
strbuf_complete(&path, '/');
143143
copy_templates_1(&path, &template_path, dir);
144144
close_free_return:

t/t0001-init.sh

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,4 +393,19 @@ test_expect_success 'remote init from does not use config from cwd' '
393393
test_cmp expect actual
394394
'
395395

396+
test_expect_success 're-init from a linked worktree' '
397+
git init main-worktree &&
398+
(
399+
cd main-worktree &&
400+
test_commit first &&
401+
git worktree add ../linked-worktree &&
402+
mv .git/info/exclude expected-exclude &&
403+
find .git/worktrees -print | sort >expected &&
404+
git -C ../linked-worktree init &&
405+
test_cmp expected-exclude .git/info/exclude &&
406+
find .git/worktrees -print | sort >actual &&
407+
test_cmp expected actual
408+
)
409+
'
410+
396411
test_done

0 commit comments

Comments
 (0)