environment: move access to core.maxTreeDepth into repo settings

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 <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

René Scharfe committed Jan 9, 2026 at 22:30 UTC e691395365b871608551bfbe20982b53140a50f0
9 files changed +36 -34
environment.c
-29
@@ -80,30 +80,6 @@ int core_sparse_checkout_cone;
80 int sparse_expect_files_outside_of_patterns;
81 int precomposed_unicode = -1; /* see probe_utf8_pathname_composition() */
82 unsigned long pack_size_limit_cfg;
83 -int max_allowed_tree_depth =
84 -#ifdef _MSC_VER
85 - /*
86 - * When traversing into too-deep trees, Visual C-compiled Git seems to
87 - * run into some internal stack overflow detection in the
88 - * `RtlpAllocateHeap()` function that is called from within
89 - * `git_inflate_init()`'s call tree. The following value seems to be
90 - * low enough to avoid that by letting Git exit with an error before
91 - * the stack overflow can occur.
92 - */
93 - 512;
94 -#elif defined(GIT_WINDOWS_NATIVE) && defined(__clang__) && defined(__aarch64__)
95 - /*
96 - * Similar to Visual C, it seems that on Windows/ARM64 the clang-based
97 - * builds have a smaller stack space available. When running out of
98 - * that stack space, a `STATUS_STACK_OVERFLOW` is produced. When the
99 - * Git command was run from an MSYS2 Bash, this unfortunately results
100 - * in an exit code 127. Let's prevent that by lowering the maximal
101 - * tree depth; This value seems to be low enough.
102 - */
103 - 1280;
104 -#else
105 - 2048;
106 -#endif
83
84 #ifndef PROTECT_HFS_DEFAULT
85 #define PROTECT_HFS_DEFAULT 0
@@ -569,11 +545,6 @@ static int git_default_core_config(const char *var, const char *value,
545 return 0;
546 }
547
572 - if (!strcmp(var, "core.maxtreedepth")) {
573 - max_allowed_tree_depth = git_config_int(var, value, ctx->kvi);
574 - return 0;
575 - }
576 -
548 /* Add other config variables here and to Documentation/config.adoc. */
549 return platform_core_config(var, value, ctx, cb);
550 }
environment.h
-1
@@ -156,7 +156,6 @@ extern char *git_attributes_file;
156 extern int zlib_compression_level;
157 extern int pack_compression_level;
158 extern unsigned long pack_size_limit_cfg;
159 -extern int max_allowed_tree_depth;
159
160 extern int precomposed_unicode;
161 extern int protect_hfs;
git-compat-util.h
+24
@@ -578,6 +578,30 @@ static inline bool strip_suffix(const char *str, const char *suffix,
578 #define DEFAULT_PACKED_GIT_LIMIT \
579 ((1024L * 1024L) * (size_t)(sizeof(void*) >= 8 ? (32 * 1024L * 1024L) : 256))
580
581 +#ifdef _MSC_VER
582 + /*
583 + * When traversing into too-deep trees, Visual C-compiled Git seems to
584 + * run into some internal stack overflow detection in the
585 + * `RtlpAllocateHeap()` function that is called from within
586 + * `git_inflate_init()`'s call tree. The following value seems to be
587 + * low enough to avoid that by letting Git exit with an error before
588 + * the stack overflow can occur.
589 + */
590 +#define DEFAULT_MAX_ALLOWED_TREE_DEPTH 512
591 +#elif defined(GIT_WINDOWS_NATIVE) && defined(__clang__) && defined(__aarch64__)
592 + /*
593 + * Similar to Visual C, it seems that on Windows/ARM64 the clang-based
594 + * builds have a smaller stack space available. When running out of
595 + * that stack space, a `STATUS_STACK_OVERFLOW` is produced. When the
596 + * Git command was run from an MSYS2 Bash, this unfortunately results
597 + * in an exit code 127. Let's prevent that by lowering the maximal
598 + * tree depth; This value seems to be low enough.
599 + */
600 +#define DEFAULT_MAX_ALLOWED_TREE_DEPTH 1280
601 +#else
602 +#define DEFAULT_MAX_ALLOWED_TREE_DEPTH 2048
603 +#endif
604 +
605 int git_open_cloexec(const char *name, int flags);
606 #define git_open(name) git_open_cloexec(name, O_RDONLY)
607
list-objects.c
+1 -1
@@ -167,7 +167,7 @@ static void process_tree(struct traversal_context *ctx,
167 !revs->include_check_obj(&tree->object, revs->include_check_data))
168 return;
169
170 - if (ctx->depth > max_allowed_tree_depth)
170 + if (ctx->depth > revs->repo->settings.max_allowed_tree_depth)
171 die("exceeded maximum allowed tree depth");
172
173 failed_parse = parse_tree_gently(tree, 1);
repo-settings.c
+3
@@ -100,6 +100,9 @@ void prepare_repo_settings(struct repository *r)
100 */
101 if (!repo_config_get_int(r, "index.version", &value))
102 r->settings.index_version = value;
103 + repo_cfg_int(r, "core.maxtreedepth",
104 + &r->settings.max_allowed_tree_depth,
105 + DEFAULT_MAX_ALLOWED_TREE_DEPTH);
106
107 if (!repo_config_get_string_tmp(r, "core.untrackedcache", &strval)) {
108 int v = git_parse_maybe_bool(strval);
repo-settings.h
+3
@@ -67,6 +67,8 @@ struct repo_settings {
67 size_t packed_git_limit;
68 unsigned long big_file_threshold;
69
70 + int max_allowed_tree_depth;
71 +
72 char *hooks_path;
73 };
74 #define REPO_SETTINGS_INIT { \
@@ -78,6 +80,7 @@ struct repo_settings {
80 .delta_base_cache_limit = DEFAULT_DELTA_BASE_CACHE_LIMIT, \
81 .packed_git_window_size = DEFAULT_PACKED_GIT_WINDOW_SIZE, \
82 .packed_git_limit = DEFAULT_PACKED_GIT_LIMIT, \
83 + .max_allowed_tree_depth = DEFAULT_MAX_ALLOWED_TREE_DEPTH, \
84 }
85
86 void prepare_repo_settings(struct repository *r);
tree-diff.c
+1 -1
@@ -439,7 +439,7 @@ static void ll_diff_tree_paths(
439 void *ttree, **tptree;
440 int i;
441
442 - if (depth > max_allowed_tree_depth)
442 + if (depth > opt->repo->settings.max_allowed_tree_depth)
443 die("exceeded maximum allowed tree depth");
444
445 FAST_ARRAY_ALLOC(tp, nparent);
tree-walk.c
+3 -1
@@ -12,6 +12,7 @@
12 #include "pathspec.h"
13 #include "json-writer.h"
14 #include "environment.h"
15 +#include "read-cache-ll.h"
16
17 static int decode_tree_entry(struct tree_desc *desc, const char *buf, unsigned long size, struct strbuf *err)
18 {
@@ -441,8 +442,9 @@ int traverse_trees(struct index_state *istate,
442 struct strbuf base = STRBUF_INIT;
443 int interesting = 1;
444 char *traverse_path;
445 + struct repository *r = istate ? istate->repo : the_repository;
446
445 - if (traverse_trees_cur_depth > max_allowed_tree_depth)
447 + if (traverse_trees_cur_depth > r->settings.max_allowed_tree_depth)
448 return error("exceeded maximum allowed tree depth");
449
450 traverse_trees_count++;
tree.c
+1 -1
@@ -25,7 +25,7 @@ int read_tree_at(struct repository *r,
25 int len, oldlen = base->len;
26 enum interesting retval = entry_not_interesting;
27
28 - if (depth > max_allowed_tree_depth)
28 + if (depth > r->settings.max_allowed_tree_depth)
29 return error("exceeded maximum allowed tree depth");
30
31 if (parse_tree(tree))