Merge branch 'bw/realpath-wo-chdir'
[git.git] / split-index.c
blob615f4cac05f7d381b3c31831b6fd9e8a72e1f982
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 * 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 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;
92 static void mark_entry_for_delete(size_t pos, void *data)
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;
102 static void replace_entry(size_t pos, void *data)
104 struct index_state *istate = data;
105 struct split_index *si = istate->split_index;
106 struct cache_entry *dst, *src;
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++;
130 void merge_base_index(struct index_state *istate)
132 struct split_index *si = istate->split_index;
133 unsigned int i;
135 mark_base_index_entries(si->base);
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);
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);
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 |
160 * we may have to replay what
161 * merge-recursive.c:update_stages()
162 * does, which has this flag on
164 ADD_CACHE_SKIP_DFCHECK);
165 si->saved_cache[i] = NULL;
168 ewah_free(si->delete_bitmap);
169 ewah_free(si->replace_bitmap);
170 free(si->saved_cache);
171 si->delete_bitmap = NULL;
172 si->replace_bitmap = NULL;
173 si->saved_cache = NULL;
174 si->saved_cache_nr = 0;
177 void prepare_to_write_split_index(struct index_state *istate)
179 struct split_index *si = init_split_index(istate);
180 struct cache_entry **entries = NULL, *ce;
181 int i, nr_entries = 0, nr_alloc = 0;
183 si->delete_bitmap = ewah_new();
184 si->replace_bitmap = ewah_new();
186 if (si->base) {
187 /* Go through istate->cache[] and mark CE_MATCHED to
188 * entry with positive index. We'll go through
189 * base->cache[] later to delete all entries in base
190 * that are not marked with either CE_MATCHED or
191 * CE_UPDATE_IN_BASE. If istate->cache[i] is a
192 * duplicate, deduplicate it.
194 for (i = 0; i < istate->cache_nr; i++) {
195 struct cache_entry *base;
196 /* namelen is checked separately */
197 const unsigned int ondisk_flags =
198 CE_STAGEMASK | CE_VALID | CE_EXTENDED_FLAGS;
199 unsigned int ce_flags, base_flags, ret;
200 ce = istate->cache[i];
201 if (!ce->index)
202 continue;
203 if (ce->index > si->base->cache_nr) {
204 ce->index = 0;
205 continue;
207 ce->ce_flags |= CE_MATCHED; /* or "shared" */
208 base = si->base->cache[ce->index - 1];
209 if (ce == base)
210 continue;
211 if (ce->ce_namelen != base->ce_namelen ||
212 strcmp(ce->name, base->name)) {
213 ce->index = 0;
214 continue;
216 ce_flags = ce->ce_flags;
217 base_flags = base->ce_flags;
218 /* only on-disk flags matter */
219 ce->ce_flags &= ondisk_flags;
220 base->ce_flags &= ondisk_flags;
221 ret = memcmp(&ce->ce_stat_data, &base->ce_stat_data,
222 offsetof(struct cache_entry, name) -
223 offsetof(struct cache_entry, ce_stat_data));
224 ce->ce_flags = ce_flags;
225 base->ce_flags = base_flags;
226 if (ret)
227 ce->ce_flags |= CE_UPDATE_IN_BASE;
228 free(base);
229 si->base->cache[ce->index - 1] = ce;
231 for (i = 0; i < si->base->cache_nr; i++) {
232 ce = si->base->cache[i];
233 if ((ce->ce_flags & CE_REMOVE) ||
234 !(ce->ce_flags & CE_MATCHED))
235 ewah_set(si->delete_bitmap, i);
236 else if (ce->ce_flags & CE_UPDATE_IN_BASE) {
237 ewah_set(si->replace_bitmap, i);
238 ce->ce_flags |= CE_STRIP_NAME;
239 ALLOC_GROW(entries, nr_entries+1, nr_alloc);
240 entries[nr_entries++] = ce;
245 for (i = 0; i < istate->cache_nr; i++) {
246 ce = istate->cache[i];
247 if ((!si->base || !ce->index) && !(ce->ce_flags & CE_REMOVE)) {
248 assert(!(ce->ce_flags & CE_STRIP_NAME));
249 ALLOC_GROW(entries, nr_entries+1, nr_alloc);
250 entries[nr_entries++] = ce;
252 ce->ce_flags &= ~CE_MATCHED;
256 * take cache[] out temporarily, put entries[] in its place
257 * for writing
259 si->saved_cache = istate->cache;
260 si->saved_cache_nr = istate->cache_nr;
261 istate->cache = entries;
262 istate->cache_nr = nr_entries;
265 void finish_writing_split_index(struct index_state *istate)
267 struct split_index *si = init_split_index(istate);
269 ewah_free(si->delete_bitmap);
270 ewah_free(si->replace_bitmap);
271 si->delete_bitmap = NULL;
272 si->replace_bitmap = NULL;
273 free(istate->cache);
274 istate->cache = si->saved_cache;
275 istate->cache_nr = si->saved_cache_nr;
278 void discard_split_index(struct index_state *istate)
280 struct split_index *si = istate->split_index;
281 if (!si)
282 return;
283 istate->split_index = NULL;
284 si->refcount--;
285 if (si->refcount)
286 return;
287 if (si->base) {
288 discard_index(si->base);
289 free(si->base);
291 free(si);
294 void save_or_free_index_entry(struct index_state *istate, struct cache_entry *ce)
296 if (ce->index &&
297 istate->split_index &&
298 istate->split_index->base &&
299 ce->index <= istate->split_index->base->cache_nr &&
300 ce == istate->split_index->base->cache[ce->index - 1])
301 ce->ce_flags |= CE_REMOVE;
302 else
303 free(ce);
306 void replace_index_entry_in_base(struct index_state *istate,
307 struct cache_entry *old,
308 struct cache_entry *new)
310 if (old->index &&
311 istate->split_index &&
312 istate->split_index->base &&
313 old->index <= istate->split_index->base->cache_nr) {
314 new->index = old->index;
315 if (old != istate->split_index->base->cache[new->index - 1])
316 free(istate->split_index->base->cache[new->index - 1]);
317 istate->split_index->base->cache[new->index - 1] = new;