Merge branch 'dl/complete-cherry-pick-revert-skip'
[git/git.git] / pack-objects.c
CommitLineData
2834bc27
VM
1#include "cache.h"
2#include "object.h"
3#include "pack.h"
4#include "pack-objects.h"
43fa44fa
NTND
5#include "packfile.h"
6#include "config.h"
2834bc27
VM
7
8static uint32_t locate_object_entry_hash(struct packing_data *pdata,
5e7ac680 9 const struct object_id *oid,
2834bc27
VM
10 int *found)
11{
039dc71a 12 uint32_t i, mask = (pdata->index_size - 1);
2834bc27 13
d40abc8e 14 i = oidhash(oid) & mask;
2834bc27
VM
15
16 while (pdata->index[i] > 0) {
17 uint32_t pos = pdata->index[i] - 1;
18
5e7ac680 19 if (oideq(oid, &pdata->objects[pos].idx.oid)) {
2834bc27
VM
20 *found = 1;
21 return i;
22 }
23
24 i = (i + 1) & mask;
25 }
26
27 *found = 0;
28 return i;
29}
30
31static inline uint32_t closest_pow2(uint32_t v)
32{
33 v = v - 1;
34 v |= v >> 1;
35 v |= v >> 2;
36 v |= v >> 4;
37 v |= v >> 8;
38 v |= v >> 16;
39 return v + 1;
40}
41
42static void rehash_objects(struct packing_data *pdata)
43{
44 uint32_t i;
45 struct object_entry *entry;
46
47 pdata->index_size = closest_pow2(pdata->nr_objects * 3);
48 if (pdata->index_size < 1024)
49 pdata->index_size = 1024;
50
fb799474
RS
51 free(pdata->index);
52 pdata->index = xcalloc(pdata->index_size, sizeof(*pdata->index));
2834bc27
VM
53
54 entry = pdata->objects;
55
56 for (i = 0; i < pdata->nr_objects; i++) {
57 int found;
e6a492b7 58 uint32_t ix = locate_object_entry_hash(pdata,
5e7ac680 59 &entry->idx.oid,
e6a492b7 60 &found);
2834bc27
VM
61
62 if (found)
033abf97 63 BUG("Duplicate object in hash");
2834bc27
VM
64
65 pdata->index[ix] = i + 1;
66 entry++;
67 }
68}
69
70struct object_entry *packlist_find(struct packing_data *pdata,
3a37876b 71 const struct object_id *oid)
2834bc27
VM
72{
73 uint32_t i;
74 int found;
75
76 if (!pdata->index_size)
77 return NULL;
78
5e7ac680 79 i = locate_object_entry_hash(pdata, oid, &found);
2834bc27 80
2834bc27
VM
81 if (!found)
82 return NULL;
83
84 return &pdata->objects[pdata->index[i] - 1];
85}
86
43fa44fa
NTND
87static void prepare_in_pack_by_idx(struct packing_data *pdata)
88{
89 struct packed_git **mapping, *p;
90 int cnt = 0, nr = 1U << OE_IN_PACK_BITS;
91
92 ALLOC_ARRAY(mapping, nr);
93 /*
94 * oe_in_pack() on an all-zero'd object_entry
95 * (i.e. in_pack_idx also zero) should return NULL.
96 */
97 mapping[cnt++] = NULL;
7c141127 98 for (p = get_all_packs(pdata->repo); p; p = p->next, cnt++) {
43fa44fa
NTND
99 if (cnt == nr) {
100 free(mapping);
101 return;
102 }
103 p->index = cnt;
104 mapping[cnt] = p;
105 }
106 pdata->in_pack_by_idx = mapping;
107}
108
109/*
110 * A new pack appears after prepare_in_pack_by_idx() has been
111 * run. This is likely a race.
112 *
113 * We could map this new pack to in_pack_by_idx[] array, but then we
114 * have to deal with full array anyway. And since it's hard to test
115 * this fall back code, just stay simple and fall back to using
116 * in_pack[] array.
117 */
c409d108 118void oe_map_new_pack(struct packing_data *pack)
43fa44fa
NTND
119{
120 uint32_t i;
121
122 REALLOC_ARRAY(pack->in_pack, pack->nr_alloc);
123
124 for (i = 0; i < pack->nr_objects; i++)
125 pack->in_pack[i] = oe_in_pack(pack, pack->objects + i);
126
127 FREE_AND_NULL(pack->in_pack_by_idx);
128}
129
130/* assume pdata is already zero'd by caller */
7c141127 131void prepare_packing_data(struct repository *r, struct packing_data *pdata)
43fa44fa 132{
7c141127
NTND
133 pdata->repo = r;
134
43fa44fa
NTND
135 if (git_env_bool("GIT_TEST_FULL_IN_PACK_ARRAY", 0)) {
136 /*
137 * do not initialize in_pack_by_idx[] to force the
138 * slow path in oe_in_pack()
139 */
140 } else {
141 prepare_in_pack_by_idx(pdata);
142 }
ac77d0c3
NTND
143
144 pdata->oe_size_limit = git_env_ulong("GIT_TEST_OE_SIZE",
145 1U << OE_SIZE_BITS);
9ac3f0e5
NTND
146 pdata->oe_delta_size_limit = git_env_ulong("GIT_TEST_OE_DELTA_SIZE",
147 1UL << OE_DELTA_SIZE_BITS);
edb673cf 148 init_recursive_mutex(&pdata->odb_lock);
43fa44fa
NTND
149}
150
2834bc27 151struct object_entry *packlist_alloc(struct packing_data *pdata,
3a37876b 152 const struct object_id *oid)
2834bc27
VM
153{
154 struct object_entry *new_entry;
155
156 if (pdata->nr_objects >= pdata->nr_alloc) {
157 pdata->nr_alloc = (pdata->nr_alloc + 1024) * 3 / 2;
2756ca43 158 REALLOC_ARRAY(pdata->objects, pdata->nr_alloc);
43fa44fa
NTND
159
160 if (!pdata->in_pack_by_idx)
161 REALLOC_ARRAY(pdata->in_pack, pdata->nr_alloc);
9ac3f0e5
NTND
162 if (pdata->delta_size)
163 REALLOC_ARRAY(pdata->delta_size, pdata->nr_alloc);
108f5303
CC
164
165 if (pdata->tree_depth)
166 REALLOC_ARRAY(pdata->tree_depth, pdata->nr_alloc);
fe0ac2fb
CC
167
168 if (pdata->layer)
169 REALLOC_ARRAY(pdata->layer, pdata->nr_alloc);
2834bc27
VM
170 }
171
172 new_entry = pdata->objects + pdata->nr_objects++;
173
174 memset(new_entry, 0, sizeof(*new_entry));
f1cbd033 175 oidcpy(&new_entry->idx.oid, oid);
2834bc27
VM
176
177 if (pdata->index_size * 3 <= pdata->nr_objects * 4)
178 rehash_objects(pdata);
3a37876b
JK
179 else {
180 int found;
181 uint32_t pos = locate_object_entry_hash(pdata,
182 &new_entry->idx.oid,
183 &found);
184 if (found)
185 BUG("duplicate object inserted into hash");
186 pdata->index[pos] = pdata->nr_objects;
187 }
2834bc27 188
43fa44fa
NTND
189 if (pdata->in_pack)
190 pdata->in_pack[pdata->nr_objects - 1] = NULL;
191
108f5303
CC
192 if (pdata->tree_depth)
193 pdata->tree_depth[pdata->nr_objects - 1] = 0;
194
fe0ac2fb
CC
195 if (pdata->layer)
196 pdata->layer[pdata->nr_objects - 1] = 0;
197
2834bc27
VM
198 return new_entry;
199}
6a1e32d5
JK
200
201void oe_set_delta_ext(struct packing_data *pdata,
202 struct object_entry *delta,
203 const unsigned char *sha1)
204{
205 struct object_entry *base;
206
207 ALLOC_GROW(pdata->ext_bases, pdata->nr_ext + 1, pdata->alloc_ext);
208 base = &pdata->ext_bases[pdata->nr_ext++];
209 memset(base, 0, sizeof(*base));
210 hashcpy(base->idx.oid.hash, sha1);
211
212 /* These flags mark that we are not part of the actual pack output. */
213 base->preferred_base = 1;
214 base->filled = 1;
215
216 delta->ext_base = 1;
217 delta->delta_idx = base - pdata->ext_bases + 1;
218}