]> git.ipfire.org Git - thirdparty/git.git/commitdiff
path-walk: add pl_sparse_trees to control tree pruning
authorDerrick Stolee <stolee@gmail.com>
Fri, 22 May 2026 18:24:32 +0000 (18:24 +0000)
committerJunio C Hamano <gitster@pobox.com>
Sun, 24 May 2026 09:41:06 +0000 (18:41 +0900)
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>
builtin/backfill.c
path-walk.c
path-walk.h
t/helper/test-path-walk.c
t/t6601-path-walk.sh

index 5254a427111bbd7e5a30a2eb6af51abc81456714..e71e0f4742c506c10ab6820d29658ae166ab229b 100644 (file)
@@ -109,6 +109,7 @@ static int do_backfill(struct backfill_context *ctx)
 
        if (ctx->sparse) {
                CALLOC_ARRAY(info.pl, 1);
+               info.pl_sparse_trees = 1;
                if (get_sparse_checkout_patterns(info.pl)) {
                        path_walk_info_clear(&info);
                        return error(_("problem loading sparse-checkout"));
index 04b924d4deccad7cc39393df4ca8f6d5216fa2ce..225857bbc8dde600cbae9af2179d81d2b9e60c91 100644 (file)
@@ -183,7 +183,6 @@ static int add_tree_entries(struct path_walk_context *ctx,
                /* Skip this object if already seen. */
                if (o->flags & SEEN)
                        continue;
-               o->flags |= SEEN;
 
                strbuf_setlen(&path, base_len);
                strbuf_add(&path, entry.path, entry.pathlen);
@@ -204,7 +203,8 @@ static int add_tree_entries(struct path_walk_context *ctx,
                                                          ctx->repo->index);
 
                        if (ctx->info->pl->use_cone_patterns &&
-                           match == NOT_MATCHED)
+                           match == NOT_MATCHED &&
+                           (type == OBJ_BLOB || ctx->info->pl_sparse_trees))
                                continue;
                        else if (!ctx->info->pl->use_cone_patterns &&
                                 type == OBJ_BLOB &&
@@ -239,6 +239,7 @@ static int add_tree_entries(struct path_walk_context *ctx,
                                continue;
                }
 
+               o->flags |= SEEN;
                add_path_to_list(ctx, path.buf, type, &entry.oid,
                                 !(o->flags & UNINTERESTING));
 
index 60ceb6543389c31e1670f97d2f22c77dae551a40..7e57ae5f65dd9864d927db6480e4e7329c5ee8ca 100644 (file)
@@ -76,8 +76,14 @@ struct path_walk_info {
         * of the cone. If not in cone mode, then all tree paths will be
         * explored but the path_fn will only be called when the path matches
         * the sparse-checkout patterns.
+        *
+        * When 'pl_sparse_trees' is zero, the sparse patterns only restrict
+        * blobs and all trees are included in the walk output. This matches
+        * the behavior of the sparse:oid object filter. When nonzero, trees
+        * are also pruned by the sparse patterns (as used by backfill).
         */
        struct pattern_list *pl;
+       int pl_sparse_trees;
 };
 
 #define PATH_WALK_INFO_INIT {   \
index 88f86ae0dc115705bcc6dca28f98711d90c49c87..3f2b50a9aa16bd469be4f18a6b1970484e34981c 100644 (file)
@@ -68,7 +68,7 @@ static int emit_block(const char *path, struct oid_array *oids,
 
 int cmd__path_walk(int argc, const char **argv)
 {
-       int res, stdin_pl = 0;
+       int res, stdin_pl = 0, pl_sparse_trees = -1;
        struct rev_info revs = REV_INFO_INIT;
        struct path_walk_info info = PATH_WALK_INFO_INIT;
        struct path_walk_test_data data = { 0 };
@@ -89,6 +89,8 @@ int cmd__path_walk(int argc, const char **argv)
                         N_("toggle aggressive edge walk")),
                OPT_BOOL(0, "stdin-pl", &stdin_pl,
                         N_("read a pattern list over stdin")),
+               OPT_BOOL(0, "pl-sparse-trees", &pl_sparse_trees,
+                        N_("toggle pruning of trees by sparse patterns")),
                OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options),
                OPT_END(),
        };
@@ -116,6 +118,8 @@ int cmd__path_walk(int argc, const char **argv)
        if (stdin_pl) {
                struct strbuf in = STRBUF_INIT;
                CALLOC_ARRAY(info.pl, 1);
+               info.pl_sparse_trees = (pl_sparse_trees >= 0) ?
+                       pl_sparse_trees : 1;
 
                info.pl->use_cone_patterns = 1;
 
index 45f366d738efaccf548c1db3db298577e6028c37..02ad83dfb0368e5f847d2e47c5d1c6d3255fed2e 100755 (executable)
@@ -206,6 +206,43 @@ test_expect_success 'base & topic, sparse' '
        test_cmp_sorted expect out
 '
 
+test_expect_success 'base & topic, sparse, no tree pruning' '
+       cat >patterns <<-EOF &&
+       /*
+       !/*/
+       /left/
+       EOF
+
+       test-tool path-walk --stdin-pl --no-pl-sparse-trees \
+               -- base topic <patterns >out &&
+
+       cat >expect <<-EOF &&
+       0:commit::$(git rev-parse topic)
+       0:commit::$(git rev-parse base)
+       0:commit::$(git rev-parse base~1)
+       0:commit::$(git rev-parse base~2)
+       1:tree::$(git rev-parse topic^{tree})
+       1:tree::$(git rev-parse base^{tree})
+       1:tree::$(git rev-parse base~1^{tree})
+       1:tree::$(git rev-parse base~2^{tree})
+       2:blob:a:$(git rev-parse base~2:a)
+       3:tree:a/:$(git rev-parse base:a)
+       4:tree:left/:$(git rev-parse base:left)
+       4:tree:left/:$(git rev-parse base~2:left)
+       5:blob:left/b:$(git rev-parse base~2:left/b)
+       5:blob:left/b:$(git rev-parse base:left/b)
+       6:tree:right/:$(git rev-parse topic:right)
+       6:tree:right/:$(git rev-parse base~1:right)
+       6:tree:right/:$(git rev-parse base~2:right)
+       blobs:3
+       commits:4
+       tags:0
+       trees:10
+       EOF
+
+       test_cmp_sorted expect out
+'
+
 test_expect_success 'topic only' '
        test-tool path-walk -- topic >out &&