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

[PATCH v2 2/7] builtin/init: simplify logic to configure worktree

From
Patrick Steinhardt <ps@pks.im>
Date
Jun 11, 2026, 06:44 UTC
Message-ID
<20260611-b4-pks-setup-drop-global-state-v2-2-a6f7269c841d@pks.im>
In-Reply-To
<20260611-b4-pks-setup-drop-global-state-v2-0-a6f7269c841d@pks.im>

In the preceding commit we have stopped modifying the global `git_work_tree_cfg` variable. With this change there's now some code paths where we end up setting the local `git_work_tree_cfg` variable, but without actually using the value for anything.

Refactor the code a bit so that we only set the worktree configuration in case it's actually needed. Furthermore, reflow it a bit to make the code easier to follow.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
---
 builtin/init-db.c | 31 ++++++++++++++++++-------------
 1 file changed, 18 insertions(+), 13 deletions(-)
diff --git a/builtin/init-db.c b/builtin/init-db.c
index 01bc27904e..b4343c2804 100644
--- a/builtin/init-db.c
+++ b/builtin/init-db.c
@@ -229,24 +229,29 @@ int cmd_init_db(int argc,
 
 	if (!is_bare_repository_cfg) {
 		const char *git_dir_parent = strrchr(git_dir, '/');
-		char *git_work_tree_cfg = NULL;
 
-		if (git_dir_parent) {
-			char *rel = xstrndup(git_dir, git_dir_parent - git_dir);
-			git_work_tree_cfg = real_pathdup(rel, 1);
-			free(rel);
-		}
-		if (!git_work_tree_cfg)
-			git_work_tree_cfg = xgetcwd();
-		if (work_tree)
+		if (work_tree) {
 			set_git_work_tree(the_repository, work_tree);
-		else
-			set_git_work_tree(the_repository, git_work_tree_cfg);
+		} else {
+			char *work_tree_cfg = NULL;
+
+			if (git_dir_parent) {
+				char *rel = xstrndup(git_dir, git_dir_parent - git_dir);
+				work_tree_cfg = real_pathdup(rel, 1);
+				free(rel);
+			}
+
+			if (!work_tree_cfg)
+				work_tree_cfg = xgetcwd();
+
+			set_git_work_tree(the_repository, work_tree_cfg);
+
+			free(work_tree_cfg);
+		}
+
 		if (access(repo_get_work_tree(the_repository), X_OK))
 			die_errno (_("Cannot access work tree '%s'"),
 				   repo_get_work_tree(the_repository));
-
-		free(git_work_tree_cfg);
 	}
 	else {
 		if (real_git_dir)
-- 
2.54.0.1189.g8c84645362.dirty
Previous: Patrick SteinhardtNext: Patrick Steinhardt
Message 22 of 33 in “setup: drop global state”
  1. 0/7 setup: drop global statePatrick Steinhardt, Jun 10, 2026
  2. 1/7 builtin/init: stop modifying global `git_work_tree_cfg` variablePatrick Steinhardt, Jun 10, 2026
  3. Justin ToblerJun 10, 2026
  4. 2/7 builtin/init: simplify logic to configure worktreePatrick Steinhardt, Jun 10, 2026
  5. Justin ToblerJun 10, 2026
  6. 3/7 setup: remove global `git_work_tree_cfg` variablePatrick Steinhardt, Jun 10, 2026
  7. Justin ToblerJun 10, 2026
  8. Patrick SteinhardtJun 11, 2026
  9. 4/7 builtin/init: stop modifying `is_bare_repository_cfg`Patrick Steinhardt, Jun 10, 2026
  10. 5/7 environment: split up concerns of `is_bare_repository_cfg`Patrick Steinhardt, Jun 10, 2026
  11. Justin ToblerJun 10, 2026
  12. Patrick SteinhardtJun 11, 2026
  13. Justin ToblerJun 11, 2026
  14. 6/7 environment: stop using `the_repository` in `is_bare_repository()`Patrick Steinhardt, Jun 10, 2026
  15. 7/7 treewide: drop USE_THE_REPOSITORY_VARIABLEPatrick Steinhardt, Jun 10, 2026
  16. Justin ToblerJun 10, 2026
  17. Toon ClaesJun 12, 2026
  18. 0/7 setup: drop global statePatrick Steinhardt, Jun 11, 2026
  19. 1/7 builtin/init: stop modifying global `git_work_tree_cfg` variablePatrick Steinhardt, Jun 11, 2026
  20. Toon ClaesJun 12, 2026
  21. Patrick SteinhardtJun 12, 2026
  22. 2/7 builtin/init: simplify logic to configure worktreePatrick Steinhardt, Jun 11, 2026
  23. 3/7 setup: remove global `git_work_tree_cfg` variablePatrick Steinhardt, Jun 11, 2026
  24. 4/7 builtin/init: stop modifying `is_bare_repository_cfg`Patrick Steinhardt, Jun 11, 2026
  25. 5/7 environment: split up concerns of `is_bare_repository_cfg`Patrick Steinhardt, Jun 11, 2026
  26. Toon ClaesJun 12, 2026
  27. Patrick SteinhardtJun 12, 2026
  28. 6/7 environment: stop using `the_repository` in `is_bare_repository()`Patrick Steinhardt, Jun 11, 2026
  29. 7/7 treewide: drop USE_THE_REPOSITORY_VARIABLEPatrick Steinhardt, Jun 11, 2026
  30. Justin ToblerJun 11, 2026
  31. Toon ClaesJun 12, 2026
  32. Patrick SteinhardtJun 12, 2026
  33. Toon ClaesJun 16, 2026

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.