Skip to content

Commit

Permalink
path-walk: visit tags and cached objects
Browse files Browse the repository at this point in the history
The rev_info that is specified for a path-walk traversal may specify
visiting tag refs (both lightweight and annotated) and also may specify
indexed objects (blobs and trees). Update the path-walk API to walk
these objects as well.

When walking tags, we need to peel the annotated objects until reaching
a non-tag object. If we reach a commit, then we can add it to the
pending objects to make sure we visit in the commit walk portion. If we
reach a tree, then we will assume that it is a root tree. If we reach a
blob, then we have no good path name and so add it to a new list of
"tagged blobs".

When the rev_info includes the "--indexed-objects" flag, then the
pending set includes blobs and trees found in the cache entries and
cache-tree. The cache entries are usually blobs, though they could be
trees in the case of a sparse index. The cache-tree stores
previously-hashed tree objects but these are cleared out when staging
objects below those paths. We add tests that demonstrate this.

The indexed objects come with a non-NULL 'path' value in the pending
item. This allows us to prepopulate the 'path_to_lists' strmap with
lists for these paths.

The tricky thing about this walk is that we will want to combine the
indexed objects walk with the commit walk, especially in the future case
of walking objects during a command like 'git repack'.

Whenever possible, we want the objects from the index to be grouped with
similar objects in history. We don't want to miss any paths that appear
only in the index and not in the commit history.

Thus, we need to be careful to let the path stack be populated initially
with only the root tree path (and possibly tags and tagged blobs) and go
through the normal depth-first search. Afterwards, if there are other
paths that are remaining in the paths_to_lists strmap, we should then
iterate through the stack and visit those objects recursively.

Signed-off-by: Derrick Stolee <stolee@gmail.com>
  • Loading branch information
derrickstolee committed Oct 29, 2024
1 parent f4bf8be commit dfd00b2
Show file tree
Hide file tree
Showing 5 changed files with 336 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Documentation/technical/api-path-walk.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ It is also important that you do not specify the `--objects` flag for the
the objects will be walked in a separate way based on those starting
commits.

`commits`, `blobs`, `trees`::
`commits`, `blobs`, `trees`, `tags`::
By default, these members are enabled and signal that the path-walk
API should call the `path_fn` on objects of these types. Specialized
applications could disable some options to make it simpler to walk
Expand Down
175 changes: 173 additions & 2 deletions path-walk.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@
#include "revision.h"
#include "string-list.h"
#include "strmap.h"
#include "tag.h"
#include "trace2.h"
#include "tree.h"
#include "tree-walk.h"

static const char *root_path = "";

struct type_and_oid_list
{
enum object_type type;
Expand Down Expand Up @@ -158,9 +161,13 @@ static int walk_path(struct path_walk_context *ctx,

list = strmap_get(&ctx->paths_to_lists, path);

if (!list)
BUG("provided path '%s' that had no associated list", path);

/* Evaluate function pointer on this data, if requested. */
if ((list->type == OBJ_TREE && ctx->info->trees) ||
(list->type == OBJ_BLOB && ctx->info->blobs))
(list->type == OBJ_BLOB && ctx->info->blobs)||
(list->type == OBJ_TAG && ctx->info->tags))
ret = ctx->info->path_fn(path, &list->oids, list->type,
ctx->info->path_fn_data);

Expand Down Expand Up @@ -191,6 +198,134 @@ static void clear_strmap(struct strmap *map)
strmap_init(map);
}

