git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 1/3] init: correct re-initialization from a linked worktree

From
Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Date
Sep 8, 2016, 13:47 UTC
Message-ID
<20160908134719.27955-2-pclouds@gmail.com>
In-Reply-To
<20160908134719.27955-1-pclouds@gmail.com>

When 'git init' is called from a linked worktree, '.git' dir 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>
---
 builtin/init-db.c |  2 +-
 t/t0001-init.sh   | 15 +++++++++++++++
 2 files changed, 16 insertions(+), 1 deletion(-)
diff --git a/builtin/init-db.c b/builtin/init-db.c
index 3a45f0b..6d9552e 100644
--- a/builtin/init-db.c
+++ b/builtin/init-db.c
@@ -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:
diff --git a/t/t0001-init.sh b/t/t0001-init.sh
index a6fdd5e..d64e5e3 100755
--- a/t/t0001-init.sh
+++ b/t/t0001-init.sh
@@ -384,4 +384,19 @@ test_expect_success MINGW 'bare git dir not hidden' '
 	! is_hidden newdir
 '
 
+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
-- 
2.8.2.524.g6ff3d78
Previous: Nguyễn Thái Ngọc DuyNext: Junio C Hamano
Message 5 of 34 in “Bug with git worktrees and git init”
  1. Max NordlundAug 23, 2016
  2. Michael J GruberAug 23, 2016
  3. Duy NguyenAug 24, 2016
  4. 0/3 Fix git-init in linked worktreesNguyễn Thái Ngọc Duy, Sep 8, 2016
  5. 1/3 init: correct re-initialization from a linked worktreeNguyễn Thái Ngọc Duy, Sep 8, 2016
  6. Junio C HamanoSep 8, 2016
  7. Duy NguyenSep 9, 2016
  8. 2/3 t0001: work around the bug that reads config file before repo setupNguyễn Thái Ngọc Duy, Sep 8, 2016
  9. Junio C HamanoSep 8, 2016
  10. Jeff KingSep 8, 2016
  11. Duy NguyenSep 9, 2016
  12. Jeff KingSep 9, 2016
  13. Jacob KellerSep 9, 2016
  14. 3/3 init: do not set core.worktree more often than necessaryNguyễn Thái Ngọc Duy, Sep 8, 2016
  15. Junio C HamanoSep 8, 2016
  16. Duy NguyenSep 9, 2016
  17. 0/3 Fix git-init in linked worktreesNguyễn Thái Ngọc Duy, Sep 21, 2016
  18. 1/3 init: correct re-initialization from a linked worktreeNguyễn Thái Ngọc Duy, Sep 21, 2016
  19. 2/3 init: do not set core.worktree more often than necessaryNguyễn Thái Ngọc Duy, Sep 21, 2016
  20. Junio C HamanoSep 21, 2016
  21. Duy NguyenSep 22, 2016
  22. Junio C HamanoSep 22, 2016
  23. 4/3 init: combine set_git_dir_init() and init_db() into oneNguyễn Thái Ngọc Duy, Sep 23, 2016
  24. Junio C HamanoSep 23, 2016
  25. Junio C HamanoSep 23, 2016
  26. Junio C HamanoSep 24, 2016
  27. Duy NguyenSep 25, 2016
  28. 1/5 init: correct re-initialization from a linked worktreeNguyễn Thái Ngọc Duy, Sep 25, 2016
  29. 2/5 init: call set_git_dir_init() from within init_db()Nguyễn Thái Ngọc Duy, Sep 25, 2016
  30. 3/5 init: kill set_git_dir_init()Nguyễn Thái Ngọc Duy, Sep 25, 2016
  31. 4/5 init: do not set unnecessary core.worktreeNguyễn Thái Ngọc Duy, Sep 25, 2016
  32. 5/5 init: kill git_link variableNguyễn Thái Ngọc Duy, Sep 25, 2016
  33. 3/3 init: reuse original_git_dir in set_git_dir_init()Nguyễn Thái Ngọc Duy, Sep 21, 2016
  34. Junio C HamanoSep 21, 2016

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.