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