l10n: fr.po v2.18.0 round 3
[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 < the_hash_algo->rawsz)
22                 return error("corrupt link extension (too short)");
23         si = init_split_index(istate);
24         hashcpy(si->base_oid.hash, data);
25         data += the_hash_algo->rawsz;
26         sz -= the_hash_algo->rawsz;
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_oid.hash, the_hash_algo->rawsz);
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                         if (is_null_oid(&ce->oid))
242                                 istate->drop_cache_tree = 1;
243                 }
244         }
245
246         for (i = 0; i < istate->cache_nr; i++) {
247                 ce = istate->cache[i];
248                 if ((!si->base || !ce->index) && !(ce->ce_flags & CE_REMOVE)) {
249                         assert(!(ce->ce_flags & CE_STRIP_NAME));
250                         ALLOC_GROW(entries, nr_entries+1, nr_alloc);
251                         entries[nr_entries++] = ce;
252                 }
253                 ce->ce_flags &= ~CE_MATCHED;
254         }
255
256         /*
257          * take cache[] out temporarily, put entries[] in its place
258          * for writing
259          */
260         si->saved_cache = istate->cache;
261         si->saved_cache_nr = istate->cache_nr;
262         istate->cache = entries;
263         istate->cache_nr = nr_entries;
264 }
265
266 void finish_writing_split_index(struct index_state *istate)
267 {
268         struct split_index *si = init_split_index(istate);
269
270         ewah_free(si->delete_bitmap);
271         ewah_free(si->replace_bitmap);
272         si->delete_bitmap = NULL;
273         si->replace_bitmap = NULL;
274         free(istate->cache);
275         istate->cache = si->saved_cache;
276         istate->cache_nr = si->saved_cache_nr;
277 }
278
279 void discard_split_index(struct index_state *istate)
280 {
281         struct split_index *si = istate->split_index;
282         if (!si)
283                 return;
284         istate->split_index = NULL;
285         si->refcount--;
286         if (si->refcount)
287                 return;
288         if (si->base) {
289                 discard_index(si->base);
290                 free(si->base);
291         }
292         free(si);
293 }
294
295 void save_or_free_index_entry(struct index_state *istate, struct cache_entry *ce)
296 {
297         if (ce->index &&
298             istate->split_index &&
299             istate->split_index->base &&
300             ce->index <= istate->split_index->base->cache_nr &&
301             ce == istate->split_index->base->cache[ce->index - 1])
302                 ce->ce_flags |= CE_REMOVE;
303         else
304                 free(ce);
305 }
306
307 void replace_index_entry_in_base(struct index_state *istate,
308                                  struct cache_entry *old_entry,
309                                  struct cache_entry *new_entry)
310 {
311         if (old_entry->index &&
312             istate->split_index &&
313             istate->split_index->base &&
314             old_entry->index <= istate->split_index->base->cache_nr) {
315                 new_entry->index = old_entry->index;
316                 if (old_entry != istate->split_index->base->cache[new_entry->index - 1])
317                         free(istate->split_index->base->cache[new_entry->index - 1]);
318                 istate->split_index->base->cache[new_entry->index - 1] = new_entry;
319         }
320 }
321
322 void add_split_index(struct index_state *istate)
323 {
324         if (!istate->split_index) {
325                 init_split_index(istate);
326                 istate->cache_changed |= SPLIT_INDEX_ORDERED;
327         }
328 }
329
330 void remove_split_index(struct index_state *istate)
331 {
332         if (istate->split_index) {
333                 /*
334                  * can't discard_split_index(&the_index); because that
335                  * will destroy split_index->base->cache[], which may
336                  * be shared with the_index.cache[]. So yeah we're
337                  * leaking a bit here.
338                  */
339                 istate->split_index = NULL;
340                 istate->cache_changed |= SOMETHING_CHANGED;
341         }
342 }