From: Taylor Blau Date: Mon, 04 May 2026 00:11:32 GMT Subject: [RFC PATCH 6/7] pack-objects: extract `record_tree_depth()` helper Message-ID: <35b4485fa5258f63d0e0996be7760ec83e9adac6.1777853408.git.me@ttaylorr.com> In-Reply-To: Prepare for a future change that needs to record tree depths from a second call site by factoring out the delta islands-specific portion of `show_object()` out into a helper, `record_tree_depth()`. `record_tree_depth()` takes a tree OID along with the path that `show_object()` received, and computes the directory depth from the slash count in the path. While we're in the area, make a few minor clean-ups: - Gate the call on `obj->type == OBJ_TREE`, as we only care to compute the depth for tree objects. The sole caller of `oe_tree_depth()` resides in `delta-islands.c::resolve_tree_islands()`, and only calls `oe_tree_depth()` behind `oe_type(...) == OBJ_TREE`. - Defer computing the depth for an object until we know it is in the `to_pack` list. Signed-off-by: Taylor Blau --- builtin/pack-objects.c | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/builtin/pack-objects.c b/builtin/pack-objects.c index 1a5f1afd32e..842d1fcac29 100644 --- a/builtin/pack-objects.c +++ b/builtin/pack-objects.c @@ -2722,6 +2722,24 @@ static inline void oe_set_tree_depth(struct packing_data *pack, pack->tree_depth[e - pack->objects] = tree_depth; } +static void record_tree_depth(const struct object_id *oid, const char *name) +{ + const char *p; + unsigned depth; + struct object_entry *ent = packlist_find(&to_pack, oid); + + if (!ent) + return; + + /* the empty string is a root tree, which is depth 0 */ + depth = *name ? 1 : 0; + for (p = strchr(name, '/'); p; p = strchr(p + 1, '/')) + depth++; + + if (depth > oe_tree_depth(&to_pack, ent)) + oe_set_tree_depth(&to_pack, ent, depth); +} + /* * Return the size of the object without doing any delta * reconstruction (so non-deltas are true object sizes, but deltas @@ -4375,20 +4393,8 @@ static void show_object(struct object *obj, const char *name, add_preferred_base_object(name); add_object_entry(&obj->oid, obj->type, name, 0); - if (use_delta_islands) { - const char *p; - unsigned depth; - struct object_entry *ent; - - /* the empty string is a root tree, which is depth 0 */ - depth = *name ? 1 : 0; - for (p = strchr(name, '/'); p; p = strchr(p + 1, '/')) - depth++; - - ent = packlist_find(&to_pack, &obj->oid); - if (ent && depth > oe_tree_depth(&to_pack, ent)) - oe_set_tree_depth(&to_pack, ent, depth); - } + if (use_delta_islands && obj->type == OBJ_TREE) + record_tree_depth(&obj->oid, name); } static void show_object__ma_allow_any(struct object *obj, const char *name, void *data) -- 2.54.0.4.g6aa0d38a4ec