Subject: [RFC GSoC PATCH v3 1/2] repo-settings: add repo_settings_get_is_bare

From: Ayush Jha <[email-masked]>
Date: Sun, 8 Feb 2026 13:29:04 +0530

The is_bare_repository() function relies on the global the_repository
variable, making it unsuitable for use in library code that may operate
on arbitrary repositories. Additionally, calling repo_config_get_bool
repeatedly to check core.bare can be expensive if done frequently.

Add a lazy-loaded is_bare field to struct repo_settings and expose
it via repo_settings_get_is_bare(). This allows call sites to check
bareness cheaply and correctly using a repository context.

Signed-off-by: Ayush Jha <[email-masked]>
---
 repo-settings.c | 12 ++++++++++++
 repo-settings.h |  5 +++++
 2 files changed, 17 insertions(+)

diff --git a/repo-settings.c b/repo-settings.c
index 208e09ff17..fb0af993ee 100644
--- a/repo-settings.c
+++ b/repo-settings.c
@@ -233,3 +233,15 @@ void repo_settings_reset_shared_repository(struct repository *repo)
 {
 	repo->settings.shared_repository_initialized = 0;
 }
+
+int repo_settings_get_is_bare(struct repository *repo)
+{
+	if (repo->settings.is_bare < 0) {
+		int is_bare_cfg = 0;
+		if (repo_config_get_bool(repo, "core.bare", &is_bare_cfg))
+			repo->settings.is_bare = !repo_get_work_tree(repo);
+		else
+			repo->settings.is_bare = is_bare_cfg && !repo_get_work_tree(repo);
+	}
+	return repo->settings.is_bare;
+}
diff --git a/repo-settings.h b/repo-settings.h
index cad9c3f0cc..bde87f9f29 100644
--- a/repo-settings.h
+++ b/repo-settings.h
@@ -26,6 +26,7 @@ enum log_refs_config {
 struct repo_settings {
 	int initialized;
 
+	int is_bare;
 	int core_commit_graph;
 	int commit_graph_generation_version;
 	int commit_graph_changed_paths_version;
@@ -74,6 +75,7 @@ struct repo_settings {
 #define REPO_SETTINGS_INIT { \
 	.shared_repository = -1, \
 	.index_version = -1, \
+	.is_bare = -1, \
 	.core_untracked_cache = UNTRACKED_CACHE_KEEP, \
 	.fetch_negotiation_algorithm = FETCH_NEGOTIATION_CONSECUTIVE, \
 	.warn_ambiguous_refs = -1, \
@@ -102,4 +104,7 @@ int repo_settings_get_shared_repository(struct repository *repo);
 void repo_settings_set_shared_repository(struct repository *repo, int value);
 void repo_settings_reset_shared_repository(struct repository *repo);
 
+/* Read and set the value for "core.bare". */
+int repo_settings_get_is_bare(struct repository *repo);
+
 #endif /* REPO_SETTINGS_H */
-- 
2.53.0.windows.1