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 int write_link_extension(struct strbuf *sb,
45 struct index_state *istate)
47 struct split_index *si = istate->split_index;
48 strbuf_add(sb, si->base_sha1, 20);
49 if (!si->delete_bitmap && !si->replace_bitmap)
51 ewah_serialize_strbuf(si->delete_bitmap, sb);
52 ewah_serialize_strbuf(si->replace_bitmap, sb);
56 static void mark_base_index_entries(struct index_state *base)
60 * To keep track of the shared entries between
61 * istate->base->cache[] and istate->cache[], base entry
62 * position is stored in each base entry. All positions start
63 * from 1 instead of 0, which is reserved to say "this is a new
66 for (i = 0; i < base->cache_nr; i++)
67 base->cache[i]->index = i + 1;
70 void move_cache_to_base_index(struct index_state *istate)
72 struct split_index *si = istate->split_index;
76 * do not delete old si->base, its index entries may be shared
77 * with istate->cache[]. Accept a bit of leaking here because
78 * this code is only used by short-lived update-index.
80 si->base = xcalloc(1, sizeof(*si->base));
81 si->base->version = istate->version;
82 /* zero timestamp disables racy test in ce_write_index() */
83 si->base->timestamp = istate->timestamp;
84 ALLOC_GROW(si->base->cache, istate->cache_nr, si->base->cache_alloc);
85 si->base->cache_nr = istate->cache_nr;
86 memcpy(si->base->cache, istate->cache,
87 sizeof(*istate->cache) * istate->cache_nr);
88 mark_base_index_entries(si->base);
89 for (i = 0; i < si->base->cache_nr; i++)
90 si->base->cache[i]->ce_flags &= ~CE_UPDATE_IN_BASE;
93 static void mark_entry_for_delete(size_t pos, void *data)
95 struct index_state *istate = data;
96 if (pos >= istate->cache_nr)
97 die("position for delete %d exceeds base index size %d",
98 (int)pos, istate->cache_nr);
99 istate->cache[pos]->ce_flags |= CE_REMOVE;
100 istate->split_index->nr_deletions = 1;
103 static void replace_entry(size_t pos, void *data)
105 struct index_state *istate = data;
106 struct split_index *si = istate->split_index;
107 struct cache_entry *dst, *src;
109 if (pos >= istate->cache_nr)
110 die("position for replacement %d exceeds base index size %d",
111 (int)pos, istate->cache_nr);
112 if (si->nr_replacements >= si->saved_cache_nr)
113 die("too many replacements (%d vs %d)",
114 si->nr_replacements, si->saved_cache_nr);
115 dst = istate->cache[pos];
116 if (dst->ce_flags & CE_REMOVE)
117 die("entry %d is marked as both replaced and deleted",
119 src = si->saved_cache[si->nr_replacements];
121 die("corrupt link extension, entry %d should have "
122 "zero length name", (int)pos);
123 src->index = pos + 1;
124 src->ce_flags |= CE_UPDATE_IN_BASE;
125 src->ce_namelen = dst->ce_namelen;
126 copy_cache_entry(dst, src);
128 si->nr_replacements++;
131 void merge_base_index(struct index_state *istate)
133 struct split_index *si = istate->split_index;
136 mark_base_index_entries(si->base);
138 si->saved_cache = istate->cache;
139 si->saved_cache_nr = istate->cache_nr;
140 istate->cache_nr = si->base->cache_nr;
141 istate->cache = NULL;
142 istate->cache_alloc = 0;
143 ALLOC_GROW(istate->cache, istate->cache_nr, istate->cache_alloc);
144 memcpy(istate->cache, si->base->cache,
145 sizeof(*istate->cache) * istate->cache_nr);
147 si->nr_deletions = 0;
148 si->nr_replacements = 0;
149 ewah_each_bit(si->replace_bitmap, replace_entry, istate);
150 ewah_each_bit(si->delete_bitmap, mark_entry_for_delete, istate);
151 if (si->nr_deletions)
152 remove_marked_cache_entries(istate);
154 for (i = si->nr_replacements; i < si->saved_cache_nr; i++) {
155 if (!ce_namelen(si->saved_cache[i]))
156 die("corrupt link extension, entry %d should "
157 "have non-zero length name", i);
158 add_index_entry(istate, si->saved_cache[i],
159 ADD_CACHE_OK_TO_ADD |
160 ADD_CACHE_KEEP_CACHE_TREE |
162 * we may have to replay what
163 * merge-recursive.c:update_stages()
164 * does, which has this flag on
166 ADD_CACHE_SKIP_DFCHECK);
167 si->saved_cache[i] = NULL;
170 ewah_free(si->delete_bitmap);
171 ewah_free(si->replace_bitmap);
172 free(si->saved_cache);
173 si->delete_bitmap = NULL;
174 si->replace_bitmap = NULL;
175 si->saved_cache = NULL;
176 si->saved_cache_nr = 0;
179 void prepare_to_write_split_index(struct index_state *istate)
181 struct split_index *si = init_split_index(istate);
182 struct cache_entry **entries = NULL, *ce;
183 int i, nr_entries = 0, nr_alloc = 0;
185 si->delete_bitmap = ewah_new();
186 si->replace_bitmap = ewah_new();
189 /* Go through istate->cache[] and mark CE_MATCHED to
190 * entry with positive index. We'll go through
191 * base->cache[] later to delete all entries in base
192 * that are not marked eith either CE_MATCHED or
193 * CE_UPDATE_IN_BASE. If istate->cache[i] is a
194 * duplicate, deduplicate it.
196 for (i = 0; i < istate->cache_nr; i++) {
197 struct cache_entry *base;
198 /* namelen is checked separately */
199 const unsigned int ondisk_flags =
200 CE_STAGEMASK | CE_VALID | CE_EXTENDED_FLAGS;
201 unsigned int ce_flags, base_flags, ret;
202 ce = istate->cache[i];
205 if (ce->index > si->base->cache_nr) {
209 ce->ce_flags |= CE_MATCHED; /* or "shared" */
210 base = si->base->cache[ce->index - 1];
213 if (ce->ce_namelen != base->ce_namelen ||
214 strcmp(ce->name, base->name)) {
218 ce_flags = ce->ce_flags;
219 base_flags = base->ce_flags;
220 /* only on-disk flags matter */
221 ce->ce_flags &= ondisk_flags;
222 base->ce_flags &= ondisk_flags;
223 ret = memcmp(&ce->ce_stat_data, &base->ce_stat_data,
224 offsetof(struct cache_entry, name) -
225 offsetof(struct cache_entry, ce_stat_data));
226 ce->ce_flags = ce_flags;
227 base->ce_flags = base_flags;
229 ce->ce_flags |= CE_UPDATE_IN_BASE;
231 si->base->cache[ce->index - 1] = ce;
233 for (i = 0; i < si->base->cache_nr; i++) {
234 ce = si->base->cache[i];
235 if ((ce->ce_flags & CE_REMOVE) ||
236 !(ce->ce_flags & CE_MATCHED))
237 ewah_set(si->delete_bitmap, i);
238 else if (ce->ce_flags & CE_UPDATE_IN_BASE) {
239 ewah_set(si->replace_bitmap, i);
240 ce->ce_flags |= CE_STRIP_NAME;
241 ALLOC_GROW(entries, nr_entries+1, nr_alloc);
242 entries[nr_entries++] = ce;
247 for (i = 0; i < istate->cache_nr; i++) {
248 ce = istate->cache[i];
249 if ((!si->base || !ce->index) && !(ce->ce_flags & CE_REMOVE)) {
250 assert(!(ce->ce_flags & CE_STRIP_NAME));
251 ALLOC_GROW(entries, nr_entries+1, nr_alloc);
252 entries[nr_entries++] = ce;
254 ce->ce_flags &= ~CE_MATCHED;
258 * take cache[] out temporarily, put entries[] in its place
261 si->saved_cache = istate->cache;
262 si->saved_cache_nr = istate->cache_nr;
263 istate->cache = entries;
264 istate->cache_nr = nr_entries;
267 void finish_writing_split_index(struct index_state *istate)
269 struct split_index *si = init_split_index(istate);
271 ewah_free(si->delete_bitmap);
272 ewah_free(si->replace_bitmap);
273 si->delete_bitmap = NULL;
274 si->replace_bitmap = NULL;
276 istate->cache = si->saved_cache;
277 istate->cache_nr = si->saved_cache_nr;
280 void discard_split_index(struct index_state *istate)
282 struct split_index *si = istate->split_index;
285 istate->split_index = NULL;
290 discard_index(si->base);
296 void save_or_free_index_entry(struct index_state *istate, struct cache_entry *ce)
299 istate->split_index &&
300 istate->split_index->base &&
301 ce->index <= istate->split_index->base->cache_nr &&
302 ce == istate->split_index->base->cache[ce->index - 1])
303 ce->ce_flags |= CE_REMOVE;
308 void replace_index_entry_in_base(struct index_state *istate,
309 struct cache_entry *old,
310 struct cache_entry *new)
313 istate->split_index &&
314 istate->split_index->base &&
315 old->index <= istate->split_index->base->cache_nr) {
316 new->index = old->index;
317 if (old != istate->split_index->base->cache[new->index - 1])
318 free(istate->split_index->base->cache[new->index - 1]);
319 istate->split_index->base->cache[new->index - 1] = new;