path-walk: add pl_sparse_trees to control tree pruning

The path-walk API prunes trees and blobs when a sparse-checkout pattern list is provided, which is the correct behavior for 'git backfill --sparse' since it only needs to fill in objects at paths within the sparse cone. However, a future change will use the path-walk API with a sparse:<oid> filter that restricts only blobs while retaining all reachable trees. To support both behaviors, add a 'pl_sparse_trees' flag to path_walk_info. When set (as in 'git backfill --sparse' and the --stdin-pl test helper mode), the sparse patterns prune both trees and blobs. When unset, only blobs are filtered and all trees are walked and reported. Additionally, move the SEEN flag assignment in add_tree_entries() to after the sparse pattern and pathspec checks. Previously, SEEN was set immediately upon discovering an object, before checking whether its path matched the sparse patterns. When the same object ID appeared at multiple paths (e.g. sibling directories with identical contents), the first path to be visited would mark the object as SEEN. If that path was outside the sparse cone, the object would be skipped there but also never discovered at its in-cone path. By deferring the SEEN flag until after the checks pass, objects that are skipped due to sparse filtering remain discoverable at other paths where they may be in scope. Signed-off-by: Derrick Stolee <stolee@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Derrick Stolee committed May 22, 2026 at 18:24 UTC 8ff8de76162387f1adcd01c159ada9b74987cdf2
5 files changed +52 -3
builtin/backfill.c
+1
@@ -109,6 +109,7 @@ static int do_backfill(struct backfill_context *ctx)
109
110 if (ctx->sparse) {
111 CALLOC_ARRAY(info.pl, 1);
112 + info.pl_sparse_trees = 1;
113 if (get_sparse_checkout_patterns(info.pl)) {
114 path_walk_info_clear(&info);
115 return error(_("problem loading sparse-checkout"));
path-walk.c
+3 -2
@@ -183,7 +183,6 @@ static int add_tree_entries(struct path_walk_context *ctx,
183 /* Skip this object if already seen. */
184 if (o->flags & SEEN)
185 continue;
186 - o->flags |= SEEN;
186
187 strbuf_setlen(&path, base_len);
188 strbuf_add(&path, entry.path, entry.pathlen);
@@ -204,7 +203,8 @@ static int add_tree_entries(struct path_walk_context *ctx,
203 ctx->repo->index);
204
205 if (ctx->info->pl->use_cone_patterns &&
207 - match == NOT_MATCHED)
206 + match == NOT_MATCHED &&
207 + (type == OBJ_BLOB || ctx->info->pl_sparse_trees))
208 continue;
209 else if (!ctx->info->pl->use_cone_patterns &&
210 type == OBJ_BLOB &&
@@ -239,6 +239,7 @@ static int add_tree_entries(struct path_walk_context *ctx,
239 continue;
240 }
241
242 + o->flags |= SEEN;
243 add_path_to_list(ctx, path.buf, type, &entry.oid,
244 !(o->flags & UNINTERESTING));
245
path-walk.h
+6
@@ -76,8 +76,14 @@ struct path_walk_info {
76 * of the cone. If not in cone mode, then all tree paths will be
77 * explored but the path_fn will only be called when the path matches
78 * the sparse-checkout patterns.
79 + *
80 + * When 'pl_sparse_trees' is zero, the sparse patterns only restrict
81 + * blobs and all trees are included in the walk output. This matches
82 + * the behavior of the sparse:oid object filter. When nonzero, trees
83 + * are also pruned by the sparse patterns (as used by backfill).
84 */
85 struct pattern_list *pl;
86 + int pl_sparse_trees;
87 };
88
89 #define PATH_WALK_INFO_INIT { \
t/helper/test-path-walk.c
+5 -1
@@ -68,7 +68,7 @@ static int emit_block(const char *path, struct oid_array *oids,
68
69 int cmd__path_walk(int argc, const char **argv)
70 {
71 - int res, stdin_pl = 0;
71 + int res, stdin_pl = 0, pl_sparse_trees = -1;
72 struct rev_info revs = REV_INFO_INIT;
73 struct path_walk_info info = PATH_WALK_INFO_INIT;
74 struct path_walk_test_data data = { 0 };
@@ -89,6 +89,8 @@ int cmd__path_walk(int argc, const char **argv)
89 N_("toggle aggressive edge walk")),
90 OPT_BOOL(0, "stdin-pl", &stdin_pl,
91 N_("read a pattern list over stdin")),
92 + OPT_BOOL(0, "pl-sparse-trees", &pl_sparse_trees,
93 + N_("toggle pruning of trees by sparse patterns")),
94 OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options),
95 OPT_END(),
96 };
@@ -116,6 +118,8 @@ int cmd__path_walk(int argc, const char **argv)
118 if (stdin_pl) {
119 struct strbuf in = STRBUF_INIT;
120 CALLOC_ARRAY(info.pl, 1);
121 + info.pl_sparse_trees = (pl_sparse_trees >= 0) ?
122 + pl_sparse_trees : 1;
123
124 info.pl->use_cone_patterns = 1;
125
t/t6601-path-walk.sh
+37
@@ -206,6 +206,43 @@ test_expect_success 'base & topic, sparse' '
206 test_cmp_sorted expect out
207 '
208
209 +test_expect_success 'base & topic, sparse, no tree pruning' '
210 + cat >patterns <<-EOF &&
211 + /*
212 + !/*/
213 + /left/
214 + EOF
215 +
216 + test-tool path-walk --stdin-pl --no-pl-sparse-trees \
217 + -- base topic <patterns >out &&
218 +
219 + cat >expect <<-EOF &&
220 + 0:commit::$(git rev-parse topic)
221 + 0:commit::$(git rev-parse base)
222 + 0:commit::$(git rev-parse base~1)
223 + 0:commit::$(git rev-parse base~2)
224 + 1:tree::$(git rev-parse topic^{tree})
225 + 1:tree::$(git rev-parse base^{tree})
226 + 1:tree::$(git rev-parse base~1^{tree})
227 + 1:tree::$(git rev-parse base~2^{tree})
228 + 2:blob:a:$(git rev-parse base~2:a)
229 + 3:tree:a/:$(git rev-parse base:a)
230 + 4:tree:left/:$(git rev-parse base:left)
231 + 4:tree:left/:$(git rev-parse base~2:left)
232 + 5:blob:left/b:$(git rev-parse base~2:left/b)
233 + 5:blob:left/b:$(git rev-parse base:left/b)
234 + 6:tree:right/:$(git rev-parse topic:right)
235 + 6:tree:right/:$(git rev-parse base~1:right)
236 + 6:tree:right/:$(git rev-parse base~2:right)
237 + blobs:3
238 + commits:4
239 + tags:0
240 + trees:10
241 + EOF
242 +
243 + test_cmp_sorted expect out
244 +'
245 +
246 test_expect_success 'topic only' '
247 test-tool path-walk -- topic >out &&
248