]> git.ipfire.org Git - thirdparty/git.git/commitdiff
builtin/init: simplify logic to configure worktree
authorPatrick Steinhardt <ps@pks.im>
Thu, 11 Jun 2026 06:44:40 +0000 (08:44 +0200)
committerJunio C Hamano <gitster@pobox.com>
Thu, 11 Jun 2026 12:05:54 +0000 (05:05 -0700)
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>
builtin/init-db.c

index 01bc27904e5fed01e3d8065822044ea22d74a4c5..b4343c2804457038fa1602f09ec50293116ef8c3 100644 (file)
@@ -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)