static void setup_pending_objects(struct path_walk_info *info,
struct path_walk_context *ctx)
{
struct type_and_oid_list *tags = NULL;
struct type_and_oid_list *tagged_blobs = NULL;
struct type_and_oid_list *root_tree_list = NULL;

if (info->tags)
CALLOC_ARRAY(tags, 1);
if (info->blobs)
CALLOC_ARRAY(tagged_blobs, 1);
if (info->trees)
root_tree_list = strmap_get(&ctx->paths_to_lists, root_path);

/*
* Pending objects include:
* * Commits at branch tips.
* * Annotated tags at tag tips.
* * Any kind of object at lightweight tag tips.
* * Trees and blobs in the index (with an associated path).
*/
for (size_t i = 0; i < info->revs->pending.nr; i++) {
struct object_array_entry *pending = info->revs->pending.objects + i;
struct object *obj = pending->item;

/* Commits will be picked up by revision walk. */
if (obj->type == OBJ_COMMIT)
continue;

/* Navigate annotated tag object chains. */
while (obj->type == OBJ_TAG) {
struct tag *tag = lookup_tag(info->revs->repo, &obj->oid);
if (!tag)
break;
if (tag->object.flags & SEEN)
break;
tag->object.flags |= SEEN;

if (tags)
oid_array_append(&tags->oids, &obj->oid);
obj = tag->tagged;
}

if (obj->type == OBJ_TAG)
continue;

/* We are now at a non-tag object. */
if (obj->flags & SEEN)
continue;
obj->flags |= SEEN;

switch (obj->type) {
case OBJ_TREE:
if (!info->trees)
continue;
if (pending->path) {
struct type_and_oid_list *list;
char *path = *pending->path ? xstrfmt("%s/", pending->path)
: xstrdup("");
if (!(list = strmap_get(&ctx->paths_to_lists, path))) {
CALLOC_ARRAY(list, 1);
list->type = OBJ_TREE;
strmap_put(&ctx->paths_to_lists, path, list);
}
oid_array_append(&list->oids, &obj->oid);
free(path);
} else {
/* assume a root tree, such as a lightweight tag. */
oid_array_append(&root_tree_list->oids, &obj->oid);
}
break;

case OBJ_BLOB:
if (!info->blobs)
continue;
if (pending->path) {
struct type_and_oid_list *list;
char *path = pending->path;
if (!(list = strmap_get(&ctx->paths_to_lists, path))) {
CALLOC_ARRAY(list, 1);
list->type = OBJ_BLOB;
strmap_put(&ctx->paths_to_lists, path, list);
}
oid_array_append(&list->oids, &obj->oid);
} else {
/* assume a root tree, such as a lightweight tag. */
oid_array_append(&tagged_blobs->oids, &obj->oid);
}
break;

case OBJ_COMMIT:
/* Make sure it is in the object walk */
if (obj != pending->item)
add_pending_object(info->revs, obj, "");
break;

default:
BUG("should not see any other type here");
}
}

/*
* Add tag objects and tagged blobs if they exist.
*/
if (tagged_blobs) {
if (tagged_blobs->oids.nr) {
const char *tagged_blob_path = "/tagged-blobs";
tagged_blobs->type = OBJ_BLOB;
push_to_stack(ctx, tagged_blob_path);
strmap_put(&ctx->paths_to_lists, tagged_blob_path, tagged_blobs);
} else {
oid_array_clear(&tagged_blobs->oids);
free(tagged_blobs);
}
}
if (tags) {
if (tags->oids.nr) {
const char *tag_path = "/tags";
tags->type = OBJ_TAG;
push_to_stack(ctx, tag_path);
strmap_put(&ctx->paths_to_lists, tag_path, tags);
} else {
oid_array_clear(&tags->oids);
free(tags);
}
}
}

