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