git
/
split-index.c
487 строк · 13.9 Кб
1#define USE_THE_REPOSITORY_VARIABLE2
3#include "git-compat-util.h"4#include "gettext.h"5#include "hash.h"6#include "mem-pool.h"7#include "read-cache-ll.h"8#include "split-index.h"9#include "strbuf.h"10#include "ewah/ewok.h"11
12struct split_index *init_split_index(struct index_state *istate)13{
14if (!istate->split_index) {15if (istate->sparse_index)16die(_("cannot use split index with a sparse index"));17
18CALLOC_ARRAY(istate->split_index, 1);19istate->split_index->refcount = 1;20}21return istate->split_index;22}
23
24int read_link_extension(struct index_state *istate,25const void *data_, unsigned long sz)26{
27const unsigned char *data = data_;28struct split_index *si;29int ret;30
31if (sz < the_hash_algo->rawsz)32return error("corrupt link extension (too short)");33si = init_split_index(istate);34oidread(&si->base_oid, data, the_repository->hash_algo);35data += the_hash_algo->rawsz;36sz -= the_hash_algo->rawsz;37if (!sz)38return 0;39si->delete_bitmap = ewah_new();40ret = ewah_read_mmap(si->delete_bitmap, data, sz);41if (ret < 0)42return error("corrupt delete bitmap in link extension");43data += ret;44sz -= ret;45si->replace_bitmap = ewah_new();46ret = ewah_read_mmap(si->replace_bitmap, data, sz);47if (ret < 0)48return error("corrupt replace bitmap in link extension");49if (ret != sz)50return error("garbage at the end of link extension");51return 0;52}
53
54int write_link_extension(struct strbuf *sb,55struct index_state *istate)56{
57struct split_index *si = istate->split_index;58strbuf_add(sb, si->base_oid.hash, the_hash_algo->rawsz);59if (!si->delete_bitmap && !si->replace_bitmap)60return 0;61ewah_serialize_strbuf(si->delete_bitmap, sb);62ewah_serialize_strbuf(si->replace_bitmap, sb);63return 0;64}
65
66static void mark_base_index_entries(struct index_state *base)67{
68int i;69/*70* To keep track of the shared entries between
71* istate->base->cache[] and istate->cache[], base entry
72* position is stored in each base entry. All positions start
73* from 1 instead of 0, which is reserved to say "this is a new
74* entry".
75*/
76for (i = 0; i < base->cache_nr; i++)77base->cache[i]->index = i + 1;78}
79
80void move_cache_to_base_index(struct index_state *istate)81{
82struct split_index *si = istate->split_index;83int i;84
85/*86* If there was a previous base index, then transfer ownership of allocated
87* entries to the parent index.
88*/
89if (si->base &&90si->base->ce_mem_pool) {91
92if (!istate->ce_mem_pool) {93istate->ce_mem_pool = xmalloc(sizeof(struct mem_pool));94mem_pool_init(istate->ce_mem_pool, 0);95}96
97mem_pool_combine(istate->ce_mem_pool, istate->split_index->base->ce_mem_pool);98}99
100ALLOC_ARRAY(si->base, 1);101index_state_init(si->base, istate->repo);102si->base->version = istate->version;103/* zero timestamp disables racy test in ce_write_index() */104si->base->timestamp = istate->timestamp;105ALLOC_GROW(si->base->cache, istate->cache_nr, si->base->cache_alloc);106si->base->cache_nr = istate->cache_nr;107
108/*109* The mem_pool needs to move with the allocated entries.
110*/
111si->base->ce_mem_pool = istate->ce_mem_pool;112istate->ce_mem_pool = NULL;113
114COPY_ARRAY(si->base->cache, istate->cache, istate->cache_nr);115mark_base_index_entries(si->base);116for (i = 0; i < si->base->cache_nr; i++)117si->base->cache[i]->ce_flags &= ~CE_UPDATE_IN_BASE;118}
119
120static void mark_entry_for_delete(size_t pos, void *data)121{
122struct index_state *istate = data;123if (pos >= istate->cache_nr)124die("position for delete %d exceeds base index size %d",125(int)pos, istate->cache_nr);126istate->cache[pos]->ce_flags |= CE_REMOVE;127istate->split_index->nr_deletions++;128}
129
130static void replace_entry(size_t pos, void *data)131{
132struct index_state *istate = data;133struct split_index *si = istate->split_index;134struct cache_entry *dst, *src;135
136if (pos >= istate->cache_nr)137die("position for replacement %d exceeds base index size %d",138(int)pos, istate->cache_nr);139if (si->nr_replacements >= si->saved_cache_nr)140die("too many replacements (%d vs %d)",141si->nr_replacements, si->saved_cache_nr);142dst = istate->cache[pos];143if (dst->ce_flags & CE_REMOVE)144die("entry %d is marked as both replaced and deleted",145(int)pos);146src = si->saved_cache[si->nr_replacements];147if (ce_namelen(src))148die("corrupt link extension, entry %d should have "149"zero length name", (int)pos);150src->index = pos + 1;151src->ce_flags |= CE_UPDATE_IN_BASE;152src->ce_namelen = dst->ce_namelen;153copy_cache_entry(dst, src);154discard_cache_entry(src);155si->nr_replacements++;156}
157
158void merge_base_index(struct index_state *istate)159{
160struct split_index *si = istate->split_index;161unsigned int i;162
163mark_base_index_entries(si->base);164
165si->saved_cache = istate->cache;166si->saved_cache_nr = istate->cache_nr;167istate->cache_nr = si->base->cache_nr;168istate->cache = NULL;169istate->cache_alloc = 0;170ALLOC_GROW(istate->cache, istate->cache_nr, istate->cache_alloc);171COPY_ARRAY(istate->cache, si->base->cache, istate->cache_nr);172
173si->nr_deletions = 0;174si->nr_replacements = 0;175ewah_each_bit(si->replace_bitmap, replace_entry, istate);176ewah_each_bit(si->delete_bitmap, mark_entry_for_delete, istate);177if (si->nr_deletions)178remove_marked_cache_entries(istate, 0);179
180for (i = si->nr_replacements; i < si->saved_cache_nr; i++) {181if (!ce_namelen(si->saved_cache[i]))182die("corrupt link extension, entry %d should "183"have non-zero length name", i);184add_index_entry(istate, si->saved_cache[i],185ADD_CACHE_OK_TO_ADD |186ADD_CACHE_KEEP_CACHE_TREE |187/*188* we may have to replay what
189* merge-recursive.c:update_stages()
190* does, which has this flag on
191*/
192ADD_CACHE_SKIP_DFCHECK);193si->saved_cache[i] = NULL;194}195
196ewah_free(si->delete_bitmap);197ewah_free(si->replace_bitmap);198FREE_AND_NULL(si->saved_cache);199si->delete_bitmap = NULL;200si->replace_bitmap = NULL;201si->saved_cache_nr = 0;202}
203
204/*
205* Compare most of the fields in two cache entries, i.e. all except the
206* hashmap_entry and the name.
207*/
208static int compare_ce_content(struct cache_entry *a, struct cache_entry *b)209{
210const unsigned int ondisk_flags = CE_STAGEMASK | CE_VALID |211CE_EXTENDED_FLAGS;212unsigned int ce_flags = a->ce_flags;213unsigned int base_flags = b->ce_flags;214int ret;215
216/* only on-disk flags matter */217a->ce_flags &= ondisk_flags;218b->ce_flags &= ondisk_flags;219ret = memcmp(&a->ce_stat_data, &b->ce_stat_data,220offsetof(struct cache_entry, name) -221offsetof(struct cache_entry, oid)) ||222!oideq(&a->oid, &b->oid);223a->ce_flags = ce_flags;224b->ce_flags = base_flags;225
226return ret;227}
228
229void prepare_to_write_split_index(struct index_state *istate)230{
231struct split_index *si = init_split_index(istate);232struct cache_entry **entries = NULL, *ce;233int i, nr_entries = 0, nr_alloc = 0;234
235si->delete_bitmap = ewah_new();236si->replace_bitmap = ewah_new();237
238if (si->base) {239/* Go through istate->cache[] and mark CE_MATCHED to240* entry with positive index. We'll go through
241* base->cache[] later to delete all entries in base
242* that are not marked with either CE_MATCHED or
243* CE_UPDATE_IN_BASE. If istate->cache[i] is a
244* duplicate, deduplicate it.
245*/
246for (i = 0; i < istate->cache_nr; i++) {247struct cache_entry *base;248ce = istate->cache[i];249if (!ce->index) {250/*251* During simple update index operations this
252* is a cache entry that is not present in
253* the shared index. It will be added to the
254* split index.
255*
256* However, it might also represent a file
257* that already has a cache entry in the
258* shared index, but a new index has just
259* been constructed by unpack_trees(), and
260* this entry now refers to different content
261* than what was recorded in the original
262* index, e.g. during 'read-tree -m HEAD^' or
263* 'checkout HEAD^'. In this case the
264* original entry in the shared index will be
265* marked as deleted, and this entry will be
266* added to the split index.
267*/
268continue;269}270if (ce->index > si->base->cache_nr) {271BUG("ce refers to a shared ce at %d, which is beyond the shared index size %d",272ce->index, si->base->cache_nr);273}274ce->ce_flags |= CE_MATCHED; /* or "shared" */275base = si->base->cache[ce->index - 1];276if (ce == base) {277/* The entry is present in the shared index. */278if (ce->ce_flags & CE_UPDATE_IN_BASE) {279/*280* Already marked for inclusion in
281* the split index, either because
282* the corresponding file was
283* modified and the cached stat data
284* was refreshed, or because there
285* is already a replacement entry in
286* the split index.
287* Nothing more to do here.
288*/
289} else if (!ce_uptodate(ce) &&290is_racy_timestamp(istate, ce)) {291/*292* A racily clean cache entry stored
293* only in the shared index: it must
294* be added to the split index, so
295* the subsequent do_write_index()
296* can smudge its stat data.
297*/
298ce->ce_flags |= CE_UPDATE_IN_BASE;299} else {300/*301* The entry is only present in the
302* shared index and it was not
303* refreshed.
304* Just leave it there.
305*/
306}307continue;308}309if (ce->ce_namelen != base->ce_namelen ||310strcmp(ce->name, base->name)) {311ce->index = 0;312continue;313}314/*315* This is the copy of a cache entry that is present
316* in the shared index, created by unpack_trees()
317* while it constructed a new index.
318*/
319if (ce->ce_flags & CE_UPDATE_IN_BASE) {320/*321* Already marked for inclusion in the split
322* index, either because the corresponding
323* file was modified and the cached stat data
324* was refreshed, or because the original
325* entry already had a replacement entry in
326* the split index.
327* Nothing to do.
328*/
329} else if (!ce_uptodate(ce) &&330is_racy_timestamp(istate, ce)) {331/*332* A copy of a racily clean cache entry from
333* the shared index. It must be added to
334* the split index, so the subsequent
335* do_write_index() can smudge its stat data.
336*/
337ce->ce_flags |= CE_UPDATE_IN_BASE;338} else {339/*340* Thoroughly compare the cached data to see
341* whether it should be marked for inclusion
342* in the split index.
343*
344* This comparison might be unnecessary, as
345* code paths modifying the cached data do
346* set CE_UPDATE_IN_BASE as well.
347*/
348if (compare_ce_content(ce, base))349ce->ce_flags |= CE_UPDATE_IN_BASE;350}351discard_cache_entry(base);352si->base->cache[ce->index - 1] = ce;353}354for (i = 0; i < si->base->cache_nr; i++) {355ce = si->base->cache[i];356if ((ce->ce_flags & CE_REMOVE) ||357!(ce->ce_flags & CE_MATCHED))358ewah_set(si->delete_bitmap, i);359else if (ce->ce_flags & CE_UPDATE_IN_BASE) {360ewah_set(si->replace_bitmap, i);361ce->ce_flags |= CE_STRIP_NAME;362ALLOC_GROW(entries, nr_entries+1, nr_alloc);363entries[nr_entries++] = ce;364}365if (is_null_oid(&ce->oid))366istate->drop_cache_tree = 1;367}368}369
370for (i = 0; i < istate->cache_nr; i++) {371ce = istate->cache[i];372if ((!si->base || !ce->index) && !(ce->ce_flags & CE_REMOVE)) {373assert(!(ce->ce_flags & CE_STRIP_NAME));374ALLOC_GROW(entries, nr_entries+1, nr_alloc);375entries[nr_entries++] = ce;376}377ce->ce_flags &= ~CE_MATCHED;378}379
380/*381* take cache[] out temporarily, put entries[] in its place
382* for writing
383*/
384si->saved_cache = istate->cache;385si->saved_cache_nr = istate->cache_nr;386istate->cache = entries;387istate->cache_nr = nr_entries;388}
389
390void finish_writing_split_index(struct index_state *istate)391{
392struct split_index *si = init_split_index(istate);393
394ewah_free(si->delete_bitmap);395ewah_free(si->replace_bitmap);396si->delete_bitmap = NULL;397si->replace_bitmap = NULL;398free(istate->cache);399istate->cache = si->saved_cache;400istate->cache_nr = si->saved_cache_nr;401}
402
403void discard_split_index(struct index_state *istate)404{
405struct split_index *si = istate->split_index;406if (!si)407return;408istate->split_index = NULL;409si->refcount--;410if (si->refcount)411return;412if (si->base) {413discard_index(si->base);414free(si->base);415}416free(si);417}
418
419void save_or_free_index_entry(struct index_state *istate, struct cache_entry *ce)420{
421if (ce->index &&422istate->split_index &&423istate->split_index->base &&424ce->index <= istate->split_index->base->cache_nr &&425ce == istate->split_index->base->cache[ce->index - 1])426ce->ce_flags |= CE_REMOVE;427else428discard_cache_entry(ce);429}
430
431void replace_index_entry_in_base(struct index_state *istate,432struct cache_entry *old_entry,433struct cache_entry *new_entry)434{
435if (old_entry->index &&436istate->split_index &&437istate->split_index->base &&438old_entry->index <= istate->split_index->base->cache_nr) {439new_entry->index = old_entry->index;440if (old_entry != istate->split_index->base->cache[new_entry->index - 1])441discard_cache_entry(istate->split_index->base->cache[new_entry->index - 1]);442istate->split_index->base->cache[new_entry->index - 1] = new_entry;443}444}
445
446void add_split_index(struct index_state *istate)447{
448if (!istate->split_index) {449init_split_index(istate);450istate->cache_changed |= SPLIT_INDEX_ORDERED;451}452}
453
454void remove_split_index(struct index_state *istate)455{
456if (istate->split_index) {457if (istate->split_index->base) {458/*459* When removing the split index, we need to move
460* ownership of the mem_pool associated with the
461* base index to the main index. There may be cache entries
462* allocated from the base's memory pool that are shared with
463* the_index.cache[].
464*/
465mem_pool_combine(istate->ce_mem_pool,466istate->split_index->base->ce_mem_pool);467
468/*469* The split index no longer owns the mem_pool backing
470* its cache array. As we are discarding this index,
471* mark the index as having no cache entries, so it
472* will not attempt to clean up the cache entries or
473* validate them.
474*/
475istate->split_index->base->cache_nr = 0;476}477
478/*479* We can discard the split index because its
480* memory pool has been incorporated into the
481* memory pool associated with the the_index.
482*/
483discard_split_index(istate);484
485istate->cache_changed |= SOMETHING_CHANGED;486}487}
488