list-objects: support filtering by tag and commit

Object filters currently only support filtering blobs or trees based on some criteria. This commit lays the foundation to also allow filtering of tags and commits. No change in behaviour is expected from this commit given that there are no filters yet for those object types. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed Apr 12, 2021 at 15:37 UTC 9a2a4f95448890d138a800c8a55c5d5dcfe16082
3 files changed +62 -3
list-objects-filter.c
+40
@@ -82,6 +82,16 @@ static enum list_objects_filter_result filter_blobs_none(
82 default:
83 BUG("unknown filter_situation: %d", filter_situation);
84
85 + case LOFS_TAG:
86 + assert(obj->type == OBJ_TAG);
87 + /* always include all tag objects */
88 + return LOFR_MARK_SEEN | LOFR_DO_SHOW;
89 +
90 + case LOFS_COMMIT:
91 + assert(obj->type == OBJ_COMMIT);
92 + /* always include all commit objects */
93 + return LOFR_MARK_SEEN | LOFR_DO_SHOW;
94 +
95 case LOFS_BEGIN_TREE:
96 assert(obj->type == OBJ_TREE);
97 /* always include all tree objects */
@@ -173,6 +183,16 @@ static enum list_objects_filter_result filter_trees_depth(
183 default:
184 BUG("unknown filter_situation: %d", filter_situation);
185
186 + case LOFS_TAG:
187 + assert(obj->type == OBJ_TAG);
188 + /* always include all tag objects */
189 + return LOFR_MARK_SEEN | LOFR_DO_SHOW;
190 +
191 + case LOFS_COMMIT:
192 + assert(obj->type == OBJ_COMMIT);
193 + /* always include all commit objects */
194 + return LOFR_MARK_SEEN | LOFR_DO_SHOW;
195 +
196 case LOFS_END_TREE:
197 assert(obj->type == OBJ_TREE);
198 filter_data->current_depth--;
@@ -267,6 +287,16 @@ static enum list_objects_filter_result filter_blobs_limit(
287 default:
288 BUG("unknown filter_situation: %d", filter_situation);
289
290 + case LOFS_TAG:
291 + assert(obj->type == OBJ_TAG);
292 + /* always include all tag objects */
293 + return LOFR_MARK_SEEN | LOFR_DO_SHOW;
294 +
295 + case LOFS_COMMIT:
296 + assert(obj->type == OBJ_COMMIT);
297 + /* always include all commit objects */
298 + return LOFR_MARK_SEEN | LOFR_DO_SHOW;
299 +
300 case LOFS_BEGIN_TREE:
301 assert(obj->type == OBJ_TREE);
302 /* always include all tree objects */
@@ -371,6 +401,16 @@ static enum list_objects_filter_result filter_sparse(
401 default:
402 BUG("unknown filter_situation: %d", filter_situation);
403
404 + case LOFS_TAG:
405 + assert(obj->type == OBJ_TAG);
406 + /* always include all tag objects */
407 + return LOFR_MARK_SEEN | LOFR_DO_SHOW;
408 +
409 + case LOFS_COMMIT:
410 + assert(obj->type == OBJ_COMMIT);
411 + /* always include all commit objects */
412 + return LOFR_MARK_SEEN | LOFR_DO_SHOW;
413 +
414 case LOFS_BEGIN_TREE:
415 assert(obj->type == OBJ_TREE);
416 dtype = DT_DIR;
list-objects-filter.h
+2
@@ -55,6 +55,8 @@ enum list_objects_filter_result {
55 };
56
57 enum list_objects_filter_situation {
58 + LOFS_COMMIT,
59 + LOFS_TAG,
60 LOFS_BEGIN_TREE,
61 LOFS_END_TREE,
62 LOFS_BLOB
list-objects.c
+20 -3
@@ -217,8 +217,15 @@ static void process_tag(struct traversal_context *ctx,
217 struct tag *tag,
218 const char *name)
219 {
220 - tag->object.flags |= SEEN;
221 - ctx->show_object(&tag->object, name, ctx->show_data);
220 + enum list_objects_filter_result r;
221 +
222 + r = list_objects_filter__filter_object(ctx->revs->repo, LOFS_TAG,
223 + &tag->object, NULL, NULL,
224 + ctx->filter);
225 + if (r & LOFR_MARK_SEEN)
226 + tag->object.flags |= SEEN;
227 + if (r & LOFR_DO_SHOW)
228 + ctx->show_object(&tag->object, name, ctx->show_data);
229 }
230
231 static void mark_edge_parents_uninteresting(struct commit *commit,
@@ -368,6 +375,12 @@ static void do_traverse(struct traversal_context *ctx)
375 strbuf_init(&csp, PATH_MAX);
376
377 while ((commit = get_revision(ctx->revs)) != NULL) {
378 + enum list_objects_filter_result r;
379 +
380 + r = list_objects_filter__filter_object(ctx->revs->repo,
381 + LOFS_COMMIT, &commit->object,
382 + NULL, NULL, ctx->filter);
383 +
384 /*
385 * an uninteresting boundary commit may not have its tree
386 * parsed yet, but we are not going to show them anyway
@@ -382,7 +395,11 @@ static void do_traverse(struct traversal_context *ctx)
395 die(_("unable to load root tree for commit %s"),
396 oid_to_hex(&commit->object.oid));
397 }
385 - ctx->show_commit(commit, ctx->show_data);
398 +
399 + if (r & LOFR_MARK_SEEN)
400 + commit->object.flags |= SEEN;
401 + if (r & LOFR_DO_SHOW)
402 + ctx->show_commit(commit, ctx->show_data);
403
404 if (ctx->revs->tree_blobs_in_commit_order)
405 /*