builtin/init: simplify logic to configure worktree
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> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Patrick Steinhardt committed
Jun 11, 2026 at 08:44 UTC
65eb5b989aa6d7f764d097c6759f6b6189eb0d27
1 file changed
+18
-13
builtin/init-db.c
+18
-13
@@ -229,24 +229,29 @@ int cmd_init_db(int argc,
229
230
if (!is_bare_repository_cfg) {
231
const char *git_dir_parent = strrchr(git_dir, '/');
232
- char *git_work_tree_cfg = NULL;
232
234
- if (git_dir_parent) {
235
- char *rel = xstrndup(git_dir, git_dir_parent - git_dir);
236
- git_work_tree_cfg = real_pathdup(rel, 1);
237
- free(rel);
238
- }
239
- if (!git_work_tree_cfg)
240
- git_work_tree_cfg = xgetcwd();
241
- if (work_tree)
233
+ if (work_tree) {
234
set_git_work_tree(the_repository, work_tree);
243
- else
244
- set_git_work_tree(the_repository, git_work_tree_cfg);
235
+ } else {
236
+ char *work_tree_cfg = NULL;
237
+
238
+ if (git_dir_parent) {
239
+ char *rel = xstrndup(git_dir, git_dir_parent - git_dir);
240
+ work_tree_cfg = real_pathdup(rel, 1);
241
+ free(rel);
242
+ }
243
+
244
+ if (!work_tree_cfg)
245
+ work_tree_cfg = xgetcwd();
246
+
247
+ set_git_work_tree(the_repository, work_tree_cfg);
248
+
249
+ free(work_tree_cfg);
250
+ }
251
+
252
if (access(repo_get_work_tree(the_repository), X_OK))
253
die_errno (_("Cannot access work tree '%s'"),
254
repo_get_work_tree(the_repository));
248
-
249
- free(git_work_tree_cfg);
255
}
256
else {
257
if (real_git_dir)