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 * A filter for list-objects to omit ALL blobs from the traversal.
31 * And to OPTIONALLY collect a list of the omitted OIDs.
33 struct filter_blobs_none_data
{
37 static enum list_objects_filter_result
filter_blobs_none(
39 enum list_objects_filter_situation filter_situation
,
45 struct filter_blobs_none_data
*filter_data
= filter_data_
;
47 switch (filter_situation
) {
49 BUG("unknown filter_situation: %d", filter_situation
);
52 assert(obj
->type
== OBJ_TREE
);
53 /* always include all tree objects */
54 return LOFR_MARK_SEEN
| LOFR_DO_SHOW
;
57 assert(obj
->type
== OBJ_TREE
);
61 assert(obj
->type
== OBJ_BLOB
);
62 assert((obj
->flags
& SEEN
) == 0);
64 if (filter_data
->omits
)
65 oidset_insert(filter_data
->omits
, &obj
->oid
);
66 return LOFR_MARK_SEEN
; /* but not LOFR_DO_SHOW (hard omit) */
70 static void *filter_blobs_none__init(
71 struct oidset
*omitted
,
72 struct list_objects_filter_options
*filter_options
,
73 filter_object_fn
*filter_fn
,
74 filter_free_fn
*filter_free_fn
)
76 struct filter_blobs_none_data
*d
= xcalloc(1, sizeof(*d
));
79 *filter_fn
= filter_blobs_none
;
80 *filter_free_fn
= free
;
85 * A filter for list-objects to omit ALL trees and blobs from the traversal.
86 * Can OPTIONALLY collect a list of the omitted OIDs.
88 struct filter_trees_depth_data
{
92 * Maps trees to the minimum depth at which they were seen. It is not
93 * necessary to re-traverse a tree at deeper or equal depths than it has
94 * already been traversed.
96 * We can't use LOFR_MARK_SEEN for tree objects since this will prevent
97 * it from being traversed at shallower depths.
99 struct oidmap seen_at_depth
;
101 unsigned long exclude_depth
;
102 unsigned long current_depth
;
105 struct seen_map_entry
{
106 struct oidmap_entry base
;
110 static void filter_trees_update_omits(
112 struct filter_trees_depth_data
*filter_data
,
115 if (!filter_data
->omits
)
119 oidset_remove(filter_data
->omits
, &obj
->oid
);
121 oidset_insert(filter_data
->omits
, &obj
->oid
);
124 static enum list_objects_filter_result
filter_trees_depth(
125 struct repository
*r
,
126 enum list_objects_filter_situation filter_situation
,
128 const char *pathname
,
129 const char *filename
,
132 struct filter_trees_depth_data
*filter_data
= filter_data_
;
133 struct seen_map_entry
*seen_info
;
134 int include_it
= filter_data
->current_depth
<
135 filter_data
->exclude_depth
;
140 * Note that we do not use _MARK_SEEN in order to allow re-traversal in
141 * case we encounter a tree or blob again at a shallower depth.
144 switch (filter_situation
) {
146 BUG("unknown filter_situation: %d", filter_situation
);
149 assert(obj
->type
== OBJ_TREE
);
150 filter_data
->current_depth
--;
154 filter_trees_update_omits(obj
, filter_data
, include_it
);
155 return include_it
? LOFR_MARK_SEEN
| LOFR_DO_SHOW
: LOFR_ZERO
;
157 case LOFS_BEGIN_TREE
:
158 seen_info
= oidmap_get(
159 &filter_data
->seen_at_depth
, &obj
->oid
);
161 seen_info
= xcalloc(1, sizeof(*seen_info
));
162 oidcpy(&seen_info
->base
.oid
, &obj
->oid
);
163 seen_info
->depth
= filter_data
->current_depth
;
164 oidmap_put(&filter_data
->seen_at_depth
, seen_info
);
168 filter_data
->current_depth
>= seen_info
->depth
;
172 filter_res
= LOFR_SKIP_TREE
;
174 seen_info
->depth
= filter_data
->current_depth
;
175 filter_trees_update_omits(obj
, filter_data
, include_it
);
178 filter_res
= LOFR_DO_SHOW
;
179 else if (filter_data
->omits
)
180 filter_res
= LOFR_ZERO
;
182 filter_res
= LOFR_SKIP_TREE
;
185 filter_data
->current_depth
++;
190 static void filter_trees_free(void *filter_data
) {
191 struct filter_trees_depth_data
*d
= filter_data
;
194 oidmap_free(&d
->seen_at_depth
, 1);
198 static void *filter_trees_depth__init(
199 struct oidset
*omitted
,
200 struct list_objects_filter_options
*filter_options
,
201 filter_object_fn
*filter_fn
,
202 filter_free_fn
*filter_free_fn
)
204 struct filter_trees_depth_data
*d
= xcalloc(1, sizeof(*d
));
206 oidmap_init(&d
->seen_at_depth
, 0);
207 d
->exclude_depth
= filter_options
->tree_exclude_depth
;
208 d
->current_depth
= 0;
210 *filter_fn
= filter_trees_depth
;
211 *filter_free_fn
= filter_trees_free
;
216 * A filter for list-objects to omit large blobs.
217 * And to OPTIONALLY collect a list of the omitted OIDs.
219 struct filter_blobs_limit_data
{
220 struct oidset
*omits
;
221 unsigned long max_bytes
;
224 static enum list_objects_filter_result
filter_blobs_limit(
225 struct repository
*r
,
226 enum list_objects_filter_situation filter_situation
,
228 const char *pathname
,
229 const char *filename
,
232 struct filter_blobs_limit_data
*filter_data
= filter_data_
;
233 unsigned long object_length
;
236 switch (filter_situation
) {
238 BUG("unknown filter_situation: %d", filter_situation
);
240 case LOFS_BEGIN_TREE
:
241 assert(obj
->type
== OBJ_TREE
);
242 /* always include all tree objects */
243 return LOFR_MARK_SEEN
| LOFR_DO_SHOW
;
246 assert(obj
->type
== OBJ_TREE
);
250 assert(obj
->type
== OBJ_BLOB
);
251 assert((obj
->flags
& SEEN
) == 0);
253 t
= oid_object_info(r
, &obj
->oid
, &object_length
);
254 if (t
!= OBJ_BLOB
) { /* probably OBJ_NONE */
256 * We DO NOT have the blob locally, so we cannot
257 * apply the size filter criteria. Be conservative
258 * and force show it (and let the caller deal with
264 if (object_length
< filter_data
->max_bytes
)
267 if (filter_data
->omits
)
268 oidset_insert(filter_data
->omits
, &obj
->oid
);
269 return LOFR_MARK_SEEN
; /* but not LOFR_DO_SHOW (hard omit) */
273 if (filter_data
->omits
)
274 oidset_remove(filter_data
->omits
, &obj
->oid
);
275 return LOFR_MARK_SEEN
| LOFR_DO_SHOW
;
278 static void *filter_blobs_limit__init(
279 struct oidset
*omitted
,
280 struct list_objects_filter_options
*filter_options
,
281 filter_object_fn
*filter_fn
,
282 filter_free_fn
*filter_free_fn
)
284 struct filter_blobs_limit_data
*d
= xcalloc(1, sizeof(*d
));
286 d
->max_bytes
= filter_options
->blob_limit_value
;
288 *filter_fn
= filter_blobs_limit
;
289 *filter_free_fn
= free
;
294 * A filter driven by a sparse-checkout specification to only
295 * include blobs that a sparse checkout would populate.
297 * The sparse-checkout spec can be loaded from a blob with the
298 * given OID or from a local pathname. We allow an OID because
299 * the repo may be bare or we may be doing the filtering on the
304 * defval is the usual default include/exclude value that
305 * should be inherited as we recurse into directories based
306 * upon pattern matching of the directory itself or of a
307 * containing directory.
312 * 1 if the directory (recursively) contains any provisionally
315 * 0 if everything (recursively) contained in this directory
316 * has been explicitly included (SHOWN) in the result and
317 * the directory may be short-cut later in the traversal.
319 unsigned child_prov_omit
: 1;
322 struct filter_sparse_data
{
323 struct oidset
*omits
;
324 struct exclude_list el
;
327 struct frame
*array_frame
;
330 static enum list_objects_filter_result
filter_sparse(
331 struct repository
*r
,
332 enum list_objects_filter_situation filter_situation
,
334 const char *pathname
,
335 const char *filename
,
338 struct filter_sparse_data
*filter_data
= filter_data_
;
342 switch (filter_situation
) {
344 BUG("unknown filter_situation: %d", filter_situation
);
346 case LOFS_BEGIN_TREE
:
347 assert(obj
->type
== OBJ_TREE
);
349 val
= is_excluded_from_list(pathname
, strlen(pathname
),
350 filename
, &dtype
, &filter_data
->el
,
353 val
= filter_data
->array_frame
[filter_data
->nr
].defval
;
355 ALLOC_GROW(filter_data
->array_frame
, filter_data
->nr
+ 1,
358 filter_data
->array_frame
[filter_data
->nr
].defval
= val
;
359 filter_data
->array_frame
[filter_data
->nr
].child_prov_omit
= 0;
362 * A directory with this tree OID may appear in multiple
363 * places in the tree. (Think of a directory move or copy,
364 * with no other changes, so the OID is the same, but the
365 * full pathnames of objects within this directory are new
366 * and may match is_excluded() patterns differently.)
367 * So we cannot mark this directory as SEEN (yet), since
368 * that will prevent process_tree() from revisiting this
369 * tree object with other pathname prefixes.
371 * Only _DO_SHOW the tree object the first time we visit
374 * We always show all tree objects. A future optimization
375 * may want to attempt to narrow this.
377 if (obj
->flags
& FILTER_SHOWN_BUT_REVISIT
)
379 obj
->flags
|= FILTER_SHOWN_BUT_REVISIT
;
383 assert(obj
->type
== OBJ_TREE
);
384 assert(filter_data
->nr
> 0);
386 frame
= &filter_data
->array_frame
[filter_data
->nr
];
390 * Tell our parent directory if any of our children were
391 * provisionally omitted.
393 filter_data
->array_frame
[filter_data
->nr
].child_prov_omit
|=
394 frame
->child_prov_omit
;
397 * If there are NO provisionally omitted child objects (ALL child
398 * objects in this folder were INCLUDED), then we can mark the
399 * folder as SEEN (so we will not have to revisit it again).
401 if (!frame
->child_prov_omit
)
402 return LOFR_MARK_SEEN
;
406 assert(obj
->type
== OBJ_BLOB
);
407 assert((obj
->flags
& SEEN
) == 0);
409 frame
= &filter_data
->array_frame
[filter_data
->nr
];
412 val
= is_excluded_from_list(pathname
, strlen(pathname
),
413 filename
, &dtype
, &filter_data
->el
,
418 if (filter_data
->omits
)
419 oidset_remove(filter_data
->omits
, &obj
->oid
);
420 return LOFR_MARK_SEEN
| LOFR_DO_SHOW
;
424 * Provisionally omit it. We've already established that
425 * this pathname is not in the sparse-checkout specification
426 * with the CURRENT pathname, so we *WANT* to omit this blob.
428 * However, a pathname elsewhere in the tree may also
429 * reference this same blob, so we cannot reject it yet.
430 * Leave the LOFR_ bits unset so that if the blob appears
431 * again in the traversal, we will be asked again.
433 if (filter_data
->omits
)
434 oidset_insert(filter_data
->omits
, &obj
->oid
);
437 * Remember that at least 1 blob in this tree was
438 * provisionally omitted. This prevents us from short
439 * cutting the tree in future iterations.
441 frame
->child_prov_omit
= 1;
447 static void filter_sparse_free(void *filter_data
)
449 struct filter_sparse_data
*d
= filter_data
;
450 /* TODO free contents of 'd' */
454 static void *filter_sparse_oid__init(
455 struct oidset
*omitted
,
456 struct list_objects_filter_options
*filter_options
,
457 filter_object_fn
*filter_fn
,
458 filter_free_fn
*filter_free_fn
)
460 struct filter_sparse_data
*d
= xcalloc(1, sizeof(*d
));
462 if (add_excludes_from_blob_to_list(filter_options
->sparse_oid_value
,
463 NULL
, 0, &d
->el
) < 0)
464 die("could not load filter specification");
466 ALLOC_GROW(d
->array_frame
, d
->nr
+ 1, d
->alloc
);
467 d
->array_frame
[d
->nr
].defval
= 0; /* default to include */
468 d
->array_frame
[d
->nr
].child_prov_omit
= 0;
470 *filter_fn
= filter_sparse
;
471 *filter_free_fn
= filter_sparse_free
;
475 static void *filter_sparse_path__init(
476 struct oidset
*omitted
,
477 struct list_objects_filter_options
*filter_options
,
478 filter_object_fn
*filter_fn
,
479 filter_free_fn
*filter_free_fn
)
481 struct filter_sparse_data
*d
= xcalloc(1, sizeof(*d
));
483 if (add_excludes_from_file_to_list(filter_options
->sparse_path_value
,
484 NULL
, 0, &d
->el
, NULL
) < 0)
485 die("could not load filter specification");
487 ALLOC_GROW(d
->array_frame
, d
->nr
+ 1, d
->alloc
);
488 d
->array_frame
[d
->nr
].defval
= 0; /* default to include */
489 d
->array_frame
[d
->nr
].child_prov_omit
= 0;
491 *filter_fn
= filter_sparse
;
492 *filter_free_fn
= filter_sparse_free
;
496 typedef void *(*filter_init_fn
)(
497 struct oidset
*omitted
,
498 struct list_objects_filter_options
*filter_options
,
499 filter_object_fn
*filter_fn
,
500 filter_free_fn
*filter_free_fn
);
503 * Must match "enum list_objects_filter_choice".
505 static filter_init_fn s_filters
[] = {
507 filter_blobs_none__init
,
508 filter_blobs_limit__init
,
509 filter_trees_depth__init
,
510 filter_sparse_oid__init
,
511 filter_sparse_path__init
,
514 void *list_objects_filter__init(
515 struct oidset
*omitted
,
516 struct list_objects_filter_options
*filter_options
,
517 filter_object_fn
*filter_fn
,
518 filter_free_fn
*filter_free_fn
)
520 filter_init_fn init_fn
;
522 assert((sizeof(s_filters
) / sizeof(s_filters
[0])) == LOFC__COUNT
);
524 if (filter_options
->choice
>= LOFC__COUNT
)
525 BUG("invalid list-objects filter choice: %d",
526 filter_options
->choice
);
528 init_fn
= s_filters
[filter_options
->choice
];
530 return init_fn(omitted
, filter_options
,
531 filter_fn
, filter_free_fn
);
533 *filter_free_fn
= NULL
;