3 #include "split-index.h"
6 struct split_index
*init_split_index(struct index_state
*istate
)
8 if (!istate
->split_index
) {
9 if (istate
->sparse_index
)
10 die(_("cannot use split index with a sparse index"));
12 CALLOC_ARRAY(istate
->split_index
, 1);
13 istate
->split_index
->refcount
= 1;
15 return istate
->split_index
;
18 int read_link_extension(struct index_state
*istate
,
19 const void *data_
, unsigned long sz
)
21 const unsigned char *data
= data_
;
22 struct split_index
*si
;
25 if (sz
< the_hash_algo
->rawsz
)
26 return error("corrupt link extension (too short)");
27 si
= init_split_index(istate
);
28 oidread(&si
->base_oid
, data
);
29 data
+= the_hash_algo
->rawsz
;
30 sz
-= the_hash_algo
->rawsz
;
33 si
->delete_bitmap
= ewah_new();
34 ret
= ewah_read_mmap(si
->delete_bitmap
, data
, sz
);
36 return error("corrupt delete bitmap in link extension");
39 si
->replace_bitmap
= ewah_new();
40 ret
= ewah_read_mmap(si
->replace_bitmap
, data
, sz
);
42 return error("corrupt replace bitmap in link extension");
44 return error("garbage at the end of link extension");
48 int write_link_extension(struct strbuf
*sb
,
49 struct index_state
*istate
)
51 struct split_index
*si
= istate
->split_index
;
52 strbuf_add(sb
, si
->base_oid
.hash
, the_hash_algo
->rawsz
);
53 if (!si
->delete_bitmap
&& !si
->replace_bitmap
)
55 ewah_serialize_strbuf(si
->delete_bitmap
, sb
);
56 ewah_serialize_strbuf(si
->replace_bitmap
, sb
);
60 static void mark_base_index_entries(struct index_state
*base
)
64 * To keep track of the shared entries between
65 * istate->base->cache[] and istate->cache[], base entry
66 * position is stored in each base entry. All positions start
67 * from 1 instead of 0, which is reserved to say "this is a new
70 for (i
= 0; i
< base
->cache_nr
; i
++)
71 base
->cache
[i
]->index
= i
+ 1;
74 void move_cache_to_base_index(struct index_state
*istate
)
76 struct split_index
*si
= istate
->split_index
;
80 * If there was a previous base index, then transfer ownership of allocated
81 * entries to the parent index.
84 si
->base
->ce_mem_pool
) {
86 if (!istate
->ce_mem_pool
) {
87 istate
->ce_mem_pool
= xmalloc(sizeof(struct mem_pool
));
88 mem_pool_init(istate
->ce_mem_pool
, 0);
91 mem_pool_combine(istate
->ce_mem_pool
, istate
->split_index
->base
->ce_mem_pool
);
94 ALLOC_ARRAY(si
->base
, 1);
95 index_state_init(si
->base
, istate
->repo
);
96 si
->base
->version
= istate
->version
;
97 /* zero timestamp disables racy test in ce_write_index() */
98 si
->base
->timestamp
= istate
->timestamp
;
99 ALLOC_GROW(si
->base
->cache
, istate
->cache_nr
, si
->base
->cache_alloc
);
100 si
->base
->cache_nr
= istate
->cache_nr
;
103 * The mem_pool needs to move with the allocated entries.
105 si
->base
->ce_mem_pool
= istate
->ce_mem_pool
;
106 istate
->ce_mem_pool
= NULL
;
108 COPY_ARRAY(si
->base
->cache
, istate
->cache
, istate
->cache_nr
);
109 mark_base_index_entries(si
->base
);
110 for (i
= 0; i
< si
->base
->cache_nr
; i
++)
111 si
->base
->cache
[i
]->ce_flags
&= ~CE_UPDATE_IN_BASE
;
114 static void mark_entry_for_delete(size_t pos
, void *data
)
116 struct index_state
*istate
= data
;
117 if (pos
>= istate
->cache_nr
)
118 die("position for delete %d exceeds base index size %d",
119 (int)pos
, istate
->cache_nr
);
120 istate
->cache
[pos
]->ce_flags
|= CE_REMOVE
;
121 istate
->split_index
->nr_deletions
++;
124 static void replace_entry(size_t pos
, void *data
)
126 struct index_state
*istate
= data
;
127 struct split_index
*si
= istate
->split_index
;
128 struct cache_entry
*dst
, *src
;
130 if (pos
>= istate
->cache_nr
)
131 die("position for replacement %d exceeds base index size %d",
132 (int)pos
, istate
->cache_nr
);
133 if (si
->nr_replacements
>= si
->saved_cache_nr
)
134 die("too many replacements (%d vs %d)",
135 si
->nr_replacements
, si
->saved_cache_nr
);
136 dst
= istate
->cache
[pos
];
137 if (dst
->ce_flags
& CE_REMOVE
)
138 die("entry %d is marked as both replaced and deleted",
140 src
= si
->saved_cache
[si
->nr_replacements
];
142 die("corrupt link extension, entry %d should have "
143 "zero length name", (int)pos
);
144 src
->index
= pos
+ 1;
145 src
->ce_flags
|= CE_UPDATE_IN_BASE
;
146 src
->ce_namelen
= dst
->ce_namelen
;
147 copy_cache_entry(dst
, src
);
148 discard_cache_entry(src
);
149 si
->nr_replacements
++;
152 void merge_base_index(struct index_state
*istate
)
154 struct split_index
*si
= istate
->split_index
;
157 mark_base_index_entries(si
->base
);
159 si
->saved_cache
= istate
->cache
;
160 si
->saved_cache_nr
= istate
->cache_nr
;
161 istate
->cache_nr
= si
->base
->cache_nr
;
162 istate
->cache
= NULL
;
163 istate
->cache_alloc
= 0;
164 ALLOC_GROW(istate
->cache
, istate
->cache_nr
, istate
->cache_alloc
);
165 COPY_ARRAY(istate
->cache
, si
->base
->cache
, istate
->cache_nr
);
167 si
->nr_deletions
= 0;
168 si
->nr_replacements
= 0;
169 ewah_each_bit(si
->replace_bitmap
, replace_entry
, istate
);
170 ewah_each_bit(si
->delete_bitmap
, mark_entry_for_delete
, istate
);
171 if (si
->nr_deletions
)
172 remove_marked_cache_entries(istate
, 0);
174 for (i
= si
->nr_replacements
; i
< si
->saved_cache_nr
; i
++) {
175 if (!ce_namelen(si
->saved_cache
[i
]))
176 die("corrupt link extension, entry %d should "
177 "have non-zero length name", i
);
178 add_index_entry(istate
, si
->saved_cache
[i
],
179 ADD_CACHE_OK_TO_ADD
|
180 ADD_CACHE_KEEP_CACHE_TREE
|
182 * we may have to replay what
183 * merge-recursive.c:update_stages()
184 * does, which has this flag on
186 ADD_CACHE_SKIP_DFCHECK
);
187 si
->saved_cache
[i
] = NULL
;
190 ewah_free(si
->delete_bitmap
);
191 ewah_free(si
->replace_bitmap
);
192 FREE_AND_NULL(si
->saved_cache
);
193 si
->delete_bitmap
= NULL
;
194 si
->replace_bitmap
= NULL
;
195 si
->saved_cache_nr
= 0;
199 * Compare most of the fields in two cache entries, i.e. all except the
200 * hashmap_entry and the name.
202 static int compare_ce_content(struct cache_entry
*a
, struct cache_entry
*b
)
204 const unsigned int ondisk_flags
= CE_STAGEMASK
| CE_VALID
|
206 unsigned int ce_flags
= a
->ce_flags
;
207 unsigned int base_flags
= b
->ce_flags
;
210 /* only on-disk flags matter */
211 a
->ce_flags
&= ondisk_flags
;
212 b
->ce_flags
&= ondisk_flags
;
213 ret
= memcmp(&a
->ce_stat_data
, &b
->ce_stat_data
,
214 offsetof(struct cache_entry
, name
) -
215 offsetof(struct cache_entry
, oid
)) ||
216 !oideq(&a
->oid
, &b
->oid
);
217 a
->ce_flags
= ce_flags
;
218 b
->ce_flags
= base_flags
;
223 void prepare_to_write_split_index(struct index_state
*istate
)
225 struct split_index
*si
= init_split_index(istate
);
226 struct cache_entry
**entries
= NULL
, *ce
;
227 int i
, nr_entries
= 0, nr_alloc
= 0;
229 si
->delete_bitmap
= ewah_new();
230 si
->replace_bitmap
= ewah_new();
233 /* Go through istate->cache[] and mark CE_MATCHED to
234 * entry with positive index. We'll go through
235 * base->cache[] later to delete all entries in base
236 * that are not marked with either CE_MATCHED or
237 * CE_UPDATE_IN_BASE. If istate->cache[i] is a
238 * duplicate, deduplicate it.
240 for (i
= 0; i
< istate
->cache_nr
; i
++) {
241 struct cache_entry
*base
;
242 ce
= istate
->cache
[i
];
245 * During simple update index operations this
246 * is a cache entry that is not present in
247 * the shared index. It will be added to the
250 * However, it might also represent a file
251 * that already has a cache entry in the
252 * shared index, but a new index has just
253 * been constructed by unpack_trees(), and
254 * this entry now refers to different content
255 * than what was recorded in the original
256 * index, e.g. during 'read-tree -m HEAD^' or
257 * 'checkout HEAD^'. In this case the
258 * original entry in the shared index will be
259 * marked as deleted, and this entry will be
260 * added to the split index.
264 if (ce
->index
> si
->base
->cache_nr
) {
265 BUG("ce refers to a shared ce at %d, which is beyond the shared index size %d",
266 ce
->index
, si
->base
->cache_nr
);
268 ce
->ce_flags
|= CE_MATCHED
; /* or "shared" */
269 base
= si
->base
->cache
[ce
->index
- 1];
271 /* The entry is present in the shared index. */
272 if (ce
->ce_flags
& CE_UPDATE_IN_BASE
) {
274 * Already marked for inclusion in
275 * the split index, either because
276 * the corresponding file was
277 * modified and the cached stat data
278 * was refreshed, or because there
279 * is already a replacement entry in
281 * Nothing more to do here.
283 } else if (!ce_uptodate(ce
) &&
284 is_racy_timestamp(istate
, ce
)) {
286 * A racily clean cache entry stored
287 * only in the shared index: it must
288 * be added to the split index, so
289 * the subsequent do_write_index()
290 * can smudge its stat data.
292 ce
->ce_flags
|= CE_UPDATE_IN_BASE
;
295 * The entry is only present in the
296 * shared index and it was not
298 * Just leave it there.
303 if (ce
->ce_namelen
!= base
->ce_namelen
||
304 strcmp(ce
->name
, base
->name
)) {
309 * This is the copy of a cache entry that is present
310 * in the shared index, created by unpack_trees()
311 * while it constructed a new index.
313 if (ce
->ce_flags
& CE_UPDATE_IN_BASE
) {
315 * Already marked for inclusion in the split
316 * index, either because the corresponding
317 * file was modified and the cached stat data
318 * was refreshed, or because the original
319 * entry already had a replacement entry in
323 } else if (!ce_uptodate(ce
) &&
324 is_racy_timestamp(istate
, ce
)) {
326 * A copy of a racily clean cache entry from
327 * the shared index. It must be added to
328 * the split index, so the subsequent
329 * do_write_index() can smudge its stat data.
331 ce
->ce_flags
|= CE_UPDATE_IN_BASE
;
334 * Thoroughly compare the cached data to see
335 * whether it should be marked for inclusion
336 * in the split index.
338 * This comparison might be unnecessary, as
339 * code paths modifying the cached data do
340 * set CE_UPDATE_IN_BASE as well.
342 if (compare_ce_content(ce
, base
))
343 ce
->ce_flags
|= CE_UPDATE_IN_BASE
;
345 discard_cache_entry(base
);
346 si
->base
->cache
[ce
->index
- 1] = ce
;
348 for (i
= 0; i
< si
->base
->cache_nr
; i
++) {
349 ce
= si
->base
->cache
[i
];
350 if ((ce
->ce_flags
& CE_REMOVE
) ||
351 !(ce
->ce_flags
& CE_MATCHED
))
352 ewah_set(si
->delete_bitmap
, i
);
353 else if (ce
->ce_flags
& CE_UPDATE_IN_BASE
) {
354 ewah_set(si
->replace_bitmap
, i
);
355 ce
->ce_flags
|= CE_STRIP_NAME
;
356 ALLOC_GROW(entries
, nr_entries
+1, nr_alloc
);
357 entries
[nr_entries
++] = ce
;
359 if (is_null_oid(&ce
->oid
))
360 istate
->drop_cache_tree
= 1;
364 for (i
= 0; i
< istate
->cache_nr
; i
++) {
365 ce
= istate
->cache
[i
];
366 if ((!si
->base
|| !ce
->index
) && !(ce
->ce_flags
& CE_REMOVE
)) {
367 assert(!(ce
->ce_flags
& CE_STRIP_NAME
));
368 ALLOC_GROW(entries
, nr_entries
+1, nr_alloc
);
369 entries
[nr_entries
++] = ce
;
371 ce
->ce_flags
&= ~CE_MATCHED
;
375 * take cache[] out temporarily, put entries[] in its place
378 si
->saved_cache
= istate
->cache
;
379 si
->saved_cache_nr
= istate
->cache_nr
;
380 istate
->cache
= entries
;
381 istate
->cache_nr
= nr_entries
;
384 void finish_writing_split_index(struct index_state
*istate
)
386 struct split_index
*si
= init_split_index(istate
);
388 ewah_free(si
->delete_bitmap
);
389 ewah_free(si
->replace_bitmap
);
390 si
->delete_bitmap
= NULL
;
391 si
->replace_bitmap
= NULL
;
393 istate
->cache
= si
->saved_cache
;
394 istate
->cache_nr
= si
->saved_cache_nr
;
397 void discard_split_index(struct index_state
*istate
)
399 struct split_index
*si
= istate
->split_index
;
402 istate
->split_index
= NULL
;
407 discard_index(si
->base
);
413 void save_or_free_index_entry(struct index_state
*istate
, struct cache_entry
*ce
)
416 istate
->split_index
&&
417 istate
->split_index
->base
&&
418 ce
->index
<= istate
->split_index
->base
->cache_nr
&&
419 ce
== istate
->split_index
->base
->cache
[ce
->index
- 1])
420 ce
->ce_flags
|= CE_REMOVE
;
422 discard_cache_entry(ce
);
425 void replace_index_entry_in_base(struct index_state
*istate
,
426 struct cache_entry
*old_entry
,
427 struct cache_entry
*new_entry
)
429 if (old_entry
->index
&&
430 istate
->split_index
&&
431 istate
->split_index
->base
&&
432 old_entry
->index
<= istate
->split_index
->base
->cache_nr
) {
433 new_entry
->index
= old_entry
->index
;
434 if (old_entry
!= istate
->split_index
->base
->cache
[new_entry
->index
- 1])
435 discard_cache_entry(istate
->split_index
->base
->cache
[new_entry
->index
- 1]);
436 istate
->split_index
->base
->cache
[new_entry
->index
- 1] = new_entry
;
440 void add_split_index(struct index_state
*istate
)
442 if (!istate
->split_index
) {
443 init_split_index(istate
);
444 istate
->cache_changed
|= SPLIT_INDEX_ORDERED
;
448 void remove_split_index(struct index_state
*istate
)
450 if (istate
->split_index
) {
451 if (istate
->split_index
->base
) {
453 * When removing the split index, we need to move
454 * ownership of the mem_pool associated with the
455 * base index to the main index. There may be cache entries
456 * allocated from the base's memory pool that are shared with
459 mem_pool_combine(istate
->ce_mem_pool
,
460 istate
->split_index
->base
->ce_mem_pool
);
463 * The split index no longer owns the mem_pool backing
464 * its cache array. As we are discarding this index,
465 * mark the index as having no cache entries, so it
466 * will not attempt to clean up the cache entries or
469 istate
->split_index
->base
->cache_nr
= 0;
473 * We can discard the split index because its
474 * memory pool has been incorporated into the
475 * memory pool associated with the the_index.
477 discard_split_index(istate
);
479 istate
->cache_changed
|= SOMETHING_CHANGED
;