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 die("unknown filter_situation");
51 assert(obj->type == OBJ_TREE);
52 /* always include all tree objects */
53 return LOFR_MARK_SEEN | LOFR_DO_SHOW;
56 assert(obj->type == OBJ_TREE);
60 assert(obj->type == OBJ_BLOB);
61 assert((obj->flags & SEEN) == 0);
63 if (filter_data->omits)
64 oidset_insert(filter_data->omits, &obj->oid);
65 return LOFR_MARK_SEEN; /* but not LOFR_DO_SHOW (hard omit) */
69 static void *filter_blobs_none__init(
70 struct oidset *omitted,
71 struct list_objects_filter_options *filter_options,
72 filter_object_fn *filter_fn,
73 filter_free_fn *filter_free_fn)
75 struct filter_blobs_none_data *d = xcalloc(1, sizeof(*d));
78 *filter_fn = filter_blobs_none;
79 *filter_free_fn = free;
84 * A filter for list-objects to omit large blobs.
85 * And to OPTIONALLY collect a list of the omitted OIDs.
87 struct filter_blobs_limit_data {
89 unsigned long max_bytes;
92 static enum list_objects_filter_result filter_blobs_limit(
93 enum list_objects_filter_situation filter_situation,
99 struct filter_blobs_limit_data *filter_data = filter_data_;
100 unsigned long object_length;
103 switch (filter_situation) {
105 die("unknown filter_situation");
108 case LOFS_BEGIN_TREE:
109 assert(obj->type == OBJ_TREE);
110 /* always include all tree objects */
111 return LOFR_MARK_SEEN | LOFR_DO_SHOW;
114 assert(obj->type == OBJ_TREE);
118 assert(obj->type == OBJ_BLOB);
119 assert((obj->flags & SEEN) == 0);
121 t = oid_object_info(the_repository, &obj->oid, &object_length);
122 if (t != OBJ_BLOB) { /* probably OBJ_NONE */
124 * We DO NOT have the blob locally, so we cannot
125 * apply the size filter criteria. Be conservative
126 * and force show it (and let the caller deal with
132 if (object_length < filter_data->max_bytes)
135 if (filter_data->omits)
136 oidset_insert(filter_data->omits, &obj->oid);
137 return LOFR_MARK_SEEN; /* but not LOFR_DO_SHOW (hard omit) */
141 if (filter_data->omits)
142 oidset_remove(filter_data->omits, &obj->oid);
143 return LOFR_MARK_SEEN | LOFR_DO_SHOW;
146 static void *filter_blobs_limit__init(
147 struct oidset *omitted,
148 struct list_objects_filter_options *filter_options,
149 filter_object_fn *filter_fn,
150 filter_free_fn *filter_free_fn)
152 struct filter_blobs_limit_data *d = xcalloc(1, sizeof(*d));
154 d->max_bytes = filter_options->blob_limit_value;
156 *filter_fn = filter_blobs_limit;
157 *filter_free_fn = free;
162 * A filter driven by a sparse-checkout specification to only
163 * include blobs that a sparse checkout would populate.
165 * The sparse-checkout spec can be loaded from a blob with the
166 * given OID or from a local pathname. We allow an OID because
167 * the repo may be bare or we may be doing the filtering on the
172 * defval is the usual default include/exclude value that
173 * should be inherited as we recurse into directories based
174 * upon pattern matching of the directory itself or of a
175 * containing directory.
180 * 1 if the directory (recursively) contains any provisionally
183 * 0 if everything (recursively) contained in this directory
184 * has been explicitly included (SHOWN) in the result and
185 * the directory may be short-cut later in the traversal.
187 unsigned child_prov_omit : 1;
190 struct filter_sparse_data {
191 struct oidset *omits;
192 struct exclude_list el;
195 struct frame *array_frame;
198 static enum list_objects_filter_result filter_sparse(
199 enum list_objects_filter_situation filter_situation,
201 const char *pathname,
202 const char *filename,
205 struct filter_sparse_data *filter_data = filter_data_;
209 switch (filter_situation) {
211 die("unknown filter_situation");
214 case LOFS_BEGIN_TREE:
215 assert(obj->type == OBJ_TREE);
217 val = is_excluded_from_list(pathname, strlen(pathname),
218 filename, &dtype, &filter_data->el,
221 val = filter_data->array_frame[filter_data->nr].defval;
223 ALLOC_GROW(filter_data->array_frame, filter_data->nr + 1,
226 filter_data->array_frame[filter_data->nr].defval = val;
227 filter_data->array_frame[filter_data->nr].child_prov_omit = 0;
230 * A directory with this tree OID may appear in multiple
231 * places in the tree. (Think of a directory move or copy,
232 * with no other changes, so the OID is the same, but the
233 * full pathnames of objects within this directory are new
234 * and may match is_excluded() patterns differently.)
235 * So we cannot mark this directory as SEEN (yet), since
236 * that will prevent process_tree() from revisiting this
237 * tree object with other pathname prefixes.
239 * Only _DO_SHOW the tree object the first time we visit
242 * We always show all tree objects. A future optimization
243 * may want to attempt to narrow this.
245 if (obj->flags & FILTER_SHOWN_BUT_REVISIT)
247 obj->flags |= FILTER_SHOWN_BUT_REVISIT;
251 assert(obj->type == OBJ_TREE);
252 assert(filter_data->nr > 0);
254 frame = &filter_data->array_frame[filter_data->nr];
258 * Tell our parent directory if any of our children were
259 * provisionally omitted.
261 filter_data->array_frame[filter_data->nr].child_prov_omit |=
262 frame->child_prov_omit;
265 * If there are NO provisionally omitted child objects (ALL child
266 * objects in this folder were INCLUDED), then we can mark the
267 * folder as SEEN (so we will not have to revisit it again).
269 if (!frame->child_prov_omit)
270 return LOFR_MARK_SEEN;
274 assert(obj->type == OBJ_BLOB);
275 assert((obj->flags & SEEN) == 0);
277 frame = &filter_data->array_frame[filter_data->nr];
280 val = is_excluded_from_list(pathname, strlen(pathname),
281 filename, &dtype, &filter_data->el,
286 if (filter_data->omits)
287 oidset_remove(filter_data->omits, &obj->oid);
288 return LOFR_MARK_SEEN | LOFR_DO_SHOW;
292 * Provisionally omit it. We've already established that
293 * this pathname is not in the sparse-checkout specification
294 * with the CURRENT pathname, so we *WANT* to omit this blob.
296 * However, a pathname elsewhere in the tree may also
297 * reference this same blob, so we cannot reject it yet.
298 * Leave the LOFR_ bits unset so that if the blob appears
299 * again in the traversal, we will be asked again.
301 if (filter_data->omits)
302 oidset_insert(filter_data->omits, &obj->oid);
305 * Remember that at least 1 blob in this tree was
306 * provisionally omitted. This prevents us from short
307 * cutting the tree in future iterations.
309 frame->child_prov_omit = 1;
315 static void filter_sparse_free(void *filter_data)
317 struct filter_sparse_data *d = filter_data;
318 /* TODO free contents of 'd' */
322 static void *filter_sparse_oid__init(
323 struct oidset *omitted,
324 struct list_objects_filter_options *filter_options,
325 filter_object_fn *filter_fn,
326 filter_free_fn *filter_free_fn)
328 struct filter_sparse_data *d = xcalloc(1, sizeof(*d));
330 if (add_excludes_from_blob_to_list(filter_options->sparse_oid_value,
331 NULL, 0, &d->el) < 0)
332 die("could not load filter specification");
334 ALLOC_GROW(d->array_frame, d->nr + 1, d->alloc);
335 d->array_frame[d->nr].defval = 0; /* default to include */
336 d->array_frame[d->nr].child_prov_omit = 0;
338 *filter_fn = filter_sparse;
339 *filter_free_fn = filter_sparse_free;
343 static void *filter_sparse_path__init(
344 struct oidset *omitted,
345 struct list_objects_filter_options *filter_options,
346 filter_object_fn *filter_fn,
347 filter_free_fn *filter_free_fn)
349 struct filter_sparse_data *d = xcalloc(1, sizeof(*d));
351 if (add_excludes_from_file_to_list(filter_options->sparse_path_value,
352 NULL, 0, &d->el, NULL) < 0)
353 die("could not load filter specification");
355 ALLOC_GROW(d->array_frame, d->nr + 1, d->alloc);
356 d->array_frame[d->nr].defval = 0; /* default to include */
357 d->array_frame[d->nr].child_prov_omit = 0;
359 *filter_fn = filter_sparse;
360 *filter_free_fn = filter_sparse_free;
364 typedef void *(*filter_init_fn)(
365 struct oidset *omitted,
366 struct list_objects_filter_options *filter_options,
367 filter_object_fn *filter_fn,
368 filter_free_fn *filter_free_fn);
371 * Must match "enum list_objects_filter_choice".
373 static filter_init_fn s_filters[] = {
375 filter_blobs_none__init,
376 filter_blobs_limit__init,
377 filter_sparse_oid__init,
378 filter_sparse_path__init,
381 void *list_objects_filter__init(
382 struct oidset *omitted,
383 struct list_objects_filter_options *filter_options,
384 filter_object_fn *filter_fn,
385 filter_free_fn *filter_free_fn)
387 filter_init_fn init_fn;
389 assert((sizeof(s_filters) / sizeof(s_filters[0])) == LOFC__COUNT);
391 if (filter_options->choice >= LOFC__COUNT)
392 die("invalid list-objects filter choice: %d",
393 filter_options->choice);
395 init_fn = s_filters[filter_options->choice];
397 return init_fn(omitted, filter_options,
398 filter_fn, filter_free_fn);
400 *filter_free_fn = NULL;