Skip to content

Commit c5aca53

Browse files
committed
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>
1 parent 2e78616 commit c5aca53

5 files changed

Lines changed: 52 additions & 3 deletions

File tree

builtin/backfill.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ static int do_backfill(struct backfill_context *ctx)
8585

8686
if (ctx->sparse) {
8787
CALLOC_ARRAY(info.pl, 1);
88+
info.pl_sparse_trees = 1;
8889
if (get_sparse_checkout_patterns(info.pl)) {
8990
path_walk_info_clear(&info);
9091
return error(_("problem loading sparse-checkout"));

path-walk.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,6 @@ static int add_tree_entries(struct path_walk_context *ctx,
183183
/* Skip this object if already seen. */
184184
if (o->flags & SEEN)
185185
continue;
186-
o->flags |= SEEN;
187186

188187
strbuf_setlen(&path, base_len);
189188
strbuf_add(&path, entry.path, entry.pathlen);
@@ -204,7 +203,8 @@ static int add_tree_entries(struct path_walk_context *ctx,
204203
ctx->repo->index);
205204

206205
if (ctx->info->pl->use_cone_patterns &&
207-
match == NOT_MATCHED)
206+
match == NOT_MATCHED &&
207+
(type == OBJ_BLOB || ctx->info->pl_sparse_trees))
208208
continue;
209209
else if (!ctx->info->pl->use_cone_patterns &&
210210
type == OBJ_BLOB &&
@@ -239,6 +239,7 @@ static int add_tree_entries(struct path_walk_context *ctx,
239239
continue;
240240
}
241241

242+
o->flags |= SEEN;
242243
add_path_to_list(ctx, path.buf, type, &entry.oid,
243244
!(o->flags & UNINTERESTING));
244245

path-walk.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,14 @@ struct path_walk_info {
7272
* of the cone. If not in cone mode, then all tree paths will be
7373
* explored but the path_fn will only be called when the path matches
7474
* the sparse-checkout patterns.
75+
*
76+
* When 'pl_sparse_trees' is zero, the sparse patterns only restrict
77+
* blobs and all trees are included in the walk output. This matches
78+
* the behavior of the sparse:oid object filter. When nonzero, trees
79+
* are also pruned by the sparse patterns (as used by backfill).
7580
*/
7681
struct pattern_list *pl;
82+
int pl_sparse_trees;
7783
};
7884

7985
#define PATH_WALK_INFO_INIT { \

t/helper/test-path-walk.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ static int emit_block(const char *path, struct oid_array *oids,
6868

6969
int cmd__path_walk(int argc, const char **argv)
7070
{
71-
int res, stdin_pl = 0;
71+
int res, stdin_pl = 0, pl_sparse_trees = -1;
7272
struct rev_info revs = REV_INFO_INIT;
7373
struct path_walk_info info = PATH_WALK_INFO_INIT;
7474
struct path_walk_test_data data = { 0 };
@@ -89,6 +89,8 @@ int cmd__path_walk(int argc, const char **argv)
8989
N_("toggle aggressive edge walk")),
9090
OPT_BOOL(0, "stdin-pl", &stdin_pl,
9191
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")),
9294
OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options),
9395
OPT_END(),
9496
};
@@ -116,6 +118,8 @@ int cmd__path_walk(int argc, const char **argv)
116118
if (stdin_pl) {
117119
struct strbuf in = STRBUF_INIT;
118120
CALLOC_ARRAY(info.pl, 1);
121+
info.pl_sparse_trees = (pl_sparse_trees >= 0) ?
122+
pl_sparse_trees : 1;
119123

120124
info.pl->use_cone_patterns = 1;
121125

t/t6601-path-walk.sh

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,43 @@ test_expect_success 'base & topic, sparse' '
206206
test_cmp_sorted expect out
207207
'
208208

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+
209246
test_expect_success 'topic only' '
210247
test-tool path-walk -- topic >out &&
211248

0 commit comments

Comments
 (0)