]> git.ipfire.org Git - thirdparty/git.git/commitdiff
setup: pass worktree to `init_db()`
authorPatrick Steinhardt <ps@pks.im>
Tue, 30 Jun 2026 11:47:51 +0000 (13:47 +0200)
committerJunio C Hamano <gitster@pobox.com>
Tue, 30 Jun 2026 18:29:41 +0000 (11:29 -0700)
In the preceding commits we have refactored how we discover and set up
repositories so that we cannot end up with partially-configured repos.
Instead, we apply the gitdir, worktree and repository format in a single
location, only.

Initializing a new repository has the same antipattern though: while
most of the information for the new repository is passed via parameters,
the work tree is instead propagated by configuring the repository's work
tree.

Refactor the code so that we also pass the work tree as an explicit
parameter. Like this, configuration fo the repository happens in a
single spot, too, just as with repository discovery.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
builtin/clone.c
builtin/init-db.c
setup.c
setup.h

index d60d1b60bc238cc9f6dd0a8418ba83107b7e944c..9d08cd8722430475ee25a1b9fb0929a8b720af80 100644 (file)
@@ -1116,7 +1116,6 @@ int cmd_clone(int argc,
                        die_errno(_("could not create work tree dir '%s'"),
                                  work_tree);
                junk_work_tree = work_tree;
-               set_git_work_tree(the_repository, work_tree);
        }
 
        if (real_git_dir) {
@@ -1186,9 +1185,10 @@ int cmd_clone(int argc,
         * repository, and reference backends may persist that information into
         * their on-disk data structures.
         */
-       init_db(the_repository, git_dir, real_git_dir, option_template, GIT_HASH_UNKNOWN,
-               ref_storage_format, NULL,
-               do_not_override_repo_unix_permissions, INIT_DB_QUIET | INIT_DB_SKIP_REFDB);
+       init_db(the_repository, git_dir, real_git_dir, work_tree, option_template,
+               GIT_HASH_UNKNOWN, ref_storage_format, NULL,
+               do_not_override_repo_unix_permissions,
+               INIT_DB_QUIET | INIT_DB_SKIP_REFDB);
 
        if (real_git_dir) {
                free((char *)git_dir);
index 566732c9f4a8eeb0231acec546b798a65ffdf309..e96b1283b7010d42cef5b843ca3c04851d72c9f0 100644 (file)
@@ -231,39 +231,25 @@ int cmd_init_db(int argc,
        if (!bare) {
                const char *git_dir_parent = strrchr(git_dir, '/');
 
-               if (work_tree) {
-                       set_git_work_tree(the_repository, work_tree);
-               } else {
-                       char *work_tree_cfg = NULL;
-
+               if (!work_tree) {
                        if (git_dir_parent) {
                                char *rel = xstrndup(git_dir, git_dir_parent - git_dir);
-                               work_tree_cfg = real_pathdup(rel, 1);
+                               work_tree = real_pathdup(rel, 1);
                                free(rel);
+                       } else {
+                               work_tree = xgetcwd();
                        }
-
-                       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));
-       }
-       else {
-               if (real_git_dir)
-                       die(_("--separate-git-dir incompatible with bare repository"));
-               if (work_tree)
-                       set_git_work_tree(the_repository, work_tree);
+               if (access(work_tree, X_OK))
+                       die_errno (_("Cannot access work tree '%s'"), work_tree);
+       } else if (real_git_dir) {
+               die(_("--separate-git-dir incompatible with bare repository"));
        }
 
        flags |= INIT_DB_EXIST_OK;
-       ret = init_db(the_repository, git_dir, real_git_dir, template_dir, hash_algo,
-                     ref_storage_format, initial_branch,
+       ret = init_db(the_repository, git_dir, real_git_dir, work_tree,
+                     template_dir, hash_algo, ref_storage_format, initial_branch,
                      init_shared_repository, flags);
 
        free(template_dir_to_free);
diff --git a/setup.c b/setup.c
index ad15a0cf1f7504062ebdf05b376ee6f12e32fba4..3a1433c618387e6e93b0397879ad1cb2fa34b989 100644 (file)
--- a/setup.c
+++ b/setup.c
@@ -2823,7 +2823,9 @@ static void repository_format_configure(struct repository_format *repo_fmt,
 }
 
 int init_db(struct repository *repo,
-           const char *git_dir, const char *real_git_dir,
+           const char *git_dir,
+           const char *real_git_dir,
+           const char *worktree,
            const char *template_dir, int hash,
            enum ref_storage_format ref_storage_format,
            const char *initial_branch,
@@ -2852,6 +2854,9 @@ int init_db(struct repository *repo,
                git_dir = repo_get_git_dir(repo);
        }
 
+       if (worktree)
+               set_git_work_tree(repo, worktree);
+
        /*
         * Check to see if the repository version is right.
         * Note that a newly created repository does not have
diff --git a/setup.h b/setup.h
index c01a244fe93a6a14c2ab0bcf2a432cb85ff1f3e4..bf3e3f3ea6ee9ded83a37a35152401771e9266b0 100644 (file)
--- a/setup.h
+++ b/setup.h
@@ -263,7 +263,9 @@ const char *get_template_dir(const char *option_template);
 #define INIT_DB_SKIP_REFDB (1 << 2)
 
 int init_db(struct repository *repo,
-           const char *git_dir, const char *real_git_dir,
+           const char *git_dir,
+           const char *real_git_dir,
+           const char *worktree,
            const char *template_dir, int hash_algo,
            enum ref_storage_format ref_storage_format,
            const char *initial_branch, int init_shared_repository,