2 #include "split-index.h"
5 struct split_index *init_split_index(struct index_state *istate)
7 if (!istate->split_index) {
8 istate->split_index = xcalloc(1, sizeof(*istate->split_index));
9 istate->split_index->refcount = 1;
11 return istate->split_index;
14 int read_link_extension(struct index_state *istate,
15 const void *data_, unsigned long sz)
17 const unsigned char *data = data_;
18 struct split_index *si;
22 return error("corrupt link extension (too short)");
23 si = init_split_index(istate);
24 hashcpy(si->base_sha1, data);
29 si->delete_bitmap = ewah_new();
30 ret = ewah_read_mmap(si->delete_bitmap, data, sz);
32 return error("corrupt delete bitmap in link extension");
35 si->replace_bitmap = ewah_new();
36 ret = ewah_read_mmap(si->replace_bitmap, data, sz);
38 return error("corrupt replace bitmap in link extension");
40 return error("garbage at the end of link extension");
44 static int write_strbuf(void *user_data, const void *data, size_t len)
46 struct strbuf *sb = user_data;
47 strbuf_add(sb, data, len);
51 int write_link_extension(struct strbuf *sb,
52 struct index_state *istate)
54 struct split_index *si = istate->split_index;
55 strbuf_add(sb, si->base_sha1, 20);
56 if (!si->delete_bitmap && !si->replace_bitmap)
58 ewah_serialize_to(si->delete_bitmap, write_strbuf, sb);
59 ewah_serialize_to(si->replace_bitmap, write_strbuf, sb);
63 static void mark_base_index_entries(struct index_state *base)
67 * To keep track of the shared entries between
68 * istate->base->cache[] and istate->cache[], base entry
69 * position is stored in each base entry. All positions start
70 * from 1 instead of 0, which is resrved to say "this is a new
73 for (i = 0; i < base->cache_nr; i++)
74 base->cache[i]->index = i + 1;
77 void move_cache_to_base_index(struct index_state *istate)
79 struct split_index *si = istate->split_index;
83 * do not delete old si->base, its index entries may be shared
84 * with istate->cache[]. Accept a bit of leaking here because
85 * this code is only used by short-lived update-index.
87 si->base = xcalloc(1, sizeof(*si->base));
88 si->base->version = istate->version;
89 /* zero timestamp disables racy test in ce_write_index() */
90 si->base->timestamp = istate->timestamp;
91 ALLOC_GROW(si->base->cache, istate->cache_nr, si->base->cache_alloc);
92 si->base->cache_nr = istate->cache_nr;
93 memcpy(si->base->cache, istate->cache,
94 sizeof(*istate->cache) * istate->cache_nr);
95 mark_base_index_entries(si->base);
96 for (i = 0; i < si->base->cache_nr; i++)
97 si->base->cache[i]->ce_flags &= ~CE_UPDATE_IN_BASE;
100 static void mark_entry_for_delete(size_t pos, void *data)
102 struct index_state *istate = data;
103 if (pos >= istate->cache_nr)
104 die("position for delete %d exceeds base index size %d",
105 (int)pos, istate->cache_nr);
106 istate->cache[pos]->ce_flags |= CE_REMOVE;
107 istate->split_index->nr_deletions = 1;
110 static void replace_entry(size_t pos, void *data)
112 struct index_state *istate = data;
113 struct split_index *si = istate->split_index;
114 struct cache_entry *dst, *src;
116 if (pos >= istate->cache_nr)
117 die("position for replacement %d exceeds base index size %d",
118 (int)pos, istate->cache_nr);
119 if (si->nr_replacements >= si->saved_cache_nr)
120 die("too many replacements (%d vs %d)",
121 si->nr_replacements, si->saved_cache_nr);
122 dst = istate->cache[pos];
123 if (dst->ce_flags & CE_REMOVE)
124 die("entry %d is marked as both replaced and deleted",
126 src = si->saved_cache[si->nr_replacements];
128 die("corrupt link extension, entry %d should have "
129 "zero length name", (int)pos);
130 src->index = pos + 1;
131 src->ce_flags |= CE_UPDATE_IN_BASE;
132 src->ce_namelen = dst->ce_namelen;
133 copy_cache_entry(dst, src);
135 si->nr_replacements++;
138 void merge_base_index(struct index_state *istate)
140 struct split_index *si = istate->split_index;
143 mark_base_index_entries(si->base);
145 si->saved_cache = istate->cache;
146 si->saved_cache_nr = istate->cache_nr;
147 istate->cache_nr = si->base->cache_nr;
148 istate->cache = NULL;
149 istate->cache_alloc = 0;
150 ALLOC_GROW(istate->cache, istate->cache_nr, istate->cache_alloc);
151 memcpy(istate->cache, si->base->cache,
152 sizeof(*istate->cache) * istate->cache_nr);
154 si->nr_deletions = 0;
155 si->nr_replacements = 0;
156 ewah_each_bit(si->replace_bitmap, replace_entry, istate);
157 ewah_each_bit(si->delete_bitmap, mark_entry_for_delete, istate);
158 if (si->nr_deletions)
159 remove_marked_cache_entries(istate);
161 for (i = si->nr_replacements; i < si->saved_cache_nr; i++) {
162 if (!ce_namelen(si->saved_cache[i]))
163 die("corrupt link extension, entry %d should "
164 "have non-zero length name", i);
165 add_index_entry(istate, si->saved_cache[i],
166 ADD_CACHE_OK_TO_ADD |
167 ADD_CACHE_KEEP_CACHE_TREE |
169 * we may have to replay what
170 * merge-recursive.c:update_stages()
171 * does, which has this flag on
173 ADD_CACHE_SKIP_DFCHECK);
174 si->saved_cache[i] = NULL;
177 ewah_free(si->delete_bitmap);
178 ewah_free(si->replace_bitmap);
179 free(si->saved_cache);
180 si->delete_bitmap = NULL;
181 si->replace_bitmap = NULL;
182 si->saved_cache = NULL;
183 si->saved_cache_nr = 0;
186 void prepare_to_write_split_index(struct index_state *istate)
188 struct split_index *si = init_split_index(istate);
189 struct cache_entry **entries = NULL, *ce;
190 int i, nr_entries = 0, nr_alloc = 0;
192 si->delete_bitmap = ewah_new();
193 si->replace_bitmap = ewah_new();
196 /* Go through istate->cache[] and mark CE_MATCHED to
197 * entry with positive index. We'll go through
198 * base->cache[] later to delete all entries in base
199 * that are not marked eith either CE_MATCHED or
200 * CE_UPDATE_IN_BASE. If istate->cache[i] is a
201 * duplicate, deduplicate it.
203 for (i = 0; i < istate->cache_nr; i++) {
204 struct cache_entry *base;
205 /* namelen is checked separately */
206 const unsigned int ondisk_flags =
207 CE_STAGEMASK | CE_VALID | CE_EXTENDED_FLAGS;
208 unsigned int ce_flags, base_flags, ret;
209 ce = istate->cache[i];
212 if (ce->index > si->base->cache_nr) {
216 ce->ce_flags |= CE_MATCHED; /* or "shared" */
217 base = si->base->cache[ce->index - 1];
220 if (ce->ce_namelen != base->ce_namelen ||
221 strcmp(ce->name, base->name)) {
225 ce_flags = ce->ce_flags;
226 base_flags = base->ce_flags;
227 /* only on-disk flags matter */
228 ce->ce_flags &= ondisk_flags;
229 base->ce_flags &= ondisk_flags;
230 ret = memcmp(&ce->ce_stat_data, &base->ce_stat_data,
231 offsetof(struct cache_entry, name) -
232 offsetof(struct cache_entry, ce_stat_data));
233 ce->ce_flags = ce_flags;
234 base->ce_flags = base_flags;
236 ce->ce_flags |= CE_UPDATE_IN_BASE;
238 si->base->cache[ce->index - 1] = ce;
240 for (i = 0; i < si->base->cache_nr; i++) {
241 ce = si->base->cache[i];
242 if ((ce->ce_flags & CE_REMOVE) ||
243 !(ce->ce_flags & CE_MATCHED))
244 ewah_set(si->delete_bitmap, i);
245 else if (ce->ce_flags & CE_UPDATE_IN_BASE) {
246 ewah_set(si->replace_bitmap, i);
247 ce->ce_flags |= CE_STRIP_NAME;
248 ALLOC_GROW(entries, nr_entries+1, nr_alloc);
249 entries[nr_entries++] = ce;
254 for (i = 0; i < istate->cache_nr; i++) {
255 ce = istate->cache[i];
256 if ((!si->base || !ce->index) && !(ce->ce_flags & CE_REMOVE)) {
257 assert(!(ce->ce_flags & CE_STRIP_NAME));
258 ALLOC_GROW(entries, nr_entries+1, nr_alloc);
259 entries[nr_entries++] = ce;
261 ce->ce_flags &= ~CE_MATCHED;
265 * take cache[] out temporarily, put entries[] in its place
268 si->saved_cache = istate->cache;
269 si->saved_cache_nr = istate->cache_nr;
270 istate->cache = entries;
271 istate->cache_nr = nr_entries;
274 void finish_writing_split_index(struct index_state *istate)
276 struct split_index *si = init_split_index(istate);
278 ewah_free(si->delete_bitmap);
279 ewah_free(si->replace_bitmap);
280 si->delete_bitmap = NULL;
281 si->replace_bitmap = NULL;
283 istate->cache = si->saved_cache;
284 istate->cache_nr = si->saved_cache_nr;
287 void discard_split_index(struct index_state *istate)
289 struct split_index *si = istate->split_index;
292 istate->split_index = NULL;
297 discard_index(si->base);
303 void save_or_free_index_entry(struct index_state *istate, struct cache_entry *ce)
306 istate->split_index &&
307 istate->split_index->base &&
308 ce->index <= istate->split_index->base->cache_nr &&
309 ce == istate->split_index->base->cache[ce->index - 1])
310 ce->ce_flags |= CE_REMOVE;
315 void replace_index_entry_in_base(struct index_state *istate,
316 struct cache_entry *old,
317 struct cache_entry *new)
320 istate->split_index &&
321 istate->split_index->base &&
322 old->index <= istate->split_index->base->cache_nr) {
323 new->index = old->index;
324 if (old != istate->split_index->base->cache[new->index - 1])
325 free(istate->split_index->base->cache[new->index - 1]);
326 istate->split_index->base->cache[new->index - 1] = new;