1 #include "git-compat-util.h"
14 #include "pack-bitmap.h"
15 #include "pack-objects.h"
16 #include "delta-islands.h"
17 #include "oid-array.h"
20 KHASH_INIT(str
, const char *, void *, 1, kh_str_hash_func
, kh_str_hash_equal
)
22 static kh_oid_map_t
*island_marks
;
23 static unsigned island_counter
;
24 static unsigned island_counter_core
;
26 struct remote_island
{
28 struct oid_array oids
;
31 struct island_bitmap
{
33 uint32_t bits
[FLEX_ARRAY
];
36 static uint32_t island_bitmap_size
;
39 * Allocate a new bitmap; if "old" is not NULL, the new bitmap will be a copy
40 * of "old". Otherwise, the new bitmap is empty.
42 static struct island_bitmap
*island_bitmap_new(const struct island_bitmap
*old
)
44 size_t size
= sizeof(struct island_bitmap
) + (island_bitmap_size
* 4);
45 struct island_bitmap
*b
= xcalloc(1, size
);
54 static void island_bitmap_or(struct island_bitmap
*a
, const struct island_bitmap
*b
)
58 for (i
= 0; i
< island_bitmap_size
; ++i
)
59 a
->bits
[i
] |= b
->bits
[i
];
62 static int island_bitmap_is_subset(struct island_bitmap
*self
,
63 struct island_bitmap
*super
)
70 for (i
= 0; i
< island_bitmap_size
; ++i
) {
71 if ((self
->bits
[i
] & super
->bits
[i
]) != self
->bits
[i
])
78 #define ISLAND_BITMAP_BLOCK(x) (x / 32)
79 #define ISLAND_BITMAP_MASK(x) (1 << (x % 32))
81 static void island_bitmap_set(struct island_bitmap
*self
, uint32_t i
)
83 self
->bits
[ISLAND_BITMAP_BLOCK(i
)] |= ISLAND_BITMAP_MASK(i
);
86 static int island_bitmap_get(struct island_bitmap
*self
, uint32_t i
)
88 return (self
->bits
[ISLAND_BITMAP_BLOCK(i
)] & ISLAND_BITMAP_MASK(i
)) != 0;
91 int in_same_island(const struct object_id
*trg_oid
, const struct object_id
*src_oid
)
93 khiter_t trg_pos
, src_pos
;
95 /* If we aren't using islands, assume everything goes together. */
100 * If we don't have a bitmap for the target, we can delta it
101 * against anything -- it's not an important object
103 trg_pos
= kh_get_oid_map(island_marks
, *trg_oid
);
104 if (trg_pos
>= kh_end(island_marks
))
108 * if the source (our delta base) doesn't have a bitmap,
109 * we don't want to base any deltas on it!
111 src_pos
= kh_get_oid_map(island_marks
, *src_oid
);
112 if (src_pos
>= kh_end(island_marks
))
115 return island_bitmap_is_subset(kh_value(island_marks
, trg_pos
),
116 kh_value(island_marks
, src_pos
));
119 int island_delta_cmp(const struct object_id
*a
, const struct object_id
*b
)
121 khiter_t a_pos
, b_pos
;
122 struct island_bitmap
*a_bitmap
= NULL
, *b_bitmap
= NULL
;
127 a_pos
= kh_get_oid_map(island_marks
, *a
);
128 if (a_pos
< kh_end(island_marks
))
129 a_bitmap
= kh_value(island_marks
, a_pos
);
131 b_pos
= kh_get_oid_map(island_marks
, *b
);
132 if (b_pos
< kh_end(island_marks
))
133 b_bitmap
= kh_value(island_marks
, b_pos
);
136 if (!b_bitmap
|| !island_bitmap_is_subset(a_bitmap
, b_bitmap
))
140 if (!a_bitmap
|| !island_bitmap_is_subset(b_bitmap
, a_bitmap
))
147 static struct island_bitmap
*create_or_get_island_marks(struct object
*obj
)
152 pos
= kh_put_oid_map(island_marks
, obj
->oid
, &hash_ret
);
154 kh_value(island_marks
, pos
) = island_bitmap_new(NULL
);
156 return kh_value(island_marks
, pos
);
159 static void set_island_marks(struct object
*obj
, struct island_bitmap
*marks
)
161 struct island_bitmap
*b
;
165 pos
= kh_put_oid_map(island_marks
, obj
->oid
, &hash_ret
);
168 * We don't have one yet; make a copy-on-write of the
172 kh_value(island_marks
, pos
) = marks
;
177 * We do have it. Make sure we split any copy-on-write before
180 b
= kh_value(island_marks
, pos
);
181 if (b
->refcount
> 1) {
183 b
= kh_value(island_marks
, pos
) = island_bitmap_new(b
);
185 island_bitmap_or(b
, marks
);
188 static void mark_remote_island_1(struct repository
*r
,
189 struct remote_island
*rl
,
194 for (i
= 0; i
< rl
->oids
.nr
; ++i
) {
195 struct island_bitmap
*marks
;
196 struct object
*obj
= parse_object(r
, &rl
->oids
.oid
[i
]);
201 marks
= create_or_get_island_marks(obj
);
202 island_bitmap_set(marks
, island_counter
);
204 if (is_core_island
&& obj
->type
== OBJ_COMMIT
)
205 obj
->flags
|= NEEDS_BITMAP
;
207 /* If it was a tag, also make sure we hit the underlying object. */
208 while (obj
&& obj
->type
== OBJ_TAG
) {
209 obj
= ((struct tag
*)obj
)->tagged
;
211 parse_object(r
, &obj
->oid
);
212 marks
= create_or_get_island_marks(obj
);
213 island_bitmap_set(marks
, island_counter
);
219 island_counter_core
= island_counter
;
224 struct tree_islands_todo
{
225 struct object_entry
*entry
;
229 static int tree_depth_compare(const void *a
, const void *b
)
231 const struct tree_islands_todo
*todo_a
= a
;
232 const struct tree_islands_todo
*todo_b
= b
;
234 return todo_a
->depth
- todo_b
->depth
;
237 void resolve_tree_islands(struct repository
*r
,
239 struct packing_data
*to_pack
)
241 struct progress
*progress_state
= NULL
;
242 struct tree_islands_todo
*todo
;
250 * We process only trees, as commits and tags have already been handled
251 * (and passed their marks on to root trees, as well. We must make sure
252 * to process them in descending tree-depth order so that marks
253 * propagate down the tree properly, even if a sub-tree is found in
254 * multiple parent trees.
256 ALLOC_ARRAY(todo
, to_pack
->nr_objects
);
257 for (i
= 0; i
< to_pack
->nr_objects
; i
++) {
258 if (oe_type(&to_pack
->objects
[i
]) == OBJ_TREE
) {
259 todo
[nr
].entry
= &to_pack
->objects
[i
];
260 todo
[nr
].depth
= oe_tree_depth(to_pack
, &to_pack
->objects
[i
]);
264 QSORT(todo
, nr
, tree_depth_compare
);
267 progress_state
= start_progress(_("Propagating island marks"), nr
);
269 for (i
= 0; i
< nr
; i
++) {
270 struct object_entry
*ent
= todo
[i
].entry
;
271 struct island_bitmap
*root_marks
;
273 struct tree_desc desc
;
274 struct name_entry entry
;
277 pos
= kh_get_oid_map(island_marks
, ent
->idx
.oid
);
278 if (pos
>= kh_end(island_marks
))
281 root_marks
= kh_value(island_marks
, pos
);
283 tree
= lookup_tree(r
, &ent
->idx
.oid
);
284 if (!tree
|| parse_tree(tree
) < 0)
285 die(_("bad tree object %s"), oid_to_hex(&ent
->idx
.oid
));
287 init_tree_desc(&desc
, &tree
->object
.oid
, tree
->buffer
, tree
->size
);
288 while (tree_entry(&desc
, &entry
)) {
291 if (S_ISGITLINK(entry
.mode
))
294 obj
= lookup_object(r
, &entry
.oid
);
298 set_island_marks(obj
, root_marks
);
301 free_tree_buffer(tree
);
303 display_progress(progress_state
, i
+1);
306 stop_progress(&progress_state
);
310 struct island_load_data
{
311 kh_str_t
*remote_islands
;
316 static const char *core_island_name
;
318 static void free_config_regexes(struct island_load_data
*ild
)
320 for (size_t i
= 0; i
< ild
->nr
; i
++)
321 regfree(&ild
->rx
[i
]);
325 static void free_remote_islands(kh_str_t
*remote_islands
)
327 const char *island_name
;
328 struct remote_island
*rl
;
330 kh_foreach(remote_islands
, island_name
, rl
, {
331 free((void *)island_name
);
332 oid_array_clear(&rl
->oids
);
335 kh_destroy_str(remote_islands
);
338 static int island_config_callback(const char *k
, const char *v
,
339 const struct config_context
*ctx UNUSED
,
342 struct island_load_data
*ild
= cb
;
344 if (!strcmp(k
, "pack.island")) {
345 struct strbuf re
= STRBUF_INIT
;
348 return config_error_nonbool(k
);
350 ALLOC_GROW(ild
->rx
, ild
->nr
+ 1, ild
->alloc
);
353 strbuf_addch(&re
, '^');
354 strbuf_addstr(&re
, v
);
356 if (regcomp(&ild
->rx
[ild
->nr
], re
.buf
, REG_EXTENDED
))
357 die(_("failed to load island regex for '%s': %s"), k
, re
.buf
);
364 if (!strcmp(k
, "pack.islandcore"))
365 return git_config_string(&core_island_name
, k
, v
);
370 static void add_ref_to_island(kh_str_t
*remote_islands
, const char *island_name
,
371 const struct object_id
*oid
)
374 struct remote_island
*rl
= NULL
;
377 khiter_t pos
= kh_put_str(remote_islands
, island_name
, &hash_ret
);
380 kh_key(remote_islands
, pos
) = xstrdup(island_name
);
381 kh_value(remote_islands
, pos
) = xcalloc(1, sizeof(struct remote_island
));
384 rl
= kh_value(remote_islands
, pos
);
385 oid_array_append(&rl
->oids
, oid
);
387 memcpy(&sha_core
, oid
->hash
, sizeof(uint64_t));
388 rl
->hash
+= sha_core
;
391 static int find_island_for_ref(const char *refname
, const struct object_id
*oid
,
392 int flags UNUSED
, void *cb
)
394 struct island_load_data
*ild
= cb
;
397 * We should advertise 'ARRAY_SIZE(matches) - 2' as the max,
398 * so we can diagnose below a config with more capture groups
401 regmatch_t matches
[16];
403 struct strbuf island_name
= STRBUF_INIT
;
405 /* walk backwards to get last-one-wins ordering */
406 for (i
= ild
->nr
- 1; i
>= 0; i
--) {
407 if (!regexec(&ild
->rx
[i
], refname
,
408 ARRAY_SIZE(matches
), matches
, 0))
415 if (matches
[ARRAY_SIZE(matches
) - 1].rm_so
!= -1)
416 warning(_("island regex from config has "
417 "too many capture groups (max=%d)"),
418 (int)ARRAY_SIZE(matches
) - 2);
420 for (m
= 1; m
< ARRAY_SIZE(matches
); m
++) {
421 regmatch_t
*match
= &matches
[m
];
423 if (match
->rm_so
== -1)
427 strbuf_addch(&island_name
, '-');
429 strbuf_add(&island_name
, refname
+ match
->rm_so
, match
->rm_eo
- match
->rm_so
);
432 add_ref_to_island(ild
->remote_islands
, island_name
.buf
, oid
);
433 strbuf_release(&island_name
);
437 static struct remote_island
*get_core_island(kh_str_t
*remote_islands
)
439 if (core_island_name
) {
440 khiter_t pos
= kh_get_str(remote_islands
, core_island_name
);
441 if (pos
< kh_end(remote_islands
))
442 return kh_value(remote_islands
, pos
);
448 static void deduplicate_islands(kh_str_t
*remote_islands
, struct repository
*r
)
450 struct remote_island
*island
, *core
= NULL
, **list
;
451 unsigned int island_count
, dst
, src
, ref
, i
= 0;
453 island_count
= kh_size(remote_islands
);
454 ALLOC_ARRAY(list
, island_count
);
456 kh_foreach_value(remote_islands
, island
, {
460 for (ref
= 0; ref
+ 1 < island_count
; ref
++) {
461 for (src
= ref
+ 1, dst
= src
; src
< island_count
; src
++) {
462 if (list
[ref
]->hash
== list
[src
]->hash
)
466 list
[dst
] = list
[src
];
473 island_bitmap_size
= (island_count
/ 32) + 1;
474 core
= get_core_island(remote_islands
);
476 for (i
= 0; i
< island_count
; ++i
) {
477 mark_remote_island_1(r
, list
[i
], core
&& list
[i
]->hash
== core
->hash
);
483 void load_delta_islands(struct repository
*r
, int progress
)
485 struct island_load_data ild
= { 0 };
487 island_marks
= kh_init_oid_map();
489 git_config(island_config_callback
, &ild
);
490 ild
.remote_islands
= kh_init_str();
491 for_each_ref(find_island_for_ref
, &ild
);
492 free_config_regexes(&ild
);
493 deduplicate_islands(ild
.remote_islands
, r
);
494 free_remote_islands(ild
.remote_islands
);
497 fprintf(stderr
, _("Marked %d islands, done.\n"), island_counter
);
500 void propagate_island_marks(struct commit
*commit
)
502 khiter_t pos
= kh_get_oid_map(island_marks
, commit
->object
.oid
);
504 if (pos
< kh_end(island_marks
)) {
505 struct commit_list
*p
;
506 struct island_bitmap
*root_marks
= kh_value(island_marks
, pos
);
508 repo_parse_commit(the_repository
, commit
);
509 set_island_marks(&repo_get_commit_tree(the_repository
, commit
)->object
,
511 for (p
= commit
->parents
; p
; p
= p
->next
)
512 set_island_marks(&p
->item
->object
, root_marks
);
516 void free_island_marks(void)
518 struct island_bitmap
*bitmap
;
521 kh_foreach_value(island_marks
, bitmap
, {
522 if (!--bitmap
->refcount
)
525 kh_destroy_oid_map(island_marks
);
528 /* detect use-after-free with a an address which is never valid: */
529 island_marks
= (void *)-1;
532 int compute_pack_layers(struct packing_data
*to_pack
)
536 if (!core_island_name
|| !island_marks
)
539 for (i
= 0; i
< to_pack
->nr_objects
; ++i
) {
540 struct object_entry
*entry
= &to_pack
->objects
[i
];
541 khiter_t pos
= kh_get_oid_map(island_marks
, entry
->idx
.oid
);
543 oe_set_layer(to_pack
, entry
, 1);
545 if (pos
< kh_end(island_marks
)) {
546 struct island_bitmap
*bitmap
= kh_value(island_marks
, pos
);
548 if (island_bitmap_get(bitmap
, island_counter_core
))
549 oe_set_layer(to_pack
, entry
, 0);