Merge branch 'ys/bisect-object-id-missing-conversion-fix'
[git] / split-index.c
1 #include "cache.h"
2 #include "split-index.h"
3 #include "ewah/ewok.h"
4
5 struct split_index *init_split_index(struct index_state *istate)
6 {
7         if (!istate->split_index) {
8                 istate->split_index = xcalloc(1, sizeof(*istate->split_index));
9                 istate->split_index->refcount = 1;
10         }
11         return istate->split_index;
12 }
13
14 int read_link_extension(struct index_state *istate,
15                          const void *data_, unsigned long sz)
16 {
17         const unsigned char *data = data_;
18         struct split_index *si;
19         int ret;
20
21         if (sz < 20)
22                 return error("corrupt link extension (too short)");
23         si = init_split_index(istate);
24         hashcpy(si->base_sha1, data);
25         data += 20;
26         sz -= 20;
27         if (!sz)
28                 return 0;
29         si->delete_bitmap = ewah_new();
30         ret = ewah_read_mmap(si->delete_bitmap, data, sz);
31         if (ret < 0)
32                 return error("corrupt delete bitmap in link extension");
33         data += ret;
34         sz -= ret;
35         si->replace_bitmap = ewah_new();
36         ret = ewah_read_mmap(si->replace_bitmap, data, sz);
37         if (ret < 0)
38                 return error("corrupt replace bitmap in link extension");
39         if (ret != sz)
40                 return error("garbage at the end of link extension");
41         return 0;
42 }
43
44 int write_link_extension(struct strbuf *sb,
45                          struct index_state *istate)
46 {
47         struct split_index *si = istate->split_index;
48         strbuf_add(sb, si->base_sha1, 20);
49         if (!si->delete_bitmap && !si->replace_bitmap)
50                 return 0;
51         ewah_serialize_strbuf(si->delete_bitmap, sb);
52         ewah_serialize_strbuf(si->replace_bitmap, sb);
53         return 0;
54 }
55
56 static void mark_base_index_entries(struct index_state *base)
57 {
58         int i;
59         /*
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
64          * entry".
65          */
66         for (i = 0; i < base->cache_nr; i++)
67                 base->cache[i]->index = i + 1;
68 }
69
70 void move_cache_to_base_index(struct index_state *istate)
71 {
72         struct split_index *si = istate->split_index;
73         int i;
74
75         /*
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.
79          */
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         COPY_ARRAY(si->base->cache, istate->cache, istate->cache_nr);
87         mark_base_index_entries(si->base);
88         for (i = 0; i < si->base->cache_nr; i++)
89                 si->base->cache[i]->ce_flags &= ~CE_UPDATE_IN_BASE;
90 }
91
92 static void mark_entry_for_delete(size_t pos, void *data)
93 {
94         struct index_state *istate = data;
95         if (pos >= istate->cache_nr)
96                 die("position for delete %d exceeds base index size %d",
97                     (int)pos, istate->cache_nr);
98         istate->cache[pos]->ce_flags |= CE_REMOVE;
99         istate->split_index->nr_deletions = 1;
100 }
101
102 static void replace_entry(size_t pos, void *data)
103 {
104         struct index_state *istate = data;
105         struct split_index *si = istate->split_index;
106         struct cache_entry *dst, *src;
107
108         if (pos >= istate->cache_nr)
109                 die("position for replacement %d exceeds base index size %d",
110                     (int)pos, istate->cache_nr);
111         if (si->nr_replacements >= si->saved_cache_nr)
112                 die("too many replacements (%d vs %d)",
113                     si->nr_replacements, si->saved_cache_nr);
114         dst = istate->cache[pos];
115         if (dst->ce_flags & CE_REMOVE)
116                 die("entry %d is marked as both replaced and deleted",
117                     (int)pos);
118         src = si->saved_cache[si->nr_replacements];
119         if (ce_namelen(src))
120                 die("corrupt link extension, entry %d should have "
121                     "zero length name", (int)pos);
122         src->index = pos + 1;
123         src->ce_flags |= CE_UPDATE_IN_BASE;
124         src->ce_namelen = dst->ce_namelen;
125         copy_cache_entry(dst, src);
126         free(src);
127         si->nr_replacements++;
128 }
129
130 void merge_base_index(struct index_state *istate)
131 {
132         struct split_index *si = istate->split_index;
133         unsigned int i;
134
135         mark_base_index_entries(si->base);
136
137         si->saved_cache     = istate->cache;
138         si->saved_cache_nr  = istate->cache_nr;
139         istate->cache_nr    = si->base->cache_nr;
140         istate->cache       = NULL;
141         istate->cache_alloc = 0;
142         ALLOC_GROW(istate->cache, istate->cache_nr, istate->cache_alloc);
143         COPY_ARRAY(istate->cache, si->base->cache, istate->cache_nr);
144
145         si->nr_deletions = 0;
146         si->nr_replacements = 0;
147         ewah_each_bit(si->replace_bitmap, replace_entry, istate);
148         ewah_each_bit(si->delete_bitmap, mark_entry_for_delete, istate);
149         if (si->nr_deletions)
150                 remove_marked_cache_entries(istate);
151
152         for (i = si->nr_replacements; i < si->saved_cache_nr; i++) {
153                 if (!ce_namelen(si->saved_cache[i]))
154                         die("corrupt link extension, entry %d should "
155                             "have non-zero length name", i);
156                 add_index_entry(istate, si->saved_cache[i],
157                                 ADD_CACHE_OK_TO_ADD |
158                                 ADD_CACHE_KEEP_CACHE_TREE |
159                                 /*
160                                  * we may have to replay what
161                                  * merge-recursive.c:update_stages()
162                                  * does, which has this flag on
163                                  */
164                                 ADD_CACHE_SKIP_DFCHECK);
165                 si->saved_cache[i] = NULL;
166         }
167
168         ewah_free(si->delete_bitmap);
169         ewah_free(si->replace_bitmap);
170         FREE_AND_NULL(si->saved_cache);
171         si->delete_bitmap  = NULL;
172         si->replace_bitmap = NULL;
173         si->saved_cache_nr = 0;
174 }
175
176 void prepare_to_write_split_index(struct index_state *istate)
177 {
178         struct split_index *si = init_split_index(istate);
179         struct cache_entry **entries = NULL, *ce;
180         int i, nr_entries = 0, nr_alloc = 0;
181
182         si->delete_bitmap = ewah_new();
183         si->replace_bitmap = ewah_new();
184
185         if (si->base) {
186                 /* Go through istate->cache[] and mark CE_MATCHED to
187                  * entry with positive index. We'll go through
188                  * base->cache[] later to delete all entries in base
189                  * that are not marked with either CE_MATCHED or
190                  * CE_UPDATE_IN_BASE. If istate->cache[i] is a
191                  * duplicate, deduplicate it.
192                  */
193                 for (i = 0; i < istate->cache_nr; i++) {
194                         struct cache_entry *base;
195                         /* namelen is checked separately */
196                         const unsigned int ondisk_flags =
197                                 CE_STAGEMASK | CE_VALID | CE_EXTENDED_FLAGS;
198                         unsigned int ce_flags, base_flags, ret;
199                         ce = istate->cache[i];
200                         if (!ce->index)
201                                 continue;
202                         if (ce->index > si->base->cache_nr) {
203                                 ce->index = 0;
204                                 continue;
205                         }
206                         ce->ce_flags |= CE_MATCHED; /* or "shared" */
207                         base = si->base->cache[ce->index - 1];
208                         if (ce == base)
209                                 continue;
210                         if (ce->ce_namelen != base->ce_namelen ||
211                             strcmp(ce->name, base->name)) {
212                                 ce->index = 0;
213                                 continue;
214                         }
215                         ce_flags = ce->ce_flags;
216                         base_flags = base->ce_flags;
217                         /* only on-disk flags matter */
218                         ce->ce_flags   &= ondisk_flags;
219                         base->ce_flags &= ondisk_flags;
220                         ret = memcmp(&ce->ce_stat_data, &base->ce_stat_data,
221                                      offsetof(struct cache_entry, name) -
222                                      offsetof(struct cache_entry, ce_stat_data));
223                         ce->ce_flags = ce_flags;
224                         base->ce_flags = base_flags;
225                         if (ret)
226                                 ce->ce_flags |= CE_UPDATE_IN_BASE;
227                         free(base);
228                         si->base->cache[ce->index - 1] = ce;
229                 }
230                 for (i = 0; i < si->base->cache_nr; i++) {
231                         ce = si->base->cache[i];
232                         if ((ce->ce_flags & CE_REMOVE) ||
233                             !(ce->ce_flags & CE_MATCHED))
234                                 ewah_set(si->delete_bitmap, i);
235                         else if (ce->ce_flags & CE_UPDATE_IN_BASE) {
236                                 ewah_set(si->replace_bitmap, i);
237                                 ce->ce_flags |= CE_STRIP_NAME;
238                                 ALLOC_GROW(entries, nr_entries+1, nr_alloc);
239                                 entries[nr_entries++] = ce;
240                         }
241                 }
242         }
243
244         for (i = 0; i < istate->cache_nr; i++) {
245                 ce = istate->cache[i];
246                 if ((!si->base || !ce->index) && !(ce->ce_flags & CE_REMOVE)) {
247                         assert(!(ce->ce_flags & CE_STRIP_NAME));
248                         ALLOC_GROW(entries, nr_entries+1, nr_alloc);
249                         entries[nr_entries++] = ce;
250                 }
251                 ce->ce_flags &= ~CE_MATCHED;
252         }
253
254         /*
255          * take cache[] out temporarily, put entries[] in its place
256          * for writing
257          */
258         si->saved_cache = istate->cache;
259         si->saved_cache_nr = istate->cache_nr;
260         istate->cache = entries;
261         istate->cache_nr = nr_entries;
262 }
263
264 void finish_writing_split_index(struct index_state *istate)
265 {
266         struct split_index *si = init_split_index(istate);
267
268         ewah_free(si->delete_bitmap);
269         ewah_free(si->replace_bitmap);
270         si->delete_bitmap = NULL;
271         si->replace_bitmap = NULL;
272         free(istate->cache);
273         istate->cache = si->saved_cache;
274         istate->cache_nr = si->saved_cache_nr;
275 }
276
277 void discard_split_index(struct index_state *istate)
278 {
279         struct split_index *si = istate->split_index;
280         if (!si)
281                 return;
282         istate->split_index = NULL;
283         si->refcount--;
284         if (si->refcount)
285                 return;
286         if (si->base) {
287                 discard_index(si->base);
288                 free(si->base);
289         }
290         free(si);
291 }
292
293 void save_or_free_index_entry(struct index_state *istate, struct cache_entry *ce)
294 {
295         if (ce->index &&
296             istate->split_index &&
297             istate->split_index->base &&
298             ce->index <= istate->split_index->base->cache_nr &&
299             ce == istate->split_index->base->cache[ce->index - 1])
300                 ce->ce_flags |= CE_REMOVE;
301         else
302                 free(ce);
303 }
304
305 void replace_index_entry_in_base(struct index_state *istate,
306                                  struct cache_entry *old,
307                                  struct cache_entry *new)
308 {
309         if (old->index &&
310             istate->split_index &&
311             istate->split_index->base &&
312             old->index <= istate->split_index->base->cache_nr) {
313                 new->index = old->index;
314                 if (old != istate->split_index->base->cache[new->index - 1])
315                         free(istate->split_index->base->cache[new->index - 1]);
316                 istate->split_index->base->cache[new->index - 1] = new;
317         }
318 }
319
320 void add_split_index(struct index_state *istate)
321 {
322         if (!istate->split_index) {
323                 init_split_index(istate);
324                 istate->cache_changed |= SPLIT_INDEX_ORDERED;
325         }
326 }
327
328 void remove_split_index(struct index_state *istate)
329 {
330         if (istate->split_index) {
331                 /*
332                  * can't discard_split_index(&the_index); because that
333                  * will destroy split_index->base->cache[], which may
334                  * be shared with the_index.cache[]. So yeah we're
335                  * leaking a bit here.
336                  */
337                 istate->split_index = NULL;
338                 istate->cache_changed |= SOMETHING_CHANGED;
339         }
340 }