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