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