blob: 794c86650e9a976a0667cb8103a4ae0a9a0deaf2 [file] [log] [blame]
Junio C Hamano8f1d2e62006-01-07 01:33:54 -08001#include "cache.h"
Daniel Barkalow175785e2005-04-18 11:39:48 -07002#include "object.h"
Stefan Beller47f351e2018-04-11 17:21:06 -07003#include "replace-object.h"
Stefan Bellercbd53a22018-05-15 16:42:15 -07004#include "object-store.h"
Daniel Barkalowe9eefa62005-04-28 07:46:33 -07005#include "blob.h"
6#include "tree.h"
7#include "commit.h"
Daniel Barkalowe9eefa62005-04-28 07:46:33 -07008#include "tag.h"
Stefan Beller14ba97f2018-05-15 14:48:42 -07009#include "alloc.h"
Stefan Bellerd0b59862018-03-23 18:21:00 +010010#include "packfile.h"
Jonathan Tan85277502018-07-11 15:42:41 -070011#include "commit-graph.h"
Daniel Barkalow175785e2005-04-18 11:39:48 -070012
Linus Torvaldsfc046a72006-06-29 21:38:55 -070013unsigned int get_max_object_index(void)
14{
Stefan Beller99bf1152018-05-08 12:37:24 -070015 return the_repository->parsed_objects->obj_hash_size;
Linus Torvaldsfc046a72006-06-29 21:38:55 -070016}
17
18struct object *get_indexed_object(unsigned int idx)
19{
Stefan Beller99bf1152018-05-08 12:37:24 -070020 return the_repository->parsed_objects->obj_hash[idx];
Linus Torvaldsfc046a72006-06-29 21:38:55 -070021}
Daniel Barkalow175785e2005-04-18 11:39:48 -070022
Nicolas Pitredf843662007-02-26 14:55:58 -050023static const char *object_type_strings[] = {
24 NULL, /* OBJ_NONE = 0 */
25 "commit", /* OBJ_COMMIT = 1 */
26 "tree", /* OBJ_TREE = 2 */
27 "blob", /* OBJ_BLOB = 3 */
28 "tag", /* OBJ_TAG = 4 */
Linus Torvalds885a86a2006-06-14 16:45:13 -070029};
30
Brandon Williamsdebca9d2018-02-14 10:59:24 -080031const char *type_name(unsigned int type)
Nicolas Pitredf843662007-02-26 14:55:58 -050032{
33 if (type >= ARRAY_SIZE(object_type_strings))
34 return NULL;
35 return object_type_strings[type];
36}
37
Johannes Schindelinfe8e3b72014-09-10 15:52:44 +020038int type_from_string_gently(const char *str, ssize_t len, int gentle)
Nicolas Pitredf843662007-02-26 14:55:58 -050039{
40 int i;
41
Johannes Schindelinfe8e3b72014-09-10 15:52:44 +020042 if (len < 0)
43 len = strlen(str);
44
Nicolas Pitredf843662007-02-26 14:55:58 -050045 for (i = 1; i < ARRAY_SIZE(object_type_strings); i++)
Jeff Kingb7994af2015-04-17 10:52:48 -040046 if (!strncmp(str, object_type_strings[i], len) &&
47 object_type_strings[i][len] == '\0')
Nicolas Pitredf843662007-02-26 14:55:58 -050048 return i;
Johannes Schindelinfe8e3b72014-09-10 15:52:44 +020049
50 if (gentle)
51 return -1;
52
Nguyễn Thái Ngọc Duy42246582018-07-21 09:49:33 +020053 die(_("invalid object type \"%s\""), str);
Nicolas Pitredf843662007-02-26 14:55:58 -050054}
55
Michael Haggerty33bef7e2014-02-28 17:29:17 +010056/*
57 * Return a numerical hash value between 0 and n-1 for the object with
58 * the specified sha1. n must be a power of 2. Please note that the
59 * return value is *not* consistent across computer architectures.
60 */
Jeff King46931d32019-06-20 03:41:17 -040061static unsigned int hash_obj(const struct object_id *oid, unsigned int n)
Linus Torvalds0556a112006-06-30 11:20:33 -070062{
Jeff Kingd40abc82019-06-20 03:41:49 -040063 return oidhash(oid) & (n - 1);
Linus Torvalds0556a112006-06-30 11:20:33 -070064}
65
Michael Haggerty33bef7e2014-02-28 17:29:17 +010066/*
67 * Insert obj into the hash table hash, which has length size (which
68 * must be a power of 2). On collisions, simply overflow to the next
69 * empty bucket.
70 */
Linus Torvalds0556a112006-06-30 11:20:33 -070071static void insert_obj_hash(struct object *obj, struct object **hash, unsigned int size)
72{
Jeff King46931d32019-06-20 03:41:17 -040073 unsigned int j = hash_obj(&obj->oid, size);
Linus Torvalds0556a112006-06-30 11:20:33 -070074
75 while (hash[j]) {
76 j++;
77 if (j >= size)
78 j = 0;
79 }
80 hash[j] = obj;
81}
82
Michael Haggerty33bef7e2014-02-28 17:29:17 +010083/*
84 * Look up the record for the given sha1 in the hash map stored in
85 * obj_hash. Return NULL if it was not found.
86 */
Jeff Kingd0229ab2019-06-20 03:41:14 -040087struct object *lookup_object(struct repository *r, const struct object_id *oid)
Daniel Barkalow175785e2005-04-18 11:39:48 -070088{
Jeff King9a414482013-05-01 16:34:50 -040089 unsigned int i, first;
Linus Torvalds0556a112006-06-30 11:20:33 -070090 struct object *obj;
91
Stefan Beller94c09a72018-06-28 18:22:07 -070092 if (!r->parsed_objects->obj_hash)
Linus Torvalds0556a112006-06-30 11:20:33 -070093 return NULL;
94
Jeff King46931d32019-06-20 03:41:17 -040095 first = i = hash_obj(oid, r->parsed_objects->obj_hash_size);
Stefan Beller94c09a72018-06-28 18:22:07 -070096 while ((obj = r->parsed_objects->obj_hash[i]) != NULL) {
Jeff Kingd0229ab2019-06-20 03:41:14 -040097 if (oideq(oid, &obj->oid))
Linus Torvalds0556a112006-06-30 11:20:33 -070098 break;
99 i++;
Stefan Beller94c09a72018-06-28 18:22:07 -0700100 if (i == r->parsed_objects->obj_hash_size)
Linus Torvalds0556a112006-06-30 11:20:33 -0700101 i = 0;
102 }
Jeff King9a414482013-05-01 16:34:50 -0400103 if (obj && i != first) {
104 /*
105 * Move object to where we started to look for it so
106 * that we do not need to walk the hash table the next
107 * time we look for it.
108 */
Stefan Beller94c09a72018-06-28 18:22:07 -0700109 SWAP(r->parsed_objects->obj_hash[i],
110 r->parsed_objects->obj_hash[first]);
Jeff King9a414482013-05-01 16:34:50 -0400111 }
Linus Torvalds0556a112006-06-30 11:20:33 -0700112 return obj;
113}
114
Michael Haggerty33bef7e2014-02-28 17:29:17 +0100115/*
116 * Increase the size of the hash map stored in obj_hash to the next
117 * power of 2 (but at least 32). Copy the existing values to the new
118 * hash map.
119 */
Stefan Beller346a8172018-05-08 12:37:34 -0700120static void grow_object_hash(struct repository *r)
Linus Torvalds0556a112006-06-30 11:20:33 -0700121{
122 int i;
Nicolas Pitre9f36c9b2013-09-10 18:17:12 -0400123 /*
124 * Note that this size must always be power-of-2 to match hash_obj
125 * above.
126 */
Stefan Beller346a8172018-05-08 12:37:34 -0700127 int new_hash_size = r->parsed_objects->obj_hash_size < 32 ? 32 : 2 * r->parsed_objects->obj_hash_size;
Linus Torvalds0556a112006-06-30 11:20:33 -0700128 struct object **new_hash;
129
Jonas Fonsecab3c952f2006-08-28 02:26:07 +0200130 new_hash = xcalloc(new_hash_size, sizeof(struct object *));
Stefan Beller346a8172018-05-08 12:37:34 -0700131 for (i = 0; i < r->parsed_objects->obj_hash_size; i++) {
132 struct object *obj = r->parsed_objects->obj_hash[i];
133
Linus Torvalds0556a112006-06-30 11:20:33 -0700134 if (!obj)
135 continue;
136 insert_obj_hash(obj, new_hash, new_hash_size);
137 }
Stefan Beller346a8172018-05-08 12:37:34 -0700138 free(r->parsed_objects->obj_hash);
139 r->parsed_objects->obj_hash = new_hash;
140 r->parsed_objects->obj_hash_size = new_hash_size;
Daniel Barkalow175785e2005-04-18 11:39:48 -0700141}
142
Jeff Kinga3785092019-06-20 03:41:21 -0400143void *create_object(struct repository *r, const struct object_id *oid, void *o)
Daniel Barkalow175785e2005-04-18 11:39:48 -0700144{
Linus Torvalds100c5f32007-04-16 22:11:43 -0700145 struct object *obj = o;
146
Daniel Barkalow175785e2005-04-18 11:39:48 -0700147 obj->parsed = 0;
Linus Torvalds0556a112006-06-30 11:20:33 -0700148 obj->flags = 0;
Jeff Kinga3785092019-06-20 03:41:21 -0400149 oidcpy(&obj->oid, oid);
Daniel Barkalow175785e2005-04-18 11:39:48 -0700150
Stefan Beller341e45e2018-05-08 12:37:35 -0700151 if (r->parsed_objects->obj_hash_size - 1 <= r->parsed_objects->nr_objs * 2)
152 grow_object_hash(r);
Johannes Schindelin070879c2006-02-12 02:57:57 +0100153
Stefan Beller341e45e2018-05-08 12:37:35 -0700154 insert_obj_hash(obj, r->parsed_objects->obj_hash,
155 r->parsed_objects->obj_hash_size);
156 r->parsed_objects->nr_objs++;
Linus Torvalds100c5f32007-04-16 22:11:43 -0700157 return obj;
Daniel Barkalow175785e2005-04-18 11:39:48 -0700158}
159
Stefan Bellera962da12018-06-28 18:22:06 -0700160void *object_as_type(struct repository *r, struct object *obj, enum object_type type, int quiet)
Jeff King8ff226a2014-07-13 02:42:03 -0400161{
162 if (obj->type == type)
163 return obj;
164 else if (obj->type == OBJ_NONE) {
Jeff Kingd66bebc2014-07-13 02:42:12 -0400165 if (type == OBJ_COMMIT)
SZEDER Gábor4468d442019-01-27 14:08:32 +0100166 init_commit_node(r, (struct commit *) obj);
167 else
168 obj->type = type;
Jeff King8ff226a2014-07-13 02:42:03 -0400169 return obj;
170 }
171 else {
172 if (!quiet)
Nguyễn Thái Ngọc Duy42246582018-07-21 09:49:33 +0200173 error(_("object %s is a %s, not a %s"),
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000174 oid_to_hex(&obj->oid),
Brandon Williamsdebca9d2018-02-14 10:59:24 -0800175 type_name(obj->type), type_name(type));
Jeff King8ff226a2014-07-13 02:42:03 -0400176 return NULL;
177 }
178}
179
Jeff King0ebbcf72019-06-20 03:41:10 -0400180struct object *lookup_unknown_object(const struct object_id *oid)
barkalow@iabervon.org66e481b2005-08-02 19:45:48 -0400181{
Jeff Kingd0229ab2019-06-20 03:41:14 -0400182 struct object *obj = lookup_object(the_repository, oid);
Linus Torvalds100c5f32007-04-16 22:11:43 -0700183 if (!obj)
Jeff Kinga3785092019-06-20 03:41:21 -0400184 obj = create_object(the_repository, oid,
Stefan Beller13e3fdc2018-05-08 12:37:31 -0700185 alloc_object_node(the_repository));
barkalow@iabervon.org66e481b2005-08-02 19:45:48 -0400186 return obj;
187}
188
Stefan Beller108ed1a2018-06-28 18:22:18 -0700189struct object *parse_object_buffer(struct repository *r, const struct object_id *oid, enum object_type type, unsigned long size, void *buffer, int *eaten_p)
Junio C Hamano9f613dd2006-09-15 13:30:02 -0700190{
191 struct object *obj;
Stefan Beller8e92e8f2013-07-18 00:09:42 +0200192 *eaten_p = 0;
Junio C Hamano9f613dd2006-09-15 13:30:02 -0700193
Jim Meyeringcc216822007-12-21 11:56:32 +0100194 obj = NULL;
Nicolas Pitre21666f12007-02-26 14:55:59 -0500195 if (type == OBJ_BLOB) {
Stefan Beller108ed1a2018-06-28 18:22:18 -0700196 struct blob *blob = lookup_blob(r, oid);
Jim Meyeringcc216822007-12-21 11:56:32 +0100197 if (blob) {
Martin Koeglerd0b8c9e2008-02-03 22:22:39 +0100198 if (parse_blob_buffer(blob, buffer, size))
199 return NULL;
Jim Meyeringcc216822007-12-21 11:56:32 +0100200 obj = &blob->object;
201 }
Nicolas Pitre21666f12007-02-26 14:55:59 -0500202 } else if (type == OBJ_TREE) {
Stefan Beller108ed1a2018-06-28 18:22:18 -0700203 struct tree *tree = lookup_tree(r, oid);
Jim Meyeringcc216822007-12-21 11:56:32 +0100204 if (tree) {
205 obj = &tree->object;
Junio C Hamano68be2fe2011-11-16 22:04:13 -0800206 if (!tree->buffer)
207 tree->object.parsed = 0;
Jim Meyeringcc216822007-12-21 11:56:32 +0100208 if (!tree->object.parsed) {
Martin Koeglerd0b8c9e2008-02-03 22:22:39 +0100209 if (parse_tree_buffer(tree, buffer, size))
210 return NULL;
Stefan Beller8e92e8f2013-07-18 00:09:42 +0200211 *eaten_p = 1;
Jim Meyeringcc216822007-12-21 11:56:32 +0100212 }
Junio C Hamano9f613dd2006-09-15 13:30:02 -0700213 }
Nicolas Pitre21666f12007-02-26 14:55:59 -0500214 } else if (type == OBJ_COMMIT) {
Stefan Beller108ed1a2018-06-28 18:22:18 -0700215 struct commit *commit = lookup_commit(r, oid);
Jim Meyeringcc216822007-12-21 11:56:32 +0100216 if (commit) {
Stefan Beller108ed1a2018-06-28 18:22:18 -0700217 if (parse_commit_buffer(r, commit, buffer, size, 1))
Martin Koeglerd0b8c9e2008-02-03 22:22:39 +0100218 return NULL;
Stefan Beller108ed1a2018-06-28 18:22:18 -0700219 if (!get_cached_commit_buffer(r, commit, NULL)) {
220 set_commit_buffer(r, commit, buffer, size);
Stefan Beller8e92e8f2013-07-18 00:09:42 +0200221 *eaten_p = 1;
Jim Meyeringcc216822007-12-21 11:56:32 +0100222 }
223 obj = &commit->object;
Junio C Hamano9f613dd2006-09-15 13:30:02 -0700224 }
Nicolas Pitre21666f12007-02-26 14:55:59 -0500225 } else if (type == OBJ_TAG) {
Stefan Beller108ed1a2018-06-28 18:22:18 -0700226 struct tag *tag = lookup_tag(r, oid);
Jim Meyeringcc216822007-12-21 11:56:32 +0100227 if (tag) {
Stefan Beller108ed1a2018-06-28 18:22:18 -0700228 if (parse_tag_buffer(r, tag, buffer, size))
Martin Koeglerd0b8c9e2008-02-03 22:22:39 +0100229 return NULL;
Jim Meyeringcc216822007-12-21 11:56:32 +0100230 obj = &tag->object;
231 }
Junio C Hamano9f613dd2006-09-15 13:30:02 -0700232 } else {
Nguyễn Thái Ngọc Duy42246582018-07-21 09:49:33 +0200233 warning(_("object %s has unknown type id %d"), oid_to_hex(oid), type);
Junio C Hamano9f613dd2006-09-15 13:30:02 -0700234 obj = NULL;
235 }
Junio C Hamano9f613dd2006-09-15 13:30:02 -0700236 return obj;
237}
238
brian m. carlsonc251c832017-05-06 22:10:38 +0000239struct object *parse_object_or_die(const struct object_id *oid,
Jeff King75a95492013-03-17 04:22:36 -0400240 const char *name)
241{
Stefan Beller109cd762018-06-28 18:21:51 -0700242 struct object *o = parse_object(the_repository, oid);
Jeff King75a95492013-03-17 04:22:36 -0400243 if (o)
244 return o;
245
brian m. carlsonc251c832017-05-06 22:10:38 +0000246 die(_("unable to parse object: %s"), name ? name : oid_to_hex(oid));
Jeff King75a95492013-03-17 04:22:36 -0400247}
248
Stefan Beller8e4b0b62018-06-28 18:22:19 -0700249struct object *parse_object(struct repository *r, const struct object_id *oid)
Daniel Barkalowe9eefa62005-04-28 07:46:33 -0700250{
Junio C Hamanoc4584ae2005-06-27 03:33:33 -0700251 unsigned long size;
Nicolas Pitre21666f12007-02-26 14:55:59 -0500252 enum object_type type;
Junio C Hamano9f613dd2006-09-15 13:30:02 -0700253 int eaten;
Stefan Beller8e4b0b62018-06-28 18:22:19 -0700254 const struct object_id *repl = lookup_replace_object(r, oid);
Jeff Kingccdc6032012-01-05 16:00:01 -0500255 void *buffer;
256 struct object *obj;
Junio C Hamano9f613dd2006-09-15 13:30:02 -0700257
Jeff Kingd0229ab2019-06-20 03:41:14 -0400258 obj = lookup_object(r, oid);
Jeff Kingccdc6032012-01-05 16:00:01 -0500259 if (obj && obj->parsed)
260 return obj;
261
Stefan Bellerd1a69022018-11-13 16:12:49 -0800262 if ((obj && obj->type == OBJ_BLOB && repo_has_object_file(r, oid)) ||
263 (!obj && repo_has_object_file(r, oid) &&
Stefan Beller8e4b0b62018-06-28 18:22:19 -0700264 oid_object_info(r, oid, NULL) == OBJ_BLOB)) {
Matheus Tavaresb98d1882020-01-30 17:32:23 -0300265 if (check_object_signature(r, repl, NULL, 0, NULL) < 0) {
Jeff King01f8d592019-01-07 03:40:34 -0500266 error(_("hash mismatch %s"), oid_to_hex(oid));
Nguyễn Thái Ngọc Duy090ea122012-03-07 17:54:18 +0700267 return NULL;
268 }
Stefan Beller8e4b0b62018-06-28 18:22:19 -0700269 parse_blob_buffer(lookup_blob(r, oid), NULL, 0);
Jeff Kingd0229ab2019-06-20 03:41:14 -0400270 return lookup_object(r, oid);
Nguyễn Thái Ngọc Duy090ea122012-03-07 17:54:18 +0700271 }
272
Stefan Bellerd1a69022018-11-13 16:12:49 -0800273 buffer = repo_read_object_file(r, oid, &type, &size);
Junio C Hamanoc4584ae2005-06-27 03:33:33 -0700274 if (buffer) {
Matheus Tavaresb98d1882020-01-30 17:32:23 -0300275 if (check_object_signature(r, repl, buffer, size,
276 type_name(type)) < 0) {
Carlos Rica0b1f1132007-05-25 03:46:22 +0200277 free(buffer);
Jeff King01f8d592019-01-07 03:40:34 -0500278 error(_("hash mismatch %s"), oid_to_hex(repl));
Linus Torvaldsacdeec62007-03-20 10:05:20 -0700279 return NULL;
280 }
Junio C Hamano9f613dd2006-09-15 13:30:02 -0700281
Stefan Beller8e4b0b62018-06-28 18:22:19 -0700282 obj = parse_object_buffer(r, oid, type, size,
Stefan Beller1ec5bfd2018-06-28 18:21:53 -0700283 buffer, &eaten);
Junio C Hamano9f613dd2006-09-15 13:30:02 -0700284 if (!eaten)
285 free(buffer);
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400286 return obj;
Daniel Barkalowe9eefa62005-04-28 07:46:33 -0700287 }
288 return NULL;
289}
barkalow@iabervon.org66e481b2005-08-02 19:45:48 -0400290
291struct object_list *object_list_insert(struct object *item,
292 struct object_list **list_p)
293{
294 struct object_list *new_list = xmalloc(sizeof(struct object_list));
Jared Hance55b4e9e2010-09-05 15:36:33 -0400295 new_list->item = item;
296 new_list->next = *list_p;
297 *list_p = new_list;
298 return new_list;
barkalow@iabervon.org66e481b2005-08-02 19:45:48 -0400299}
300
barkalow@iabervon.org66e481b2005-08-02 19:45:48 -0400301int object_list_contains(struct object_list *list, struct object *obj)
302{
303 while (list) {
304 if (list->item == obj)
305 return 1;
306 list = list->next;
307 }
308 return 0;
309}
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700310
Jeff Kingacac50d2020-02-12 21:16:33 -0500311void object_list_free(struct object_list **list)
312{
313 while (*list) {
314 struct object_list *p = *list;
315 *list = p->next;
316 free(p);
317 }
318}
319
Michael Haggerty31faeb22013-05-25 11:08:14 +0200320/*
321 * A zero-length string to which object_array_entry::name can be
322 * initialized without requiring a malloc/free.
323 */
324static char object_array_slopbuf[1];
325
Jeff King9e0c3c42014-10-15 18:42:57 -0400326void add_object_array_with_path(struct object *obj, const char *name,
327 struct object_array *array,
328 unsigned mode, const char *path)
Martin Koeglere5709a42007-04-22 18:43:58 +0200329{
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700330 unsigned nr = array->nr;
331 unsigned alloc = array->alloc;
332 struct object_array_entry *objects = array->objects;
Michael Haggerty31faeb22013-05-25 11:08:14 +0200333 struct object_array_entry *entry;
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700334
335 if (nr >= alloc) {
336 alloc = (alloc + 32) * 2;
René Scharfe2756ca42014-09-16 20:56:57 +0200337 REALLOC_ARRAY(objects, alloc);
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700338 array->alloc = alloc;
339 array->objects = objects;
340 }
Michael Haggerty31faeb22013-05-25 11:08:14 +0200341 entry = &objects[nr];
342 entry->item = obj;
343 if (!name)
344 entry->name = NULL;
345 else if (!*name)
346 /* Use our own empty string instead of allocating one: */
347 entry->name = object_array_slopbuf;
348 else
349 entry->name = xstrdup(name);
350 entry->mode = mode;
Jeff King9e0c3c42014-10-15 18:42:57 -0400351 if (path)
352 entry->path = xstrdup(path);
353 else
354 entry->path = NULL;
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700355 array->nr = ++nr;
356}
Junio C Hamanob2a6d1c2009-01-17 22:27:08 -0800357
Michael J Gruberafa15f32013-05-10 17:10:16 +0200358void add_object_array(struct object *obj, const char *name, struct object_array *array)
359{
Jeff King189a1222014-10-18 22:03:19 -0400360 add_object_array_with_path(obj, name, array, S_IFINVALID, NULL);
Michael J Gruberafa15f32013-05-10 17:10:16 +0200361}
362
Jeff King68f49232014-10-15 18:34:19 -0400363/*
364 * Free all memory associated with an entry; the result is
365 * in an unspecified state and should not be examined.
366 */
367static void object_array_release_entry(struct object_array_entry *ent)
368{
369 if (ent->name != object_array_slopbuf)
370 free(ent->name);
Jeff King9e0c3c42014-10-15 18:42:57 -0400371 free(ent->path);
Jeff King68f49232014-10-15 18:34:19 -0400372}
373
Martin Ågren71992032017-09-23 01:34:53 +0200374struct object *object_array_pop(struct object_array *array)
375{
376 struct object *ret;
377
378 if (!array->nr)
379 return NULL;
380
381 ret = array->objects[array->nr - 1].item;
382 object_array_release_entry(&array->objects[array->nr - 1]);
383 array->nr--;
384 return ret;
385}
386
Michael Haggertyaeb4a512013-05-25 11:08:08 +0200387void object_array_filter(struct object_array *array,
388 object_array_each_func_t want, void *cb_data)
389{
390 unsigned nr = array->nr, src, dst;
391 struct object_array_entry *objects = array->objects;
392
393 for (src = dst = 0; src < nr; src++) {
394 if (want(&objects[src], cb_data)) {
395 if (src != dst)
396 objects[dst] = objects[src];
397 dst++;
Michael Haggerty31faeb22013-05-25 11:08:14 +0200398 } else {
Jeff King68f49232014-10-15 18:34:19 -0400399 object_array_release_entry(&objects[src]);
Michael Haggertyaeb4a512013-05-25 11:08:08 +0200400 }
401 }
402 array->nr = dst;
403}
404
Jeff King46be8232014-10-15 18:34:34 -0400405void object_array_clear(struct object_array *array)
406{
407 int i;
408 for (i = 0; i < array->nr; i++)
409 object_array_release_entry(&array->objects[i]);
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +0000410 FREE_AND_NULL(array->objects);
Jeff King46be8232014-10-15 18:34:34 -0400411 array->nr = array->alloc = 0;
412}
413
Michael Haggerty15065102013-05-25 11:08:10 +0200414/*
415 * Return true iff array already contains an entry with name.
416 */
417static int contains_name(struct object_array *array, const char *name)
418{
419 unsigned nr = array->nr, i;
420 struct object_array_entry *object = array->objects;
421
422 for (i = 0; i < nr; i++, object++)
423 if (!strcmp(object->name, name))
424 return 1;
425 return 0;
426}
427
Junio C Hamanob2a6d1c2009-01-17 22:27:08 -0800428void object_array_remove_duplicates(struct object_array *array)
429{
Michael Haggerty15065102013-05-25 11:08:10 +0200430 unsigned nr = array->nr, src;
Junio C Hamanob2a6d1c2009-01-17 22:27:08 -0800431 struct object_array_entry *objects = array->objects;
432
Michael Haggerty15065102013-05-25 11:08:10 +0200433 array->nr = 0;
434 for (src = 0; src < nr; src++) {
435 if (!contains_name(array, objects[src].name)) {
436 if (src != array->nr)
437 objects[array->nr] = objects[src];
438 array->nr++;
Michael Haggerty31faeb22013-05-25 11:08:14 +0200439 } else {
Jeff King68f49232014-10-15 18:34:19 -0400440 object_array_release_entry(&objects[src]);
Junio C Hamanob2a6d1c2009-01-17 22:27:08 -0800441 }
Junio C Hamanob2a6d1c2009-01-17 22:27:08 -0800442 }
443}
Heiko Voigtbcc0a3e2012-03-29 09:21:21 +0200444
445void clear_object_flags(unsigned flags)
446{
447 int i;
448
Stefan Beller99bf1152018-05-08 12:37:24 -0700449 for (i=0; i < the_repository->parsed_objects->obj_hash_size; i++) {
450 struct object *obj = the_repository->parsed_objects->obj_hash[i];
Heiko Voigtbcc0a3e2012-03-29 09:21:21 +0200451 if (obj)
452 obj->flags &= ~flags;
453 }
454}
René Scharfe4ad315f2017-12-25 18:44:58 +0100455
456void clear_commit_marks_all(unsigned int flags)
457{
458 int i;
459
Stefan Beller99bf1152018-05-08 12:37:24 -0700460 for (i = 0; i < the_repository->parsed_objects->obj_hash_size; i++) {
461 struct object *obj = the_repository->parsed_objects->obj_hash[i];
René Scharfe4ad315f2017-12-25 18:44:58 +0100462 if (obj && obj->type == OBJ_COMMIT)
463 obj->flags &= ~flags;
464 }
465}
Stefan Beller90c62152018-03-23 18:20:55 +0100466
Stefan Beller99bf1152018-05-08 12:37:24 -0700467struct parsed_object_pool *parsed_object_pool_new(void)
468{
469 struct parsed_object_pool *o = xmalloc(sizeof(*o));
470 memset(o, 0, sizeof(*o));
Stefan Beller14ba97f2018-05-15 14:48:42 -0700471
472 o->blob_state = allocate_alloc_state();
473 o->tree_state = allocate_alloc_state();
474 o->commit_state = allocate_alloc_state();
475 o->tag_state = allocate_alloc_state();
476 o->object_state = allocate_alloc_state();
477
Stefan Bellereee45022018-05-17 15:51:52 -0700478 o->is_shallow = -1;
479 o->shallow_stat = xcalloc(1, sizeof(*o->shallow_stat));
480
Stefan Beller65ea9d42018-06-28 18:22:15 -0700481 o->buffer_slab = allocate_commit_buffer_slab();
482
Stefan Beller99bf1152018-05-08 12:37:24 -0700483 return o;
484}
485
Stefan Beller90c62152018-03-23 18:20:55 +0100486struct raw_object_store *raw_object_store_new(void)
487{
488 struct raw_object_store *o = xmalloc(sizeof(*o));
489
490 memset(o, 0, sizeof(*o));
Stefan Bellera80d72d2018-03-23 18:20:59 +0100491 INIT_LIST_HEAD(&o->packed_git_mru);
Colin Stolleyec485402019-11-27 16:24:53 -0600492 hashmap_init(&o->pack_map, pack_map_entry_cmp, NULL, 0);
Matheus Tavaresb1fc9da2020-01-15 23:39:52 -0300493 pthread_mutex_init(&o->replace_mutex, NULL);
Stefan Beller90c62152018-03-23 18:20:55 +0100494 return o;
495}
Stefan Beller97501e92018-03-23 18:20:58 +0100496
Jeff Kingf0eaf632018-11-12 09:50:39 -0500497static void free_object_directory(struct object_directory *odb)
Stefan Beller97501e92018-03-23 18:20:58 +0100498{
Jeff Kingf0eaf632018-11-12 09:50:39 -0500499 free(odb->path);
René Scharfed4e19e52019-01-06 17:45:39 +0100500 odb_clear_loose_cache(odb);
Jeff King263db402018-11-12 09:48:47 -0500501 free(odb);
Stefan Beller97501e92018-03-23 18:20:58 +0100502}
503
Jeff Kingf0eaf632018-11-12 09:50:39 -0500504static void free_object_directories(struct raw_object_store *o)
Stefan Beller97501e92018-03-23 18:20:58 +0100505{
Jeff Kingf0eaf632018-11-12 09:50:39 -0500506 while (o->odb) {
Jeff King263db402018-11-12 09:48:47 -0500507 struct object_directory *next;
Stefan Beller97501e92018-03-23 18:20:58 +0100508
Jeff Kingf0eaf632018-11-12 09:50:39 -0500509 next = o->odb->next;
510 free_object_directory(o->odb);
511 o->odb = next;
Stefan Beller97501e92018-03-23 18:20:58 +0100512 }
513}
514
Stefan Beller90c62152018-03-23 18:20:55 +0100515void raw_object_store_clear(struct raw_object_store *o)
516{
Stefan Beller90c62152018-03-23 18:20:55 +0100517 FREE_AND_NULL(o->alternate_db);
Stefan Beller7a1dc602018-05-17 11:29:57 -0700518
519 oidmap_free(o->replace_map, 1);
Stefan Bellerd5873072018-05-09 16:40:58 -0700520 FREE_AND_NULL(o->replace_map);
Matheus Tavaresb1fc9da2020-01-15 23:39:52 -0300521 pthread_mutex_destroy(&o->replace_mutex);
Stefan Beller97501e92018-03-23 18:20:58 +0100522
Jonathan Tan85277502018-07-11 15:42:41 -0700523 free_commit_graph(o->commit_graph);
524 o->commit_graph = NULL;
525 o->commit_graph_attempted = 0;
526
Jeff Kingf0eaf632018-11-12 09:50:39 -0500527 free_object_directories(o);
528 o->odb_tail = NULL;
529 o->loaded_alternates = 0;
Stefan Bellera80d72d2018-03-23 18:20:59 +0100530
531 INIT_LIST_HEAD(&o->packed_git_mru);
Derrick Stolee2d511cf2019-05-17 11:41:49 -0700532 close_object_store(o);
Stefan Bellerd0b59862018-03-23 18:21:00 +0100533 o->packed_git = NULL;
Colin Stolleyec485402019-11-27 16:24:53 -0600534
535 hashmap_free(&o->pack_map);
Stefan Beller90c62152018-03-23 18:20:55 +0100536}
Stefan Beller99bf1152018-05-08 12:37:24 -0700537
538void parsed_object_pool_clear(struct parsed_object_pool *o)
539{
540 /*
Stefan Beller99bf1152018-05-08 12:37:24 -0700541 * As objects are allocated in slabs (see alloc.c), we do
542 * not need to free each object, but each slab instead.
Stefan Beller14ba97f2018-05-15 14:48:42 -0700543 *
544 * Before doing so, we need to free any additional memory
545 * the objects may hold.
Stefan Beller99bf1152018-05-08 12:37:24 -0700546 */
Stefan Beller14ba97f2018-05-15 14:48:42 -0700547 unsigned i;
548
549 for (i = 0; i < o->obj_hash_size; i++) {
550 struct object *obj = o->obj_hash[i];
551
552 if (!obj)
553 continue;
554
555 if (obj->type == OBJ_TREE)
556 free_tree_buffer((struct tree*)obj);
557 else if (obj->type == OBJ_COMMIT)
Stefan Beller6a7895f2018-12-14 16:09:40 -0800558 release_commit_memory(o, (struct commit*)obj);
Stefan Beller14ba97f2018-05-15 14:48:42 -0700559 else if (obj->type == OBJ_TAG)
560 release_tag_memory((struct tag*)obj);
561 }
562
563 FREE_AND_NULL(o->obj_hash);
564 o->obj_hash_size = 0;
565
Stefan Beller65ea9d42018-06-28 18:22:15 -0700566 free_commit_buffer_slab(o->buffer_slab);
567 o->buffer_slab = NULL;
568
Stefan Beller14ba97f2018-05-15 14:48:42 -0700569 clear_alloc_state(o->blob_state);
570 clear_alloc_state(o->tree_state);
571 clear_alloc_state(o->commit_state);
572 clear_alloc_state(o->tag_state);
573 clear_alloc_state(o->object_state);
Josh Steadmon96b07102019-02-07 12:05:54 -0800574 stat_validity_clear(o->shallow_stat);
Stefan Beller14ba97f2018-05-15 14:48:42 -0700575 FREE_AND_NULL(o->blob_state);
576 FREE_AND_NULL(o->tree_state);
577 FREE_AND_NULL(o->commit_state);
578 FREE_AND_NULL(o->tag_state);
579 FREE_AND_NULL(o->object_state);
Josh Steadmon96b07102019-02-07 12:05:54 -0800580 FREE_AND_NULL(o->shallow_stat);
Stefan Beller99bf1152018-05-08 12:37:24 -0700581}