doc: describe git svn init --ignore-refs
[git.git] / split-index.c
blob49bd197f715ae783d7fdbe8e1d638a1bcac0073c
1 #include "cache.h"
2 #include "split-index.h"
3 #include "ewah/ewok.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;
19 int ret;
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;
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)
50 return 0;
51 ewah_serialize_strbuf(si->delete_bitmap, sb);
52 ewah_serialize_strbuf(si->replace_bitmap, sb);
53 return 0;
56 static void mark_base_index_entries(struct index_state *base)
58 int i;
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".
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;
73 int i;
76 * If "si" is shared with another index_state (e.g. by
77 * unpack-trees code), we will need to duplicate split_index
78 * struct. It's not happening now though, luckily.
80 assert(si->refcount <= 1);
82 unshare_split_index(istate, 0);
83 if (si->base) {
84 discard_index(si->base);
85 free(si->base);
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 COPY_ARRAY(si->base->cache, istate->cache, istate->cache_nr);
94 mark_base_index_entries(si->base);
95 for (i = 0; i < si->base->cache_nr; i++)
96 si->base->cache[i]->ce_flags &= ~CE_UPDATE_IN_BASE;
99 static void mark_entry_for_delete(size_t pos, void *data)
101 struct index_state *istate = data;
102 if (pos >= istate->cache_nr)
103 die("position for delete %d exceeds base index size %d",
104 (int)pos, istate->cache_nr);
105 istate->cache[pos]->ce_flags |= CE_REMOVE;
106 istate->split_index->nr_deletions = 1;
109 static void replace_entry(size_t pos, void *data)
111 struct index_state *istate = data;
112 struct split_index *si = istate->split_index;
113 struct cache_entry *dst, *src;
115 if (pos >= istate->cache_nr)
116 die("position for replacement %d exceeds base index size %d",
117 (int)pos, istate->cache_nr);
118 if (si->nr_replacements >= si->saved_cache_nr)
119 die("too many replacements (%d vs %d)",
120 si->nr_replacements, si->saved_cache_nr);
121 dst = istate->cache[pos];
122 if (dst->ce_flags & CE_REMOVE)
123 die("entry %d is marked as both replaced and deleted",
124 (int)pos);
125 src = si->saved_cache[si->nr_replacements];
126 if (ce_namelen(src))
127 die("corrupt link extension, entry %d should have "
128 "zero length name", (int)pos);
129 src->index = pos + 1;
130 src->ce_flags |= CE_UPDATE_IN_BASE;
131 src->ce_namelen = dst->ce_namelen;
132 copy_cache_entry(dst, src);
133 free(src);
134 si->nr_replacements++;
137 void merge_base_index(struct index_state *istate)
139 struct split_index *si = istate->split_index;
140 unsigned int i;
142 mark_base_index_entries(si->base);
144 si->saved_cache = istate->cache;
145 si->saved_cache_nr = istate->cache_nr;
146 istate->cache_nr = si->base->cache_nr;
147 istate->cache = NULL;
148 istate->cache_alloc = 0;
149 ALLOC_GROW(istate->cache, istate->cache_nr, istate->cache_alloc);
150 COPY_ARRAY(istate->cache, si->base->cache, istate->cache_nr);
152 si->nr_deletions = 0;
153 si->nr_replacements = 0;
154 ewah_each_bit(si->replace_bitmap, replace_entry, istate);
155 ewah_each_bit(si->delete_bitmap, mark_entry_for_delete, istate);
156 if (si->nr_deletions)
157 remove_marked_cache_entries(istate);
159 for (i = si->nr_replacements; i < si->saved_cache_nr; i++) {
160 if (!ce_namelen(si->saved_cache[i]))
161 die("corrupt link extension, entry %d should "
162 "have non-zero length name", i);
163 add_index_entry(istate, si->saved_cache[i],
164 ADD_CACHE_OK_TO_ADD |
165 ADD_CACHE_KEEP_CACHE_TREE |
167 * we may have to replay what
168 * merge-recursive.c:update_stages()
169 * does, which has this flag on
171 ADD_CACHE_SKIP_DFCHECK);
172 si->saved_cache[i] = NULL;
175 ewah_free(si->delete_bitmap);
176 ewah_free(si->replace_bitmap);
177 free(si->saved_cache);
178 si->delete_bitmap = NULL;
179 si->replace_bitmap = NULL;
180 si->saved_cache = NULL;
181 si->saved_cache_nr = 0;
184 void prepare_to_write_split_index(struct index_state *istate)
186 struct split_index *si = init_split_index(istate);
187 struct cache_entry **entries = NULL, *ce;
188 int i, nr_entries = 0, nr_alloc = 0;
190 si->delete_bitmap = ewah_new();
191 si->replace_bitmap = ewah_new();
193 if (si->base) {
194 /* Go through istate->cache[] and mark CE_MATCHED to
195 * entry with positive index. We'll go through
196 * base->cache[] later to delete all entries in base
197 * that are not marked with either CE_MATCHED or
198 * CE_UPDATE_IN_BASE. If istate->cache[i] is a
199 * duplicate, deduplicate it.
201 for (i = 0; i < istate->cache_nr; i++) {
202 struct cache_entry *base;
203 /* namelen is checked separately */
204 const unsigned int ondisk_flags =
205 CE_STAGEMASK | CE_VALID | CE_EXTENDED_FLAGS;
206 unsigned int ce_flags, base_flags, ret;
207 ce = istate->cache[i];
208 if (!ce->index)
209 continue;
210 if (ce->index > si->base->cache_nr) {
211 ce->index = 0;
212 continue;
214 ce->ce_flags |= CE_MATCHED; /* or "shared" */
215 base = si->base->cache[ce->index - 1];
216 if (ce == base)
217 continue;
218 if (ce->ce_namelen != base->ce_namelen ||
219 strcmp(ce->name, base->name)) {
220 ce->index = 0;
221 continue;
223 ce_flags = ce->ce_flags;
224 base_flags = base->ce_flags;
225 /* only on-disk flags matter */
226 ce->ce_flags &= ondisk_flags;
227 base->ce_flags &= ondisk_flags;
228 ret = memcmp(&ce->ce_stat_data, &base->ce_stat_data,
229 offsetof(struct cache_entry, name) -
230 offsetof(struct cache_entry, ce_stat_data));
231 ce->ce_flags = ce_flags;
232 base->ce_flags = base_flags;
233 if (ret)
234 ce->ce_flags |= CE_UPDATE_IN_BASE;
235 free(base);
236 si->base->cache[ce->index - 1] = ce;
238 for (i = 0; i < si->base->cache_nr; i++) {
239 ce = si->base->cache[i];
240 if ((ce->ce_flags & CE_REMOVE) ||
241 !(ce->ce_flags & CE_MATCHED))
242 ewah_set(si->delete_bitmap, i);
243 else if (ce->ce_flags & CE_UPDATE_IN_BASE) {
244 ewah_set(si->replace_bitmap, i);
245 ce->ce_flags |= CE_STRIP_NAME;
246 ALLOC_GROW(entries, nr_entries+1, nr_alloc);
247 entries[nr_entries++] = ce;
252 for (i = 0; i < istate->cache_nr; i++) {
253 ce = istate->cache[i];
254 if ((!si->base || !ce->index) && !(ce->ce_flags & CE_REMOVE)) {
255 assert(!(ce->ce_flags & CE_STRIP_NAME));
256 ALLOC_GROW(entries, nr_entries+1, nr_alloc);
257 entries[nr_entries++] = ce;
259 ce->ce_flags &= ~CE_MATCHED;
263 * take cache[] out temporarily, put entries[] in its place
264 * for writing
266 si->saved_cache = istate->cache;
267 si->saved_cache_nr = istate->cache_nr;
268 istate->cache = entries;
269 istate->cache_nr = nr_entries;
272 void finish_writing_split_index(struct index_state *istate)
274 struct split_index *si = init_split_index(istate);
276 ewah_free(si->delete_bitmap);
277 ewah_free(si->replace_bitmap);
278 si->delete_bitmap = NULL;
279 si->replace_bitmap = NULL;
280 free(istate->cache);
281 istate->cache = si->saved_cache;
282 istate->cache_nr = si->saved_cache_nr;
285 void unshare_split_index(struct index_state *istate, int discard)
287 struct split_index *si = istate->split_index;
288 int i;
290 if (!si || !si->base)
291 return;
293 for (i = 0; i < istate->cache_nr; i++) {
294 struct cache_entry *ce = istate->cache[i];
295 struct cache_entry *new = NULL;
297 if (!ce->index ||
298 ce->index > si->base->cache_nr ||
299 ce != si->base->cache[ce->index - 1])
300 continue;
302 if (!discard) {
303 int len = ce_namelen(ce);
304 new = xcalloc(1, cache_entry_size(len));
305 copy_cache_entry(new, ce);
306 memcpy(new->name, ce->name, len);
307 new->index = 0;
309 istate->cache[i] = new;
314 void discard_split_index(struct index_state *istate)
316 struct split_index *si = istate->split_index;
317 if (!si)
318 return;
319 unshare_split_index(istate, 0);
320 istate->split_index = NULL;
321 si->refcount--;
322 if (si->refcount)
323 return;
324 if (si->base) {
325 discard_index(si->base);
326 free(si->base);
328 free(si);
331 void save_or_free_index_entry(struct index_state *istate, struct cache_entry *ce)
333 if (ce->index &&
334 istate->split_index &&
335 istate->split_index->base &&
336 ce->index <= istate->split_index->base->cache_nr &&
337 ce == istate->split_index->base->cache[ce->index - 1])
338 ce->ce_flags |= CE_REMOVE;
339 else
340 free(ce);
343 void replace_index_entry_in_base(struct index_state *istate,
344 struct cache_entry *old,
345 struct cache_entry *new)
347 if (old->index &&
348 istate->split_index &&
349 istate->split_index->base &&
350 old->index <= istate->split_index->base->cache_nr) {
351 new->index = old->index;
352 if (old != istate->split_index->base->cache[new->index - 1])
353 free(istate->split_index->base->cache[new->index - 1]);
354 istate->split_index->base->cache[new->index - 1] = new;
358 void add_split_index(struct index_state *istate)
360 if (!istate->split_index) {
361 init_split_index(istate);
362 istate->cache_changed |= SPLIT_INDEX_ORDERED;
366 void remove_split_index(struct index_state *istate)
368 if (!istate->split_index)
369 return;
370 discard_split_index(istate);
371 istate->cache_changed |= SOMETHING_CHANGED;