]> git.ipfire.org Git - thirdparty/git.git/blob - list-objects-filter.c
alloc.h: move ALLOC_GROW() functions from cache.h
[thirdparty/git.git] / list-objects-filter.c
1 #include "cache.h"
2 #include "alloc.h"
3 #include "dir.h"
4 #include "tag.h"
5 #include "commit.h"
6 #include "tree.h"
7 #include "blob.h"
8 #include "diff.h"
9 #include "tree-walk.h"
10 #include "revision.h"
11 #include "list-objects.h"
12 #include "list-objects-filter.h"
13 #include "list-objects-filter-options.h"
14 #include "oidmap.h"
15 #include "oidset.h"
16 #include "object-store.h"
17
18 /* Remember to update object flag allocation in object.h */
19 /*
20 * FILTER_SHOWN_BUT_REVISIT -- we set this bit on tree objects
21 * that have been shown, but should be revisited if they appear
22 * in the traversal (until we mark it SEEN). This is a way to
23 * let us silently de-dup calls to show() in the caller. This
24 * is subtly different from the "revision.h:SHOWN" and the
25 * "object-name.c:ONELINE_SEEN" bits. And also different from
26 * the non-de-dup usage in pack-bitmap.c
27 */
28 #define FILTER_SHOWN_BUT_REVISIT (1<<21)
29
30 struct subfilter {
31 struct filter *filter;
32 struct oidset seen;
33 struct oidset omits;
34 struct object_id skip_tree;
35 unsigned is_skipping_tree : 1;
36 };
37
38 struct filter {
39 enum list_objects_filter_result (*filter_object_fn)(
40 struct repository *r,
41 enum list_objects_filter_situation filter_situation,
42 struct object *obj,
43 const char *pathname,
44 const char *filename,
45 struct oidset *omits,
46 void *filter_data);
47
48 /*
49 * Optional. If this function is supplied and the filter needs
50 * to collect omits, then this function is called once before
51 * free_fn is called.
52 *
53 * This is required because the following two conditions hold:
54 *
55 * a. A tree filter can add and remove objects as an object
56 * graph is traversed.
57 * b. A combine filter's omit set is the union of all its
58 * subfilters, which may include tree: filters.
59 *
60 * As such, the omits sets must be separate sets, and can only
61 * be unioned after the traversal is completed.
62 */
63 void (*finalize_omits_fn)(struct oidset *omits, void *filter_data);
64
65 void (*free_fn)(void *filter_data);
66
67 void *filter_data;
68
69 /* If non-NULL, the filter collects a list of the omitted OIDs here. */
70 struct oidset *omits;
71 };
72
73 static enum list_objects_filter_result filter_blobs_none(
74 struct repository *r UNUSED,
75 enum list_objects_filter_situation filter_situation,
76 struct object *obj,
77 const char *pathname UNUSED,
78 const char *filename UNUSED,
79 struct oidset *omits,
80 void *filter_data_ UNUSED)
81 {
82 switch (filter_situation) {
83 default:
84 BUG("unknown filter_situation: %d", filter_situation);
85
86 case LOFS_TAG:
87 assert(obj->type == OBJ_TAG);
88 /* always include all tag objects */
89 return LOFR_MARK_SEEN | LOFR_DO_SHOW;
90
91 case LOFS_COMMIT:
92 assert(obj->type == OBJ_COMMIT);
93 /* always include all commit objects */
94 return LOFR_MARK_SEEN | LOFR_DO_SHOW;
95
96 case LOFS_BEGIN_TREE:
97 assert(obj->type == OBJ_TREE);
98 /* always include all tree objects */
99 return LOFR_MARK_SEEN | LOFR_DO_SHOW;
100
101 case LOFS_END_TREE:
102 assert(obj->type == OBJ_TREE);
103 return LOFR_ZERO;
104
105 case LOFS_BLOB:
106 assert(obj->type == OBJ_BLOB);
107 assert((obj->flags & SEEN) == 0);
108
109 if (omits)
110 oidset_insert(omits, &obj->oid);
111 return LOFR_MARK_SEEN; /* but not LOFR_DO_SHOW (hard omit) */
112 }
113 }
114
115 static void filter_blobs_none__init(
116 struct list_objects_filter_options *filter_options UNUSED,
117 struct filter *filter)
118 {
119 filter->filter_object_fn = filter_blobs_none;
120 filter->free_fn = free;
121 }
122
123 /*
124 * A filter for list-objects to omit ALL trees and blobs from the traversal.
125 * Can OPTIONALLY collect a list of the omitted OIDs.
126 */
127 struct filter_trees_depth_data {
128 /*
129 * Maps trees to the minimum depth at which they were seen. It is not
130 * necessary to re-traverse a tree at deeper or equal depths than it has
131 * already been traversed.
132 *
133 * We can't use LOFR_MARK_SEEN for tree objects since this will prevent
134 * it from being traversed at shallower depths.
135 */
136 struct oidmap seen_at_depth;
137
138 unsigned long exclude_depth;
139 unsigned long current_depth;
140 };
141
142 struct seen_map_entry {
143 struct oidmap_entry base;
144 size_t depth;
145 };
146
147 /* Returns 1 if the oid was in the omits set before it was invoked. */
148 static int filter_trees_update_omits(
149 struct object *obj,
150 struct oidset *omits,
151 int include_it)
152 {
153 if (!omits)
154 return 0;
155
156 if (include_it)
157 return oidset_remove(omits, &obj->oid);
158 else
159 return oidset_insert(omits, &obj->oid);
160 }
161
162 static enum list_objects_filter_result filter_trees_depth(
163 struct repository *r UNUSED,
164 enum list_objects_filter_situation filter_situation,
165 struct object *obj,
166 const char *pathname UNUSED,
167 const char *filename UNUSED,
168 struct oidset *omits,
169 void *filter_data_)
170 {
171 struct filter_trees_depth_data *filter_data = filter_data_;
172 struct seen_map_entry *seen_info;
173 int include_it = filter_data->current_depth <
174 filter_data->exclude_depth;
175 int filter_res;
176 int already_seen;
177
178 /*
179 * Note that we do not use _MARK_SEEN in order to allow re-traversal in
180 * case we encounter a tree or blob again at a shallower depth.
181 */
182
183 switch (filter_situation) {
184 default:
185 BUG("unknown filter_situation: %d", filter_situation);
186
187 case LOFS_TAG:
188 assert(obj->type == OBJ_TAG);
189 /* always include all tag objects */
190 return LOFR_MARK_SEEN | LOFR_DO_SHOW;
191
192 case LOFS_COMMIT:
193 assert(obj->type == OBJ_COMMIT);
194 /* always include all commit objects */
195 return LOFR_MARK_SEEN | LOFR_DO_SHOW;
196
197 case LOFS_END_TREE:
198 assert(obj->type == OBJ_TREE);
199 filter_data->current_depth--;
200 return LOFR_ZERO;
201
202 case LOFS_BLOB:
203 filter_trees_update_omits(obj, omits, include_it);
204 return include_it ? LOFR_MARK_SEEN | LOFR_DO_SHOW : LOFR_ZERO;
205
206 case LOFS_BEGIN_TREE:
207 seen_info = oidmap_get(
208 &filter_data->seen_at_depth, &obj->oid);
209 if (!seen_info) {
210 CALLOC_ARRAY(seen_info, 1);
211 oidcpy(&seen_info->base.oid, &obj->oid);
212 seen_info->depth = filter_data->current_depth;
213 oidmap_put(&filter_data->seen_at_depth, seen_info);
214 already_seen = 0;
215 } else {
216 already_seen =
217 filter_data->current_depth >= seen_info->depth;
218 }
219
220 if (already_seen) {
221 filter_res = LOFR_SKIP_TREE;
222 } else {
223 int been_omitted = filter_trees_update_omits(
224 obj, omits, include_it);
225 seen_info->depth = filter_data->current_depth;
226
227 if (include_it)
228 filter_res = LOFR_DO_SHOW;
229 else if (omits && !been_omitted)
230 /*
231 * Must update omit information of children
232 * recursively; they have not been omitted yet.
233 */
234 filter_res = LOFR_ZERO;
235 else
236 filter_res = LOFR_SKIP_TREE;
237 }
238
239 filter_data->current_depth++;
240 return filter_res;
241 }
242 }
243
244 static void filter_trees_free(void *filter_data) {
245 struct filter_trees_depth_data *d = filter_data;
246 if (!d)
247 return;
248 oidmap_free(&d->seen_at_depth, 1);
249 free(d);
250 }
251
252 static void filter_trees_depth__init(
253 struct list_objects_filter_options *filter_options,
254 struct filter *filter)
255 {
256 struct filter_trees_depth_data *d = xcalloc(1, sizeof(*d));
257 oidmap_init(&d->seen_at_depth, 0);
258 d->exclude_depth = filter_options->tree_exclude_depth;
259 d->current_depth = 0;
260
261 filter->filter_data = d;
262 filter->filter_object_fn = filter_trees_depth;
263 filter->free_fn = filter_trees_free;
264 }
265
266 /*
267 * A filter for list-objects to omit large blobs.
268 * And to OPTIONALLY collect a list of the omitted OIDs.
269 */
270 struct filter_blobs_limit_data {
271 unsigned long max_bytes;
272 };
273
274 static enum list_objects_filter_result filter_blobs_limit(
275 struct repository *r,
276 enum list_objects_filter_situation filter_situation,
277 struct object *obj,
278 const char *pathname UNUSED,
279 const char *filename UNUSED,
280 struct oidset *omits,
281 void *filter_data_)
282 {
283 struct filter_blobs_limit_data *filter_data = filter_data_;
284 unsigned long object_length;
285 enum object_type t;
286
287 switch (filter_situation) {
288 default:
289 BUG("unknown filter_situation: %d", filter_situation);
290
291 case LOFS_TAG:
292 assert(obj->type == OBJ_TAG);
293 /* always include all tag objects */
294 return LOFR_MARK_SEEN | LOFR_DO_SHOW;
295
296 case LOFS_COMMIT:
297 assert(obj->type == OBJ_COMMIT);
298 /* always include all commit objects */
299 return LOFR_MARK_SEEN | LOFR_DO_SHOW;
300
301 case LOFS_BEGIN_TREE:
302 assert(obj->type == OBJ_TREE);
303 /* always include all tree objects */
304 return LOFR_MARK_SEEN | LOFR_DO_SHOW;
305
306 case LOFS_END_TREE:
307 assert(obj->type == OBJ_TREE);
308 return LOFR_ZERO;
309
310 case LOFS_BLOB:
311 assert(obj->type == OBJ_BLOB);
312 assert((obj->flags & SEEN) == 0);
313
314 t = oid_object_info(r, &obj->oid, &object_length);
315 if (t != OBJ_BLOB) { /* probably OBJ_NONE */
316 /*
317 * We DO NOT have the blob locally, so we cannot
318 * apply the size filter criteria. Be conservative
319 * and force show it (and let the caller deal with
320 * the ambiguity).
321 */
322 goto include_it;
323 }
324
325 if (object_length < filter_data->max_bytes)
326 goto include_it;
327
328 if (omits)
329 oidset_insert(omits, &obj->oid);
330 return LOFR_MARK_SEEN; /* but not LOFR_DO_SHOW (hard omit) */
331 }
332
333 include_it:
334 if (omits)
335 oidset_remove(omits, &obj->oid);
336 return LOFR_MARK_SEEN | LOFR_DO_SHOW;
337 }
338
339 static void filter_blobs_limit__init(
340 struct list_objects_filter_options *filter_options,
341 struct filter *filter)
342 {
343 struct filter_blobs_limit_data *d = xcalloc(1, sizeof(*d));
344 d->max_bytes = filter_options->blob_limit_value;
345
346 filter->filter_data = d;
347 filter->filter_object_fn = filter_blobs_limit;
348 filter->free_fn = free;
349 }
350
351 /*
352 * A filter driven by a sparse-checkout specification to only
353 * include blobs that a sparse checkout would populate.
354 *
355 * The sparse-checkout spec can be loaded from a blob with the
356 * given OID or from a local pathname. We allow an OID because
357 * the repo may be bare or we may be doing the filtering on the
358 * server.
359 */
360 struct frame {
361 /*
362 * default_match is the usual default include/exclude value that
363 * should be inherited as we recurse into directories based
364 * upon pattern matching of the directory itself or of a
365 * containing directory.
366 */
367 enum pattern_match_result default_match;
368
369 /*
370 * 1 if the directory (recursively) contains any provisionally
371 * omitted objects.
372 *
373 * 0 if everything (recursively) contained in this directory
374 * has been explicitly included (SHOWN) in the result and
375 * the directory may be short-cut later in the traversal.
376 */
377 unsigned child_prov_omit : 1;
378 };
379
380 struct filter_sparse_data {
381 struct pattern_list pl;
382
383 size_t nr, alloc;
384 struct frame *array_frame;
385 };
386
387 static enum list_objects_filter_result filter_sparse(
388 struct repository *r,
389 enum list_objects_filter_situation filter_situation,
390 struct object *obj,
391 const char *pathname,
392 const char *filename,
393 struct oidset *omits,
394 void *filter_data_)
395 {
396 struct filter_sparse_data *filter_data = filter_data_;
397 int dtype;
398 struct frame *frame;
399 enum pattern_match_result match;
400
401 switch (filter_situation) {
402 default:
403 BUG("unknown filter_situation: %d", filter_situation);
404
405 case LOFS_TAG:
406 assert(obj->type == OBJ_TAG);
407 /* always include all tag objects */
408 return LOFR_MARK_SEEN | LOFR_DO_SHOW;
409
410 case LOFS_COMMIT:
411 assert(obj->type == OBJ_COMMIT);
412 /* always include all commit objects */
413 return LOFR_MARK_SEEN | LOFR_DO_SHOW;
414
415 case LOFS_BEGIN_TREE:
416 assert(obj->type == OBJ_TREE);
417 dtype = DT_DIR;
418 match = path_matches_pattern_list(pathname, strlen(pathname),
419 filename, &dtype, &filter_data->pl,
420 r->index);
421 if (match == UNDECIDED)
422 match = filter_data->array_frame[filter_data->nr - 1].default_match;
423
424 ALLOC_GROW(filter_data->array_frame, filter_data->nr + 1,
425 filter_data->alloc);
426 filter_data->array_frame[filter_data->nr].default_match = match;
427 filter_data->array_frame[filter_data->nr].child_prov_omit = 0;
428 filter_data->nr++;
429
430 /*
431 * A directory with this tree OID may appear in multiple
432 * places in the tree. (Think of a directory move or copy,
433 * with no other changes, so the OID is the same, but the
434 * full pathnames of objects within this directory are new
435 * and may match is_excluded() patterns differently.)
436 * So we cannot mark this directory as SEEN (yet), since
437 * that will prevent process_tree() from revisiting this
438 * tree object with other pathname prefixes.
439 *
440 * Only _DO_SHOW the tree object the first time we visit
441 * this tree object.
442 *
443 * We always show all tree objects. A future optimization
444 * may want to attempt to narrow this.
445 */
446 if (obj->flags & FILTER_SHOWN_BUT_REVISIT)
447 return LOFR_ZERO;
448 obj->flags |= FILTER_SHOWN_BUT_REVISIT;
449 return LOFR_DO_SHOW;
450
451 case LOFS_END_TREE:
452 assert(obj->type == OBJ_TREE);
453 assert(filter_data->nr > 1);
454
455 frame = &filter_data->array_frame[--filter_data->nr];
456
457 /*
458 * Tell our parent directory if any of our children were
459 * provisionally omitted.
460 */
461 filter_data->array_frame[filter_data->nr - 1].child_prov_omit |=
462 frame->child_prov_omit;
463
464 /*
465 * If there are NO provisionally omitted child objects (ALL child
466 * objects in this folder were INCLUDED), then we can mark the
467 * folder as SEEN (so we will not have to revisit it again).
468 */
469 if (!frame->child_prov_omit)
470 return LOFR_MARK_SEEN;
471 return LOFR_ZERO;
472
473 case LOFS_BLOB:
474 assert(obj->type == OBJ_BLOB);
475 assert((obj->flags & SEEN) == 0);
476
477 frame = &filter_data->array_frame[filter_data->nr - 1];
478
479 dtype = DT_REG;
480 match = path_matches_pattern_list(pathname, strlen(pathname),
481 filename, &dtype, &filter_data->pl,
482 r->index);
483 if (match == UNDECIDED)
484 match = frame->default_match;
485 if (match == MATCHED) {
486 if (omits)
487 oidset_remove(omits, &obj->oid);
488 return LOFR_MARK_SEEN | LOFR_DO_SHOW;
489 }
490
491 /*
492 * Provisionally omit it. We've already established that
493 * this pathname is not in the sparse-checkout specification
494 * with the CURRENT pathname, so we *WANT* to omit this blob.
495 *
496 * However, a pathname elsewhere in the tree may also
497 * reference this same blob, so we cannot reject it yet.
498 * Leave the LOFR_ bits unset so that if the blob appears
499 * again in the traversal, we will be asked again.
500 */
501 if (omits)
502 oidset_insert(omits, &obj->oid);
503
504 /*
505 * Remember that at least 1 blob in this tree was
506 * provisionally omitted. This prevents us from short
507 * cutting the tree in future iterations.
508 */
509 frame->child_prov_omit = 1;
510 return LOFR_ZERO;
511 }
512 }
513
514
515 static void filter_sparse_free(void *filter_data)
516 {
517 struct filter_sparse_data *d = filter_data;
518 clear_pattern_list(&d->pl);
519 free(d->array_frame);
520 free(d);
521 }
522
523 static void filter_sparse_oid__init(
524 struct list_objects_filter_options *filter_options,
525 struct filter *filter)
526 {
527 struct filter_sparse_data *d = xcalloc(1, sizeof(*d));
528 struct object_context oc;
529 struct object_id sparse_oid;
530
531 if (get_oid_with_context(the_repository,
532 filter_options->sparse_oid_name,
533 GET_OID_BLOB, &sparse_oid, &oc))
534 die(_("unable to access sparse blob in '%s'"),
535 filter_options->sparse_oid_name);
536 if (add_patterns_from_blob_to_list(&sparse_oid, "", 0, &d->pl) < 0)
537 die(_("unable to parse sparse filter data in %s"),
538 oid_to_hex(&sparse_oid));
539
540 ALLOC_GROW(d->array_frame, d->nr + 1, d->alloc);
541 d->array_frame[d->nr].default_match = 0; /* default to include */
542 d->array_frame[d->nr].child_prov_omit = 0;
543 d->nr++;
544
545 filter->filter_data = d;
546 filter->filter_object_fn = filter_sparse;
547 filter->free_fn = filter_sparse_free;
548 }
549
550 /*
551 * A filter for list-objects to omit large blobs.
552 * And to OPTIONALLY collect a list of the omitted OIDs.
553 */
554 struct filter_object_type_data {
555 enum object_type object_type;
556 };
557
558 static enum list_objects_filter_result filter_object_type(
559 struct repository *r UNUSED,
560 enum list_objects_filter_situation filter_situation,
561 struct object *obj,
562 const char *pathname UNUSED,
563 const char *filename UNUSED,
564 struct oidset *omits UNUSED,
565 void *filter_data_)
566 {
567 struct filter_object_type_data *filter_data = filter_data_;
568
569 switch (filter_situation) {
570 default:
571 BUG("unknown filter_situation: %d", filter_situation);
572
573 case LOFS_TAG:
574 assert(obj->type == OBJ_TAG);
575 if (filter_data->object_type == OBJ_TAG)
576 return LOFR_MARK_SEEN | LOFR_DO_SHOW;
577 return LOFR_MARK_SEEN;
578
579 case LOFS_COMMIT:
580 assert(obj->type == OBJ_COMMIT);
581 if (filter_data->object_type == OBJ_COMMIT)
582 return LOFR_MARK_SEEN | LOFR_DO_SHOW;
583 return LOFR_MARK_SEEN;
584
585 case LOFS_BEGIN_TREE:
586 assert(obj->type == OBJ_TREE);
587
588 /*
589 * If we only want to show commits or tags, then there is no
590 * need to walk down trees.
591 */
592 if (filter_data->object_type == OBJ_COMMIT ||
593 filter_data->object_type == OBJ_TAG)
594 return LOFR_SKIP_TREE;
595
596 if (filter_data->object_type == OBJ_TREE)
597 return LOFR_MARK_SEEN | LOFR_DO_SHOW;
598
599 return LOFR_MARK_SEEN;
600
601 case LOFS_BLOB:
602 assert(obj->type == OBJ_BLOB);
603
604 if (filter_data->object_type == OBJ_BLOB)
605 return LOFR_MARK_SEEN | LOFR_DO_SHOW;
606 return LOFR_MARK_SEEN;
607
608 case LOFS_END_TREE:
609 return LOFR_ZERO;
610 }
611 }
612
613 static void filter_object_type__init(
614 struct list_objects_filter_options *filter_options,
615 struct filter *filter)
616 {
617 struct filter_object_type_data *d = xcalloc(1, sizeof(*d));
618 d->object_type = filter_options->object_type;
619
620 filter->filter_data = d;
621 filter->filter_object_fn = filter_object_type;
622 filter->free_fn = free;
623 }
624
625 /* A filter which only shows objects shown by all sub-filters. */
626 struct combine_filter_data {
627 struct subfilter *sub;
628 size_t nr;
629 };
630
631 static enum list_objects_filter_result process_subfilter(
632 struct repository *r,
633 enum list_objects_filter_situation filter_situation,
634 struct object *obj,
635 const char *pathname,
636 const char *filename,
637 struct subfilter *sub)
638 {
639 enum list_objects_filter_result result;
640
641 /*
642 * Check and update is_skipping_tree before oidset_contains so
643 * that is_skipping_tree gets unset even when the object is
644 * marked as seen. As of this writing, no filter uses
645 * LOFR_MARK_SEEN on trees that also uses LOFR_SKIP_TREE, so the
646 * ordering is only theoretically important. Be cautious if you
647 * change the order of the below checks and more filters have
648 * been added!
649 */
650 if (sub->is_skipping_tree) {
651 if (filter_situation == LOFS_END_TREE &&
652 oideq(&obj->oid, &sub->skip_tree))
653 sub->is_skipping_tree = 0;
654 else
655 return LOFR_ZERO;
656 }
657 if (oidset_contains(&sub->seen, &obj->oid))
658 return LOFR_ZERO;
659
660 result = list_objects_filter__filter_object(
661 r, filter_situation, obj, pathname, filename, sub->filter);
662
663 if (result & LOFR_MARK_SEEN)
664 oidset_insert(&sub->seen, &obj->oid);
665
666 if (result & LOFR_SKIP_TREE) {
667 sub->is_skipping_tree = 1;
668 sub->skip_tree = obj->oid;
669 }
670
671 return result;
672 }
673
674 static enum list_objects_filter_result filter_combine(
675 struct repository *r,
676 enum list_objects_filter_situation filter_situation,
677 struct object *obj,
678 const char *pathname,
679 const char *filename,
680 struct oidset *omits UNUSED,
681 void *filter_data)
682 {
683 struct combine_filter_data *d = filter_data;
684 enum list_objects_filter_result combined_result =
685 LOFR_DO_SHOW | LOFR_MARK_SEEN | LOFR_SKIP_TREE;
686 size_t sub;
687
688 for (sub = 0; sub < d->nr; sub++) {
689 enum list_objects_filter_result sub_result = process_subfilter(
690 r, filter_situation, obj, pathname, filename,
691 &d->sub[sub]);
692 if (!(sub_result & LOFR_DO_SHOW))
693 combined_result &= ~LOFR_DO_SHOW;
694 if (!(sub_result & LOFR_MARK_SEEN))
695 combined_result &= ~LOFR_MARK_SEEN;
696 if (!d->sub[sub].is_skipping_tree)
697 combined_result &= ~LOFR_SKIP_TREE;
698 }
699
700 return combined_result;
701 }
702
703 static void filter_combine__free(void *filter_data)
704 {
705 struct combine_filter_data *d = filter_data;
706 size_t sub;
707 for (sub = 0; sub < d->nr; sub++) {
708 list_objects_filter__free(d->sub[sub].filter);
709 oidset_clear(&d->sub[sub].seen);
710 if (d->sub[sub].omits.set.size)
711 BUG("expected oidset to be cleared already");
712 }
713 free(d->sub);
714 free(d);
715 }
716
717 static void add_all(struct oidset *dest, struct oidset *src) {
718 struct oidset_iter iter;
719 struct object_id *src_oid;
720
721 oidset_iter_init(src, &iter);
722 while ((src_oid = oidset_iter_next(&iter)) != NULL)
723 oidset_insert(dest, src_oid);
724 }
725
726 static void filter_combine__finalize_omits(
727 struct oidset *omits,
728 void *filter_data)
729 {
730 struct combine_filter_data *d = filter_data;
731 size_t sub;
732
733 for (sub = 0; sub < d->nr; sub++) {
734 add_all(omits, &d->sub[sub].omits);
735 oidset_clear(&d->sub[sub].omits);
736 }
737 }
738
739 static void filter_combine__init(
740 struct list_objects_filter_options *filter_options,
741 struct filter* filter)
742 {
743 struct combine_filter_data *d = xcalloc(1, sizeof(*d));
744 size_t sub;
745
746 d->nr = filter_options->sub_nr;
747 CALLOC_ARRAY(d->sub, d->nr);
748 for (sub = 0; sub < d->nr; sub++)
749 d->sub[sub].filter = list_objects_filter__init(
750 filter->omits ? &d->sub[sub].omits : NULL,
751 &filter_options->sub[sub]);
752
753 filter->filter_data = d;
754 filter->filter_object_fn = filter_combine;
755 filter->free_fn = filter_combine__free;
756 filter->finalize_omits_fn = filter_combine__finalize_omits;
757 }
758
759 typedef void (*filter_init_fn)(
760 struct list_objects_filter_options *filter_options,
761 struct filter *filter);
762
763 /*
764 * Must match "enum list_objects_filter_choice".
765 */
766 static filter_init_fn s_filters[] = {
767 NULL,
768 filter_blobs_none__init,
769 filter_blobs_limit__init,
770 filter_trees_depth__init,
771 filter_sparse_oid__init,
772 filter_object_type__init,
773 filter_combine__init,
774 };
775
776 struct filter *list_objects_filter__init(
777 struct oidset *omitted,
778 struct list_objects_filter_options *filter_options)
779 {
780 struct filter *filter;
781 filter_init_fn init_fn;
782
783 assert((sizeof(s_filters) / sizeof(s_filters[0])) == LOFC__COUNT);
784
785 if (!filter_options)
786 return NULL;
787
788 if (filter_options->choice >= LOFC__COUNT)
789 BUG("invalid list-objects filter choice: %d",
790 filter_options->choice);
791
792 init_fn = s_filters[filter_options->choice];
793 if (!init_fn)
794 return NULL;
795
796 CALLOC_ARRAY(filter, 1);
797 filter->omits = omitted;
798 init_fn(filter_options, filter);
799 return filter;
800 }
801
802 enum list_objects_filter_result list_objects_filter__filter_object(
803 struct repository *r,
804 enum list_objects_filter_situation filter_situation,
805 struct object *obj,
806 const char *pathname,
807 const char *filename,
808 struct filter *filter)
809 {
810 if (filter && (obj->flags & NOT_USER_GIVEN))
811 return filter->filter_object_fn(r, filter_situation, obj,
812 pathname, filename,
813 filter->omits,
814 filter->filter_data);
815 /*
816 * No filter is active or user gave object explicitly. In this case,
817 * always show the object (except when LOFS_END_TREE, since this tree
818 * had already been shown when LOFS_BEGIN_TREE).
819 */
820 if (filter_situation == LOFS_END_TREE)
821 return 0;
822 return LOFR_MARK_SEEN | LOFR_DO_SHOW;
823 }
824
825 void list_objects_filter__free(struct filter *filter)
826 {
827 if (!filter)
828 return;
829 if (filter->finalize_omits_fn && filter->omits)
830 filter->finalize_omits_fn(filter->omits, filter->filter_data);
831 filter->free_fn(filter->filter_data);
832 free(filter);
833 }