]>
Commit | Line | Data |
---|---|---|
8f1d2e6f | 1 | #include "cache.h" |
f394e093 | 2 | #include "gettext.h" |
41771fa4 | 3 | #include "hex.h" |
175785e5 | 4 | #include "object.h" |
47f351e9 | 5 | #include "replace-object.h" |
cbd53a21 | 6 | #include "object-store.h" |
e9eefa67 DB |
7 | #include "blob.h" |
8 | #include "tree.h" | |
9 | #include "commit.h" | |
e9eefa67 | 10 | #include "tag.h" |
14ba97f8 | 11 | #include "alloc.h" |
d0b59866 | 12 | #include "packfile.h" |
85277506 | 13 | #include "commit-graph.h" |
175785e5 | 14 | |
fc046a75 LT |
15 | unsigned int get_max_object_index(void) |
16 | { | |
99bf115c | 17 | return the_repository->parsed_objects->obj_hash_size; |
fc046a75 LT |
18 | } |
19 | ||
20 | struct object *get_indexed_object(unsigned int idx) | |
21 | { | |
99bf115c | 22 | return the_repository->parsed_objects->obj_hash[idx]; |
fc046a75 | 23 | } |
175785e5 | 24 | |
df843662 NP |
25 | static const char *object_type_strings[] = { |
26 | NULL, /* OBJ_NONE = 0 */ | |
27 | "commit", /* OBJ_COMMIT = 1 */ | |
28 | "tree", /* OBJ_TREE = 2 */ | |
29 | "blob", /* OBJ_BLOB = 3 */ | |
30 | "tag", /* OBJ_TAG = 4 */ | |
885a86ab LT |
31 | }; |
32 | ||
debca9d2 | 33 | const char *type_name(unsigned int type) |
df843662 NP |
34 | { |
35 | if (type >= ARRAY_SIZE(object_type_strings)) | |
36 | return NULL; | |
37 | return object_type_strings[type]; | |
38 | } | |
39 | ||
fe8e3b71 | 40 | int type_from_string_gently(const char *str, ssize_t len, int gentle) |
df843662 NP |
41 | { |
42 | int i; | |
43 | ||
fe8e3b71 JS |
44 | if (len < 0) |
45 | len = strlen(str); | |
46 | ||
df843662 | 47 | for (i = 1; i < ARRAY_SIZE(object_type_strings); i++) |
b7994af0 JK |
48 | if (!strncmp(str, object_type_strings[i], len) && |
49 | object_type_strings[i][len] == '\0') | |
df843662 | 50 | return i; |
fe8e3b71 JS |
51 | |
52 | if (gentle) | |
53 | return -1; | |
54 | ||
42246589 | 55 | die(_("invalid object type \"%s\""), str); |
df843662 NP |
56 | } |
57 | ||
33bef7ea MH |
58 | /* |
59 | * Return a numerical hash value between 0 and n-1 for the object with | |
60 | * the specified sha1. n must be a power of 2. Please note that the | |
61 | * return value is *not* consistent across computer architectures. | |
62 | */ | |
46931d39 | 63 | static unsigned int hash_obj(const struct object_id *oid, unsigned int n) |
0556a11a | 64 | { |
d40abc8e | 65 | return oidhash(oid) & (n - 1); |
0556a11a LT |
66 | } |
67 | ||
33bef7ea MH |
68 | /* |
69 | * Insert obj into the hash table hash, which has length size (which | |
70 | * must be a power of 2). On collisions, simply overflow to the next | |
71 | * empty bucket. | |
72 | */ | |
0556a11a LT |
73 | static void insert_obj_hash(struct object *obj, struct object **hash, unsigned int size) |
74 | { | |
46931d39 | 75 | unsigned int j = hash_obj(&obj->oid, size); |
0556a11a LT |
76 | |
77 | while (hash[j]) { | |
78 | j++; | |
79 | if (j >= size) | |
80 | j = 0; | |
81 | } | |
82 | hash[j] = obj; | |
83 | } | |
84 | ||
33bef7ea MH |
85 | /* |
86 | * Look up the record for the given sha1 in the hash map stored in | |
87 | * obj_hash. Return NULL if it was not found. | |
88 | */ | |
d0229abd | 89 | struct object *lookup_object(struct repository *r, const struct object_id *oid) |
175785e5 | 90 | { |
9a414486 | 91 | unsigned int i, first; |
0556a11a | 92 | struct object *obj; |
175785e5 | 93 | |
94c09a71 | 94 | if (!r->parsed_objects->obj_hash) |
0556a11a | 95 | return NULL; |
175785e5 | 96 | |
46931d39 | 97 | first = i = hash_obj(oid, r->parsed_objects->obj_hash_size); |
94c09a71 | 98 | while ((obj = r->parsed_objects->obj_hash[i]) != NULL) { |
d0229abd | 99 | if (oideq(oid, &obj->oid)) |
0556a11a | 100 | break; |
070879ca | 101 | i++; |
94c09a71 | 102 | if (i == r->parsed_objects->obj_hash_size) |
070879ca JS |
103 | i = 0; |
104 | } | |
9a414486 JK |
105 | if (obj && i != first) { |
106 | /* | |
107 | * Move object to where we started to look for it so | |
108 | * that we do not need to walk the hash table the next | |
109 | * time we look for it. | |
110 | */ | |
94c09a71 SB |
111 | SWAP(r->parsed_objects->obj_hash[i], |
112 | r->parsed_objects->obj_hash[first]); | |
9a414486 | 113 | } |
0556a11a | 114 | return obj; |
175785e5 DB |
115 | } |
116 | ||
33bef7ea MH |
117 | /* |
118 | * Increase the size of the hash map stored in obj_hash to the next | |
119 | * power of 2 (but at least 32). Copy the existing values to the new | |
120 | * hash map. | |
121 | */ | |
346a817a | 122 | static void grow_object_hash(struct repository *r) |
175785e5 | 123 | { |
0556a11a | 124 | int i; |
9f36c9b7 NP |
125 | /* |
126 | * Note that this size must always be power-of-2 to match hash_obj | |
127 | * above. | |
128 | */ | |
346a817a | 129 | int new_hash_size = r->parsed_objects->obj_hash_size < 32 ? 32 : 2 * r->parsed_objects->obj_hash_size; |
0556a11a LT |
130 | struct object **new_hash; |
131 | ||
ca56dadb | 132 | CALLOC_ARRAY(new_hash, new_hash_size); |
346a817a SB |
133 | for (i = 0; i < r->parsed_objects->obj_hash_size; i++) { |
134 | struct object *obj = r->parsed_objects->obj_hash[i]; | |
135 | ||
0556a11a LT |
136 | if (!obj) |
137 | continue; | |
138 | insert_obj_hash(obj, new_hash, new_hash_size); | |
139 | } | |
346a817a SB |
140 | free(r->parsed_objects->obj_hash); |
141 | r->parsed_objects->obj_hash = new_hash; | |
142 | r->parsed_objects->obj_hash_size = new_hash_size; | |
175785e5 DB |
143 | } |
144 | ||
a378509e | 145 | void *create_object(struct repository *r, const struct object_id *oid, void *o) |
175785e5 | 146 | { |
100c5f3b LT |
147 | struct object *obj = o; |
148 | ||
175785e5 | 149 | obj->parsed = 0; |
0556a11a | 150 | obj->flags = 0; |
a378509e | 151 | oidcpy(&obj->oid, oid); |
175785e5 | 152 | |
341e45e4 SB |
153 | if (r->parsed_objects->obj_hash_size - 1 <= r->parsed_objects->nr_objs * 2) |
154 | grow_object_hash(r); | |
175785e5 | 155 | |
341e45e4 SB |
156 | insert_obj_hash(obj, r->parsed_objects->obj_hash, |
157 | r->parsed_objects->obj_hash_size); | |
158 | r->parsed_objects->nr_objs++; | |
100c5f3b | 159 | return obj; |
175785e5 DB |
160 | } |
161 | ||
6da43d93 | 162 | void *object_as_type(struct object *obj, enum object_type type, int quiet) |
8ff226a9 JK |
163 | { |
164 | if (obj->type == type) | |
165 | return obj; | |
166 | else if (obj->type == OBJ_NONE) { | |
d66bebcb | 167 | if (type == OBJ_COMMIT) |
6da43d93 | 168 | init_commit_node((struct commit *) obj); |
4468d443 SG |
169 | else |
170 | obj->type = type; | |
8ff226a9 JK |
171 | return obj; |
172 | } | |
173 | else { | |
174 | if (!quiet) | |
42246589 | 175 | error(_("object %s is a %s, not a %s"), |
f2fd0760 | 176 | oid_to_hex(&obj->oid), |
debca9d2 | 177 | type_name(obj->type), type_name(type)); |
8ff226a9 JK |
178 | return NULL; |
179 | } | |
180 | } | |
181 | ||
45a187cc | 182 | struct object *lookup_unknown_object(struct repository *r, const struct object_id *oid) |
66e481b0 | 183 | { |
45a187cc | 184 | struct object *obj = lookup_object(r, oid); |
100c5f3b | 185 | if (!obj) |
45a187cc | 186 | obj = create_object(r, oid, alloc_object_node(r)); |
66e481b0 DB |
187 | return obj; |
188 | } | |
189 | ||
7463064b JK |
190 | struct object *lookup_object_by_type(struct repository *r, |
191 | const struct object_id *oid, | |
192 | enum object_type type) | |
193 | { | |
194 | switch (type) { | |
195 | case OBJ_COMMIT: | |
196 | return (struct object *)lookup_commit(r, oid); | |
197 | case OBJ_TREE: | |
198 | return (struct object *)lookup_tree(r, oid); | |
199 | case OBJ_TAG: | |
200 | return (struct object *)lookup_tag(r, oid); | |
201 | case OBJ_BLOB: | |
202 | return (struct object *)lookup_blob(r, oid); | |
203 | default: | |
eafd6e7e | 204 | BUG("unknown object type %d", type); |
7463064b JK |
205 | } |
206 | } | |
207 | ||
108ed1a3 | 208 | struct object *parse_object_buffer(struct repository *r, const struct object_id *oid, enum object_type type, unsigned long size, void *buffer, int *eaten_p) |
9f613ddd JH |
209 | { |
210 | struct object *obj; | |
8e92e8f2 | 211 | *eaten_p = 0; |
9f613ddd | 212 | |
cc216827 | 213 | obj = NULL; |
21666f1a | 214 | if (type == OBJ_BLOB) { |
108ed1a3 | 215 | struct blob *blob = lookup_blob(r, oid); |
cc216827 | 216 | if (blob) { |
c1166ca0 | 217 | parse_blob_buffer(blob); |
cc216827 JM |
218 | obj = &blob->object; |
219 | } | |
21666f1a | 220 | } else if (type == OBJ_TREE) { |
108ed1a3 | 221 | struct tree *tree = lookup_tree(r, oid); |
cc216827 JM |
222 | if (tree) { |
223 | obj = &tree->object; | |
68be2fea JH |
224 | if (!tree->buffer) |
225 | tree->object.parsed = 0; | |
cc216827 | 226 | if (!tree->object.parsed) { |
d0b8c9e5 MK |
227 | if (parse_tree_buffer(tree, buffer, size)) |
228 | return NULL; | |
8e92e8f2 | 229 | *eaten_p = 1; |
cc216827 | 230 | } |
9f613ddd | 231 | } |
21666f1a | 232 | } else if (type == OBJ_COMMIT) { |
108ed1a3 | 233 | struct commit *commit = lookup_commit(r, oid); |
cc216827 | 234 | if (commit) { |
108ed1a3 | 235 | if (parse_commit_buffer(r, commit, buffer, size, 1)) |
d0b8c9e5 | 236 | return NULL; |
51b27747 JK |
237 | if (save_commit_buffer && |
238 | !get_cached_commit_buffer(r, commit, NULL)) { | |
108ed1a3 | 239 | set_commit_buffer(r, commit, buffer, size); |
8e92e8f2 | 240 | *eaten_p = 1; |
cc216827 JM |
241 | } |
242 | obj = &commit->object; | |
9f613ddd | 243 | } |
21666f1a | 244 | } else if (type == OBJ_TAG) { |
108ed1a3 | 245 | struct tag *tag = lookup_tag(r, oid); |
cc216827 | 246 | if (tag) { |
108ed1a3 | 247 | if (parse_tag_buffer(r, tag, buffer, size)) |
d0b8c9e5 | 248 | return NULL; |
cc216827 JM |
249 | obj = &tag->object; |
250 | } | |
9f613ddd | 251 | } else { |
42246589 | 252 | warning(_("object %s has unknown type id %d"), oid_to_hex(oid), type); |
9f613ddd JH |
253 | obj = NULL; |
254 | } | |
9f613ddd JH |
255 | return obj; |
256 | } | |
257 | ||
c251c83d | 258 | struct object *parse_object_or_die(const struct object_id *oid, |
75a95490 JK |
259 | const char *name) |
260 | { | |
109cd76d | 261 | struct object *o = parse_object(the_repository, oid); |
75a95490 JK |
262 | if (o) |
263 | return o; | |
264 | ||
c251c83d | 265 | die(_("unable to parse object: %s"), name ? name : oid_to_hex(oid)); |
75a95490 JK |
266 | } |
267 | ||
c868d8e9 JK |
268 | struct object *parse_object_with_flags(struct repository *r, |
269 | const struct object_id *oid, | |
270 | enum parse_object_flags flags) | |
e9eefa67 | 271 | { |
c868d8e9 | 272 | int skip_hash = !!(flags & PARSE_OBJECT_SKIP_HASH_CHECK); |
c4584ae3 | 273 | unsigned long size; |
21666f1a | 274 | enum object_type type; |
9f613ddd | 275 | int eaten; |
8e4b0b60 | 276 | const struct object_id *repl = lookup_replace_object(r, oid); |
ccdc6037 JK |
277 | void *buffer; |
278 | struct object *obj; | |
279 | ||
d0229abd | 280 | obj = lookup_object(r, oid); |
ccdc6037 JK |
281 | if (obj && obj->parsed) |
282 | return obj; | |
9f613ddd | 283 | |
9a8c3c4a JK |
284 | if (skip_hash) { |
285 | struct commit *commit = lookup_commit_in_graph(r, repl); | |
286 | if (commit) | |
287 | return &commit->object; | |
288 | } | |
289 | ||
40286ca2 | 290 | if ((!obj || obj->type == OBJ_BLOB) && |
8db2dad7 | 291 | oid_object_info(r, oid, NULL) == OBJ_BLOB) { |
c868d8e9 | 292 | if (!skip_hash && stream_object_signature(r, repl) < 0) { |
01f8d594 | 293 | error(_("hash mismatch %s"), oid_to_hex(oid)); |
090ea126 NTND |
294 | return NULL; |
295 | } | |
c1166ca0 | 296 | parse_blob_buffer(lookup_blob(r, oid)); |
d0229abd | 297 | return lookup_object(r, oid); |
090ea126 NTND |
298 | } |
299 | ||
d1a69022 | 300 | buffer = repo_read_object_file(r, oid, &type, &size); |
c4584ae3 | 301 | if (buffer) { |
c868d8e9 JK |
302 | if (!skip_hash && |
303 | check_object_signature(r, repl, buffer, size, type) < 0) { | |
0b1f1130 | 304 | free(buffer); |
01f8d594 | 305 | error(_("hash mismatch %s"), oid_to_hex(repl)); |
acdeec62 LT |
306 | return NULL; |
307 | } | |
9f613ddd | 308 | |
8e4b0b60 | 309 | obj = parse_object_buffer(r, oid, type, size, |
1ec5bfd2 | 310 | buffer, &eaten); |
9f613ddd JH |
311 | if (!eaten) |
312 | free(buffer); | |
bd2c39f5 | 313 | return obj; |
e9eefa67 DB |
314 | } |
315 | return NULL; | |
316 | } | |
66e481b0 | 317 | |
c868d8e9 JK |
318 | struct object *parse_object(struct repository *r, const struct object_id *oid) |
319 | { | |
320 | return parse_object_with_flags(r, oid, 0); | |
321 | } | |
322 | ||
66e481b0 DB |
323 | struct object_list *object_list_insert(struct object *item, |
324 | struct object_list **list_p) | |
325 | { | |
326 | struct object_list *new_list = xmalloc(sizeof(struct object_list)); | |
55b4e9e4 JH |
327 | new_list->item = item; |
328 | new_list->next = *list_p; | |
329 | *list_p = new_list; | |
330 | return new_list; | |
66e481b0 DB |
331 | } |
332 | ||
66e481b0 DB |
333 | int object_list_contains(struct object_list *list, struct object *obj) |
334 | { | |
335 | while (list) { | |
336 | if (list->item == obj) | |
337 | return 1; | |
338 | list = list->next; | |
339 | } | |
340 | return 0; | |
341 | } | |
1f1e895f | 342 | |
acac50dd JK |
343 | void object_list_free(struct object_list **list) |
344 | { | |
345 | while (*list) { | |
346 | struct object_list *p = *list; | |
347 | *list = p->next; | |
348 | free(p); | |
349 | } | |
350 | } | |
351 | ||
31faeb20 MH |
352 | /* |
353 | * A zero-length string to which object_array_entry::name can be | |
354 | * initialized without requiring a malloc/free. | |
355 | */ | |
356 | static char object_array_slopbuf[1]; | |
357 | ||
9e0c3c4f JK |
358 | void add_object_array_with_path(struct object *obj, const char *name, |
359 | struct object_array *array, | |
360 | unsigned mode, const char *path) | |
1f1e895f LT |
361 | { |
362 | unsigned nr = array->nr; | |
363 | unsigned alloc = array->alloc; | |
364 | struct object_array_entry *objects = array->objects; | |
31faeb20 | 365 | struct object_array_entry *entry; |
1f1e895f LT |
366 | |
367 | if (nr >= alloc) { | |
368 | alloc = (alloc + 32) * 2; | |
2756ca43 | 369 | REALLOC_ARRAY(objects, alloc); |
1f1e895f LT |
370 | array->alloc = alloc; |
371 | array->objects = objects; | |
372 | } | |
31faeb20 MH |
373 | entry = &objects[nr]; |
374 | entry->item = obj; | |
375 | if (!name) | |
376 | entry->name = NULL; | |
377 | else if (!*name) | |
378 | /* Use our own empty string instead of allocating one: */ | |
379 | entry->name = object_array_slopbuf; | |
380 | else | |
381 | entry->name = xstrdup(name); | |
382 | entry->mode = mode; | |
9e0c3c4f JK |
383 | if (path) |
384 | entry->path = xstrdup(path); | |
385 | else | |
386 | entry->path = NULL; | |
1f1e895f LT |
387 | array->nr = ++nr; |
388 | } | |
b2a6d1c6 | 389 | |
afa15f3c MG |
390 | void add_object_array(struct object *obj, const char *name, struct object_array *array) |
391 | { | |
189a1222 | 392 | add_object_array_with_path(obj, name, array, S_IFINVALID, NULL); |
afa15f3c MG |
393 | } |
394 | ||
68f49235 JK |
395 | /* |
396 | * Free all memory associated with an entry; the result is | |
397 | * in an unspecified state and should not be examined. | |
398 | */ | |
399 | static void object_array_release_entry(struct object_array_entry *ent) | |
400 | { | |
401 | if (ent->name != object_array_slopbuf) | |
402 | free(ent->name); | |
9e0c3c4f | 403 | free(ent->path); |
68f49235 JK |
404 | } |
405 | ||
71992039 MÅ |
406 | struct object *object_array_pop(struct object_array *array) |
407 | { | |
408 | struct object *ret; | |
409 | ||
410 | if (!array->nr) | |
411 | return NULL; | |
412 | ||
413 | ret = array->objects[array->nr - 1].item; | |
414 | object_array_release_entry(&array->objects[array->nr - 1]); | |
415 | array->nr--; | |
416 | return ret; | |
417 | } | |
418 | ||
aeb4a51e MH |
419 | void object_array_filter(struct object_array *array, |
420 | object_array_each_func_t want, void *cb_data) | |
b2a6d1c6 | 421 | { |
aeb4a51e | 422 | unsigned nr = array->nr, src, dst; |
b2a6d1c6 JH |
423 | struct object_array_entry *objects = array->objects; |
424 | ||
aeb4a51e MH |
425 | for (src = dst = 0; src < nr; src++) { |
426 | if (want(&objects[src], cb_data)) { | |
b2a6d1c6 JH |
427 | if (src != dst) |
428 | objects[dst] = objects[src]; | |
429 | dst++; | |
31faeb20 | 430 | } else { |
68f49235 | 431 | object_array_release_entry(&objects[src]); |
aeb4a51e MH |
432 | } |
433 | } | |
434 | array->nr = dst; | |
435 | } | |
436 | ||
46be8231 JK |
437 | void object_array_clear(struct object_array *array) |
438 | { | |
439 | int i; | |
440 | for (i = 0; i < array->nr; i++) | |
441 | object_array_release_entry(&array->objects[i]); | |
6a83d902 | 442 | FREE_AND_NULL(array->objects); |
46be8231 JK |
443 | array->nr = array->alloc = 0; |
444 | } | |
445 | ||
1506510c | 446 | /* |
ce1d6d9f | 447 | * Return true if array already contains an entry. |
1506510c | 448 | */ |
ce1d6d9f JX |
449 | static int contains_object(struct object_array *array, |
450 | const struct object *item, const char *name) | |
1506510c MH |
451 | { |
452 | unsigned nr = array->nr, i; | |
453 | struct object_array_entry *object = array->objects; | |
454 | ||
455 | for (i = 0; i < nr; i++, object++) | |
ce1d6d9f | 456 | if (item == object->item && !strcmp(object->name, name)) |
1506510c MH |
457 | return 1; |
458 | return 0; | |
459 | } | |
460 | ||
b2a6d1c6 JH |
461 | void object_array_remove_duplicates(struct object_array *array) |
462 | { | |
1506510c | 463 | unsigned nr = array->nr, src; |
b2a6d1c6 JH |
464 | struct object_array_entry *objects = array->objects; |
465 | ||
1506510c MH |
466 | array->nr = 0; |
467 | for (src = 0; src < nr; src++) { | |
ce1d6d9f JX |
468 | if (!contains_object(array, objects[src].item, |
469 | objects[src].name)) { | |
1506510c MH |
470 | if (src != array->nr) |
471 | objects[array->nr] = objects[src]; | |
472 | array->nr++; | |
31faeb20 | 473 | } else { |
68f49235 | 474 | object_array_release_entry(&objects[src]); |
b2a6d1c6 | 475 | } |
b2a6d1c6 JH |
476 | } |
477 | } | |
bcc0a3ea HV |
478 | |
479 | void clear_object_flags(unsigned flags) | |
480 | { | |
481 | int i; | |
482 | ||
99bf115c SB |
483 | for (i=0; i < the_repository->parsed_objects->obj_hash_size; i++) { |
484 | struct object *obj = the_repository->parsed_objects->obj_hash[i]; | |
bcc0a3ea HV |
485 | if (obj) |
486 | obj->flags &= ~flags; | |
487 | } | |
488 | } | |
4ad315fc | 489 | |
cd888845 | 490 | void repo_clear_commit_marks(struct repository *r, unsigned int flags) |
4ad315fc RS |
491 | { |
492 | int i; | |
493 | ||
cd888845 RS |
494 | for (i = 0; i < r->parsed_objects->obj_hash_size; i++) { |
495 | struct object *obj = r->parsed_objects->obj_hash[i]; | |
4ad315fc RS |
496 | if (obj && obj->type == OBJ_COMMIT) |
497 | obj->flags &= ~flags; | |
498 | } | |
499 | } | |
90c62155 | 500 | |
99bf115c SB |
501 | struct parsed_object_pool *parsed_object_pool_new(void) |
502 | { | |
503 | struct parsed_object_pool *o = xmalloc(sizeof(*o)); | |
504 | memset(o, 0, sizeof(*o)); | |
14ba97f8 SB |
505 | |
506 | o->blob_state = allocate_alloc_state(); | |
507 | o->tree_state = allocate_alloc_state(); | |
508 | o->commit_state = allocate_alloc_state(); | |
509 | o->tag_state = allocate_alloc_state(); | |
510 | o->object_state = allocate_alloc_state(); | |
511 | ||
eee4502b | 512 | o->is_shallow = -1; |
ca56dadb | 513 | CALLOC_ARRAY(o->shallow_stat, 1); |
eee4502b | 514 | |
65ea9d4b SB |
515 | o->buffer_slab = allocate_commit_buffer_slab(); |
516 | ||
99bf115c SB |
517 | return o; |
518 | } | |
519 | ||
90c62155 SB |
520 | struct raw_object_store *raw_object_store_new(void) |
521 | { | |
522 | struct raw_object_store *o = xmalloc(sizeof(*o)); | |
523 | ||
524 | memset(o, 0, sizeof(*o)); | |
a80d72db | 525 | INIT_LIST_HEAD(&o->packed_git_mru); |
ec48540f | 526 | hashmap_init(&o->pack_map, pack_map_entry_cmp, NULL, 0); |
b1fc9da1 | 527 | pthread_mutex_init(&o->replace_mutex, NULL); |
90c62155 SB |
528 | return o; |
529 | } | |
97501e93 | 530 | |
b3cecf49 | 531 | void free_object_directory(struct object_directory *odb) |
97501e93 | 532 | { |
f0eaf638 | 533 | free(odb->path); |
d4e19e51 | 534 | odb_clear_loose_cache(odb); |
263db403 | 535 | free(odb); |
97501e93 SB |
536 | } |
537 | ||
f0eaf638 | 538 | static void free_object_directories(struct raw_object_store *o) |
97501e93 | 539 | { |
f0eaf638 | 540 | while (o->odb) { |
263db403 | 541 | struct object_directory *next; |
97501e93 | 542 | |
f0eaf638 JK |
543 | next = o->odb->next; |
544 | free_object_directory(o->odb); | |
545 | o->odb = next; | |
97501e93 | 546 | } |
cf2dc1c2 EW |
547 | kh_destroy_odb_path_map(o->odb_by_path); |
548 | o->odb_by_path = NULL; | |
97501e93 SB |
549 | } |
550 | ||
90c62155 SB |
551 | void raw_object_store_clear(struct raw_object_store *o) |
552 | { | |
90c62155 | 553 | FREE_AND_NULL(o->alternate_db); |
7a1dc605 SB |
554 | |
555 | oidmap_free(o->replace_map, 1); | |
d5873072 | 556 | FREE_AND_NULL(o->replace_map); |
b1fc9da1 | 557 | pthread_mutex_destroy(&o->replace_mutex); |
97501e93 | 558 | |
85277506 JT |
559 | free_commit_graph(o->commit_graph); |
560 | o->commit_graph = NULL; | |
561 | o->commit_graph_attempted = 0; | |
562 | ||
f0eaf638 JK |
563 | free_object_directories(o); |
564 | o->odb_tail = NULL; | |
565 | o->loaded_alternates = 0; | |
a80d72db SB |
566 | |
567 | INIT_LIST_HEAD(&o->packed_git_mru); | |
2d511cfc | 568 | close_object_store(o); |
d0b59866 | 569 | o->packed_git = NULL; |
ec48540f | 570 | |
6da1a258 | 571 | hashmap_clear(&o->pack_map); |
90c62155 | 572 | } |
99bf115c SB |
573 | |
574 | void parsed_object_pool_clear(struct parsed_object_pool *o) | |
575 | { | |
576 | /* | |
99bf115c SB |
577 | * As objects are allocated in slabs (see alloc.c), we do |
578 | * not need to free each object, but each slab instead. | |
14ba97f8 SB |
579 | * |
580 | * Before doing so, we need to free any additional memory | |
581 | * the objects may hold. | |
99bf115c | 582 | */ |
14ba97f8 SB |
583 | unsigned i; |
584 | ||
585 | for (i = 0; i < o->obj_hash_size; i++) { | |
586 | struct object *obj = o->obj_hash[i]; | |
587 | ||
588 | if (!obj) | |
589 | continue; | |
590 | ||
591 | if (obj->type == OBJ_TREE) | |
592 | free_tree_buffer((struct tree*)obj); | |
593 | else if (obj->type == OBJ_COMMIT) | |
6a7895fd | 594 | release_commit_memory(o, (struct commit*)obj); |
14ba97f8 SB |
595 | else if (obj->type == OBJ_TAG) |
596 | release_tag_memory((struct tag*)obj); | |
597 | } | |
598 | ||
599 | FREE_AND_NULL(o->obj_hash); | |
600 | o->obj_hash_size = 0; | |
601 | ||
65ea9d4b SB |
602 | free_commit_buffer_slab(o->buffer_slab); |
603 | o->buffer_slab = NULL; | |
604 | ||
14ba97f8 SB |
605 | clear_alloc_state(o->blob_state); |
606 | clear_alloc_state(o->tree_state); | |
607 | clear_alloc_state(o->commit_state); | |
608 | clear_alloc_state(o->tag_state); | |
609 | clear_alloc_state(o->object_state); | |
96b0710e | 610 | stat_validity_clear(o->shallow_stat); |
14ba97f8 SB |
611 | FREE_AND_NULL(o->blob_state); |
612 | FREE_AND_NULL(o->tree_state); | |
613 | FREE_AND_NULL(o->commit_state); | |
614 | FREE_AND_NULL(o->tag_state); | |
615 | FREE_AND_NULL(o->object_state); | |
96b0710e | 616 | FREE_AND_NULL(o->shallow_stat); |
99bf115c | 617 | } |