git
/
delta-islands.c
557 строк · 12.8 Кб
1#define USE_THE_REPOSITORY_VARIABLE2
3#include "git-compat-util.h"4#include "object.h"5#include "commit.h"6#include "gettext.h"7#include "hex.h"8#include "tag.h"9#include "tree.h"10#include "pack.h"11#include "tree-walk.h"12#include "diff.h"13#include "progress.h"14#include "refs.h"15#include "khash.h"16#include "pack-bitmap.h"17#include "pack-objects.h"18#include "delta-islands.h"19#include "oid-array.h"20#include "config.h"21
22KHASH_INIT(str, const char *, void *, 1, kh_str_hash_func, kh_str_hash_equal)23
24static kh_oid_map_t *island_marks;25static unsigned island_counter;26static unsigned island_counter_core;27
28struct remote_island {29uint64_t hash;30struct oid_array oids;31};32
33struct island_bitmap {34uint32_t refcount;35uint32_t bits[FLEX_ARRAY];36};37
38static uint32_t island_bitmap_size;39
40/*
41* Allocate a new bitmap; if "old" is not NULL, the new bitmap will be a copy
42* of "old". Otherwise, the new bitmap is empty.
43*/
44static struct island_bitmap *island_bitmap_new(const struct island_bitmap *old)45{
46size_t size = sizeof(struct island_bitmap) + (island_bitmap_size * 4);47struct island_bitmap *b = xcalloc(1, size);48
49if (old)50memcpy(b, old, size);51
52b->refcount = 1;53return b;54}
55
56static void island_bitmap_or(struct island_bitmap *a, const struct island_bitmap *b)57{
58uint32_t i;59
60for (i = 0; i < island_bitmap_size; ++i)61a->bits[i] |= b->bits[i];62}
63
64static int island_bitmap_is_subset(struct island_bitmap *self,65struct island_bitmap *super)66{
67uint32_t i;68
69if (self == super)70return 1;71
72for (i = 0; i < island_bitmap_size; ++i) {73if ((self->bits[i] & super->bits[i]) != self->bits[i])74return 0;75}76
77return 1;78}
79
80#define ISLAND_BITMAP_BLOCK(x) (x / 32)81#define ISLAND_BITMAP_MASK(x) (1 << (x % 32))82
83static void island_bitmap_set(struct island_bitmap *self, uint32_t i)84{
85self->bits[ISLAND_BITMAP_BLOCK(i)] |= ISLAND_BITMAP_MASK(i);86}
87
88static int island_bitmap_get(struct island_bitmap *self, uint32_t i)89{
90return (self->bits[ISLAND_BITMAP_BLOCK(i)] & ISLAND_BITMAP_MASK(i)) != 0;91}
92
93int in_same_island(const struct object_id *trg_oid, const struct object_id *src_oid)94{
95khiter_t trg_pos, src_pos;96
97/* If we aren't using islands, assume everything goes together. */98if (!island_marks)99return 1;100
101/*102* If we don't have a bitmap for the target, we can delta it
103* against anything -- it's not an important object
104*/
105trg_pos = kh_get_oid_map(island_marks, *trg_oid);106if (trg_pos >= kh_end(island_marks))107return 1;108
109/*110* if the source (our delta base) doesn't have a bitmap,
111* we don't want to base any deltas on it!
112*/
113src_pos = kh_get_oid_map(island_marks, *src_oid);114if (src_pos >= kh_end(island_marks))115return 0;116
117return island_bitmap_is_subset(kh_value(island_marks, trg_pos),118kh_value(island_marks, src_pos));119}
120
121int island_delta_cmp(const struct object_id *a, const struct object_id *b)122{
123khiter_t a_pos, b_pos;124struct island_bitmap *a_bitmap = NULL, *b_bitmap = NULL;125
126if (!island_marks)127return 0;128
129a_pos = kh_get_oid_map(island_marks, *a);130if (a_pos < kh_end(island_marks))131a_bitmap = kh_value(island_marks, a_pos);132
133b_pos = kh_get_oid_map(island_marks, *b);134if (b_pos < kh_end(island_marks))135b_bitmap = kh_value(island_marks, b_pos);136
137if (a_bitmap) {138if (!b_bitmap || !island_bitmap_is_subset(a_bitmap, b_bitmap))139return -1;140}141if (b_bitmap) {142if (!a_bitmap || !island_bitmap_is_subset(b_bitmap, a_bitmap))143return 1;144}145
146return 0;147}
148
149static struct island_bitmap *create_or_get_island_marks(struct object *obj)150{
151khiter_t pos;152int hash_ret;153
154pos = kh_put_oid_map(island_marks, obj->oid, &hash_ret);155if (hash_ret)156kh_value(island_marks, pos) = island_bitmap_new(NULL);157
158return kh_value(island_marks, pos);159}
160
161static void set_island_marks(struct object *obj, struct island_bitmap *marks)162{
163struct island_bitmap *b;164khiter_t pos;165int hash_ret;166
167pos = kh_put_oid_map(island_marks, obj->oid, &hash_ret);168if (hash_ret) {169/*170* We don't have one yet; make a copy-on-write of the
171* parent.
172*/
173marks->refcount++;174kh_value(island_marks, pos) = marks;175return;176}177
178/*179* We do have it. Make sure we split any copy-on-write before
180* updating.
181*/
182b = kh_value(island_marks, pos);183if (b->refcount > 1) {184b->refcount--;185b = kh_value(island_marks, pos) = island_bitmap_new(b);186}187island_bitmap_or(b, marks);188}
189
190static void mark_remote_island_1(struct repository *r,191struct remote_island *rl,192int is_core_island)193{
194uint32_t i;195
196for (i = 0; i < rl->oids.nr; ++i) {197struct island_bitmap *marks;198struct object *obj = parse_object(r, &rl->oids.oid[i]);199
200if (!obj)201continue;202
203marks = create_or_get_island_marks(obj);204island_bitmap_set(marks, island_counter);205
206if (is_core_island && obj->type == OBJ_COMMIT)207obj->flags |= NEEDS_BITMAP;208
209/* If it was a tag, also make sure we hit the underlying object. */210while (obj && obj->type == OBJ_TAG) {211obj = ((struct tag *)obj)->tagged;212if (obj) {213parse_object(r, &obj->oid);214marks = create_or_get_island_marks(obj);215island_bitmap_set(marks, island_counter);216}217}218}219
220if (is_core_island)221island_counter_core = island_counter;222
223island_counter++;224}
225
226struct tree_islands_todo {227struct object_entry *entry;228unsigned int depth;229};230
231static int tree_depth_compare(const void *a, const void *b)232{
233const struct tree_islands_todo *todo_a = a;234const struct tree_islands_todo *todo_b = b;235
236return todo_a->depth - todo_b->depth;237}
238
239void resolve_tree_islands(struct repository *r,240int progress,241struct packing_data *to_pack)242{
243struct progress *progress_state = NULL;244struct tree_islands_todo *todo;245int nr = 0;246int i;247
248if (!island_marks)249return;250
251/*252* We process only trees, as commits and tags have already been handled
253* (and passed their marks on to root trees, as well. We must make sure
254* to process them in descending tree-depth order so that marks
255* propagate down the tree properly, even if a sub-tree is found in
256* multiple parent trees.
257*/
258ALLOC_ARRAY(todo, to_pack->nr_objects);259for (i = 0; i < to_pack->nr_objects; i++) {260if (oe_type(&to_pack->objects[i]) == OBJ_TREE) {261todo[nr].entry = &to_pack->objects[i];262todo[nr].depth = oe_tree_depth(to_pack, &to_pack->objects[i]);263nr++;264}265}266QSORT(todo, nr, tree_depth_compare);267
268if (progress)269progress_state = start_progress(_("Propagating island marks"), nr);270
271for (i = 0; i < nr; i++) {272struct object_entry *ent = todo[i].entry;273struct island_bitmap *root_marks;274struct tree *tree;275struct tree_desc desc;276struct name_entry entry;277khiter_t pos;278
279pos = kh_get_oid_map(island_marks, ent->idx.oid);280if (pos >= kh_end(island_marks))281continue;282
283root_marks = kh_value(island_marks, pos);284
285tree = lookup_tree(r, &ent->idx.oid);286if (!tree || parse_tree(tree) < 0)287die(_("bad tree object %s"), oid_to_hex(&ent->idx.oid));288
289init_tree_desc(&desc, &tree->object.oid, tree->buffer, tree->size);290while (tree_entry(&desc, &entry)) {291struct object *obj;292
293if (S_ISGITLINK(entry.mode))294continue;295
296obj = lookup_object(r, &entry.oid);297if (!obj)298continue;299
300set_island_marks(obj, root_marks);301}302
303free_tree_buffer(tree);304
305display_progress(progress_state, i+1);306}307
308stop_progress(&progress_state);309free(todo);310}
311
312struct island_load_data {313kh_str_t *remote_islands;314regex_t *rx;315size_t nr;316size_t alloc;317};318static char *core_island_name;319
320static void free_config_regexes(struct island_load_data *ild)321{
322for (size_t i = 0; i < ild->nr; i++)323regfree(&ild->rx[i]);324free(ild->rx);325}
326
327static void free_remote_islands(kh_str_t *remote_islands)328{
329const char *island_name;330struct remote_island *rl;331
332kh_foreach(remote_islands, island_name, rl, {333free((void *)island_name);334oid_array_clear(&rl->oids);335free(rl);336});337kh_destroy_str(remote_islands);338}
339
340static int island_config_callback(const char *k, const char *v,341const struct config_context *ctx UNUSED,342void *cb)343{
344struct island_load_data *ild = cb;345
346if (!strcmp(k, "pack.island")) {347struct strbuf re = STRBUF_INIT;348
349if (!v)350return config_error_nonbool(k);351
352ALLOC_GROW(ild->rx, ild->nr + 1, ild->alloc);353
354if (*v != '^')355strbuf_addch(&re, '^');356strbuf_addstr(&re, v);357
358if (regcomp(&ild->rx[ild->nr], re.buf, REG_EXTENDED))359die(_("failed to load island regex for '%s': %s"), k, re.buf);360
361strbuf_release(&re);362ild->nr++;363return 0;364}365
366if (!strcmp(k, "pack.islandcore"))367return git_config_string(&core_island_name, k, v);368
369return 0;370}
371
372static void add_ref_to_island(kh_str_t *remote_islands, const char *island_name,373const struct object_id *oid)374{
375uint64_t sha_core;376struct remote_island *rl = NULL;377
378int hash_ret;379khiter_t pos = kh_put_str(remote_islands, island_name, &hash_ret);380
381if (hash_ret) {382kh_key(remote_islands, pos) = xstrdup(island_name);383kh_value(remote_islands, pos) = xcalloc(1, sizeof(struct remote_island));384}385
386rl = kh_value(remote_islands, pos);387oid_array_append(&rl->oids, oid);388
389memcpy(&sha_core, oid->hash, sizeof(uint64_t));390rl->hash += sha_core;391}
392
393static int find_island_for_ref(const char *refname, const char *referent UNUSED, const struct object_id *oid,394int flags UNUSED, void *cb)395{
396struct island_load_data *ild = cb;397
398/*399* We should advertise 'ARRAY_SIZE(matches) - 2' as the max,
400* so we can diagnose below a config with more capture groups
401* than we support.
402*/
403regmatch_t matches[16];404int i, m;405struct strbuf island_name = STRBUF_INIT;406
407/* walk backwards to get last-one-wins ordering */408for (i = ild->nr - 1; i >= 0; i--) {409if (!regexec(&ild->rx[i], refname,410ARRAY_SIZE(matches), matches, 0))411break;412}413
414if (i < 0)415return 0;416
417if (matches[ARRAY_SIZE(matches) - 1].rm_so != -1)418warning(_("island regex from config has "419"too many capture groups (max=%d)"),420(int)ARRAY_SIZE(matches) - 2);421
422for (m = 1; m < ARRAY_SIZE(matches); m++) {423regmatch_t *match = &matches[m];424
425if (match->rm_so == -1)426continue;427
428if (island_name.len)429strbuf_addch(&island_name, '-');430
431strbuf_add(&island_name, refname + match->rm_so, match->rm_eo - match->rm_so);432}433
434add_ref_to_island(ild->remote_islands, island_name.buf, oid);435strbuf_release(&island_name);436return 0;437}
438
439static struct remote_island *get_core_island(kh_str_t *remote_islands)440{
441if (core_island_name) {442khiter_t pos = kh_get_str(remote_islands, core_island_name);443if (pos < kh_end(remote_islands))444return kh_value(remote_islands, pos);445}446
447return NULL;448}
449
450static void deduplicate_islands(kh_str_t *remote_islands, struct repository *r)451{
452struct remote_island *island, *core = NULL, **list;453unsigned int island_count, dst, src, ref, i = 0;454
455island_count = kh_size(remote_islands);456ALLOC_ARRAY(list, island_count);457
458kh_foreach_value(remote_islands, island, {459list[i++] = island;460});461
462for (ref = 0; ref + 1 < island_count; ref++) {463for (src = ref + 1, dst = src; src < island_count; src++) {464if (list[ref]->hash == list[src]->hash)465continue;466
467if (src != dst)468list[dst] = list[src];469
470dst++;471}472island_count = dst;473}474
475island_bitmap_size = (island_count / 32) + 1;476core = get_core_island(remote_islands);477
478for (i = 0; i < island_count; ++i) {479mark_remote_island_1(r, list[i], core && list[i]->hash == core->hash);480}481
482free(list);483}
484
485void load_delta_islands(struct repository *r, int progress)486{
487struct island_load_data ild = { 0 };488
489island_marks = kh_init_oid_map();490
491git_config(island_config_callback, &ild);492ild.remote_islands = kh_init_str();493refs_for_each_ref(get_main_ref_store(the_repository),494find_island_for_ref, &ild);495free_config_regexes(&ild);496deduplicate_islands(ild.remote_islands, r);497free_remote_islands(ild.remote_islands);498
499if (progress)500fprintf(stderr, _("Marked %d islands, done.\n"), island_counter);501}
502
503void propagate_island_marks(struct commit *commit)504{
505khiter_t pos = kh_get_oid_map(island_marks, commit->object.oid);506
507if (pos < kh_end(island_marks)) {508struct commit_list *p;509struct island_bitmap *root_marks = kh_value(island_marks, pos);510
511repo_parse_commit(the_repository, commit);512set_island_marks(&repo_get_commit_tree(the_repository, commit)->object,513root_marks);514for (p = commit->parents; p; p = p->next)515set_island_marks(&p->item->object, root_marks);516}517}
518
519void free_island_marks(void)520{
521struct island_bitmap *bitmap;522
523if (island_marks) {524kh_foreach_value(island_marks, bitmap, {525if (!--bitmap->refcount)526free(bitmap);527});528kh_destroy_oid_map(island_marks);529}530
531/* detect use-after-free with a an address which is never valid: */532island_marks = (void *)-1;533}
534
535int compute_pack_layers(struct packing_data *to_pack)536{
537uint32_t i;538
539if (!core_island_name || !island_marks)540return 1;541
542for (i = 0; i < to_pack->nr_objects; ++i) {543struct object_entry *entry = &to_pack->objects[i];544khiter_t pos = kh_get_oid_map(island_marks, entry->idx.oid);545
546oe_set_layer(to_pack, entry, 1);547
548if (pos < kh_end(island_marks)) {549struct island_bitmap *bitmap = kh_value(island_marks, pos);550
551if (island_bitmap_get(bitmap, island_counter_core))552oe_set_layer(to_pack, entry, 0);553}554}555
556return 2;557}
558