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