public inbox for git@vger.kernel.org
 help / color / mirror / Atom feed
From: "René Scharfe" <l.s.r@web.de>
To: git@vger.kernel.org
Subject: [PATCH 01/10] environment: move access to core.maxTreeDepth into repo settings
Date: Fri,  9 Jan 2026 22:30:12 +0100	[thread overview]
Message-ID: <20260109213021.2546-2-l.s.r@web.de> (raw)
In-Reply-To: <20260109213021.2546-1-l.s.r@web.de>

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>
---
 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


  reply	other threads:[~2026-01-09 21:30 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-01-09 21:30 [PATCH 00/10] tree: stop using the_repository René Scharfe
2026-01-09 21:30 ` René Scharfe [this message]
2026-01-12  9:21   ` [PATCH 01/10] environment: move access to core.maxTreeDepth into repo settings Patrick Steinhardt
2026-01-12 19:37     ` René Scharfe
2026-01-09 21:30 ` [PATCH 02/10] tree: add repo_parse_tree*() René Scharfe
2026-01-09 21:30 ` [PATCH 03/10] add-interactive: use repo_parse_tree_indirect() René Scharfe
2026-01-09 21:30 ` [PATCH 04/10] bloom: use repo_parse_tree() René Scharfe
2026-01-09 21:30 ` [PATCH 05/10] delta-islands: " René Scharfe
2026-01-09 21:30 ` [PATCH 06/10] pack-bitmap-write: " René Scharfe
2026-01-09 21:30 ` [PATCH 07/10] path-walk: use repo_parse_tree_gently() René Scharfe
2026-01-09 21:30 ` [PATCH 08/10] tree: use repo_parse_tree() René Scharfe
2026-01-12  9:21   ` Patrick Steinhardt
2026-01-09 21:30 ` [PATCH 09/10] tree: stop using the_repository René Scharfe
2026-01-12  9:21   ` Patrick Steinhardt
2026-01-12 14:22     ` Junio C Hamano
2026-01-12 15:00       ` Patrick Steinhardt
2026-01-12 15:17         ` Junio C Hamano
2026-01-12 15:20         ` Junio C Hamano
2026-01-12 15:28           ` Patrick Steinhardt
2026-01-12 19:37             ` René Scharfe
2026-01-13  6:13               ` Patrick Steinhardt
2026-01-09 21:30 ` [PATCH 10/10] cocci: convert parse_tree functions to repo_ variants René Scharfe
2026-01-15 22:01 ` [PATCH 11/10] cocci: remove obsolete the_repository rules René Scharfe
2026-01-16 10:02   ` Patrick Steinhardt
2026-01-16 17:28     ` Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260109213021.2546-2-l.s.r@web.de \
    --to=l.s.r@web.de \
    --cc=git@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox