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