10 #include "list-objects.h"
11 #include "list-objects-filter.h"
12 #include "list-objects-filter-options.h"
14 #include "object-store.h"
16 /* Remember to update object flag allocation in object.h */
18 * FILTER_SHOWN_BUT_REVISIT -- we set this bit on tree objects
19 * that have been shown, but should be revisited if they appear
20 * in the traversal (until we mark it SEEN). This is a way to
21 * let us silently de-dup calls to show() in the caller. This
22 * is subtly different from the "revision.h:SHOWN" and the
23 * "sha1-name.c:ONELINE_SEEN" bits. And also different from
24 * the non-de-dup usage in pack-bitmap.c
26 #define FILTER_SHOWN_BUT_REVISIT (1<<21)
29 * A filter for list-objects to omit ALL blobs from the traversal.
30 * And to OPTIONALLY collect a list of the omitted OIDs.
32 struct filter_blobs_none_data
{
36 static enum list_objects_filter_result
filter_blobs_none(
37 enum list_objects_filter_situation filter_situation
,
43 struct filter_blobs_none_data
*filter_data
= filter_data_
;
45 switch (filter_situation
) {
47 BUG("unknown filter_situation: %d", filter_situation
);
50 assert(obj
->type
== OBJ_TREE
);
51 /* always include all tree objects */
52 return LOFR_MARK_SEEN
| LOFR_DO_SHOW
;
55 assert(obj
->type
== OBJ_TREE
);
59 assert(obj
->type
== OBJ_BLOB
);
60 assert((obj
->flags
& SEEN
) == 0);
62 if (filter_data
->omits
)
63 oidset_insert(filter_data
->omits
, &obj
->oid
);
64 return LOFR_MARK_SEEN
; /* but not LOFR_DO_SHOW (hard omit) */
68 static void *filter_blobs_none__init(
69 struct oidset
*omitted
,
70 struct list_objects_filter_options
*filter_options
,
71 filter_object_fn
*filter_fn
,
72 filter_free_fn
*filter_free_fn
)
74 struct filter_blobs_none_data
*d
= xcalloc(1, sizeof(*d
));
77 *filter_fn
= filter_blobs_none
;
78 *filter_free_fn
= free
;
83 * A filter for list-objects to omit ALL trees and blobs from the traversal.
84 * Can OPTIONALLY collect a list of the omitted OIDs.
86 struct filter_trees_none_data
{
90 static enum list_objects_filter_result
filter_trees_none(
91 enum list_objects_filter_situation filter_situation
,
97 struct filter_trees_none_data
*filter_data
= filter_data_
;
99 switch (filter_situation
) {
101 BUG("unknown filter_situation: %d", filter_situation
);
103 case LOFS_BEGIN_TREE
:
105 if (filter_data
->omits
) {
106 oidset_insert(filter_data
->omits
, &obj
->oid
);
107 /* _MARK_SEEN but not _DO_SHOW (hard omit) */
108 return LOFR_MARK_SEEN
;
111 * Not collecting omits so no need to to traverse tree.
113 return LOFR_SKIP_TREE
| LOFR_MARK_SEEN
;
117 assert(obj
->type
== OBJ_TREE
);
123 static void* filter_trees_none__init(
124 struct oidset
*omitted
,
125 struct list_objects_filter_options
*filter_options
,
126 filter_object_fn
*filter_fn
,
127 filter_free_fn
*filter_free_fn
)
129 struct filter_trees_none_data
*d
= xcalloc(1, sizeof(*d
));
132 *filter_fn
= filter_trees_none
;
133 *filter_free_fn
= free
;
138 * A filter for list-objects to omit large blobs.
139 * And to OPTIONALLY collect a list of the omitted OIDs.
141 struct filter_blobs_limit_data
{
142 struct oidset
*omits
;
143 unsigned long max_bytes
;
146 static enum list_objects_filter_result
filter_blobs_limit(
147 enum list_objects_filter_situation filter_situation
,
149 const char *pathname
,
150 const char *filename
,
153 struct filter_blobs_limit_data
*filter_data
= filter_data_
;
154 unsigned long object_length
;
157 switch (filter_situation
) {
159 BUG("unknown filter_situation: %d", filter_situation
);
161 case LOFS_BEGIN_TREE
:
162 assert(obj
->type
== OBJ_TREE
);
163 /* always include all tree objects */
164 return LOFR_MARK_SEEN
| LOFR_DO_SHOW
;
167 assert(obj
->type
== OBJ_TREE
);
171 assert(obj
->type
== OBJ_BLOB
);
172 assert((obj
->flags
& SEEN
) == 0);
174 t
= oid_object_info(the_repository
, &obj
->oid
, &object_length
);
175 if (t
!= OBJ_BLOB
) { /* probably OBJ_NONE */
177 * We DO NOT have the blob locally, so we cannot
178 * apply the size filter criteria. Be conservative
179 * and force show it (and let the caller deal with
185 if (object_length
< filter_data
->max_bytes
)
188 if (filter_data
->omits
)
189 oidset_insert(filter_data
->omits
, &obj
->oid
);
190 return LOFR_MARK_SEEN
; /* but not LOFR_DO_SHOW (hard omit) */
194 if (filter_data
->omits
)
195 oidset_remove(filter_data
->omits
, &obj
->oid
);
196 return LOFR_MARK_SEEN
| LOFR_DO_SHOW
;
199 static void *filter_blobs_limit__init(
200 struct oidset
*omitted
,
201 struct list_objects_filter_options
*filter_options
,
202 filter_object_fn
*filter_fn
,
203 filter_free_fn
*filter_free_fn
)
205 struct filter_blobs_limit_data
*d
= xcalloc(1, sizeof(*d
));
207 d
->max_bytes
= filter_options
->blob_limit_value
;
209 *filter_fn
= filter_blobs_limit
;
210 *filter_free_fn
= free
;
215 * A filter driven by a sparse-checkout specification to only
216 * include blobs that a sparse checkout would populate.
218 * The sparse-checkout spec can be loaded from a blob with the
219 * given OID or from a local pathname. We allow an OID because
220 * the repo may be bare or we may be doing the filtering on the
225 * defval is the usual default include/exclude value that
226 * should be inherited as we recurse into directories based
227 * upon pattern matching of the directory itself or of a
228 * containing directory.
233 * 1 if the directory (recursively) contains any provisionally
236 * 0 if everything (recursively) contained in this directory
237 * has been explicitly included (SHOWN) in the result and
238 * the directory may be short-cut later in the traversal.
240 unsigned child_prov_omit
: 1;
243 struct filter_sparse_data
{
244 struct oidset
*omits
;
245 struct exclude_list el
;
248 struct frame
*array_frame
;
251 static enum list_objects_filter_result
filter_sparse(
252 enum list_objects_filter_situation filter_situation
,
254 const char *pathname
,
255 const char *filename
,
258 struct filter_sparse_data
*filter_data
= filter_data_
;
262 switch (filter_situation
) {
264 BUG("unknown filter_situation: %d", filter_situation
);
266 case LOFS_BEGIN_TREE
:
267 assert(obj
->type
== OBJ_TREE
);
269 val
= is_excluded_from_list(pathname
, strlen(pathname
),
270 filename
, &dtype
, &filter_data
->el
,
273 val
= filter_data
->array_frame
[filter_data
->nr
].defval
;
275 ALLOC_GROW(filter_data
->array_frame
, filter_data
->nr
+ 1,
278 filter_data
->array_frame
[filter_data
->nr
].defval
= val
;
279 filter_data
->array_frame
[filter_data
->nr
].child_prov_omit
= 0;
282 * A directory with this tree OID may appear in multiple
283 * places in the tree. (Think of a directory move or copy,
284 * with no other changes, so the OID is the same, but the
285 * full pathnames of objects within this directory are new
286 * and may match is_excluded() patterns differently.)
287 * So we cannot mark this directory as SEEN (yet), since
288 * that will prevent process_tree() from revisiting this
289 * tree object with other pathname prefixes.
291 * Only _DO_SHOW the tree object the first time we visit
294 * We always show all tree objects. A future optimization
295 * may want to attempt to narrow this.
297 if (obj
->flags
& FILTER_SHOWN_BUT_REVISIT
)
299 obj
->flags
|= FILTER_SHOWN_BUT_REVISIT
;
303 assert(obj
->type
== OBJ_TREE
);
304 assert(filter_data
->nr
> 0);
306 frame
= &filter_data
->array_frame
[filter_data
->nr
];
310 * Tell our parent directory if any of our children were
311 * provisionally omitted.
313 filter_data
->array_frame
[filter_data
->nr
].child_prov_omit
|=
314 frame
->child_prov_omit
;
317 * If there are NO provisionally omitted child objects (ALL child
318 * objects in this folder were INCLUDED), then we can mark the
319 * folder as SEEN (so we will not have to revisit it again).
321 if (!frame
->child_prov_omit
)
322 return LOFR_MARK_SEEN
;
326 assert(obj
->type
== OBJ_BLOB
);
327 assert((obj
->flags
& SEEN
) == 0);
329 frame
= &filter_data
->array_frame
[filter_data
->nr
];
332 val
= is_excluded_from_list(pathname
, strlen(pathname
),
333 filename
, &dtype
, &filter_data
->el
,
338 if (filter_data
->omits
)
339 oidset_remove(filter_data
->omits
, &obj
->oid
);
340 return LOFR_MARK_SEEN
| LOFR_DO_SHOW
;
344 * Provisionally omit it. We've already established that
345 * this pathname is not in the sparse-checkout specification
346 * with the CURRENT pathname, so we *WANT* to omit this blob.
348 * However, a pathname elsewhere in the tree may also
349 * reference this same blob, so we cannot reject it yet.
350 * Leave the LOFR_ bits unset so that if the blob appears
351 * again in the traversal, we will be asked again.
353 if (filter_data
->omits
)
354 oidset_insert(filter_data
->omits
, &obj
->oid
);
357 * Remember that at least 1 blob in this tree was
358 * provisionally omitted. This prevents us from short
359 * cutting the tree in future iterations.
361 frame
->child_prov_omit
= 1;
367 static void filter_sparse_free(void *filter_data
)
369 struct filter_sparse_data
*d
= filter_data
;
370 /* TODO free contents of 'd' */
374 static void *filter_sparse_oid__init(
375 struct oidset
*omitted
,
376 struct list_objects_filter_options
*filter_options
,
377 filter_object_fn
*filter_fn
,
378 filter_free_fn
*filter_free_fn
)
380 struct filter_sparse_data
*d
= xcalloc(1, sizeof(*d
));
382 if (add_excludes_from_blob_to_list(filter_options
->sparse_oid_value
,
383 NULL
, 0, &d
->el
) < 0)
384 die("could not load filter specification");
386 ALLOC_GROW(d
->array_frame
, d
->nr
+ 1, d
->alloc
);
387 d
->array_frame
[d
->nr
].defval
= 0; /* default to include */
388 d
->array_frame
[d
->nr
].child_prov_omit
= 0;
390 *filter_fn
= filter_sparse
;
391 *filter_free_fn
= filter_sparse_free
;
395 static void *filter_sparse_path__init(
396 struct oidset
*omitted
,
397 struct list_objects_filter_options
*filter_options
,
398 filter_object_fn
*filter_fn
,
399 filter_free_fn
*filter_free_fn
)
401 struct filter_sparse_data
*d
= xcalloc(1, sizeof(*d
));
403 if (add_excludes_from_file_to_list(filter_options
->sparse_path_value
,
404 NULL
, 0, &d
->el
, NULL
) < 0)
405 die("could not load filter specification");
407 ALLOC_GROW(d
->array_frame
, d
->nr
+ 1, d
->alloc
);
408 d
->array_frame
[d
->nr
].defval
= 0; /* default to include */
409 d
->array_frame
[d
->nr
].child_prov_omit
= 0;
411 *filter_fn
= filter_sparse
;
412 *filter_free_fn
= filter_sparse_free
;
416 typedef void *(*filter_init_fn
)(
417 struct oidset
*omitted
,
418 struct list_objects_filter_options
*filter_options
,
419 filter_object_fn
*filter_fn
,
420 filter_free_fn
*filter_free_fn
);
423 * Must match "enum list_objects_filter_choice".
425 static filter_init_fn s_filters
[] = {
427 filter_blobs_none__init
,
428 filter_blobs_limit__init
,
429 filter_trees_none__init
,
430 filter_sparse_oid__init
,
431 filter_sparse_path__init
,
434 void *list_objects_filter__init(
435 struct oidset
*omitted
,
436 struct list_objects_filter_options
*filter_options
,
437 filter_object_fn
*filter_fn
,
438 filter_free_fn
*filter_free_fn
)
440 filter_init_fn init_fn
;
442 assert((sizeof(s_filters
) / sizeof(s_filters
[0])) == LOFC__COUNT
);
444 if (filter_options
->choice
>= LOFC__COUNT
)
445 BUG("invalid list-objects filter choice: %d",
446 filter_options
->choice
);
448 init_fn
= s_filters
[filter_options
->choice
];
450 return init_fn(omitted
, filter_options
,
451 filter_fn
, filter_free_fn
);
453 *filter_free_fn
= NULL
;