read-cache: mark updated entries for split index
[git/mjg.git] / split-index.c
blobb36c73b3aa95f41d1f3170deeda796bdc34996bc
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);
92 void save_or_free_index_entry(struct index_state *istate, struct cache_entry *ce)
94 if (ce->index &&
95 istate->split_index &&
96 istate->split_index->base &&
97 ce->index <= istate->split_index->base->cache_nr &&
98 ce == istate->split_index->base->cache[ce->index - 1])
99 ce->ce_flags |= CE_REMOVE;
100 else
101 free(ce);
104 void replace_index_entry_in_base(struct index_state *istate,
105 struct cache_entry *old,
106 struct cache_entry *new)
108 if (old->index &&
109 istate->split_index &&
110 istate->split_index->base &&
111 old->index <= istate->split_index->base->cache_nr) {
112 new->index = old->index;
113 if (old != istate->split_index->base->cache[new->index - 1])
114 free(istate->split_index->base->cache[new->index - 1]);
115 istate->split_index->base->cache[new->index - 1] = new;