10 #include "list-objects.h"
11 #include "list-objects-filter.h"
12 #include "list-objects-filter-options.h"
15 #include "object-store.h"
17 /* Remember to update object flag allocation in object.h */
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
24 * "sha1-name.c:ONELINE_SEEN" bits. And also different from
25 * the non-de-dup usage in pack-bitmap.c
27 #define FILTER_SHOWN_BUT_REVISIT (1<<21)
30 enum list_objects_filter_result (*filter_object_fn)(
32 enum list_objects_filter_situation filter_situation,
38 void (*free_fn)(void *filter_data);
44 * A filter for list-objects to omit ALL blobs from the traversal.
45 * And to OPTIONALLY collect a list of the omitted OIDs.
47 struct filter_blobs_none_data {
51 static enum list_objects_filter_result filter_blobs_none(
53 enum list_objects_filter_situation filter_situation,
59 struct filter_blobs_none_data *filter_data = filter_data_;
61 switch (filter_situation) {
63 BUG("unknown filter_situation: %d", filter_situation);
66 assert(obj->type == OBJ_TREE);
67 /* always include all tree objects */
68 return LOFR_MARK_SEEN | LOFR_DO_SHOW;
71 assert(obj->type == OBJ_TREE);
75 assert(obj->type == OBJ_BLOB);
76 assert((obj->flags & SEEN) == 0);
78 if (filter_data->omits)
79 oidset_insert(filter_data->omits, &obj->oid);
80 return LOFR_MARK_SEEN; /* but not LOFR_DO_SHOW (hard omit) */
84 static void filter_blobs_none__init(
85 struct oidset *omitted,
86 struct list_objects_filter_options *filter_options,
87 struct filter *filter)
89 struct filter_blobs_none_data *d = xcalloc(1, sizeof(*d));
92 filter->filter_data = d;
93 filter->filter_object_fn = filter_blobs_none;
94 filter->free_fn = free;
98 * A filter for list-objects to omit ALL trees and blobs from the traversal.
99 * Can OPTIONALLY collect a list of the omitted OIDs.
101 struct filter_trees_depth_data {
102 struct oidset *omits;
105 * Maps trees to the minimum depth at which they were seen. It is not
106 * necessary to re-traverse a tree at deeper or equal depths than it has
107 * already been traversed.
109 * We can't use LOFR_MARK_SEEN for tree objects since this will prevent
110 * it from being traversed at shallower depths.
112 struct oidmap seen_at_depth;
114 unsigned long exclude_depth;
115 unsigned long current_depth;
118 struct seen_map_entry {
119 struct oidmap_entry base;
123 /* Returns 1 if the oid was in the omits set before it was invoked. */
124 static int filter_trees_update_omits(
126 struct filter_trees_depth_data *filter_data,
129 if (!filter_data->omits)
133 return oidset_remove(filter_data->omits, &obj->oid);
135 return oidset_insert(filter_data->omits, &obj->oid);
138 static enum list_objects_filter_result filter_trees_depth(
139 struct repository *r,
140 enum list_objects_filter_situation filter_situation,
142 const char *pathname,
143 const char *filename,
146 struct filter_trees_depth_data *filter_data = filter_data_;
147 struct seen_map_entry *seen_info;
148 int include_it = filter_data->current_depth <
149 filter_data->exclude_depth;
154 * Note that we do not use _MARK_SEEN in order to allow re-traversal in
155 * case we encounter a tree or blob again at a shallower depth.
158 switch (filter_situation) {
160 BUG("unknown filter_situation: %d", filter_situation);
163 assert(obj->type == OBJ_TREE);
164 filter_data->current_depth--;
168 filter_trees_update_omits(obj, filter_data, include_it);
169 return include_it ? LOFR_MARK_SEEN | LOFR_DO_SHOW : LOFR_ZERO;
171 case LOFS_BEGIN_TREE:
172 seen_info = oidmap_get(
173 &filter_data->seen_at_depth, &obj->oid);
175 seen_info = xcalloc(1, sizeof(*seen_info));
176 oidcpy(&seen_info->base.oid, &obj->oid);
177 seen_info->depth = filter_data->current_depth;
178 oidmap_put(&filter_data->seen_at_depth, seen_info);
182 filter_data->current_depth >= seen_info->depth;
186 filter_res = LOFR_SKIP_TREE;
188 int been_omitted = filter_trees_update_omits(
189 obj, filter_data, include_it);
190 seen_info->depth = filter_data->current_depth;
193 filter_res = LOFR_DO_SHOW;
194 else if (filter_data->omits && !been_omitted)
196 * Must update omit information of children
197 * recursively; they have not been omitted yet.
199 filter_res = LOFR_ZERO;
201 filter_res = LOFR_SKIP_TREE;
204 filter_data->current_depth++;
209 static void filter_trees_free(void *filter_data) {
210 struct filter_trees_depth_data *d = filter_data;
213 oidmap_free(&d->seen_at_depth, 1);
217 static void filter_trees_depth__init(
218 struct oidset *omitted,
219 struct list_objects_filter_options *filter_options,
220 struct filter *filter)
222 struct filter_trees_depth_data *d = xcalloc(1, sizeof(*d));
224 oidmap_init(&d->seen_at_depth, 0);
225 d->exclude_depth = filter_options->tree_exclude_depth;
226 d->current_depth = 0;
228 filter->filter_data = d;
229 filter->filter_object_fn = filter_trees_depth;
230 filter->free_fn = filter_trees_free;
234 * A filter for list-objects to omit large blobs.
235 * And to OPTIONALLY collect a list of the omitted OIDs.
237 struct filter_blobs_limit_data {
238 struct oidset *omits;
239 unsigned long max_bytes;
242 static enum list_objects_filter_result filter_blobs_limit(
243 struct repository *r,
244 enum list_objects_filter_situation filter_situation,
246 const char *pathname,
247 const char *filename,
250 struct filter_blobs_limit_data *filter_data = filter_data_;
251 unsigned long object_length;
254 switch (filter_situation) {
256 BUG("unknown filter_situation: %d", filter_situation);
258 case LOFS_BEGIN_TREE:
259 assert(obj->type == OBJ_TREE);
260 /* always include all tree objects */
261 return LOFR_MARK_SEEN | LOFR_DO_SHOW;
264 assert(obj->type == OBJ_TREE);
268 assert(obj->type == OBJ_BLOB);
269 assert((obj->flags & SEEN) == 0);
271 t = oid_object_info(r, &obj->oid, &object_length);
272 if (t != OBJ_BLOB) { /* probably OBJ_NONE */
274 * We DO NOT have the blob locally, so we cannot
275 * apply the size filter criteria. Be conservative
276 * and force show it (and let the caller deal with
282 if (object_length < filter_data->max_bytes)
285 if (filter_data->omits)
286 oidset_insert(filter_data->omits, &obj->oid);
287 return LOFR_MARK_SEEN; /* but not LOFR_DO_SHOW (hard omit) */
291 if (filter_data->omits)
292 oidset_remove(filter_data->omits, &obj->oid);
293 return LOFR_MARK_SEEN | LOFR_DO_SHOW;
296 static void filter_blobs_limit__init(
297 struct oidset *omitted,
298 struct list_objects_filter_options *filter_options,
299 struct filter *filter)
301 struct filter_blobs_limit_data *d = xcalloc(1, sizeof(*d));
303 d->max_bytes = filter_options->blob_limit_value;
305 filter->filter_data = d;
306 filter->filter_object_fn = filter_blobs_limit;
307 filter->free_fn = free;
311 * A filter driven by a sparse-checkout specification to only
312 * include blobs that a sparse checkout would populate.
314 * The sparse-checkout spec can be loaded from a blob with the
315 * given OID or from a local pathname. We allow an OID because
316 * the repo may be bare or we may be doing the filtering on the
321 * defval is the usual default include/exclude value that
322 * should be inherited as we recurse into directories based
323 * upon pattern matching of the directory itself or of a
324 * containing directory.
329 * 1 if the directory (recursively) contains any provisionally
332 * 0 if everything (recursively) contained in this directory
333 * has been explicitly included (SHOWN) in the result and
334 * the directory may be short-cut later in the traversal.
336 unsigned child_prov_omit : 1;
339 struct filter_sparse_data {
340 struct oidset *omits;
341 struct exclude_list el;
344 struct frame *array_frame;
347 static enum list_objects_filter_result filter_sparse(
348 struct repository *r,
349 enum list_objects_filter_situation filter_situation,
351 const char *pathname,
352 const char *filename,
355 struct filter_sparse_data *filter_data = filter_data_;
359 switch (filter_situation) {
361 BUG("unknown filter_situation: %d", filter_situation);
363 case LOFS_BEGIN_TREE:
364 assert(obj->type == OBJ_TREE);
366 val = is_excluded_from_list(pathname, strlen(pathname),
367 filename, &dtype, &filter_data->el,
370 val = filter_data->array_frame[filter_data->nr - 1].defval;
372 ALLOC_GROW(filter_data->array_frame, filter_data->nr + 1,
374 filter_data->array_frame[filter_data->nr].defval = val;
375 filter_data->array_frame[filter_data->nr].child_prov_omit = 0;
379 * A directory with this tree OID may appear in multiple
380 * places in the tree. (Think of a directory move or copy,
381 * with no other changes, so the OID is the same, but the
382 * full pathnames of objects within this directory are new
383 * and may match is_excluded() patterns differently.)
384 * So we cannot mark this directory as SEEN (yet), since
385 * that will prevent process_tree() from revisiting this
386 * tree object with other pathname prefixes.
388 * Only _DO_SHOW the tree object the first time we visit
391 * We always show all tree objects. A future optimization
392 * may want to attempt to narrow this.
394 if (obj->flags & FILTER_SHOWN_BUT_REVISIT)
396 obj->flags |= FILTER_SHOWN_BUT_REVISIT;
400 assert(obj->type == OBJ_TREE);
401 assert(filter_data->nr > 1);
403 frame = &filter_data->array_frame[--filter_data->nr];
406 * Tell our parent directory if any of our children were
407 * provisionally omitted.
409 filter_data->array_frame[filter_data->nr - 1].child_prov_omit |=
410 frame->child_prov_omit;
413 * If there are NO provisionally omitted child objects (ALL child
414 * objects in this folder were INCLUDED), then we can mark the
415 * folder as SEEN (so we will not have to revisit it again).
417 if (!frame->child_prov_omit)
418 return LOFR_MARK_SEEN;
422 assert(obj->type == OBJ_BLOB);
423 assert((obj->flags & SEEN) == 0);
425 frame = &filter_data->array_frame[filter_data->nr - 1];
428 val = is_excluded_from_list(pathname, strlen(pathname),
429 filename, &dtype, &filter_data->el,
434 if (filter_data->omits)
435 oidset_remove(filter_data->omits, &obj->oid);
436 return LOFR_MARK_SEEN | LOFR_DO_SHOW;
440 * Provisionally omit it. We've already established that
441 * this pathname is not in the sparse-checkout specification
442 * with the CURRENT pathname, so we *WANT* to omit this blob.
444 * However, a pathname elsewhere in the tree may also
445 * reference this same blob, so we cannot reject it yet.
446 * Leave the LOFR_ bits unset so that if the blob appears
447 * again in the traversal, we will be asked again.
449 if (filter_data->omits)
450 oidset_insert(filter_data->omits, &obj->oid);
453 * Remember that at least 1 blob in this tree was
454 * provisionally omitted. This prevents us from short
455 * cutting the tree in future iterations.
457 frame->child_prov_omit = 1;
463 static void filter_sparse_free(void *filter_data)
465 struct filter_sparse_data *d = filter_data;
466 free(d->array_frame);
470 static void filter_sparse_oid__init(
471 struct oidset *omitted,
472 struct list_objects_filter_options *filter_options,
473 struct filter *filter)
475 struct filter_sparse_data *d = xcalloc(1, sizeof(*d));
477 if (add_excludes_from_blob_to_list(filter_options->sparse_oid_value,
478 NULL, 0, &d->el) < 0)
479 die("could not load filter specification");
481 ALLOC_GROW(d->array_frame, d->nr + 1, d->alloc);
482 d->array_frame[d->nr].defval = 0; /* default to include */
483 d->array_frame[d->nr].child_prov_omit = 0;
486 filter->filter_data = d;
487 filter->filter_object_fn = filter_sparse;
488 filter->free_fn = filter_sparse_free;
491 typedef void (*filter_init_fn)(
492 struct oidset *omitted,
493 struct list_objects_filter_options *filter_options,
494 struct filter *filter);
497 * Must match "enum list_objects_filter_choice".
499 static filter_init_fn s_filters[] = {
501 filter_blobs_none__init,
502 filter_blobs_limit__init,
503 filter_trees_depth__init,
504 filter_sparse_oid__init,
507 struct filter *list_objects_filter__init(
508 struct oidset *omitted,
509 struct list_objects_filter_options *filter_options)
511 struct filter *filter;
512 filter_init_fn init_fn;
514 assert((sizeof(s_filters) / sizeof(s_filters[0])) == LOFC__COUNT);
516 if (filter_options->choice >= LOFC__COUNT)
517 BUG("invalid list-objects filter choice: %d",
518 filter_options->choice);
520 init_fn = s_filters[filter_options->choice];
524 filter = xcalloc(1, sizeof(*filter));
525 init_fn(omitted, filter_options, filter);
529 enum list_objects_filter_result list_objects_filter__filter_object(
530 struct repository *r,
531 enum list_objects_filter_situation filter_situation,
533 const char *pathname,
534 const char *filename,
535 struct filter *filter)
537 if (filter && (obj->flags & NOT_USER_GIVEN))
538 return filter->filter_object_fn(r, filter_situation, obj,
540 filter->filter_data);
542 * No filter is active or user gave object explicitly. In this case,
543 * always show the object (except when LOFS_END_TREE, since this tree
544 * had already been shown when LOFS_BEGIN_TREE).
546 if (filter_situation == LOFS_END_TREE)
548 return LOFR_MARK_SEEN | LOFR_DO_SHOW;
551 void list_objects_filter__free(struct filter *filter)
555 filter->free_fn(filter->filter_data);