Subject: [PATCH 01/10] environment: move access to core.maxTreeDepth into repo settings

From: René Scharfe <[email-masked]>
Date: Fri, 9 Jan 2026 22:30:12 +0100

The config setting core.maxTreeDepth is stored in a global variable and
populated by the function git_default_core_config.  This won't work if
we need to access multiple repositories with different values of that
setting in the same process.  Store the setting in struct repo_settings
instead and track it separately for each repository.

Signed-off-by: René Scharfe <[email-masked]>
---
 environment.c     | 29 -----------------------------
 environment.h     |  1 -
 git-compat-util.h | 24 ++++++++++++++++++++++++
 list-objects.c    |  2 +-
 repo-settings.c   |  3 +++
 repo-settings.h   |  3 +++
 tree-diff.c       |  2 +-
 tree-walk.c       |  4 +++-
 tree.c            |  2 +-
 9 files changed, 36 insertions(+), 34 deletions(-)

diff --git a/environment.c b/environment.c
index a770b5921d9..c6e5b65abac 100644
--- a/environment.c
+++ b/environment.c
@@ -80,30 +80,6 @@ int core_sparse_checkout_cone;
 int sparse_expect_files_outside_of_patterns;
 int precomposed_unicode = -1; /* see probe_utf8_pathname_composition() */
 unsigned long pack_size_limit_cfg;
-int max_allowed_tree_depth =
-#ifdef _MSC_VER
-	/*
-	 * When traversing into too-deep trees, Visual C-compiled Git seems to
-	 * run into some internal stack overflow detection in the
-	 * `RtlpAllocateHeap()` function that is called from within
-	 * `git_inflate_init()`'s call tree. The following value seems to be
-	 * low enough to avoid that by letting Git exit with an error before
-	 * the stack overflow can occur.
-	 */
-	512;
-#elif defined(GIT_WINDOWS_NATIVE) && defined(__clang__) && defined(__aarch64__)
-	/*
-	 * Similar to Visual C, it seems that on Windows/ARM64 the clang-based
-	 * builds have a smaller stack space available. When running out of
-	 * that stack space, a `STATUS_STACK_OVERFLOW` is produced. When the
-	 * Git command was run from an MSYS2 Bash, this unfortunately results
-	 * in an exit code 127. Let's prevent that by lowering the maximal
-	 * tree depth; This value seems to be low enough.
-	 */
-	1280;
-#else
-	2048;
-#endif
 
 #ifndef PROTECT_HFS_DEFAULT
 #define PROTECT_HFS_DEFAULT 0
@@ -569,11 +545,6 @@ static int git_default_core_config(const char *var, const char *value,
 		return 0;
 	}
 
-	if (!strcmp(var, "core.maxtreedepth")) {
-		max_allowed_tree_depth = git_config_int(var, value, ctx->kvi);
-		return 0;
-	}
-
 	/* Add other config variables here and to Documentation/config.adoc. */
 	return platform_core_config(var, value, ctx, cb);
 }
diff --git a/environment.h b/environment.h
index 51898c99cd1..9efe0b30fb3 100644
--- a/environment.h
+++ b/environment.h
@@ -156,7 +156,6 @@ extern char *git_attributes_file;
 extern int zlib_compression_level;
 extern int pack_compression_level;
 extern unsigned long pack_size_limit_cfg;
-extern int max_allowed_tree_depth;
 
 extern int precomposed_unicode;
 extern int protect_hfs;
