2 #include "split-index.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
;
22 return error("corrupt link extension (too short)");
23 si
= init_split_index(istate
);
24 hashcpy(si
->base_sha1
, data
);
29 si
->delete_bitmap
= ewah_new();
30 ret
= ewah_read_mmap(si
->delete_bitmap
, data
, sz
);
32 return error("corrupt delete bitmap in link extension");
35 si
->replace_bitmap
= ewah_new();
36 ret
= ewah_read_mmap(si
->replace_bitmap
, data
, sz
);
38 return error("corrupt replace bitmap in link extension");
40 return error("garbage at the end of link extension");
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
)
51 ewah_serialize_strbuf(si
->delete_bitmap
, sb
);
52 ewah_serialize_strbuf(si
->replace_bitmap
, sb
);
56 static void mark_base_index_entries(struct index_state
*base
)
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
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
;
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",
118 src
= si
->saved_cache
[si
->nr_replacements
];
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
);
127 si
->nr_replacements
++;
130 void merge_base_index(struct index_state
*istate
)
132 struct split_index
*si
= istate
->split_index
;
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_AND_NULL(si
->saved_cache
);
171 si
->delete_bitmap
= NULL
;
172 si
->replace_bitmap
= NULL
;
173 si
->saved_cache_nr
= 0;
176 void prepare_to_write_split_index(struct index_state
*istate
)
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;
182 si
->delete_bitmap
= ewah_new();
183 si
->replace_bitmap
= ewah_new();
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.
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
];
202 if (ce
->index
> si
->base
->cache_nr
) {
206 ce
->ce_flags
|= CE_MATCHED
; /* or "shared" */
207 base
= si
->base
->cache
[ce
->index
- 1];
210 if (ce
->ce_namelen
!= base
->ce_namelen
||
211 strcmp(ce
->name
, base
->name
)) {
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
;
226 ce
->ce_flags
|= CE_UPDATE_IN_BASE
;
228 si
->base
->cache
[ce
->index
- 1] = ce
;
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
;
241 if (is_null_oid(&ce
->oid
))
242 istate
->drop_cache_tree
= 1;
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
;
253 ce
->ce_flags
&= ~CE_MATCHED
;
257 * take cache[] out temporarily, put entries[] in its place
260 si
->saved_cache
= istate
->cache
;
261 si
->saved_cache_nr
= istate
->cache_nr
;
262 istate
->cache
= entries
;
263 istate
->cache_nr
= nr_entries
;
266 void finish_writing_split_index(struct index_state
*istate
)
268 struct split_index
*si
= init_split_index(istate
);
270 ewah_free(si
->delete_bitmap
);
271 ewah_free(si
->replace_bitmap
);
272 si
->delete_bitmap
= NULL
;
273 si
->replace_bitmap
= NULL
;
275 istate
->cache
= si
->saved_cache
;
276 istate
->cache_nr
= si
->saved_cache_nr
;
279 void discard_split_index(struct index_state
*istate
)
281 struct split_index
*si
= istate
->split_index
;
284 istate
->split_index
= NULL
;
289 discard_index(si
->base
);
295 void save_or_free_index_entry(struct index_state
*istate
, struct cache_entry
*ce
)
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
;
307 void replace_index_entry_in_base(struct index_state
*istate
,
308 struct cache_entry
*old
,
309 struct cache_entry
*new)
312 istate
->split_index
&&
313 istate
->split_index
->base
&&
314 old
->index
<= istate
->split_index
->base
->cache_nr
) {
315 new->index
= old
->index
;
316 if (old
!= istate
->split_index
->base
->cache
[new->index
- 1])
317 free(istate
->split_index
->base
->cache
[new->index
- 1]);
318 istate
->split_index
->base
->cache
[new->index
- 1] = new;
322 void add_split_index(struct index_state
*istate
)
324 if (!istate
->split_index
) {
325 init_split_index(istate
);
326 istate
->cache_changed
|= SPLIT_INDEX_ORDERED
;
330 void remove_split_index(struct index_state
*istate
)
332 if (istate
->split_index
) {
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.
339 istate
->split_index
= NULL
;
340 istate
->cache_changed
|= SOMETHING_CHANGED
;