split-index: strip pathname of on-disk replaced entries
[git/git.git] / split-index.c
CommitLineData
5fc2fc8f
NTND
1#include "cache.h"
2#include "split-index.h"
96a1d8d3 3#include "ewah/ewok.h"
5fc2fc8f
NTND
4
5struct 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
14int 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;
76b07b37
NTND
19 int ret;
20
5fc2fc8f
NTND
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;
76b07b37
NTND
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)
5fc2fc8f
NTND
40 return error("garbage at the end of link extension");
41 return 0;
42}
43
96a1d8d3
NTND
44static int write_strbuf(void *user_data, const void *data, size_t len)
45{
46 struct strbuf *sb = user_data;
47 strbuf_add(sb, data, len);
48 return len;
49}
50
5fc2fc8f
NTND
51int write_link_extension(struct strbuf *sb,
52 struct index_state *istate)
53{
54 struct split_index *si = istate->split_index;
55 strbuf_add(sb, si->base_sha1, 20);
96a1d8d3
NTND
56 if (!si->delete_bitmap && !si->replace_bitmap)
57 return 0;
58 ewah_serialize_to(si->delete_bitmap, write_strbuf, sb);
59 ewah_serialize_to(si->replace_bitmap, write_strbuf, sb);
5fc2fc8f
NTND
60 return 0;
61}
62
63static void mark_base_index_entries(struct index_state *base)
64{
65 int i;
66 /*
67 * To keep track of the shared entries between
68 * istate->base->cache[] and istate->cache[], base entry
69 * position is stored in each base entry. All positions start
70 * from 1 instead of 0, which is resrved to say "this is a new
71 * entry".
72 */
73 for (i = 0; i < base->cache_nr; i++)
74 base->cache[i]->index = i + 1;
75}
76
76b07b37
NTND
77static void mark_entry_for_delete(size_t pos, void *data)
78{
79 struct index_state *istate = data;
80 if (pos >= istate->cache_nr)
81 die("position for delete %d exceeds base index size %d",
82 (int)pos, istate->cache_nr);
83 istate->cache[pos]->ce_flags |= CE_REMOVE;
84 istate->split_index->nr_deletions = 1;
85}
86
87static void replace_entry(size_t pos, void *data)
88{
89 struct index_state *istate = data;
90 struct split_index *si = istate->split_index;
91 struct cache_entry *dst, *src;
b3c96fb1 92
76b07b37
NTND
93 if (pos >= istate->cache_nr)
94 die("position for replacement %d exceeds base index size %d",
95 (int)pos, istate->cache_nr);
96 if (si->nr_replacements >= si->saved_cache_nr)
97 die("too many replacements (%d vs %d)",
98 si->nr_replacements, si->saved_cache_nr);
99 dst = istate->cache[pos];
100 if (dst->ce_flags & CE_REMOVE)
101 die("entry %d is marked as both replaced and deleted",
102 (int)pos);
103 src = si->saved_cache[si->nr_replacements];
b3c96fb1
NTND
104 if (ce_namelen(src))
105 die("corrupt link extension, entry %d should have "
106 "zero length name", (int)pos);
76b07b37
NTND
107 src->index = pos + 1;
108 src->ce_flags |= CE_UPDATE_IN_BASE;
b3c96fb1
NTND
109 src->ce_namelen = dst->ce_namelen;
110 copy_cache_entry(dst, src);
111 free(src);
76b07b37
NTND
112 si->nr_replacements++;
113}
114
5fc2fc8f
NTND
115void merge_base_index(struct index_state *istate)
116{
117 struct split_index *si = istate->split_index;
76b07b37 118 unsigned int i;
5fc2fc8f
NTND
119
120 mark_base_index_entries(si->base);
76b07b37
NTND
121
122 si->saved_cache = istate->cache;
123 si->saved_cache_nr = istate->cache_nr;
124 istate->cache_nr = si->base->cache_nr;
125 istate->cache = NULL;
126 istate->cache_alloc = 0;
5fc2fc8f
NTND
127 ALLOC_GROW(istate->cache, istate->cache_nr, istate->cache_alloc);
128 memcpy(istate->cache, si->base->cache,
129 sizeof(*istate->cache) * istate->cache_nr);
76b07b37
NTND
130
131 si->nr_deletions = 0;
132 si->nr_replacements = 0;
133 ewah_each_bit(si->replace_bitmap, replace_entry, istate);
134 ewah_each_bit(si->delete_bitmap, mark_entry_for_delete, istate);
135 if (si->nr_deletions)
136 remove_marked_cache_entries(istate);
137
138 for (i = si->nr_replacements; i < si->saved_cache_nr; i++) {
b3c96fb1
NTND
139 if (!ce_namelen(si->saved_cache[i]))
140 die("corrupt link extension, entry %d should "
141 "have non-zero length name", i);
76b07b37
NTND
142 add_index_entry(istate, si->saved_cache[i],
143 ADD_CACHE_OK_TO_ADD |
ce7c614b 144 ADD_CACHE_KEEP_CACHE_TREE |
76b07b37
NTND
145 /*
146 * we may have to replay what
147 * merge-recursive.c:update_stages()
148 * does, which has this flag on
149 */
150 ADD_CACHE_SKIP_DFCHECK);
151 si->saved_cache[i] = NULL;
152 }
153
154 ewah_free(si->delete_bitmap);
155 ewah_free(si->replace_bitmap);
156 free(si->saved_cache);
157 si->delete_bitmap = NULL;
158 si->replace_bitmap = NULL;
159 si->saved_cache = NULL;
160 si->saved_cache_nr = 0;
5fc2fc8f
NTND
161}
162
163void prepare_to_write_split_index(struct index_state *istate)
164{
165 struct split_index *si = init_split_index(istate);
96a1d8d3
NTND
166 struct cache_entry **entries = NULL, *ce;
167 int i, nr_entries = 0, nr_alloc = 0;
168
169 si->delete_bitmap = ewah_new();
170 si->replace_bitmap = ewah_new();
171
172 if (si->base) {
173 /* Go through istate->cache[] and mark CE_MATCHED to
174 * entry with positive index. We'll go through
175 * base->cache[] later to delete all entries in base
176 * that are not marked eith either CE_MATCHED or
177 * CE_UPDATE_IN_BASE. If istate->cache[i] is a
178 * duplicate, deduplicate it.
179 */
180 for (i = 0; i < istate->cache_nr; i++) {
181 struct cache_entry *base;
182 /* namelen is checked separately */
183 const unsigned int ondisk_flags =
184 CE_STAGEMASK | CE_VALID | CE_EXTENDED_FLAGS;
185 unsigned int ce_flags, base_flags, ret;
186 ce = istate->cache[i];
187 if (!ce->index)
188 continue;
189 if (ce->index > si->base->cache_nr) {
190 ce->index = 0;
191 continue;
192 }
193 ce->ce_flags |= CE_MATCHED; /* or "shared" */
194 base = si->base->cache[ce->index - 1];
195 if (ce == base)
196 continue;
197 if (ce->ce_namelen != base->ce_namelen ||
198 strcmp(ce->name, base->name)) {
199 ce->index = 0;
200 continue;
201 }
202 ce_flags = ce->ce_flags;
203 base_flags = base->ce_flags;
204 /* only on-disk flags matter */
205 ce->ce_flags &= ondisk_flags;
206 base->ce_flags &= ondisk_flags;
207 ret = memcmp(&ce->ce_stat_data, &base->ce_stat_data,
208 offsetof(struct cache_entry, name) -
209 offsetof(struct cache_entry, ce_stat_data));
210 ce->ce_flags = ce_flags;
211 base->ce_flags = base_flags;
212 if (ret)
213 ce->ce_flags |= CE_UPDATE_IN_BASE;
214 free(base);
215 si->base->cache[ce->index - 1] = ce;
216 }
217 for (i = 0; i < si->base->cache_nr; i++) {
218 ce = si->base->cache[i];
219 if ((ce->ce_flags & CE_REMOVE) ||
220 !(ce->ce_flags & CE_MATCHED))
221 ewah_set(si->delete_bitmap, i);
222 else if (ce->ce_flags & CE_UPDATE_IN_BASE) {
223 ewah_set(si->replace_bitmap, i);
b3c96fb1 224 ce->ce_flags |= CE_STRIP_NAME;
96a1d8d3
NTND
225 ALLOC_GROW(entries, nr_entries+1, nr_alloc);
226 entries[nr_entries++] = ce;
227 }
228 }
229 }
230
231 for (i = 0; i < istate->cache_nr; i++) {
232 ce = istate->cache[i];
233 if ((!si->base || !ce->index) && !(ce->ce_flags & CE_REMOVE)) {
b3c96fb1 234 assert(!(ce->ce_flags & CE_STRIP_NAME));
96a1d8d3
NTND
235 ALLOC_GROW(entries, nr_entries+1, nr_alloc);
236 entries[nr_entries++] = ce;
237 }
238 ce->ce_flags &= ~CE_MATCHED;
239 }
240
241 /*
242 * take cache[] out temporarily, put entries[] in its place
243 * for writing
244 */
245 si->saved_cache = istate->cache;
5fc2fc8f 246 si->saved_cache_nr = istate->cache_nr;
96a1d8d3
NTND
247 istate->cache = entries;
248 istate->cache_nr = nr_entries;
5fc2fc8f
NTND
249}
250
251void finish_writing_split_index(struct index_state *istate)
252{
253 struct split_index *si = init_split_index(istate);
96a1d8d3
NTND
254
255 ewah_free(si->delete_bitmap);
256 ewah_free(si->replace_bitmap);
257 si->delete_bitmap = NULL;
258 si->replace_bitmap = NULL;
259 free(istate->cache);
260 istate->cache = si->saved_cache;
5fc2fc8f
NTND
261 istate->cache_nr = si->saved_cache_nr;
262}
263
264void discard_split_index(struct index_state *istate)
265{
266 struct split_index *si = istate->split_index;
267 if (!si)
268 return;
269 istate->split_index = NULL;
270 si->refcount--;
271 if (si->refcount)
272 return;
273 if (si->base) {
274 discard_index(si->base);
275 free(si->base);
276 }
277 free(si);
278}
045113a5
NTND
279
280void save_or_free_index_entry(struct index_state *istate, struct cache_entry *ce)
281{
282 if (ce->index &&
283 istate->split_index &&
284 istate->split_index->base &&
285 ce->index <= istate->split_index->base->cache_nr &&
286 ce == istate->split_index->base->cache[ce->index - 1])
287 ce->ce_flags |= CE_REMOVE;
288 else
289 free(ce);
290}
078a58e8
NTND
291
292void replace_index_entry_in_base(struct index_state *istate,
293 struct cache_entry *old,
294 struct cache_entry *new)
295{
296 if (old->index &&
297 istate->split_index &&
298 istate->split_index->base &&
299 old->index <= istate->split_index->base->cache_nr) {
300 new->index = old->index;
301 if (old != istate->split_index->base->cache[new->index - 1])
302 free(istate->split_index->base->cache[new->index - 1]);
303 istate->split_index->base->cache[new->index - 1] = new;
304 }
305}