diff --git a/git-compat-util.h b/git-compat-util.h
index b0673d1a450..bebcf9f698c 100644
--- a/git-compat-util.h
+++ b/git-compat-util.h
@@ -578,6 +578,30 @@ static inline bool strip_suffix(const char *str, const char *suffix,
 #define DEFAULT_PACKED_GIT_LIMIT \
 	((1024L * 1024L) * (size_t)(sizeof(void*) >= 8 ? (32 * 1024L * 1024L) : 256))
 
+#ifdef _MSC_VER
+  /*
+   * When traversing into too-deep trees, Visual C-compiled Git seems to
+   * run into some internal stack overflow detection in the
+   * `RtlpAllocateHeap()` function that is called from within
+   * `git_inflate_init()`'s call tree. The following value seems to be
+   * low enough to avoid that by letting Git exit with an error before
+   * the stack overflow can occur.
+   */
+#define DEFAULT_MAX_ALLOWED_TREE_DEPTH 512
+#elif defined(GIT_WINDOWS_NATIVE) && defined(__clang__) && defined(__aarch64__)
+  /*
+   * Similar to Visual C, it seems that on Windows/ARM64 the clang-based
+   * builds have a smaller stack space available. When running out of
+   * that stack space, a `STATUS_STACK_OVERFLOW` is produced. When the
+   * Git command was run from an MSYS2 Bash, this unfortunately results
+   * in an exit code 127. Let's prevent that by lowering the maximal
+   * tree depth; This value seems to be low enough.
+   */
+#define DEFAULT_MAX_ALLOWED_TREE_DEPTH 1280
+#else
+#define DEFAULT_MAX_ALLOWED_TREE_DEPTH 2048
+#endif
+
 int git_open_cloexec(const char *name, int flags);
 #define git_open(name) git_open_cloexec(name, O_RDONLY)
 
diff --git a/list-objects.c b/list-objects.c
index 42c17d95739..1279676ddca 100644
--- a/list-objects.c
+++ b/list-objects.c
@@ -167,7 +167,7 @@ static void process_tree(struct traversal_context *ctx,
 	    !revs->include_check_obj(&tree->object, revs->include_check_data))
 		return;
 
-	if (ctx->depth > max_allowed_tree_depth)
+	if (ctx->depth > revs->repo->settings.max_allowed_tree_depth)
 		die("exceeded maximum allowed tree depth");
 
 	failed_parse = parse_tree_gently(tree, 1);
diff --git a/repo-settings.c b/repo-settings.c
index 195c24e9c07..208e09ff17f 100644
--- a/repo-settings.c
+++ b/repo-settings.c
@@ -100,6 +100,9 @@ void prepare_repo_settings(struct repository *r)
 	 */
 	if (!repo_config_get_int(r, "index.version", &value))
 		r->settings.index_version = value;
+	repo_cfg_int(r, "core.maxtreedepth",
+		     &r->settings.max_allowed_tree_depth,
+		     DEFAULT_MAX_ALLOWED_TREE_DEPTH);
 
 	if (!repo_config_get_string_tmp(r, "core.untrackedcache", &strval)) {
 		int v = git_parse_maybe_bool(strval);
diff --git a/repo-settings.h b/repo-settings.h
index d4778855614..cad9c3f0cc1 100644
--- a/repo-settings.h
+++ b/repo-settings.h
@@ -67,6 +67,8 @@ struct repo_settings {
 	size_t packed_git_limit;
 	unsigned long big_file_threshold;
 
+	int max_allowed_tree_depth;
+
 	char *hooks_path;
 };
 #define REPO_SETTINGS_INIT { \
@@ -78,6 +80,7 @@ struct repo_settings {
 	.delta_base_cache_limit = DEFAULT_DELTA_BASE_CACHE_LIMIT, \
 	.packed_git_window_size = DEFAULT_PACKED_GIT_WINDOW_SIZE, \
 	.packed_git_limit = DEFAULT_PACKED_GIT_LIMIT, \
+	.max_allowed_tree_depth = DEFAULT_MAX_ALLOWED_TREE_DEPTH, \
 }
 
 void prepare_repo_settings(struct repository *r);
diff --git a/tree-diff.c b/tree-diff.c
index 5988148b602..631ea868124 100644
--- a/tree-diff.c
+++ b/tree-diff.c
@@ -439,7 +439,7 @@ static void ll_diff_tree_paths(
 	void *ttree, **tptree;
 	int i;
 
-	if (depth > max_allowed_tree_depth)
+	if (depth > opt->repo->settings.max_allowed_tree_depth)
 		die("exceeded maximum allowed tree depth");
 
 	FAST_ARRAY_ALLOC(tp, nparent);
diff --git a/tree-walk.c b/tree-walk.c
index e449a1320e5..7e1b956f278 100644
--- a/tree-walk.c
+++ b/tree-walk.c
@@ -12,6 +12,7 @@
 #include "pathspec.h"
 #include "json-writer.h"
 #include "environment.h"
+#include "read-cache-ll.h"
 
 static int decode_tree_entry(struct tree_desc *desc, const char *buf, unsigned long size, struct strbuf *err)
 {
@@ -441,8 +442,9 @@ int traverse_trees(struct index_state *istate,
 	struct strbuf base = STRBUF_INIT;
 	int interesting = 1;
 	char *traverse_path;
+	struct repository *r = istate ? istate->repo : the_repository;
 
-	if (traverse_trees_cur_depth > max_allowed_tree_depth)
+	if (traverse_trees_cur_depth > r->settings.max_allowed_tree_depth)
 		return error("exceeded maximum allowed tree depth");
 
 	traverse_trees_count++;
diff --git a/tree.c b/tree.c
index 1ef743d90f4..2a677234d60 100644
--- a/tree.c
+++ b/tree.c
@@ -25,7 +25,7 @@ int read_tree_at(struct repository *r,
 	int len, oldlen = base->len;
 	enum interesting retval = entry_not_interesting;
 
-	if (depth > max_allowed_tree_depth)
+	if (depth > r->settings.max_allowed_tree_depth)
 		return error("exceeded maximum allowed tree depth");
 
 	if (parse_tree(tree))
-- 
2.52.0