Skip to content

Commit 42e71e6

Browse files
committed
path-walk: allow consumer to specify object types
We add the ability to filter the object types in the path-walk API so the callback function is called fewer times. This adds the ability to ask for the commits in a list, as well. We re-use the empty string for this set of objects because these are passed directly to the callback function instead of being part of the 'path_stack'. Future changes will add the ability to visit annotated tags. Signed-off-by: Derrick Stolee <[email protected]>
1 parent 54886fc commit 42e71e6

File tree

5 files changed

+170
-50
lines changed

5 files changed

+170
-50
lines changed

Documentation/technical/api-path-walk.txt

+9
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,15 @@ It is also important that you do not specify the `--objects` flag for the
3939
the objects will be walked in a separate way based on those starting
4040
commits.
4141

42+
`commits`, `blobs`, `trees`::
43+
By default, these members are enabled and signal that the path-walk
44+
API should call the `path_fn` on objects of these types. Specialized
45+
applications could disable some options to make it simpler to walk
46+
the objects or to have fewer calls to `path_fn`.
47+
+
48+
While it is possible to walk only commits in this way, consumers would be
49+
better off using the revision walk API instead.
50+
4251
Examples
4352
--------
4453

path-walk.c

+29-4
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,10 @@ static int add_children(struct path_walk_context *ctx,
9898
if (S_ISGITLINK(entry.mode))
9999
continue;
100100

101+
/* If the caller doesn't want blobs, then don't bother. */
102+
if (!ctx->info->blobs && type == OBJ_BLOB)
103+
continue;
104+
101105
if (type == OBJ_TREE) {
102106
struct tree *child = lookup_tree(ctx->repo, &entry.oid);
103107
o = child ? &child->object : NULL;
@@ -157,9 +161,11 @@ static int walk_path(struct path_walk_context *ctx,
157161
if (!list->oids.nr)
158162
return 0;
159163

160-
/* Evaluate function pointer on this data. */
161-
ret = ctx->info->path_fn(path, &list->oids, list->type,
162-
ctx->info->path_fn_data);
164+
/* Evaluate function pointer on this data, if requested. */
165+
if ((list->type == OBJ_TREE && ctx->info->trees) ||
166+
(list->type == OBJ_BLOB && ctx->info->blobs))
167+
ret = ctx->info->path_fn(path, &list->oids, list->type,
168+
ctx->info->path_fn_data);
163169

164170
/* Expand data for children. */
165171
if (list->type == OBJ_TREE) {
@@ -201,6 +207,7 @@ int walk_objects_by_path(struct path_walk_info *info)
201207
size_t commits_nr = 0, paths_nr = 0;
202208
struct commit *c;
203209
struct type_and_oid_list *root_tree_list;
210+
struct type_and_oid_list *commit_list;
204211
struct path_walk_context ctx = {
205212
.repo = info->revs->repo,
206213
.revs = info->revs,
@@ -212,6 +219,9 @@ int walk_objects_by_path(struct path_walk_info *info)
212219

213220
trace2_region_enter("path-walk", "commit-walk", info->revs->repo);
214221

222+
CALLOC_ARRAY(commit_list, 1);
223+
commit_list->type = OBJ_COMMIT;
224+
215225
/* Insert a single list for the root tree into the paths. */
216226
CALLOC_ARRAY(root_tree_list, 1);
217227
root_tree_list->type = OBJ_TREE;
@@ -222,10 +232,18 @@ int walk_objects_by_path(struct path_walk_info *info)
222232
die(_("failed to setup revision walk"));
223233

224234
while ((c = get_revision(info->revs))) {
225-
struct object_id *oid = get_commit_tree_oid(c);
235+
struct object_id *oid;
226236
struct tree *t;
227237
commits_nr++;
228238

239+
if (info->commits)
240+
oid_array_append(&commit_list->oids,
241+
&c->object.oid);
242+
243+
/* If we only care about commits, then skip trees. */
244+
if (!info->trees && !info->blobs)
245+
continue;
246+
229247
oid = get_commit_tree_oid(c);
230248
t = lookup_tree(info->revs->repo, oid);
231249

@@ -243,6 +261,13 @@ int walk_objects_by_path(struct path_walk_info *info)
243261
trace2_data_intmax("path-walk", ctx.repo, "commits", commits_nr);
244262
trace2_region_leave("path-walk", "commit-walk", info->revs->repo);
245263

264+
/* Track all commits. */
265+
if (info->commits && commit_list->oids.nr)
266+
ret = info->path_fn("", &commit_list->oids, OBJ_COMMIT,
267+
info->path_fn_data);
268+
oid_array_clear(&commit_list->oids);
269+
free(commit_list);
270+
246271
trace2_region_enter("path-walk", "path-walk", info->revs->repo);
247272
while (!ret && ctx.path_stack.nr) {
248273
char *path = ctx.path_stack.items[ctx.path_stack.nr - 1].string;

path-walk.h

+13-1
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,21 @@ struct path_walk_info {
3030
*/
3131
path_fn path_fn;
3232
void *path_fn_data;
33+
34+
/**
35+
* Initialize which object types the path_fn should be called on. This
36+
* could also limit the walk to skip blobs if not set.
37+
*/
38+
int commits;
39+
int trees;
40+
int blobs;
3341
};
3442

35-
#define PATH_WALK_INFO_INIT { 0 }
43+
#define PATH_WALK_INFO_INIT { \
44+
.blobs = 1, \
45+
.trees = 1, \
46+
.commits = 1, \
47+
}
3648

3749
/**
3850
* Given the configuration of 'info', walk the commits based on 'info->revs' and

t/helper/test-path-walk.c

+13-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ static const char * const path_walk_usage[] = {
1919

2020
struct path_walk_test_data {
2121
uintmax_t batch_nr;
22+
23+
uintmax_t commit_nr;
2224
uintmax_t tree_nr;
2325
uintmax_t blob_nr;
2426
};
@@ -33,6 +35,8 @@ static int emit_block(const char *path, struct oid_array *oids,
3335
tdata->tree_nr += oids->nr;
3436
else if (type == OBJ_BLOB)
3537
tdata->blob_nr += oids->nr;
38+
else if (type == OBJ_COMMIT)
39+
tdata->commit_nr += oids->nr;
3640
else
3741
BUG("we do not understand this type");
3842

@@ -54,6 +58,12 @@ int cmd__path_walk(int argc, const char **argv)
5458
struct path_walk_info info = PATH_WALK_INFO_INIT;
5559
struct path_walk_test_data data = { 0 };
5660
struct option options[] = {
61+
OPT_BOOL(0, "blobs", &info.blobs,
62+
N_("toggle inclusion of blob objects")),
63+
OPT_BOOL(0, "commits", &info.commits,
64+
N_("toggle inclusion of commit objects")),
65+
OPT_BOOL(0, "trees", &info.trees,
66+
N_("toggle inclusion of tree objects")),
5767
OPT_END(),
5868
};
5969

@@ -75,9 +85,10 @@ int cmd__path_walk(int argc, const char **argv)
7585

7686
res = walk_objects_by_path(&info);
7787

78-
printf("trees:%" PRIuMAX "\n"
88+
printf("commits:%" PRIuMAX "\n"
89+
"trees:%" PRIuMAX "\n"
7990
"blobs:%" PRIuMAX "\n",
80-
data.tree_nr, data.blob_nr);
91+
data.commit_nr, data.tree_nr, data.blob_nr);
8192

8293
release_revisions(&revs);
8394
return res;

t/t6601-path-walk.sh

+106-43
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,27 @@ test_expect_success 'all' '
3333
test-tool path-walk -- --all >out &&
3434
3535
cat >expect <<-EOF &&
36-
0:tree::$(git rev-parse topic^{tree})
37-
0:tree::$(git rev-parse base^{tree})
38-
0:tree::$(git rev-parse base~1^{tree})
39-
0:tree::$(git rev-parse base~2^{tree})
40-
1:tree:right/:$(git rev-parse topic:right)
41-
1:tree:right/:$(git rev-parse base~1:right)
42-
1:tree:right/:$(git rev-parse base~2:right)
43-
2:blob:right/d:$(git rev-parse base~1:right/d)
44-
3:blob:right/c:$(git rev-parse base~2:right/c)
45-
3:blob:right/c:$(git rev-parse topic:right/c)
46-
4:tree:left/:$(git rev-parse base:left)
47-
4:tree:left/:$(git rev-parse base~2:left)
48-
5:blob:left/b:$(git rev-parse base~2:left/b)
49-
5:blob:left/b:$(git rev-parse base:left/b)
50-
6:blob:a:$(git rev-parse base~2:a)
36+
0:commit::$(git rev-parse topic)
37+
0:commit::$(git rev-parse base)
38+
0:commit::$(git rev-parse base~1)
39+
0:commit::$(git rev-parse base~2)
40+
1:tree::$(git rev-parse topic^{tree})
41+
1:tree::$(git rev-parse base^{tree})
42+
1:tree::$(git rev-parse base~1^{tree})
43+
1:tree::$(git rev-parse base~2^{tree})
44+
2:tree:right/:$(git rev-parse topic:right)
45+
2:tree:right/:$(git rev-parse base~1:right)
46+
2:tree:right/:$(git rev-parse base~2:right)
47+
3:blob:right/d:$(git rev-parse base~1:right/d)
48+
4:blob:right/c:$(git rev-parse base~2:right/c)
49+
4:blob:right/c:$(git rev-parse topic:right/c)
50+
5:tree:left/:$(git rev-parse base:left)
51+
5:tree:left/:$(git rev-parse base~2:left)
52+
6:blob:left/b:$(git rev-parse base~2:left/b)
53+
6:blob:left/b:$(git rev-parse base:left/b)
54+
7:blob:a:$(git rev-parse base~2:a)
5155
blobs:6
56+
commits:4
5257
trees:9
5358
EOF
5459
@@ -59,19 +64,23 @@ test_expect_success 'topic only' '
5964
test-tool path-walk -- topic >out &&
6065
6166
cat >expect <<-EOF &&
62-
0:tree::$(git rev-parse topic^{tree})
63-
0:tree::$(git rev-parse base~1^{tree})
64-
0:tree::$(git rev-parse base~2^{tree})
65-
1:tree:right/:$(git rev-parse topic:right)
66-
1:tree:right/:$(git rev-parse base~1:right)
67-
1:tree:right/:$(git rev-parse base~2:right)
68-
2:blob:right/d:$(git rev-parse base~1:right/d)
69-
3:blob:right/c:$(git rev-parse base~2:right/c)
70-
3:blob:right/c:$(git rev-parse topic:right/c)
71-
4:tree:left/:$(git rev-parse base~2:left)
72-
5:blob:left/b:$(git rev-parse base~2:left/b)
73-
6:blob:a:$(git rev-parse base~2:a)
67+
0:commit::$(git rev-parse topic)
68+
0:commit::$(git rev-parse base~1)
69+
0:commit::$(git rev-parse base~2)
70+
1:tree::$(git rev-parse topic^{tree})
71+
1:tree::$(git rev-parse base~1^{tree})
72+
1:tree::$(git rev-parse base~2^{tree})
73+
2:tree:right/:$(git rev-parse topic:right)
74+
2:tree:right/:$(git rev-parse base~1:right)
75+
2:tree:right/:$(git rev-parse base~2:right)
76+
3:blob:right/d:$(git rev-parse base~1:right/d)
77+
4:blob:right/c:$(git rev-parse base~2:right/c)
78+
4:blob:right/c:$(git rev-parse topic:right/c)
79+
5:tree:left/:$(git rev-parse base~2:left)
80+
6:blob:left/b:$(git rev-parse base~2:left/b)
81+
7:blob:a:$(git rev-parse base~2:a)
7482
blobs:5
83+
commits:3
7584
trees:7
7685
EOF
7786
@@ -82,15 +91,66 @@ test_expect_success 'topic, not base' '
8291
test-tool path-walk -- topic --not base >out &&
8392
8493
cat >expect <<-EOF &&
94+
0:commit::$(git rev-parse topic)
95+
1:tree::$(git rev-parse topic^{tree})
96+
2:tree:right/:$(git rev-parse topic:right)
97+
3:blob:right/d:$(git rev-parse topic:right/d)
98+
4:blob:right/c:$(git rev-parse topic:right/c)
99+
5:tree:left/:$(git rev-parse topic:left)
100+
6:blob:left/b:$(git rev-parse topic:left/b)
101+
7:blob:a:$(git rev-parse topic:a)
102+
blobs:4
103+
commits:1
104+
trees:3
105+
EOF
106+
107+
test_cmp_sorted expect out
108+
'
109+
110+
test_expect_success 'topic, not base, only blobs' '
111+
test-tool path-walk --no-trees --no-commits \
112+
-- topic --not base >out &&
113+
114+
cat >expect <<-EOF &&
115+
commits:0
116+
trees:0
117+
0:blob:right/d:$(git rev-parse topic:right/d)
118+
1:blob:right/c:$(git rev-parse topic:right/c)
119+
2:blob:left/b:$(git rev-parse topic:left/b)
120+
3:blob:a:$(git rev-parse topic:a)
121+
blobs:4
122+
EOF
123+
124+
test_cmp_sorted expect out
125+
'
126+
127+
# No, this doesn't make a lot of sense for the path-walk API,
128+
# but it is possible to do.
129+
test_expect_success 'topic, not base, only commits' '
130+
test-tool path-walk --no-blobs --no-trees \
131+
-- topic --not base >out &&
132+
133+
cat >expect <<-EOF &&
134+
0:commit::$(git rev-parse topic)
135+
commits:1
136+
trees:0
137+
blobs:0
138+
EOF
139+
140+
test_cmp_sorted expect out
141+
'
142+
143+
test_expect_success 'topic, not base, only trees' '
144+
test-tool path-walk --no-blobs --no-commits \
145+
-- topic --not base >out &&
146+
147+
cat >expect <<-EOF &&
148+
commits:0
85149
0:tree::$(git rev-parse topic^{tree})
86150
1:tree:right/:$(git rev-parse topic:right)
87-
2:blob:right/d:$(git rev-parse topic:right/d)
88-
3:blob:right/c:$(git rev-parse topic:right/c)
89-
4:tree:left/:$(git rev-parse topic:left)
90-
5:blob:left/b:$(git rev-parse topic:left/b)
91-
6:blob:a:$(git rev-parse topic:a)
92-
blobs:4
151+
2:tree:left/:$(git rev-parse topic:left)
93152
trees:3
153+
blobs:0
94154
EOF
95155
96156
test_cmp_sorted expect out
@@ -100,17 +160,20 @@ test_expect_success 'topic, not base, boundary' '
100160
test-tool path-walk -- --boundary topic --not base >out &&
101161
102162
cat >expect <<-EOF &&
103-
0:tree::$(git rev-parse topic^{tree})
104-
0:tree::$(git rev-parse base~1^{tree})
105-
1:tree:right/:$(git rev-parse topic:right)
106-
1:tree:right/:$(git rev-parse base~1:right)
107-
2:blob:right/d:$(git rev-parse base~1:right/d)
108-
3:blob:right/c:$(git rev-parse base~1:right/c)
109-
3:blob:right/c:$(git rev-parse topic:right/c)
110-
4:tree:left/:$(git rev-parse base~1:left)
111-
5:blob:left/b:$(git rev-parse base~1:left/b)
112-
6:blob:a:$(git rev-parse base~1:a)
163+
0:commit::$(git rev-parse topic)
164+
0:commit::$(git rev-parse base~1)
165+
1:tree::$(git rev-parse topic^{tree})
166+
1:tree::$(git rev-parse base~1^{tree})
167+
2:tree:right/:$(git rev-parse topic:right)
168+
2:tree:right/:$(git rev-parse base~1:right)
169+
3:blob:right/d:$(git rev-parse base~1:right/d)
170+
4:blob:right/c:$(git rev-parse base~1:right/c)
171+
4:blob:right/c:$(git rev-parse topic:right/c)
172+
5:tree:left/:$(git rev-parse base~1:left)
173+
6:blob:left/b:$(git rev-parse base~1:left/b)
174+
7:blob:a:$(git rev-parse base~1:a)
113175
blobs:5
176+
commits:2
114177
trees:5
115178
EOF
116179

0 commit comments

Comments
 (0)