]> git.ipfire.org Git - thirdparty/git.git/commitdiff
setup: mark bogus worktree in `apply_repository_format()`
authorPatrick Steinhardt <ps@pks.im>
Tue, 30 Jun 2026 11:47:41 +0000 (13:47 +0200)
committerJunio C Hamano <gitster@pobox.com>
Tue, 30 Jun 2026 18:29:39 +0000 (11:29 -0700)
When a repository is configured to have both "core.worktree" and
"core.bare" we emit a warning and mark the worktree configuration as
bogus so that the next call to `setup_work_tree()` will cause us to die.
This allows us to still use the misconfigured repository, at least as
long as we don't try to use its worktree.

This condition is handled in `setup_explicit_git_dir()`. In a subsequent
commit we'll refactor this function so that it doesn't receive a repo as
input anymore though, and consequently we cannot set the "bogus" bit
anymore.

Move the logic into `apply_repository_format()` instead to prepare for
this. While at it, fix up formatting a bit.

Note that this change requires us to also explicitly unset the value of
"core.worktree" in case we have the "GIT_WORK_TREE" environment variable
set. This is because the environment variable overrides the repository's
configuration, and we don't want to warn or die in case the work tree
has been configured explicitly regardless of whether or not "core.bare"
is set.

Signed-off-by: Patrick Steinhardt <ps@pks.im>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
setup.c

diff --git a/setup.c b/setup.c
index 118416e3500a163219cde78df7129df5706c610e..1d8c193375c4d8caf266a01515256a68bc0773c1 100644 (file)
--- a/setup.c
+++ b/setup.c
@@ -1147,24 +1147,24 @@ static const char *setup_explicit_git_dir(struct repository *repo,
        }
 
        /* #3, #7, #11, #15, #19, #23, #27, #31 (see t1510) */
-       if (work_tree_env)
+       if (work_tree_env) {
+               /*
+                * The environment variable overrides "core.worktree". This
+                * also has the consequence that we don't want to flag cases as
+                * bogus where we have both "core.worktree" and "core.bare", so
+                * we have to explicitly unset the configuration.
+                */
+               FREE_AND_NULL(repo_fmt->work_tree);
                set_git_work_tree(repo, work_tree_env);
-       else if (repo_fmt->is_bare > 0) {
-               if (repo_fmt->work_tree) {
-                       /* #22.2, #30 */
-                       warning("core.bare and core.worktree do not make sense");
-                       repo->worktree_config_is_bogus = true;
-               }
-
+       } else if (repo_fmt->is_bare > 0) {
                /* #18, #26 */
                set_git_dir(repo, gitdirenv, 0);
                free(gitfile);
                return NULL;
-       }
-       else if (repo_fmt->work_tree) { /* #6, #14 */
-               if (is_absolute_path(repo_fmt->work_tree))
+       } else if (repo_fmt->work_tree) { /* #6, #14 */
+               if (is_absolute_path(repo_fmt->work_tree)) {
                        set_git_work_tree(repo, repo_fmt->work_tree);
-               else {
+               else {
                        char *core_worktree;
                        if (chdir(gitdirenv))
                                die_errno(_("cannot chdir to '%s'"), gitdirenv);
@@ -1176,15 +1176,14 @@ static const char *setup_explicit_git_dir(struct repository *repo,
                        set_git_work_tree(repo, core_worktree);
                        free(core_worktree);
                }
-       }
-       else if (!git_env_bool(GIT_IMPLICIT_WORK_TREE_ENVIRONMENT, 1)) {
+       } else if (!git_env_bool(GIT_IMPLICIT_WORK_TREE_ENVIRONMENT, 1)) {
                /* #16d */
                set_git_dir(repo, gitdirenv, 0);
                free(gitfile);
                return NULL;
-       }
-       else /* #2, #10 */
+       } else { /* #2, #10 */
                set_git_work_tree(repo, ".");
+       }
 
        /* set_git_work_tree() must have been called by now */
        worktree = repo_get_work_tree(repo);
@@ -1768,6 +1767,12 @@ int apply_repository_format(struct repository *repo,
        if (verify_repository_format(format, err) < 0)
                return -1;
 
+       if (format->is_bare > 0 && format->work_tree) {
+               /* #22.2, #30 */
+               warning("core.bare and core.worktree do not make sense");
+               repo->worktree_config_is_bogus = true;
+       }
+
        if (flags & APPLY_REPOSITORY_FORMAT_HONOR_ENV) {
                object_directory = xstrdup_or_null(getenv(DB_ENVIRONMENT));
                alternate_object_directories = xstrdup_or_null(getenv(ALTERNATE_DB_ENVIRONMENT));