]>
Commit | Line | Data |
---|---|---|
25ec7bca | 1 | #include "cache.h" |
36bf1958 | 2 | #include "alloc.h" |
25ec7bca | 3 | #include "dir.h" |
41771fa4 | 4 | #include "hex.h" |
25ec7bca JH |
5 | #include "tag.h" |
6 | #include "commit.h" | |
7 | #include "tree.h" | |
8 | #include "blob.h" | |
9 | #include "diff.h" | |
10 | #include "tree-walk.h" | |
11 | #include "revision.h" | |
12 | #include "list-objects.h" | |
13 | #include "list-objects-filter.h" | |
14 | #include "list-objects-filter-options.h" | |
c813a7c3 | 15 | #include "oidmap.h" |
25ec7bca | 16 | #include "oidset.h" |
cbd53a21 | 17 | #include "object-store.h" |
25ec7bca JH |
18 | |
19 | /* Remember to update object flag allocation in object.h */ | |
20 | /* | |
21 | * FILTER_SHOWN_BUT_REVISIT -- we set this bit on tree objects | |
22 | * that have been shown, but should be revisited if they appear | |
23 | * in the traversal (until we mark it SEEN). This is a way to | |
24 | * let us silently de-dup calls to show() in the caller. This | |
25 | * is subtly different from the "revision.h:SHOWN" and the | |
1e6771e5 | 26 | * "object-name.c:ONELINE_SEEN" bits. And also different from |
25ec7bca JH |
27 | * the non-de-dup usage in pack-bitmap.c |
28 | */ | |
29 | #define FILTER_SHOWN_BUT_REVISIT (1<<21) | |
30 | ||
e987df5f MD |
31 | struct subfilter { |
32 | struct filter *filter; | |
33 | struct oidset seen; | |
34 | struct oidset omits; | |
35 | struct object_id skip_tree; | |
36 | unsigned is_skipping_tree : 1; | |
37 | }; | |
38 | ||
9430147c MD |
39 | struct filter { |
40 | enum list_objects_filter_result (*filter_object_fn)( | |
41 | struct repository *r, | |
42 | enum list_objects_filter_situation filter_situation, | |
43 | struct object *obj, | |
44 | const char *pathname, | |
45 | const char *filename, | |
7a7c7f4a | 46 | struct oidset *omits, |
9430147c MD |
47 | void *filter_data); |
48 | ||
e987df5f MD |
49 | /* |
50 | * Optional. If this function is supplied and the filter needs | |
51 | * to collect omits, then this function is called once before | |
52 | * free_fn is called. | |
53 | * | |
54 | * This is required because the following two conditions hold: | |
55 | * | |
56 | * a. A tree filter can add and remove objects as an object | |
57 | * graph is traversed. | |
58 | * b. A combine filter's omit set is the union of all its | |
59 | * subfilters, which may include tree: filters. | |
60 | * | |
61 | * As such, the omits sets must be separate sets, and can only | |
62 | * be unioned after the traversal is completed. | |
63 | */ | |
64 | void (*finalize_omits_fn)(struct oidset *omits, void *filter_data); | |
65 | ||
9430147c MD |
66 | void (*free_fn)(void *filter_data); |
67 | ||
68 | void *filter_data; | |
9430147c | 69 | |
7a7c7f4a | 70 | /* If non-NULL, the filter collects a list of the omitted OIDs here. */ |
25ec7bca JH |
71 | struct oidset *omits; |
72 | }; | |
73 | ||
74 | static enum list_objects_filter_result filter_blobs_none( | |
d3beb61f | 75 | struct repository *r UNUSED, |
25ec7bca JH |
76 | enum list_objects_filter_situation filter_situation, |
77 | struct object *obj, | |
d3beb61f JK |
78 | const char *pathname UNUSED, |
79 | const char *filename UNUSED, | |
7a7c7f4a | 80 | struct oidset *omits, |
d3beb61f | 81 | void *filter_data_ UNUSED) |
25ec7bca | 82 | { |
25ec7bca JH |
83 | switch (filter_situation) { |
84 | default: | |
696aa739 | 85 | BUG("unknown filter_situation: %d", filter_situation); |
25ec7bca | 86 | |
9a2a4f95 PS |
87 | case LOFS_TAG: |
88 | assert(obj->type == OBJ_TAG); | |
89 | /* always include all tag objects */ | |
90 | return LOFR_MARK_SEEN | LOFR_DO_SHOW; | |
91 | ||
92 | case LOFS_COMMIT: | |
93 | assert(obj->type == OBJ_COMMIT); | |
94 | /* always include all commit objects */ | |
95 | return LOFR_MARK_SEEN | LOFR_DO_SHOW; | |
96 | ||
25ec7bca JH |
97 | case LOFS_BEGIN_TREE: |
98 | assert(obj->type == OBJ_TREE); | |
99 | /* always include all tree objects */ | |
100 | return LOFR_MARK_SEEN | LOFR_DO_SHOW; | |
101 | ||
102 | case LOFS_END_TREE: | |
103 | assert(obj->type == OBJ_TREE); | |
104 | return LOFR_ZERO; | |
105 | ||
106 | case LOFS_BLOB: | |
107 | assert(obj->type == OBJ_BLOB); | |
108 | assert((obj->flags & SEEN) == 0); | |
109 | ||
7a7c7f4a MD |
110 | if (omits) |
111 | oidset_insert(omits, &obj->oid); | |
25ec7bca JH |
112 | return LOFR_MARK_SEEN; /* but not LOFR_DO_SHOW (hard omit) */ |
113 | } | |
114 | } | |
115 | ||
9430147c | 116 | static void filter_blobs_none__init( |
d3beb61f | 117 | struct list_objects_filter_options *filter_options UNUSED, |
9430147c | 118 | struct filter *filter) |
25ec7bca | 119 | { |
9430147c MD |
120 | filter->filter_object_fn = filter_blobs_none; |
121 | filter->free_fn = free; | |
25ec7bca JH |
122 | } |
123 | ||
bc5975d2 MD |
124 | /* |
125 | * A filter for list-objects to omit ALL trees and blobs from the traversal. | |
126 | * Can OPTIONALLY collect a list of the omitted OIDs. | |
127 | */ | |
c813a7c3 | 128 | struct filter_trees_depth_data { |
c813a7c3 MD |
129 | /* |
130 | * Maps trees to the minimum depth at which they were seen. It is not | |
131 | * necessary to re-traverse a tree at deeper or equal depths than it has | |
132 | * already been traversed. | |
133 | * | |
134 | * We can't use LOFR_MARK_SEEN for tree objects since this will prevent | |
135 | * it from being traversed at shallower depths. | |
136 | */ | |
137 | struct oidmap seen_at_depth; | |
138 | ||
139 | unsigned long exclude_depth; | |
140 | unsigned long current_depth; | |
bc5975d2 MD |
141 | }; |
142 | ||
c813a7c3 MD |
143 | struct seen_map_entry { |
144 | struct oidmap_entry base; | |
145 | size_t depth; | |
146 | }; | |
147 | ||
8272f260 MD |
148 | /* Returns 1 if the oid was in the omits set before it was invoked. */ |
149 | static int filter_trees_update_omits( | |
c813a7c3 | 150 | struct object *obj, |
7a7c7f4a | 151 | struct oidset *omits, |
c813a7c3 MD |
152 | int include_it) |
153 | { | |
7a7c7f4a | 154 | if (!omits) |
8272f260 | 155 | return 0; |
c813a7c3 MD |
156 | |
157 | if (include_it) | |
7a7c7f4a | 158 | return oidset_remove(omits, &obj->oid); |
c813a7c3 | 159 | else |
7a7c7f4a | 160 | return oidset_insert(omits, &obj->oid); |
c813a7c3 MD |
161 | } |
162 | ||
163 | static enum list_objects_filter_result filter_trees_depth( | |
d3beb61f | 164 | struct repository *r UNUSED, |
bc5975d2 MD |
165 | enum list_objects_filter_situation filter_situation, |
166 | struct object *obj, | |
d3beb61f JK |
167 | const char *pathname UNUSED, |
168 | const char *filename UNUSED, | |
7a7c7f4a | 169 | struct oidset *omits, |
bc5975d2 MD |
170 | void *filter_data_) |
171 | { | |
c813a7c3 MD |
172 | struct filter_trees_depth_data *filter_data = filter_data_; |
173 | struct seen_map_entry *seen_info; | |
174 | int include_it = filter_data->current_depth < | |
175 | filter_data->exclude_depth; | |
176 | int filter_res; | |
177 | int already_seen; | |
178 | ||
179 | /* | |
180 | * Note that we do not use _MARK_SEEN in order to allow re-traversal in | |
181 | * case we encounter a tree or blob again at a shallower depth. | |
182 | */ | |
bc5975d2 MD |
183 | |
184 | switch (filter_situation) { | |
185 | default: | |
186 | BUG("unknown filter_situation: %d", filter_situation); | |
187 | ||
9a2a4f95 PS |
188 | case LOFS_TAG: |
189 | assert(obj->type == OBJ_TAG); | |
190 | /* always include all tag objects */ | |
191 | return LOFR_MARK_SEEN | LOFR_DO_SHOW; | |
192 | ||
193 | case LOFS_COMMIT: | |
194 | assert(obj->type == OBJ_COMMIT); | |
195 | /* always include all commit objects */ | |
196 | return LOFR_MARK_SEEN | LOFR_DO_SHOW; | |
197 | ||
c813a7c3 MD |
198 | case LOFS_END_TREE: |
199 | assert(obj->type == OBJ_TREE); | |
200 | filter_data->current_depth--; | |
201 | return LOFR_ZERO; | |
202 | ||
bc5975d2 | 203 | case LOFS_BLOB: |
7a7c7f4a | 204 | filter_trees_update_omits(obj, omits, include_it); |
c813a7c3 MD |
205 | return include_it ? LOFR_MARK_SEEN | LOFR_DO_SHOW : LOFR_ZERO; |
206 | ||
207 | case LOFS_BEGIN_TREE: | |
208 | seen_info = oidmap_get( | |
209 | &filter_data->seen_at_depth, &obj->oid); | |
210 | if (!seen_info) { | |
ca56dadb | 211 | CALLOC_ARRAY(seen_info, 1); |
c813a7c3 MD |
212 | oidcpy(&seen_info->base.oid, &obj->oid); |
213 | seen_info->depth = filter_data->current_depth; | |
214 | oidmap_put(&filter_data->seen_at_depth, seen_info); | |
215 | already_seen = 0; | |
8b10a206 | 216 | } else { |
c813a7c3 MD |
217 | already_seen = |
218 | filter_data->current_depth >= seen_info->depth; | |
8b10a206 | 219 | } |
bc5975d2 | 220 | |
c813a7c3 MD |
221 | if (already_seen) { |
222 | filter_res = LOFR_SKIP_TREE; | |
223 | } else { | |
8272f260 | 224 | int been_omitted = filter_trees_update_omits( |
7a7c7f4a | 225 | obj, omits, include_it); |
c813a7c3 | 226 | seen_info->depth = filter_data->current_depth; |
c813a7c3 MD |
227 | |
228 | if (include_it) | |
229 | filter_res = LOFR_DO_SHOW; | |
7a7c7f4a | 230 | else if (omits && !been_omitted) |
8272f260 MD |
231 | /* |
232 | * Must update omit information of children | |
233 | * recursively; they have not been omitted yet. | |
234 | */ | |
c813a7c3 MD |
235 | filter_res = LOFR_ZERO; |
236 | else | |
237 | filter_res = LOFR_SKIP_TREE; | |
238 | } | |
bc5975d2 | 239 | |
c813a7c3 MD |
240 | filter_data->current_depth++; |
241 | return filter_res; | |
bc5975d2 MD |
242 | } |
243 | } | |
244 | ||
c813a7c3 MD |
245 | static void filter_trees_free(void *filter_data) { |
246 | struct filter_trees_depth_data *d = filter_data; | |
247 | if (!d) | |
248 | return; | |
249 | oidmap_free(&d->seen_at_depth, 1); | |
250 | free(d); | |
251 | } | |
252 | ||
9430147c | 253 | static void filter_trees_depth__init( |
bc5975d2 | 254 | struct list_objects_filter_options *filter_options, |
9430147c | 255 | struct filter *filter) |
bc5975d2 | 256 | { |
c813a7c3 | 257 | struct filter_trees_depth_data *d = xcalloc(1, sizeof(*d)); |
c813a7c3 MD |
258 | oidmap_init(&d->seen_at_depth, 0); |
259 | d->exclude_depth = filter_options->tree_exclude_depth; | |
260 | d->current_depth = 0; | |
bc5975d2 | 261 | |
9430147c MD |
262 | filter->filter_data = d; |
263 | filter->filter_object_fn = filter_trees_depth; | |
264 | filter->free_fn = filter_trees_free; | |
bc5975d2 MD |
265 | } |
266 | ||
25ec7bca JH |
267 | /* |
268 | * A filter for list-objects to omit large blobs. | |
269 | * And to OPTIONALLY collect a list of the omitted OIDs. | |
270 | */ | |
271 | struct filter_blobs_limit_data { | |
25ec7bca JH |
272 | unsigned long max_bytes; |
273 | }; | |
274 | ||
275 | static enum list_objects_filter_result filter_blobs_limit( | |
01d40c84 | 276 | struct repository *r, |
25ec7bca JH |
277 | enum list_objects_filter_situation filter_situation, |
278 | struct object *obj, | |
d3beb61f JK |
279 | const char *pathname UNUSED, |
280 | const char *filename UNUSED, | |
7a7c7f4a | 281 | struct oidset *omits, |
25ec7bca JH |
282 | void *filter_data_) |
283 | { | |
284 | struct filter_blobs_limit_data *filter_data = filter_data_; | |
285 | unsigned long object_length; | |
286 | enum object_type t; | |
287 | ||
288 | switch (filter_situation) { | |
289 | default: | |
696aa739 | 290 | BUG("unknown filter_situation: %d", filter_situation); |
25ec7bca | 291 | |
9a2a4f95 PS |
292 | case LOFS_TAG: |
293 | assert(obj->type == OBJ_TAG); | |
294 | /* always include all tag objects */ | |
295 | return LOFR_MARK_SEEN | LOFR_DO_SHOW; | |
296 | ||
297 | case LOFS_COMMIT: | |
298 | assert(obj->type == OBJ_COMMIT); | |
299 | /* always include all commit objects */ | |
300 | return LOFR_MARK_SEEN | LOFR_DO_SHOW; | |
301 | ||
25ec7bca JH |
302 | case LOFS_BEGIN_TREE: |
303 | assert(obj->type == OBJ_TREE); | |
304 | /* always include all tree objects */ | |
305 | return LOFR_MARK_SEEN | LOFR_DO_SHOW; | |
306 | ||
307 | case LOFS_END_TREE: | |
308 | assert(obj->type == OBJ_TREE); | |
309 | return LOFR_ZERO; | |
310 | ||
311 | case LOFS_BLOB: | |
312 | assert(obj->type == OBJ_BLOB); | |
313 | assert((obj->flags & SEEN) == 0); | |
314 | ||
01d40c84 | 315 | t = oid_object_info(r, &obj->oid, &object_length); |
25ec7bca JH |
316 | if (t != OBJ_BLOB) { /* probably OBJ_NONE */ |
317 | /* | |
318 | * We DO NOT have the blob locally, so we cannot | |
319 | * apply the size filter criteria. Be conservative | |
320 | * and force show it (and let the caller deal with | |
321 | * the ambiguity). | |
322 | */ | |
323 | goto include_it; | |
324 | } | |
325 | ||
326 | if (object_length < filter_data->max_bytes) | |
327 | goto include_it; | |
328 | ||
7a7c7f4a MD |
329 | if (omits) |
330 | oidset_insert(omits, &obj->oid); | |
25ec7bca JH |
331 | return LOFR_MARK_SEEN; /* but not LOFR_DO_SHOW (hard omit) */ |
332 | } | |
333 | ||
334 | include_it: | |
7a7c7f4a MD |
335 | if (omits) |
336 | oidset_remove(omits, &obj->oid); | |
25ec7bca JH |
337 | return LOFR_MARK_SEEN | LOFR_DO_SHOW; |
338 | } | |
339 | ||
9430147c | 340 | static void filter_blobs_limit__init( |
25ec7bca | 341 | struct list_objects_filter_options *filter_options, |
9430147c | 342 | struct filter *filter) |
25ec7bca JH |
343 | { |
344 | struct filter_blobs_limit_data *d = xcalloc(1, sizeof(*d)); | |
25ec7bca JH |
345 | d->max_bytes = filter_options->blob_limit_value; |
346 | ||
9430147c MD |
347 | filter->filter_data = d; |
348 | filter->filter_object_fn = filter_blobs_limit; | |
349 | filter->free_fn = free; | |
25ec7bca JH |
350 | } |
351 | ||
352 | /* | |
353 | * A filter driven by a sparse-checkout specification to only | |
354 | * include blobs that a sparse checkout would populate. | |
355 | * | |
356 | * The sparse-checkout spec can be loaded from a blob with the | |
357 | * given OID or from a local pathname. We allow an OID because | |
358 | * the repo may be bare or we may be doing the filtering on the | |
359 | * server. | |
360 | */ | |
361 | struct frame { | |
362 | /* | |
468ce99b | 363 | * default_match is the usual default include/exclude value that |
25ec7bca JH |
364 | * should be inherited as we recurse into directories based |
365 | * upon pattern matching of the directory itself or of a | |
366 | * containing directory. | |
367 | */ | |
468ce99b | 368 | enum pattern_match_result default_match; |
25ec7bca JH |
369 | |
370 | /* | |
371 | * 1 if the directory (recursively) contains any provisionally | |
372 | * omitted objects. | |
373 | * | |
374 | * 0 if everything (recursively) contained in this directory | |
375 | * has been explicitly included (SHOWN) in the result and | |
376 | * the directory may be short-cut later in the traversal. | |
377 | */ | |
378 | unsigned child_prov_omit : 1; | |
379 | }; | |
380 | ||
381 | struct filter_sparse_data { | |
caa3d554 | 382 | struct pattern_list pl; |
25ec7bca JH |
383 | |
384 | size_t nr, alloc; | |
385 | struct frame *array_frame; | |
386 | }; | |
387 | ||
388 | static enum list_objects_filter_result filter_sparse( | |
01d40c84 | 389 | struct repository *r, |
25ec7bca JH |
390 | enum list_objects_filter_situation filter_situation, |
391 | struct object *obj, | |
392 | const char *pathname, | |
393 | const char *filename, | |
7a7c7f4a | 394 | struct oidset *omits, |
25ec7bca JH |
395 | void *filter_data_) |
396 | { | |
397 | struct filter_sparse_data *filter_data = filter_data_; | |
468ce99b | 398 | int dtype; |
25ec7bca | 399 | struct frame *frame; |
468ce99b | 400 | enum pattern_match_result match; |
25ec7bca JH |
401 | |
402 | switch (filter_situation) { | |
403 | default: | |
696aa739 | 404 | BUG("unknown filter_situation: %d", filter_situation); |
25ec7bca | 405 | |
9a2a4f95 PS |
406 | case LOFS_TAG: |
407 | assert(obj->type == OBJ_TAG); | |
408 | /* always include all tag objects */ | |
409 | return LOFR_MARK_SEEN | LOFR_DO_SHOW; | |
410 | ||
411 | case LOFS_COMMIT: | |
412 | assert(obj->type == OBJ_COMMIT); | |
413 | /* always include all commit objects */ | |
414 | return LOFR_MARK_SEEN | LOFR_DO_SHOW; | |
415 | ||
25ec7bca JH |
416 | case LOFS_BEGIN_TREE: |
417 | assert(obj->type == OBJ_TREE); | |
418 | dtype = DT_DIR; | |
468ce99b DS |
419 | match = path_matches_pattern_list(pathname, strlen(pathname), |
420 | filename, &dtype, &filter_data->pl, | |
421 | r->index); | |
422 | if (match == UNDECIDED) | |
423 | match = filter_data->array_frame[filter_data->nr - 1].default_match; | |
25ec7bca JH |
424 | |
425 | ALLOC_GROW(filter_data->array_frame, filter_data->nr + 1, | |
426 | filter_data->alloc); | |
468ce99b | 427 | filter_data->array_frame[filter_data->nr].default_match = match; |
25ec7bca | 428 | filter_data->array_frame[filter_data->nr].child_prov_omit = 0; |
7140600e | 429 | filter_data->nr++; |
25ec7bca JH |
430 | |
431 | /* | |
432 | * A directory with this tree OID may appear in multiple | |
433 | * places in the tree. (Think of a directory move or copy, | |
434 | * with no other changes, so the OID is the same, but the | |
435 | * full pathnames of objects within this directory are new | |
436 | * and may match is_excluded() patterns differently.) | |
437 | * So we cannot mark this directory as SEEN (yet), since | |
438 | * that will prevent process_tree() from revisiting this | |
439 | * tree object with other pathname prefixes. | |
440 | * | |
441 | * Only _DO_SHOW the tree object the first time we visit | |
442 | * this tree object. | |
443 | * | |
444 | * We always show all tree objects. A future optimization | |
445 | * may want to attempt to narrow this. | |
446 | */ | |
447 | if (obj->flags & FILTER_SHOWN_BUT_REVISIT) | |
448 | return LOFR_ZERO; | |
449 | obj->flags |= FILTER_SHOWN_BUT_REVISIT; | |
450 | return LOFR_DO_SHOW; | |
451 | ||
452 | case LOFS_END_TREE: | |
453 | assert(obj->type == OBJ_TREE); | |
7140600e | 454 | assert(filter_data->nr > 1); |
25ec7bca | 455 | |
7140600e | 456 | frame = &filter_data->array_frame[--filter_data->nr]; |
25ec7bca JH |
457 | |
458 | /* | |
459 | * Tell our parent directory if any of our children were | |
460 | * provisionally omitted. | |
461 | */ | |
7140600e | 462 | filter_data->array_frame[filter_data->nr - 1].child_prov_omit |= |
25ec7bca JH |
463 | frame->child_prov_omit; |
464 | ||
465 | /* | |
466 | * If there are NO provisionally omitted child objects (ALL child | |
467 | * objects in this folder were INCLUDED), then we can mark the | |
468 | * folder as SEEN (so we will not have to revisit it again). | |
469 | */ | |
470 | if (!frame->child_prov_omit) | |
471 | return LOFR_MARK_SEEN; | |
472 | return LOFR_ZERO; | |
473 | ||
474 | case LOFS_BLOB: | |
475 | assert(obj->type == OBJ_BLOB); | |
476 | assert((obj->flags & SEEN) == 0); | |
477 | ||
7140600e | 478 | frame = &filter_data->array_frame[filter_data->nr - 1]; |
25ec7bca JH |
479 | |
480 | dtype = DT_REG; | |
468ce99b | 481 | match = path_matches_pattern_list(pathname, strlen(pathname), |
caa3d554 | 482 | filename, &dtype, &filter_data->pl, |
01d40c84 | 483 | r->index); |
468ce99b DS |
484 | if (match == UNDECIDED) |
485 | match = frame->default_match; | |
486 | if (match == MATCHED) { | |
7a7c7f4a MD |
487 | if (omits) |
488 | oidset_remove(omits, &obj->oid); | |
25ec7bca JH |
489 | return LOFR_MARK_SEEN | LOFR_DO_SHOW; |
490 | } | |
491 | ||
492 | /* | |
493 | * Provisionally omit it. We've already established that | |
494 | * this pathname is not in the sparse-checkout specification | |
495 | * with the CURRENT pathname, so we *WANT* to omit this blob. | |
496 | * | |
497 | * However, a pathname elsewhere in the tree may also | |
498 | * reference this same blob, so we cannot reject it yet. | |
499 | * Leave the LOFR_ bits unset so that if the blob appears | |
500 | * again in the traversal, we will be asked again. | |
501 | */ | |
7a7c7f4a MD |
502 | if (omits) |
503 | oidset_insert(omits, &obj->oid); | |
25ec7bca JH |
504 | |
505 | /* | |
506 | * Remember that at least 1 blob in this tree was | |
507 | * provisionally omitted. This prevents us from short | |
508 | * cutting the tree in future iterations. | |
509 | */ | |
510 | frame->child_prov_omit = 1; | |
511 | return LOFR_ZERO; | |
512 | } | |
513 | } | |
514 | ||
515 | ||
516 | static void filter_sparse_free(void *filter_data) | |
517 | { | |
518 | struct filter_sparse_data *d = filter_data; | |
faebba43 | 519 | clear_pattern_list(&d->pl); |
7140600e | 520 | free(d->array_frame); |
25ec7bca JH |
521 | free(d); |
522 | } | |
523 | ||
9430147c | 524 | static void filter_sparse_oid__init( |
25ec7bca | 525 | struct list_objects_filter_options *filter_options, |
9430147c | 526 | struct filter *filter) |
25ec7bca JH |
527 | { |
528 | struct filter_sparse_data *d = xcalloc(1, sizeof(*d)); | |
4c96a775 JK |
529 | struct object_context oc; |
530 | struct object_id sparse_oid; | |
531 | ||
532 | if (get_oid_with_context(the_repository, | |
533 | filter_options->sparse_oid_name, | |
534 | GET_OID_BLOB, &sparse_oid, &oc)) | |
cf34337f | 535 | die(_("unable to access sparse blob in '%s'"), |
4c96a775 | 536 | filter_options->sparse_oid_name); |
ad8f0368 | 537 | if (add_patterns_from_blob_to_list(&sparse_oid, "", 0, &d->pl) < 0) |
cf34337f JS |
538 | die(_("unable to parse sparse filter data in %s"), |
539 | oid_to_hex(&sparse_oid)); | |
25ec7bca JH |
540 | |
541 | ALLOC_GROW(d->array_frame, d->nr + 1, d->alloc); | |
468ce99b | 542 | d->array_frame[d->nr].default_match = 0; /* default to include */ |
25ec7bca | 543 | d->array_frame[d->nr].child_prov_omit = 0; |
7140600e | 544 | d->nr++; |
25ec7bca | 545 | |
9430147c MD |
546 | filter->filter_data = d; |
547 | filter->filter_object_fn = filter_sparse; | |
548 | filter->free_fn = filter_sparse_free; | |
25ec7bca JH |
549 | } |
550 | ||
b0c42a53 PS |
551 | /* |
552 | * A filter for list-objects to omit large blobs. | |
553 | * And to OPTIONALLY collect a list of the omitted OIDs. | |
554 | */ | |
555 | struct filter_object_type_data { | |
556 | enum object_type object_type; | |
557 | }; | |
558 | ||
559 | static enum list_objects_filter_result filter_object_type( | |
d3beb61f | 560 | struct repository *r UNUSED, |
b0c42a53 PS |
561 | enum list_objects_filter_situation filter_situation, |
562 | struct object *obj, | |
d3beb61f JK |
563 | const char *pathname UNUSED, |
564 | const char *filename UNUSED, | |
565 | struct oidset *omits UNUSED, | |
b0c42a53 PS |
566 | void *filter_data_) |
567 | { | |
568 | struct filter_object_type_data *filter_data = filter_data_; | |
569 | ||
570 | switch (filter_situation) { | |
571 | default: | |
572 | BUG("unknown filter_situation: %d", filter_situation); | |
573 | ||
574 | case LOFS_TAG: | |
575 | assert(obj->type == OBJ_TAG); | |
576 | if (filter_data->object_type == OBJ_TAG) | |
577 | return LOFR_MARK_SEEN | LOFR_DO_SHOW; | |
578 | return LOFR_MARK_SEEN; | |
579 | ||
580 | case LOFS_COMMIT: | |
581 | assert(obj->type == OBJ_COMMIT); | |
582 | if (filter_data->object_type == OBJ_COMMIT) | |
583 | return LOFR_MARK_SEEN | LOFR_DO_SHOW; | |
584 | return LOFR_MARK_SEEN; | |
585 | ||
586 | case LOFS_BEGIN_TREE: | |
587 | assert(obj->type == OBJ_TREE); | |
588 | ||
589 | /* | |
590 | * If we only want to show commits or tags, then there is no | |
591 | * need to walk down trees. | |
592 | */ | |
593 | if (filter_data->object_type == OBJ_COMMIT || | |
594 | filter_data->object_type == OBJ_TAG) | |
595 | return LOFR_SKIP_TREE; | |
596 | ||
597 | if (filter_data->object_type == OBJ_TREE) | |
598 | return LOFR_MARK_SEEN | LOFR_DO_SHOW; | |
599 | ||
600 | return LOFR_MARK_SEEN; | |
601 | ||
602 | case LOFS_BLOB: | |
603 | assert(obj->type == OBJ_BLOB); | |
604 | ||
605 | if (filter_data->object_type == OBJ_BLOB) | |
606 | return LOFR_MARK_SEEN | LOFR_DO_SHOW; | |
607 | return LOFR_MARK_SEEN; | |
608 | ||
609 | case LOFS_END_TREE: | |
610 | return LOFR_ZERO; | |
611 | } | |
612 | } | |
613 | ||
614 | static void filter_object_type__init( | |
615 | struct list_objects_filter_options *filter_options, | |
616 | struct filter *filter) | |
617 | { | |
618 | struct filter_object_type_data *d = xcalloc(1, sizeof(*d)); | |
619 | d->object_type = filter_options->object_type; | |
620 | ||
621 | filter->filter_data = d; | |
622 | filter->filter_object_fn = filter_object_type; | |
623 | filter->free_fn = free; | |
624 | } | |
625 | ||
e987df5f MD |
626 | /* A filter which only shows objects shown by all sub-filters. */ |
627 | struct combine_filter_data { | |
628 | struct subfilter *sub; | |
629 | size_t nr; | |
630 | }; | |
631 | ||
632 | static enum list_objects_filter_result process_subfilter( | |
633 | struct repository *r, | |
634 | enum list_objects_filter_situation filter_situation, | |
635 | struct object *obj, | |
636 | const char *pathname, | |
637 | const char *filename, | |
638 | struct subfilter *sub) | |
639 | { | |
640 | enum list_objects_filter_result result; | |
641 | ||
642 | /* | |
643 | * Check and update is_skipping_tree before oidset_contains so | |
644 | * that is_skipping_tree gets unset even when the object is | |
645 | * marked as seen. As of this writing, no filter uses | |
646 | * LOFR_MARK_SEEN on trees that also uses LOFR_SKIP_TREE, so the | |
647 | * ordering is only theoretically important. Be cautious if you | |
648 | * change the order of the below checks and more filters have | |
649 | * been added! | |
650 | */ | |
651 | if (sub->is_skipping_tree) { | |
652 | if (filter_situation == LOFS_END_TREE && | |
653 | oideq(&obj->oid, &sub->skip_tree)) | |
654 | sub->is_skipping_tree = 0; | |
655 | else | |
656 | return LOFR_ZERO; | |
657 | } | |
658 | if (oidset_contains(&sub->seen, &obj->oid)) | |
659 | return LOFR_ZERO; | |
660 | ||
661 | result = list_objects_filter__filter_object( | |
662 | r, filter_situation, obj, pathname, filename, sub->filter); | |
663 | ||
664 | if (result & LOFR_MARK_SEEN) | |
665 | oidset_insert(&sub->seen, &obj->oid); | |
666 | ||
667 | if (result & LOFR_SKIP_TREE) { | |
668 | sub->is_skipping_tree = 1; | |
669 | sub->skip_tree = obj->oid; | |
670 | } | |
671 | ||
672 | return result; | |
673 | } | |
674 | ||
675 | static enum list_objects_filter_result filter_combine( | |
676 | struct repository *r, | |
677 | enum list_objects_filter_situation filter_situation, | |
678 | struct object *obj, | |
679 | const char *pathname, | |
680 | const char *filename, | |
d3beb61f | 681 | struct oidset *omits UNUSED, |
e987df5f MD |
682 | void *filter_data) |
683 | { | |
684 | struct combine_filter_data *d = filter_data; | |
685 | enum list_objects_filter_result combined_result = | |
686 | LOFR_DO_SHOW | LOFR_MARK_SEEN | LOFR_SKIP_TREE; | |
687 | size_t sub; | |
688 | ||
689 | for (sub = 0; sub < d->nr; sub++) { | |
690 | enum list_objects_filter_result sub_result = process_subfilter( | |
691 | r, filter_situation, obj, pathname, filename, | |
692 | &d->sub[sub]); | |
693 | if (!(sub_result & LOFR_DO_SHOW)) | |
694 | combined_result &= ~LOFR_DO_SHOW; | |
695 | if (!(sub_result & LOFR_MARK_SEEN)) | |
696 | combined_result &= ~LOFR_MARK_SEEN; | |
697 | if (!d->sub[sub].is_skipping_tree) | |
698 | combined_result &= ~LOFR_SKIP_TREE; | |
699 | } | |
700 | ||
701 | return combined_result; | |
702 | } | |
703 | ||
704 | static void filter_combine__free(void *filter_data) | |
705 | { | |
706 | struct combine_filter_data *d = filter_data; | |
707 | size_t sub; | |
708 | for (sub = 0; sub < d->nr; sub++) { | |
709 | list_objects_filter__free(d->sub[sub].filter); | |
710 | oidset_clear(&d->sub[sub].seen); | |
711 | if (d->sub[sub].omits.set.size) | |
712 | BUG("expected oidset to be cleared already"); | |
713 | } | |
714 | free(d->sub); | |
7c2dc122 | 715 | free(d); |
e987df5f MD |
716 | } |
717 | ||
718 | static void add_all(struct oidset *dest, struct oidset *src) { | |
719 | struct oidset_iter iter; | |
720 | struct object_id *src_oid; | |
721 | ||
722 | oidset_iter_init(src, &iter); | |
723 | while ((src_oid = oidset_iter_next(&iter)) != NULL) | |
724 | oidset_insert(dest, src_oid); | |
725 | } | |
726 | ||
727 | static void filter_combine__finalize_omits( | |
728 | struct oidset *omits, | |
729 | void *filter_data) | |
730 | { | |
731 | struct combine_filter_data *d = filter_data; | |
732 | size_t sub; | |
733 | ||
734 | for (sub = 0; sub < d->nr; sub++) { | |
735 | add_all(omits, &d->sub[sub].omits); | |
736 | oidset_clear(&d->sub[sub].omits); | |
737 | } | |
738 | } | |
739 | ||
740 | static void filter_combine__init( | |
741 | struct list_objects_filter_options *filter_options, | |
742 | struct filter* filter) | |
743 | { | |
744 | struct combine_filter_data *d = xcalloc(1, sizeof(*d)); | |
745 | size_t sub; | |
746 | ||
747 | d->nr = filter_options->sub_nr; | |
ca56dadb | 748 | CALLOC_ARRAY(d->sub, d->nr); |
e987df5f MD |
749 | for (sub = 0; sub < d->nr; sub++) |
750 | d->sub[sub].filter = list_objects_filter__init( | |
751 | filter->omits ? &d->sub[sub].omits : NULL, | |
752 | &filter_options->sub[sub]); | |
753 | ||
754 | filter->filter_data = d; | |
755 | filter->filter_object_fn = filter_combine; | |
756 | filter->free_fn = filter_combine__free; | |
757 | filter->finalize_omits_fn = filter_combine__finalize_omits; | |
758 | } | |
759 | ||
9430147c | 760 | typedef void (*filter_init_fn)( |
25ec7bca | 761 | struct list_objects_filter_options *filter_options, |
9430147c | 762 | struct filter *filter); |
25ec7bca JH |
763 | |
764 | /* | |
765 | * Must match "enum list_objects_filter_choice". | |
766 | */ | |
767 | static filter_init_fn s_filters[] = { | |
768 | NULL, | |
769 | filter_blobs_none__init, | |
770 | filter_blobs_limit__init, | |
c813a7c3 | 771 | filter_trees_depth__init, |
25ec7bca | 772 | filter_sparse_oid__init, |
b0c42a53 | 773 | filter_object_type__init, |
e987df5f | 774 | filter_combine__init, |
25ec7bca JH |
775 | }; |
776 | ||
9430147c | 777 | struct filter *list_objects_filter__init( |
25ec7bca | 778 | struct oidset *omitted, |
9430147c | 779 | struct list_objects_filter_options *filter_options) |
25ec7bca | 780 | { |
9430147c | 781 | struct filter *filter; |
25ec7bca JH |
782 | filter_init_fn init_fn; |
783 | ||
784 | assert((sizeof(s_filters) / sizeof(s_filters[0])) == LOFC__COUNT); | |
785 | ||
5bf7f1ea JK |
786 | if (!filter_options) |
787 | return NULL; | |
788 | ||
25ec7bca | 789 | if (filter_options->choice >= LOFC__COUNT) |
696aa739 | 790 | BUG("invalid list-objects filter choice: %d", |
25ec7bca JH |
791 | filter_options->choice); |
792 | ||
793 | init_fn = s_filters[filter_options->choice]; | |
9430147c MD |
794 | if (!init_fn) |
795 | return NULL; | |
796 | ||
ca56dadb | 797 | CALLOC_ARRAY(filter, 1); |
7a7c7f4a MD |
798 | filter->omits = omitted; |
799 | init_fn(filter_options, filter); | |
9430147c MD |
800 | return filter; |
801 | } | |
802 | ||
803 | enum list_objects_filter_result list_objects_filter__filter_object( | |
804 | struct repository *r, | |
805 | enum list_objects_filter_situation filter_situation, | |
806 | struct object *obj, | |
807 | const char *pathname, | |
808 | const char *filename, | |
809 | struct filter *filter) | |
810 | { | |
811 | if (filter && (obj->flags & NOT_USER_GIVEN)) | |
812 | return filter->filter_object_fn(r, filter_situation, obj, | |
813 | pathname, filename, | |
7a7c7f4a | 814 | filter->omits, |
9430147c MD |
815 | filter->filter_data); |
816 | /* | |
817 | * No filter is active or user gave object explicitly. In this case, | |
818 | * always show the object (except when LOFS_END_TREE, since this tree | |
819 | * had already been shown when LOFS_BEGIN_TREE). | |
820 | */ | |
821 | if (filter_situation == LOFS_END_TREE) | |
822 | return 0; | |
823 | return LOFR_MARK_SEEN | LOFR_DO_SHOW; | |
824 | } | |
825 | ||
826 | void list_objects_filter__free(struct filter *filter) | |
827 | { | |
828 | if (!filter) | |
829 | return; | |
e987df5f MD |
830 | if (filter->finalize_omits_fn && filter->omits) |
831 | filter->finalize_omits_fn(filter->omits, filter->filter_data); | |
9430147c MD |
832 | filter->free_fn(filter->filter_data); |
833 | free(filter); | |
25ec7bca | 834 | } |