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 return LOFR_MARK_SEEN; /* but not LOFR_DO_SHOW (hard omit) */
110 assert(obj->type == OBJ_TREE);
116 static void* filter_trees_none__init(
117 struct oidset *omitted,
118 struct list_objects_filter_options *filter_options,
119 filter_object_fn *filter_fn,
120 filter_free_fn *filter_free_fn)
122 struct filter_trees_none_data *d = xcalloc(1, sizeof(*d));
125 *filter_fn = filter_trees_none;
126 *filter_free_fn = free;
131 * A filter for list-objects to omit large blobs.
132 * And to OPTIONALLY collect a list of the omitted OIDs.
134 struct filter_blobs_limit_data {
135 struct oidset *omits;
136 unsigned long max_bytes;
139 static enum list_objects_filter_result filter_blobs_limit(
140 enum list_objects_filter_situation filter_situation,
142 const char *pathname,
143 const char *filename,
146 struct filter_blobs_limit_data *filter_data = filter_data_;
147 unsigned long object_length;
150 switch (filter_situation) {
152 BUG("unknown filter_situation: %d", filter_situation);
154 case LOFS_BEGIN_TREE:
155 assert(obj->type == OBJ_TREE);
156 /* always include all tree objects */
157 return LOFR_MARK_SEEN | LOFR_DO_SHOW;
160 assert(obj->type == OBJ_TREE);
164 assert(obj->type == OBJ_BLOB);
165 assert((obj->flags & SEEN) == 0);
167 t = oid_object_info(the_repository, &obj->oid, &object_length);
168 if (t != OBJ_BLOB) { /* probably OBJ_NONE */
170 * We DO NOT have the blob locally, so we cannot
171 * apply the size filter criteria. Be conservative
172 * and force show it (and let the caller deal with
178 if (object_length < filter_data->max_bytes)
181 if (filter_data->omits)
182 oidset_insert(filter_data->omits, &obj->oid);
183 return LOFR_MARK_SEEN; /* but not LOFR_DO_SHOW (hard omit) */
187 if (filter_data->omits)
188 oidset_remove(filter_data->omits, &obj->oid);
189 return LOFR_MARK_SEEN | LOFR_DO_SHOW;
192 static void *filter_blobs_limit__init(
193 struct oidset *omitted,
194 struct list_objects_filter_options *filter_options,
195 filter_object_fn *filter_fn,
196 filter_free_fn *filter_free_fn)
198 struct filter_blobs_limit_data *d = xcalloc(1, sizeof(*d));
200 d->max_bytes = filter_options->blob_limit_value;
202 *filter_fn = filter_blobs_limit;
203 *filter_free_fn = free;
208 * A filter driven by a sparse-checkout specification to only
209 * include blobs that a sparse checkout would populate.
211 * The sparse-checkout spec can be loaded from a blob with the
212 * given OID or from a local pathname. We allow an OID because
213 * the repo may be bare or we may be doing the filtering on the
218 * defval is the usual default include/exclude value that
219 * should be inherited as we recurse into directories based
220 * upon pattern matching of the directory itself or of a
221 * containing directory.
226 * 1 if the directory (recursively) contains any provisionally
229 * 0 if everything (recursively) contained in this directory
230 * has been explicitly included (SHOWN) in the result and
231 * the directory may be short-cut later in the traversal.
233 unsigned child_prov_omit : 1;
236 struct filter_sparse_data {
237 struct oidset *omits;
238 struct exclude_list el;
241 struct frame *array_frame;
244 static enum list_objects_filter_result filter_sparse(
245 enum list_objects_filter_situation filter_situation,
247 const char *pathname,
248 const char *filename,
251 struct filter_sparse_data *filter_data = filter_data_;
255 switch (filter_situation) {
257 BUG("unknown filter_situation: %d", filter_situation);
259 case LOFS_BEGIN_TREE:
260 assert(obj->type == OBJ_TREE);
262 val = is_excluded_from_list(pathname, strlen(pathname),
263 filename, &dtype, &filter_data->el,
266 val = filter_data->array_frame[filter_data->nr].defval;
268 ALLOC_GROW(filter_data->array_frame, filter_data->nr + 1,
271 filter_data->array_frame[filter_data->nr].defval = val;
272 filter_data->array_frame[filter_data->nr].child_prov_omit = 0;
275 * A directory with this tree OID may appear in multiple
276 * places in the tree. (Think of a directory move or copy,
277 * with no other changes, so the OID is the same, but the
278 * full pathnames of objects within this directory are new
279 * and may match is_excluded() patterns differently.)
280 * So we cannot mark this directory as SEEN (yet), since
281 * that will prevent process_tree() from revisiting this
282 * tree object with other pathname prefixes.
284 * Only _DO_SHOW the tree object the first time we visit
287 * We always show all tree objects. A future optimization
288 * may want to attempt to narrow this.
290 if (obj->flags & FILTER_SHOWN_BUT_REVISIT)
292 obj->flags |= FILTER_SHOWN_BUT_REVISIT;
296 assert(obj->type == OBJ_TREE);
297 assert(filter_data->nr > 0);
299 frame = &filter_data->array_frame[filter_data->nr];
303 * Tell our parent directory if any of our children were
304 * provisionally omitted.
306 filter_data->array_frame[filter_data->nr].child_prov_omit |=
307 frame->child_prov_omit;
310 * If there are NO provisionally omitted child objects (ALL child
311 * objects in this folder were INCLUDED), then we can mark the
312 * folder as SEEN (so we will not have to revisit it again).
314 if (!frame->child_prov_omit)
315 return LOFR_MARK_SEEN;
319 assert(obj->type == OBJ_BLOB);
320 assert((obj->flags & SEEN) == 0);
322 frame = &filter_data->array_frame[filter_data->nr];
325 val = is_excluded_from_list(pathname, strlen(pathname),
326 filename, &dtype, &filter_data->el,
331 if (filter_data->omits)
332 oidset_remove(filter_data->omits, &obj->oid);
333 return LOFR_MARK_SEEN | LOFR_DO_SHOW;
337 * Provisionally omit it. We've already established that
338 * this pathname is not in the sparse-checkout specification
339 * with the CURRENT pathname, so we *WANT* to omit this blob.
341 * However, a pathname elsewhere in the tree may also
342 * reference this same blob, so we cannot reject it yet.
343 * Leave the LOFR_ bits unset so that if the blob appears
344 * again in the traversal, we will be asked again.
346 if (filter_data->omits)
347 oidset_insert(filter_data->omits, &obj->oid);
350 * Remember that at least 1 blob in this tree was
351 * provisionally omitted. This prevents us from short
352 * cutting the tree in future iterations.
354 frame->child_prov_omit = 1;
360 static void filter_sparse_free(void *filter_data)
362 struct filter_sparse_data *d = filter_data;
363 /* TODO free contents of 'd' */
367 static void *filter_sparse_oid__init(
368 struct oidset *omitted,
369 struct list_objects_filter_options *filter_options,
370 filter_object_fn *filter_fn,
371 filter_free_fn *filter_free_fn)
373 struct filter_sparse_data *d = xcalloc(1, sizeof(*d));
375 if (add_excludes_from_blob_to_list(filter_options->sparse_oid_value,
376 NULL, 0, &d->el) < 0)
377 die("could not load filter specification");
379 ALLOC_GROW(d->array_frame, d->nr + 1, d->alloc);
380 d->array_frame[d->nr].defval = 0; /* default to include */
381 d->array_frame[d->nr].child_prov_omit = 0;
383 *filter_fn = filter_sparse;
384 *filter_free_fn = filter_sparse_free;
388 static void *filter_sparse_path__init(
389 struct oidset *omitted,
390 struct list_objects_filter_options *filter_options,
391 filter_object_fn *filter_fn,
392 filter_free_fn *filter_free_fn)
394 struct filter_sparse_data *d = xcalloc(1, sizeof(*d));
396 if (add_excludes_from_file_to_list(filter_options->sparse_path_value,
397 NULL, 0, &d->el, NULL) < 0)
398 die("could not load filter specification");
400 ALLOC_GROW(d->array_frame, d->nr + 1, d->alloc);
401 d->array_frame[d->nr].defval = 0; /* default to include */
402 d->array_frame[d->nr].child_prov_omit = 0;
404 *filter_fn = filter_sparse;
405 *filter_free_fn = filter_sparse_free;
409 typedef void *(*filter_init_fn)(
410 struct oidset *omitted,
411 struct list_objects_filter_options *filter_options,
412 filter_object_fn *filter_fn,
413 filter_free_fn *filter_free_fn);
416 * Must match "enum list_objects_filter_choice".
418 static filter_init_fn s_filters[] = {
420 filter_blobs_none__init,
421 filter_blobs_limit__init,
422 filter_trees_none__init,
423 filter_sparse_oid__init,
424 filter_sparse_path__init,
427 void *list_objects_filter__init(
428 struct oidset *omitted,
429 struct list_objects_filter_options *filter_options,
430 filter_object_fn *filter_fn,
431 filter_free_fn *filter_free_fn)
433 filter_init_fn init_fn;
435 assert((sizeof(s_filters) / sizeof(s_filters[0])) == LOFC__COUNT);
437 if (filter_options->choice >= LOFC__COUNT)
438 BUG("invalid list-objects filter choice: %d",
439 filter_options->choice);
441 init_fn = s_filters[filter_options->choice];
443 return init_fn(omitted, filter_options,
444 filter_fn, filter_free_fn);
446 *filter_free_fn = NULL;