Skip to content

Commit a41f53f

Browse files
committed
path-walk: visit tags and cached objects
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 <[email protected]>
1 parent 42e71e6 commit a41f53f

File tree

5 files changed

+353
-26
lines changed

5 files changed

+353
-26
lines changed

Documentation/technical/api-path-walk.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ 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`::
42+
`commits`, `blobs`, `trees`, `tags`::
4343
By default, these members are enabled and signal that the path-walk
4444
API should call the `path_fn` on objects of these types. Specialized
4545
applications could disable some options to make it simpler to walk

path-walk.c

+172-2
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,13 @@
1313
#include "revision.h"
1414
#include "string-list.h"
1515
#include "strmap.h"
16+
#include "tag.h"
1617
#include "trace2.h"
1718
#include "tree.h"
1819
#include "tree-walk.h"
1920

21+
static const char *root_path = "";
22+
2023
struct type_and_oid_list
2124
{
2225
enum object_type type;
@@ -158,12 +161,16 @@ static int walk_path(struct path_walk_context *ctx,
158161

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

164+
if (!list)
165+
BUG("provided path '%s' that had no associated list", path);
166+
161167
if (!list->oids.nr)
162168
return 0;
163169

164170
/* Evaluate function pointer on this data, if requested. */
165171
if ((list->type == OBJ_TREE && ctx->info->trees) ||
166-
(list->type == OBJ_BLOB && ctx->info->blobs))
172+
(list->type == OBJ_BLOB && ctx->info->blobs) ||
173+
(list->type == OBJ_TAG && ctx->info->tags))
167174
ret = ctx->info->path_fn(path, &list->oids, list->type,
168175
ctx->info->path_fn_data);
169176

@@ -194,6 +201,134 @@ static void clear_strmap(struct strmap *map)
194201
strmap_init(map);
195202
}
196203

204+
static void setup_pending_objects(struct path_walk_info *info,
205+
struct path_walk_context *ctx)
206+
{
207+
struct type_and_oid_list *tags = NULL;
208+
struct type_and_oid_list *tagged_blobs = NULL;
209+
struct type_and_oid_list *root_tree_list = NULL;
210+
211+
if (info->tags)
212+
CALLOC_ARRAY(tags, 1);
213+
if (info->blobs)
214+
CALLOC_ARRAY(tagged_blobs, 1);
215+
if (info->trees)
216+
root_tree_list = strmap_get(&ctx->paths_to_lists, root_path);
217+
218+
/*
219+
* Pending objects include:
220+
* * Commits at branch tips.
221+
* * Annotated tags at tag tips.
222+
* * Any kind of object at lightweight tag tips.
223+
* * Trees and blobs in the index (with an associated path).
224+
*/
225+
for (size_t i = 0; i < info->revs->pending.nr; i++) {
226+
struct object_array_entry *pending = info->revs->pending.objects + i;
227+
struct object *obj = pending->item;
228+
229+
/* Commits will be picked up by revision walk. */
230+
if (obj->type == OBJ_COMMIT)
231+
continue;
232+
233+
/* Navigate annotated tag object chains. */
234+
while (obj->type == OBJ_TAG) {
235+
struct tag *tag = lookup_tag(info->revs->repo, &obj->oid);
236+
if (!tag)
237+
break;
238+
if (tag->object.flags & SEEN)
239+
break;
240+
tag->object.flags |= SEEN;
241+
242+
if (tags)
243+
oid_array_append(&tags->oids, &obj->oid);
244+
obj = tag->tagged;
245+
}
246+
247+
if (obj->type == OBJ_TAG)
248+
continue;
249+
250+
/* We are now at a non-tag object. */
251+
if (obj->flags & SEEN)
252+
continue;
253+
obj->flags |= SEEN;
254+
255+
switch (obj->type) {
256+
case OBJ_TREE:
257+
if (!info->trees)
258+
continue;
259+
if (pending->path) {
260+
struct type_and_oid_list *list;
261+
char *path = *pending->path ? xstrfmt("%s/", pending->path)
262+
: xstrdup("");
263+
if (!(list = strmap_get(&ctx->paths_to_lists, path))) {
264+
CALLOC_ARRAY(list, 1);
265+
list->type = OBJ_TREE;
266+
strmap_put(&ctx->paths_to_lists, path, list);
267+
}
268+
oid_array_append(&list->oids, &obj->oid);
269+
free(path);
270+
} else {
271+
/* assume a root tree, such as a lightweight tag. */
272+
oid_array_append(&root_tree_list->oids, &obj->oid);
273+
}
274+
break;
275+
276+
case OBJ_BLOB:
277+
if (!info->blobs)
278+
continue;
279+
if (pending->path) {
280+
struct type_and_oid_list *list;
281+
char *path = pending->path;
282+
if (!(list = strmap_get(&ctx->paths_to_lists, path))) {
283+
CALLOC_ARRAY(list, 1);
284+
list->type = OBJ_BLOB;
285+
strmap_put(&ctx->paths_to_lists, path, list);
286+
}
287+
oid_array_append(&list->oids, &obj->oid);
288+
} else {
289+
/* assume a root tree, such as a lightweight tag. */
290+
oid_array_append(&tagged_blobs->oids, &obj->oid);
291+
}
292+
break;
293+
294+
case OBJ_COMMIT:
295+
/* Make sure it is in the object walk */
296+
if (obj != pending->item)
297+
add_pending_object(info->revs, obj, "");
298+
break;
299+
300+
default:
301+
BUG("should not see any other type here");
302+
}
303+
}
304+
305+
/*
306+
* Add tag objects and tagged blobs if they exist.
307+
*/
308+
if (tagged_blobs) {
309+
if (tagged_blobs->oids.nr) {
310+
const char *tagged_blob_path = "/tagged-blobs";
311+
tagged_blobs->type = OBJ_BLOB;
312+
push_to_stack(ctx, tagged_blob_path);
313+
strmap_put(&ctx->paths_to_lists, tagged_blob_path, tagged_blobs);
314+
} else {
315+
oid_array_clear(&tagged_blobs->oids);
316+
free(tagged_blobs);
317+
}
318+
}
319+
if (tags) {
320+
if (tags->oids.nr) {
321+
const char *tag_path = "/tags";
322+
tags->type = OBJ_TAG;
323+
push_to_stack(ctx, tag_path);
324+
strmap_put(&ctx->paths_to_lists, tag_path, tags);
325+
} else {
326+
oid_array_clear(&tags->oids);
327+
free(tags);
328+
}
329+
}
330+
}
331+
197332
/**
198333
* Given the configuration of 'info', walk the commits based on 'info->revs' and
199334
* call 'info->path_fn' on each discovered path.
@@ -202,7 +337,6 @@ static void clear_strmap(struct strmap *map)
202337
*/
203338
int walk_objects_by_path(struct path_walk_info *info)
204339
{
205-
const char *root_path = "";
206340
int ret = 0;
207341
size_t commits_nr = 0, paths_nr = 0;
208342
struct commit *c;
@@ -222,15 +356,31 @@ int walk_objects_by_path(struct path_walk_info *info)
222356
CALLOC_ARRAY(commit_list, 1);
223357
commit_list->type = OBJ_COMMIT;
224358

359+
if (info->tags)
360+
info->revs->tag_objects = 1;
361+
225362
/* Insert a single list for the root tree into the paths. */
226363
CALLOC_ARRAY(root_tree_list, 1);
227364
root_tree_list->type = OBJ_TREE;
228365
strmap_put(&ctx.paths_to_lists, root_path, root_tree_list);
229366
push_to_stack(&ctx, root_path);
230367

368+
/*
369+
* Set these values before preparing the walk to catch
370+
* lightweight tags pointing to non-commits and indexed objects.
371+
*/
372+
info->revs->blob_objects = info->blobs;
373+
info->revs->tree_objects = info->trees;
374+
231375
if (prepare_revision_walk(info->revs))
232376
die(_("failed to setup revision walk"));
233377

378+
info->revs->blob_objects = info->revs->tree_objects = 0;
379+
380+
trace2_region_enter("path-walk", "pending-walk", info->revs->repo);
381+
setup_pending_objects(info, &ctx);
382+
trace2_region_leave("path-walk", "pending-walk", info->revs->repo);
383+
234384
while ((c = get_revision(info->revs))) {
235385
struct object_id *oid;
236386
struct tree *t;
@@ -278,6 +428,26 @@ int walk_objects_by_path(struct path_walk_info *info)
278428

279429
free(path);
280430
}
431+
432+
/* Are there paths remaining? Likely they are from indexed objects. */
433+
if (!strmap_empty(&ctx.paths_to_lists)) {
434+
struct hashmap_iter iter;
435+
struct strmap_entry *entry;
436+
437+
strmap_for_each_entry(&ctx.paths_to_lists, &iter, entry)
438+
push_to_stack(&ctx, entry->key);
439+
440+
while (!ret && ctx.path_stack.nr) {
441+
char *path = ctx.path_stack.items[ctx.path_stack.nr - 1].string;
442+
ctx.path_stack.nr--;
443+
paths_nr++;
444+
445+
ret = walk_path(&ctx, path);
446+
447+
free(path);
448+
}
449+
}
450+
281451
trace2_data_intmax("path-walk", ctx.repo, "paths", paths_nr);
282452
trace2_region_leave("path-walk", "path-walk", info->revs->repo);
283453

path-walk.h

+2
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,14 @@ struct path_walk_info {
3838
int commits;
3939
int trees;
4040
int blobs;
41+
int tags;
4142
};
4243

4344
#define PATH_WALK_INFO_INIT { \
4445
.blobs = 1, \
4546
.trees = 1, \
4647
.commits = 1, \
48+
.tags = 1, \
4749
}
4850

4951
/**

t/helper/test-path-walk.c

+13-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ struct path_walk_test_data {
2323
uintmax_t commit_nr;
2424
uintmax_t tree_nr;
2525
uintmax_t blob_nr;
26+
uintmax_t tag_nr;
2627
};
2728

2829
static int emit_block(const char *path, struct oid_array *oids,
@@ -37,11 +38,18 @@ static int emit_block(const char *path, struct oid_array *oids,
3738
tdata->blob_nr += oids->nr;
3839
else if (type == OBJ_COMMIT)
3940
tdata->commit_nr += oids->nr;
41+
else if (type == OBJ_TAG)
42+
tdata->tag_nr += oids->nr;
4043
else
4144
BUG("we do not understand this type");
4245

4346
typestr = type_name(type);
4447

48+
/* This should never be output during tests. */
49+
if (!oids->nr)
50+
printf("%"PRIuMAX":%s:%s:EMPTY\n",
51+
tdata->batch_nr, typestr, path);
52+
4553
for (size_t i = 0; i < oids->nr; i++)
4654
printf("%"PRIuMAX":%s:%s:%s\n",
4755
tdata->batch_nr, typestr, path,
@@ -62,6 +70,8 @@ int cmd__path_walk(int argc, const char **argv)
6270
N_("toggle inclusion of blob objects")),
6371
OPT_BOOL(0, "commits", &info.commits,
6472
N_("toggle inclusion of commit objects")),
73+
OPT_BOOL(0, "tags", &info.tags,
74+
N_("toggle inclusion of tag objects")),
6575
OPT_BOOL(0, "trees", &info.trees,
6676
N_("toggle inclusion of tree objects")),
6777
OPT_END(),
@@ -87,8 +97,9 @@ int cmd__path_walk(int argc, const char **argv)
8797

8898
printf("commits:%" PRIuMAX "\n"
8999
"trees:%" PRIuMAX "\n"
90-
"blobs:%" PRIuMAX "\n",
91-
data.commit_nr, data.tree_nr, data.blob_nr);
100+
"blobs:%" PRIuMAX "\n"
101+
"tags:%" PRIuMAX "\n",
102+
data.commit_nr, data.tree_nr, data.blob_nr, data.tag_nr);
92103

93104
release_revisions(&revs);
94105
return res;

0 commit comments

Comments
 (0)