read-cache: split-index mode
[git.git] / split-index.c
blob63b52bb086e336ab14cd070f63bf880a912162f4
1 #include "cache.h"
2 #include "split-index.h"
4 struct split_index *init_split_index(struct index_state *istate)
6 if (!istate->split_index) {
7 istate->split_index = xcalloc(1, sizeof(*istate->split_index));
8 istate->split_index->refcount = 1;
10 return istate->split_index;
13 int read_link_extension(struct index_state *istate,
14 const void *data_, unsigned long sz)
16 const unsigned char *data = data_;
17 struct split_index *si;
18 if (sz < 20)
19 return error("corrupt link extension (too short)");
20 si = init_split_index(istate);
21 hashcpy(si->base_sha1, data);
22 data += 20;
23 sz -= 20;
24 if (sz)
25 return error("garbage at the end of link extension");
26 return 0;
29 int write_link_extension(struct strbuf *sb,
30 struct index_state *istate)
32 struct split_index *si = istate->split_index;
33 strbuf_add(sb, si->base_sha1, 20);
34 return 0;
37 static void mark_base_index_entries(struct index_state *base)
39 int i;
41 * To keep track of the shared entries between
42 * istate->base->cache[] and istate->cache[], base entry
43 * position is stored in each base entry. All positions start
44 * from 1 instead of 0, which is resrved to say "this is a new
45 * entry".
47 for (i = 0; i < base->cache_nr; i++)
48 base->cache[i]->index = i + 1;
51 void merge_base_index(struct index_state *istate)
53 struct split_index *si = istate->split_index;
55 mark_base_index_entries(si->base);
56 istate->cache_nr = si->base->cache_nr;
57 ALLOC_GROW(istate->cache, istate->cache_nr, istate->cache_alloc);
58 memcpy(istate->cache, si->base->cache,
59 sizeof(*istate->cache) * istate->cache_nr);
62 void prepare_to_write_split_index(struct index_state *istate)
64 struct split_index *si = init_split_index(istate);
65 /* take cache[] out temporarily */
66 si->saved_cache_nr = istate->cache_nr;
67 istate->cache_nr = 0;
70 void finish_writing_split_index(struct index_state *istate)
72 struct split_index *si = init_split_index(istate);
73 istate->cache_nr = si->saved_cache_nr;
76 void discard_split_index(struct index_state *istate)
78 struct split_index *si = istate->split_index;
79 if (!si)
80 return;
81 istate->split_index = NULL;
82 si->refcount--;
83 if (si->refcount)
84 return;
85 if (si->base) {
86 discard_index(si->base);
87 free(si->base);
89 free(si);