environment: split up concerns of `is_bare_repository_cfg`

The `is_bare_repository_cfg` variable tracks two different pieces of information: - It tracks whether the user has invoked git with the "--bare" flag, which makes us treat any discovered Git repository as if it was a bare repository. - Otherwise it tracks whether the discovered `the_repository` is bare. This makes the flag extremely confusing and creates a bit of a challenge when handling multiple repositories in the same process. Split up the concerns of this variable into two pieces: - `startup_info.force_bare_repository` tracks whether the user has passed the "--bare" flag. This is used as a hint to treat newly set up repositories as bare regardless of whether or not they have a worktree. - `struct repository::bare_cfg` tracks whether or not a repository is considered bare. This takes into account both whether the user has passed "--bare" and the discovered state of the repository itself. Whether or not a repository is bare is now resolved when checking the repository's format, and is then later applied to the repository itself via `apply_repository_format()`. This enables a subsequent change where we make `is_bare_repository()` not depend on global state anymore. 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 7ff3a5895b6bf4c14d361e4c845d2de7e4006259
9 files changed +39 -14
builtin/init-db.c
+1 -1
@@ -81,7 +81,7 @@ int cmd_init_db(int argc,
81 const char *template_dir = NULL;
82 char *template_dir_to_free = NULL;
83 unsigned int flags = 0;
84 - int bare = is_bare_repository_cfg;
84 + int bare = startup_info->force_bare_repository ? 1 : -1;
85 const char *object_format = NULL;
86 const char *ref_format = NULL;
87 const char *initial_branch = NULL;
environment.c
+2 -3
@@ -48,7 +48,6 @@ int has_symlinks = 1;
48 int minimum_abbrev = 4, default_abbrev = -1;
49 int ignore_case;
50 int assume_unchanged;
51 -int is_bare_repository_cfg = -1; /* unspecified */
51 int warn_on_object_refname_ambiguity = 1;
52 char *git_commit_encoding;
53 char *git_log_output_encoding;
@@ -136,7 +135,7 @@ const char *getenv_safe(struct strvec *argv, const char *name)
135 int is_bare_repository(void)
136 {
137 /* if core.bare is not 'false', let's see if there is a work tree */
139 - return is_bare_repository_cfg && !repo_get_work_tree(the_repository);
138 + return the_repository->bare_cfg && !repo_get_work_tree(the_repository);
139 }
140
141 int have_git_dir(void)
@@ -342,7 +341,7 @@ int git_default_core_config(const char *var, const char *value,
341 }
342
343 if (!strcmp(var, "core.bare")) {
345 - is_bare_repository_cfg = git_config_bool(var, value);
344 + the_repository->bare_cfg = git_config_bool(var, value);
345 return 0;
346 }
347
environment.h
-1
@@ -147,7 +147,6 @@ void repo_config_values_init(struct repo_config_values *cfg);
147 */
148 int have_git_dir(void);
149
150 -extern int is_bare_repository_cfg;
150 int is_bare_repository(void);
151
152 /* Environment bits from configuration mechanism */
git.c
+1 -1
@@ -255,7 +255,7 @@ static int handle_options(const char ***argv, int *argc, int *envchanged)
255 *envchanged = 1;
256 } else if (!strcmp(cmd, "--bare")) {
257 char *cwd = xgetcwd();
258 - is_bare_repository_cfg = 1;
258 + startup_info->force_bare_repository = true;
259 setenv(GIT_DIR_ENVIRONMENT, cwd, 0);
260 free(cwd);
261 setenv(GIT_IMPLICIT_WORK_TREE_ENVIRONMENT, "0", 1);
repository.c
+1
@@ -73,6 +73,7 @@ void initialize_repository(struct repository *repo)
73 ALLOC_ARRAY(repo->index, 1);
74 index_state_init(repo->index, repo);
75 repo->check_deprecated_config = true;
76 + repo->bare_cfg = -1;
77 repo_config_values_init(&repo->config_values_private_);
78
79 /*
repository.h
+7
@@ -117,6 +117,13 @@ struct repository {
117 bool worktree_initialized;
118 bool worktree_config_is_bogus;
119
120 + /*
121 + * Whether the repository is bare, as set by "core.bare" config or
122 + * inferred during repository discovery. -1 means unset/unknown, 0
123 + * means non-bare, 1 means bare.
124 + */
125 + int bare_cfg;
126 +
127 /*
128 * Path from the root of the top-level superproject down to this
129 * repository. This is only non-NULL if the repository is initialized
setup.c
+20 -7
@@ -795,10 +795,22 @@ static int check_repository_format_gently(const char *gitdir,
795 has_common = 0;
796 }
797
798 - if (!has_common) {
799 - if (candidate->is_bare != -1)
800 - is_bare_repository_cfg = candidate->is_bare;
801 - } else {
798 + if (startup_info->force_bare_repository) {
799 + candidate->is_bare = 1;
800 + FREE_AND_NULL(candidate->work_tree);
801 + } else if (has_common) {
802 + /*
803 + * When sharing a common dir with another repository (e.g. a
804 + * linked worktree), do not let this repository's config
805 + * dictate bareness; it is inherited from the main worktree.
806 + */
807 + candidate->is_bare = -1;
808 +
809 + /*
810 + * Furthermore, "core.worktree" is supposed to be ignored when
811 + * we have a commondir configured, unless it comes from the
812 + * per-worktree configuration.
813 + */
814 FREE_AND_NULL(candidate->work_tree);
815 }
816
@@ -1138,7 +1150,7 @@ static const char *setup_explicit_git_dir(struct repository *repo,
1150 /* #3, #7, #11, #15, #19, #23, #27, #31 (see t1510) */
1151 if (work_tree_env)
1152 set_git_work_tree(repo, work_tree_env);
1141 - else if (is_bare_repository_cfg > 0) {
1153 + else if (repo_fmt->is_bare > 0) {
1154 if (repo_fmt->work_tree) {
1155 /* #22.2, #30 */
1156 warning("core.bare and core.worktree do not make sense");
@@ -1225,7 +1237,7 @@ static const char *setup_discovered_git_dir(struct repository *repo,
1237 }
1238
1239 /* #16.2, #17.2, #20.2, #21.2, #24, #25, #28, #29 (see t1510) */
1228 - if (is_bare_repository_cfg > 0) {
1240 + if (repo_fmt->is_bare > 0) {
1241 set_git_dir(repo, gitdir, (offset != cwd->len));
1242 if (chdir(cwd->buf))
1243 die_errno(_("cannot come back to cwd"));
@@ -1762,6 +1774,7 @@ int apply_repository_format(struct repository *repo,
1774 alternate_object_directories = xstrdup_or_null(getenv(ALTERNATE_DB_ENVIRONMENT));
1775 }
1776
1777 + repo->bare_cfg = format->is_bare;
1778 repo_set_hash_algo(repo, format->hash_algo);
1779 repo->objects = odb_new(repo, object_directory,
1780 alternate_object_directories);
@@ -2571,7 +2584,7 @@ static int create_default_files(struct repository *repo,
2584 repo_settings_set_shared_repository(repo,
2585 init_shared_repository);
2586
2574 - is_bare_repository_cfg = !work_tree;
2587 + repo->bare_cfg = !work_tree;
2588
2589 /*
2590 * We would have created the above under user's umask -- under
setup.h
+6
@@ -292,6 +292,12 @@ enum sharedrepo {
292 int git_config_perm(const char *var, const char *value);
293
294 struct startup_info {
295 + /*
296 + * Whether the user is asking us to treat the repository as bare via
297 + * `git --bare`, even if it's not.
298 + */
299 + bool force_bare_repository;
300 +
301 int have_repository;
302 const char *prefix;
303 const char *original_cwd;
worktree.c
+1 -1
@@ -123,7 +123,7 @@ static struct worktree *get_main_worktree(int skip_reading_head)
123 worktree->repo = the_repository;
124 worktree->path = strbuf_detach(&worktree_path, NULL);
125 worktree->is_current = is_current_worktree(worktree);
126 - worktree->is_bare = (is_bare_repository_cfg == 1) ||
126 + worktree->is_bare = (the_repository->bare_cfg == 1) ||
127 is_bare_repository() ||
128 /*
129 * When in a secondary worktree we have to also verify if the main