/**
* Given the configuration of 'info', walk the commits based on 'info->revs' and
* call 'info->path_fn' on each discovered path.
Expand All @@ -199,7 +334,6 @@ static void clear_strmap(struct strmap *map)
*/
int walk_objects_by_path(struct path_walk_info *info)
{
const char *root_path = "";
int ret = 0;
size_t commits_nr = 0, paths_nr = 0;
struct commit *c;
Expand All @@ -219,15 +353,31 @@ int walk_objects_by_path(struct path_walk_info *info)
CALLOC_ARRAY(commit_list, 1);
commit_list->type = OBJ_COMMIT;

if (info->tags)
info->revs->tag_objects = 1;

/* Insert a single list for the root tree into the paths. */
CALLOC_ARRAY(root_tree_list, 1);
root_tree_list->type = OBJ_TREE;
strmap_put(&ctx.paths_to_lists, root_path, root_tree_list);
push_to_stack(&ctx, root_path);

/*
* Set these values before preparing the walk to catch
* lightweight tags pointing to non-commits and indexed objects.
*/
info->revs->blob_objects = info->blobs;
info->revs->tree_objects = info->trees;

if (prepare_revision_walk(info->revs))
die(_("failed to setup revision walk"));

info->revs->blob_objects = info->revs->tree_objects = 0;

trace2_region_enter("path-walk", "pending-walk", info->revs->repo);
setup_pending_objects(info, &ctx);
trace2_region_leave("path-walk", "pending-walk", info->revs->repo);

while ((c = get_revision(info->revs))) {
struct object_id *oid;
struct tree *t;
Expand Down Expand Up @@ -275,6 +425,27 @@ int walk_objects_by_path(struct path_walk_info *info)

free(path);
}

/* Are there paths remaining? Likely they are from indexed objects. */
if (!strmap_empty(&ctx.paths_to_lists)) {
struct hashmap_iter iter;
struct strmap_entry *entry;

strmap_for_each_entry(&ctx.paths_to_lists, &iter, entry) {
push_to_stack(&ctx, entry->key);
}

while (!ret && ctx.path_stack.nr) {
char *path = ctx.path_stack.items[ctx.path_stack.nr - 1].string;
ctx.path_stack.nr--;
paths_nr++;

ret = walk_path(&ctx, path);

free(path);
}
}

trace2_data_intmax("path-walk", ctx.repo, "paths", paths_nr);
trace2_region_leave("path-walk", "path-walk", info->revs->repo);

Expand Down
2 changes: 2 additions & 0 deletions path-walk.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,14 @@ struct path_walk_info {
int commits;
int trees;
int blobs;
int tags;
};

#define PATH_WALK_INFO_INIT { \
.blobs = 1, \
.trees = 1, \
.commits = 1, \
.tags = 1, \
}

/**
Expand Down
13 changes: 11 additions & 2 deletions t/helper/test-path-walk.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ struct path_walk_test_data {
uintmax_t commit_nr;
uintmax_t tree_nr;
uintmax_t blob_nr;
uintmax_t tag_nr;
};

static int emit_block(const char *path, struct oid_array *oids,
Expand All @@ -45,6 +46,11 @@ static int emit_block(const char *path, struct oid_array *oids,
tdata->blob_nr += oids->nr;
break;

case OBJ_TAG:
typestr = "TAG";
tdata->tag_nr += oids->nr;
break;

default:
BUG("we do not understand this type");
}
Expand All @@ -66,6 +72,8 @@ int cmd__path_walk(int argc, const char **argv)
N_("toggle inclusion of blob objects")),
OPT_BOOL(0, "commits", &info.commits,
N_("toggle inclusion of commit objects")),
OPT_BOOL(0, "tags", &info.tags,
N_("toggle inclusion of tag objects")),
OPT_BOOL(0, "trees", &info.trees,
N_("toggle inclusion of tree objects")),
OPT_END(),
Expand All @@ -92,8 +100,9 @@ int cmd__path_walk(int argc, const char **argv)

printf("commits:%" PRIuMAX "\n"
"trees:%" PRIuMAX "\n"
"blobs:%" PRIuMAX "\n",
data.commit_nr, data.tree_nr, data.blob_nr);
"blobs:%" PRIuMAX "\n"
"tags:%" PRIuMAX "\n",
data.commit_nr, data.tree_nr, data.blob_nr, data.tag_nr);

return res;
}
Loading

0 comments on commit dfd00b2

Please sign in to comment.