Skip to content

Commit dfd00b2

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 f4bf8be commit dfd00b2

File tree

5 files changed

+336
-10
lines changed

5 files changed

+336
-10
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

+173-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,9 +161,13 @@ 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
/* Evaluate function pointer on this data, if requested. */
162168
if ((list->type == OBJ_TREE && ctx->info->trees) ||
163-
(list->type == OBJ_BLOB && ctx->info->blobs))
169+
(list->type == OBJ_BLOB && ctx->info->blobs)||
170+
(list->type == OBJ_TAG && ctx->info->tags))
164171
ret = ctx->info->path_fn(path, &list->oids, list->type,
165172
ctx->info->path_fn_data);
166173

@@ -191,6 +198,134 @@ static void clear_strmap(struct strmap *map)
191198
strmap_init(map);
192199
}
193200

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

356+
if (info->tags)
357+
info->revs->tag_objects = 1;
358+
222359
/* Insert a single list for the root tree into the paths. */
223360
CALLOC_ARRAY(root_tree_list, 1);
224361
root_tree_list->type = OBJ_TREE;
225362
strmap_put(&ctx.paths_to_lists, root_path, root_tree_list);
226363
push_to_stack(&ctx, root_path);
227364

365+
/*
366+
* Set these values before preparing the walk to catch
367+
* lightweight tags pointing to non-commits and indexed objects.
368+
*/
369+
info->revs->blob_objects = info->blobs;
370+
info->revs->tree_objects = info->trees;
371+
228372
if (prepare_revision_walk(info->revs))
229373
die(_("failed to setup revision walk"));
230374

375+
info->revs->blob_objects = info->revs->tree_objects = 0;
376+
377+
trace2_region_enter("path-walk", "pending-walk", info->revs->repo);
378+
setup_pending_objects(info, &ctx);
379+
trace2_region_leave("path-walk", "pending-walk", info->revs->repo);
380+
231381
while ((c = get_revision(info->revs))) {
232382
struct object_id *oid;
233383
struct tree *t;
@@ -275,6 +425,27 @@ int walk_objects_by_path(struct path_walk_info *info)
275425

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

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

+11-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ struct path_walk_test_data {
2121
uintmax_t commit_nr;
2222
uintmax_t tree_nr;
2323
uintmax_t blob_nr;
24+
uintmax_t tag_nr;
2425
};
2526

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

49+
case OBJ_TAG:
50+
typestr = "TAG";
51+
tdata->tag_nr += oids->nr;
52+
break;
53+
4854
default:
4955
BUG("we do not understand this type");
5056
}
@@ -66,6 +72,8 @@ int cmd__path_walk(int argc, const char **argv)
6672
N_("toggle inclusion of blob objects")),
6773
OPT_BOOL(0, "commits", &info.commits,
6874
N_("toggle inclusion of commit objects")),
75+
OPT_BOOL(0, "tags", &info.tags,
76+
N_("toggle inclusion of tag objects")),
6977
OPT_BOOL(0, "trees", &info.trees,
7078
N_("toggle inclusion of tree objects")),
7179
OPT_END(),
@@ -92,8 +100,9 @@ int cmd__path_walk(int argc, const char **argv)
92100

93101
printf("commits:%" PRIuMAX "\n"
94102
"trees:%" PRIuMAX "\n"
95-
"blobs:%" PRIuMAX "\n",
96-
data.commit_nr, data.tree_nr, data.blob_nr);
103+
"blobs:%" PRIuMAX "\n"
104+
"tags:%" PRIuMAX "\n",
105+
data.commit_nr, data.tree_nr, data.blob_nr, data.tag_nr);
97106

98107
return res;
99108
}

0 commit comments

Comments
 (0)