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 <stolee@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:

committed by
Junio C Hamano

parent
d190124f27
commit
c8dba310d7
33
path-walk.c
33
path-walk.c
@ -98,6 +98,10 @@ static int add_tree_entries(struct path_walk_context *ctx,
|
||||
if (S_ISGITLINK(entry.mode))
|
||||
continue;
|
||||
|
||||
/* If the caller doesn't want blobs, then don't bother. */
|
||||
if (!ctx->info->blobs && type == OBJ_BLOB)
|
||||
continue;
|
||||
|
||||
if (type == OBJ_TREE) {
|
||||
struct tree *child = lookup_tree(ctx->repo, &entry.oid);
|
||||
o = child ? &child->object : NULL;
|
||||
@ -159,9 +163,11 @@ static int walk_path(struct path_walk_context *ctx,
|
||||
if (!list->oids.nr)
|
||||
return 0;
|
||||
|
||||
/* Evaluate function pointer on this data. */
|
||||
ret = ctx->info->path_fn(path, &list->oids, list->type,
|
||||
ctx->info->path_fn_data);
|
||||
/* Evaluate function pointer on this data, if requested. */
|
||||
if ((list->type == OBJ_TREE && ctx->info->trees) ||
|
||||
(list->type == OBJ_BLOB && ctx->info->blobs))
|
||||
ret = ctx->info->path_fn(path, &list->oids, list->type,
|
||||
ctx->info->path_fn_data);
|
||||
|
||||
/* Expand data for children. */
|
||||
if (list->type == OBJ_TREE) {
|
||||
@ -203,6 +209,7 @@ int walk_objects_by_path(struct path_walk_info *info)
|
||||
size_t commits_nr = 0, paths_nr = 0;
|
||||
struct commit *c;
|
||||
struct type_and_oid_list *root_tree_list;
|
||||
struct type_and_oid_list *commit_list;
|
||||
struct path_walk_context ctx = {
|
||||
.repo = info->revs->repo,
|
||||
.revs = info->revs,
|
||||
@ -214,6 +221,9 @@ int walk_objects_by_path(struct path_walk_info *info)
|
||||
|
||||
trace2_region_enter("path-walk", "commit-walk", info->revs->repo);
|
||||
|
||||
CALLOC_ARRAY(commit_list, 1);
|
||||
commit_list->type = OBJ_COMMIT;
|
||||
|
||||
/* Insert a single list for the root tree into the paths. */
|
||||
CALLOC_ARRAY(root_tree_list, 1);
|
||||
root_tree_list->type = OBJ_TREE;
|
||||
@ -224,10 +234,18 @@ int walk_objects_by_path(struct path_walk_info *info)
|
||||
die(_("failed to setup revision walk"));
|
||||
|
||||
while ((c = get_revision(info->revs))) {
|
||||
struct object_id *oid = get_commit_tree_oid(c);
|
||||
struct object_id *oid;
|
||||
struct tree *t;
|
||||
commits_nr++;
|
||||
|
||||
if (info->commits)
|
||||
oid_array_append(&commit_list->oids,
|
||||
&c->object.oid);
|
||||
|
||||
/* If we only care about commits, then skip trees. */
|
||||
if (!info->trees && !info->blobs)
|
||||
continue;
|
||||
|
||||
oid = get_commit_tree_oid(c);
|
||||
t = lookup_tree(info->revs->repo, oid);
|
||||
|
||||
@ -245,6 +263,13 @@ int walk_objects_by_path(struct path_walk_info *info)
|
||||
trace2_data_intmax("path-walk", ctx.repo, "commits", commits_nr);
|
||||
trace2_region_leave("path-walk", "commit-walk", info->revs->repo);
|
||||
|
||||
/* Track all commits. */
|
||||
if (info->commits && commit_list->oids.nr)
|
||||
ret = info->path_fn("", &commit_list->oids, OBJ_COMMIT,
|
||||
info->path_fn_data);
|
||||
oid_array_clear(&commit_list->oids);
|
||||
free(commit_list);
|
||||
|
||||
trace2_region_enter("path-walk", "path-walk", info->revs->repo);
|
||||
while (!ret && ctx.path_stack.nr) {
|
||||
char *path = ctx.path_stack.items[ctx.path_stack.nr - 1].string;
|
||||
|
Reference in New Issue
Block a user