blob: e81d47a79cd6eb42fae7c104be0e9ac1e5173e79 [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 Beller90c62152018-03-23 18:20:55 +010010#include "object-store.h"
Stefan Bellerd0b59862018-03-23 18:21:00 +010011#include "packfile.h"
Jonathan Tan85277502018-07-11 15:42:41 -070012#include "commit-graph.h"
Daniel Barkalow175785e2005-04-18 11:39:48 -070013
Linus Torvaldsfc046a72006-06-29 21:38:55 -070014unsigned int get_max_object_index(void)
15{
Stefan Beller99bf1152018-05-08 12:37:24 -070016 return the_repository->parsed_objects->obj_hash_size;
Linus Torvaldsfc046a72006-06-29 21:38:55 -070017}
18
19struct object *get_indexed_object(unsigned int idx)
20{
Stefan Beller99bf1152018-05-08 12:37:24 -070021 return the_repository->parsed_objects->obj_hash[idx];
Linus Torvaldsfc046a72006-06-29 21:38:55 -070022}
Daniel Barkalow175785e2005-04-18 11:39:48 -070023
Nicolas Pitredf843662007-02-26 14:55:58 -050024static const char *object_type_strings[] = {
25 NULL, /* OBJ_NONE = 0 */
26 "commit", /* OBJ_COMMIT = 1 */
27 "tree", /* OBJ_TREE = 2 */
28 "blob", /* OBJ_BLOB = 3 */
29 "tag", /* OBJ_TAG = 4 */
Linus Torvalds885a86a2006-06-14 16:45:13 -070030};
31
Brandon Williamsdebca9d2018-02-14 10:59:24 -080032const char *type_name(unsigned int type)
Nicolas Pitredf843662007-02-26 14:55:58 -050033{
34 if (type >= ARRAY_SIZE(object_type_strings))
35 return NULL;
36 return object_type_strings[type];
37}
38
Johannes Schindelinfe8e3b72014-09-10 15:52:44 +020039int type_from_string_gently(const char *str, ssize_t len, int gentle)
Nicolas Pitredf843662007-02-26 14:55:58 -050040{
41 int i;
42
Johannes Schindelinfe8e3b72014-09-10 15:52:44 +020043 if (len < 0)
44 len = strlen(str);
45
Nicolas Pitredf843662007-02-26 14:55:58 -050046 for (i = 1; i < ARRAY_SIZE(object_type_strings); i++)
Jeff Kingb7994af2015-04-17 10:52:48 -040047 if (!strncmp(str, object_type_strings[i], len) &&
48 object_type_strings[i][len] == '\0')
Nicolas Pitredf843662007-02-26 14:55:58 -050049 return i;
Johannes Schindelinfe8e3b72014-09-10 15:52:44 +020050
51 if (gentle)
52 return -1;
53
Nguyễn Thái Ngọc Duy42246582018-07-21 09:49:33 +020054 die(_("invalid object type \"%s\""), str);
Nicolas Pitredf843662007-02-26 14:55:58 -050055}
56
Michael Haggerty33bef7e2014-02-28 17:29:17 +010057/*
58 * Return a numerical hash value between 0 and n-1 for the object with
59 * the specified sha1. n must be a power of 2. Please note that the
60 * return value is *not* consistent across computer architectures.
61 */
Nicolas Pitre9f36c9b2013-09-10 18:17:12 -040062static unsigned int hash_obj(const unsigned char *sha1, unsigned int n)
Linus Torvalds0556a112006-06-30 11:20:33 -070063{
Karsten Blees039dc712014-07-03 00:20:20 +020064 return sha1hash(sha1) & (n - 1);
Linus Torvalds0556a112006-06-30 11:20:33 -070065}
66
Michael Haggerty33bef7e2014-02-28 17:29:17 +010067/*
68 * Insert obj into the hash table hash, which has length size (which
69 * must be a power of 2). On collisions, simply overflow to the next
70 * empty bucket.
71 */
Linus Torvalds0556a112006-06-30 11:20:33 -070072static void insert_obj_hash(struct object *obj, struct object **hash, unsigned int size)
73{
brian m. carlsoned1c9972015-11-10 02:22:29 +000074 unsigned int j = hash_obj(obj->oid.hash, size);
Linus Torvalds0556a112006-06-30 11:20:33 -070075
76 while (hash[j]) {
77 j++;
78 if (j >= size)
79 j = 0;
80 }
81 hash[j] = obj;
82}
83
Michael Haggerty33bef7e2014-02-28 17:29:17 +010084/*
85 * Look up the record for the given sha1 in the hash map stored in
86 * obj_hash. Return NULL if it was not found.
87 */
Stefan Beller94c09a72018-06-28 18:22:07 -070088struct object *lookup_object(struct repository *r, const unsigned char *sha1)
Daniel Barkalow175785e2005-04-18 11:39:48 -070089{
Jeff King9a414482013-05-01 16:34:50 -040090 unsigned int i, first;
Linus Torvalds0556a112006-06-30 11:20:33 -070091 struct object *obj;
92
Stefan Beller94c09a72018-06-28 18:22:07 -070093 if (!r->parsed_objects->obj_hash)
Linus Torvalds0556a112006-06-30 11:20:33 -070094 return NULL;
95
Stefan Beller94c09a72018-06-28 18:22:07 -070096 first = i = hash_obj(sha1, r->parsed_objects->obj_hash_size);
97 while ((obj = r->parsed_objects->obj_hash[i]) != NULL) {
Jeff Kinge3ff0682018-08-28 17:22:44 -040098 if (hasheq(sha1, obj->oid.hash))
Linus Torvalds0556a112006-06-30 11:20:33 -070099 break;
100 i++;
Stefan Beller94c09a72018-06-28 18:22:07 -0700101 if (i == r->parsed_objects->obj_hash_size)
Linus Torvalds0556a112006-06-30 11:20:33 -0700102 i = 0;
103 }
Jeff King9a414482013-05-01 16:34:50 -0400104 if (obj && i != first) {
105 /*
106 * Move object to where we started to look for it so
107 * that we do not need to walk the hash table the next
108 * time we look for it.
109 */
Stefan Beller94c09a72018-06-28 18:22:07 -0700110 SWAP(r->parsed_objects->obj_hash[i],
111 r->parsed_objects->obj_hash[first]);
Jeff King9a414482013-05-01 16:34:50 -0400112 }
Linus Torvalds0556a112006-06-30 11:20:33 -0700113 return obj;
114}
115
Michael Haggerty33bef7e2014-02-28 17:29:17 +0100116/*
117 * Increase the size of the hash map stored in obj_hash to the next
118 * power of 2 (but at least 32). Copy the existing values to the new
119 * hash map.
120 */
Stefan Beller346a8172018-05-08 12:37:34 -0700121static void grow_object_hash(struct repository *r)
Linus Torvalds0556a112006-06-30 11:20:33 -0700122{
123 int i;
Nicolas Pitre9f36c9b2013-09-10 18:17:12 -0400124 /*
125 * Note that this size must always be power-of-2 to match hash_obj
126 * above.
127 */
Stefan Beller346a8172018-05-08 12:37:34 -0700128 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 -0700129 struct object **new_hash;
130
Jonas Fonsecab3c952f2006-08-28 02:26:07 +0200131 new_hash = xcalloc(new_hash_size, sizeof(struct object *));
Stefan Beller346a8172018-05-08 12:37:34 -0700132 for (i = 0; i < r->parsed_objects->obj_hash_size; i++) {
133 struct object *obj = r->parsed_objects->obj_hash[i];
134
Linus Torvalds0556a112006-06-30 11:20:33 -0700135 if (!obj)
136 continue;
137 insert_obj_hash(obj, new_hash, new_hash_size);
138 }
Stefan Beller346a8172018-05-08 12:37:34 -0700139 free(r->parsed_objects->obj_hash);
140 r->parsed_objects->obj_hash = new_hash;
141 r->parsed_objects->obj_hash_size = new_hash_size;
Daniel Barkalow175785e2005-04-18 11:39:48 -0700142}
143
Stefan Beller341e45e2018-05-08 12:37:35 -0700144void *create_object(struct repository *r, const unsigned char *sha1, void *o)
Daniel Barkalow175785e2005-04-18 11:39:48 -0700145{
Linus Torvalds100c5f32007-04-16 22:11:43 -0700146 struct object *obj = o;
147
Daniel Barkalow175785e2005-04-18 11:39:48 -0700148 obj->parsed = 0;
Linus Torvalds0556a112006-06-30 11:20:33 -0700149 obj->flags = 0;
brian m. carlsoned1c9972015-11-10 02:22:29 +0000150 hashcpy(obj->oid.hash, sha1);
Daniel Barkalow175785e2005-04-18 11:39:48 -0700151
Stefan Beller341e45e2018-05-08 12:37:35 -0700152 if (r->parsed_objects->obj_hash_size - 1 <= r->parsed_objects->nr_objs * 2)
153 grow_object_hash(r);
Johannes Schindelin070879c2006-02-12 02:57:57 +0100154
Stefan Beller341e45e2018-05-08 12:37:35 -0700155 insert_obj_hash(obj, r->parsed_objects->obj_hash,
156 r->parsed_objects->obj_hash_size);
157 r->parsed_objects->nr_objs++;
Linus Torvalds100c5f32007-04-16 22:11:43 -0700158 return obj;
Daniel Barkalow175785e2005-04-18 11:39:48 -0700159}
160
Stefan Bellera962da12018-06-28 18:22:06 -0700161void *object_as_type(struct repository *r, struct object *obj, enum object_type type, int quiet)
Jeff King8ff226a2014-07-13 02:42:03 -0400162{
163 if (obj->type == type)
164 return obj;
165 else if (obj->type == OBJ_NONE) {
Jeff Kingd66bebc2014-07-13 02:42:12 -0400166 if (type == OBJ_COMMIT)
SZEDER Gábor4468d442019-01-27 14:08:32 +0100167 init_commit_node(r, (struct commit *) obj);
168 else
169 obj->type = type;
Jeff King8ff226a2014-07-13 02:42:03 -0400170 return obj;
171 }
172 else {
173 if (!quiet)
Nguyễn Thái Ngọc Duy42246582018-07-21 09:49:33 +0200174 error(_("object %s is a %s, not a %s"),
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000175 oid_to_hex(&obj->oid),
Brandon Williamsdebca9d2018-02-14 10:59:24 -0800176 type_name(obj->type), type_name(type));
Jeff King8ff226a2014-07-13 02:42:03 -0400177 return NULL;
178 }
179}
180
barkalow@iabervon.org66e481b2005-08-02 19:45:48 -0400181struct object *lookup_unknown_object(const unsigned char *sha1)
182{
Stefan Beller5abddd12018-06-28 18:21:52 -0700183 struct object *obj = lookup_object(the_repository, sha1);
Linus Torvalds100c5f32007-04-16 22:11:43 -0700184 if (!obj)
Stefan Beller68f95d32018-05-08 12:37:25 -0700185 obj = create_object(the_repository, sha1,
Stefan Beller13e3fdc2018-05-08 12:37:31 -0700186 alloc_object_node(the_repository));
barkalow@iabervon.org66e481b2005-08-02 19:45:48 -0400187 return obj;
188}
189
Stefan Beller108ed1a2018-06-28 18:22:18 -0700190struct 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 -0700191{
192 struct object *obj;
Stefan Beller8e92e8f2013-07-18 00:09:42 +0200193 *eaten_p = 0;
Junio C Hamano9f613dd2006-09-15 13:30:02 -0700194
Jim Meyeringcc216822007-12-21 11:56:32 +0100195 obj = NULL;
Nicolas Pitre21666f12007-02-26 14:55:59 -0500196 if (type == OBJ_BLOB) {
Stefan Beller108ed1a2018-06-28 18:22:18 -0700197 struct blob *blob = lookup_blob(r, oid);
Jim Meyeringcc216822007-12-21 11:56:32 +0100198 if (blob) {
Martin Koeglerd0b8c9e2008-02-03 22:22:39 +0100199 if (parse_blob_buffer(blob, buffer, size))
200 return NULL;
Jim Meyeringcc216822007-12-21 11:56:32 +0100201 obj = &blob->object;
202 }
Nicolas Pitre21666f12007-02-26 14:55:59 -0500203 } else if (type == OBJ_TREE) {
Stefan Beller108ed1a2018-06-28 18:22:18 -0700204 struct tree *tree = lookup_tree(r, oid);
Jim Meyeringcc216822007-12-21 11:56:32 +0100205 if (tree) {
206 obj = &tree->object;
Junio C Hamano68be2fe2011-11-16 22:04:13 -0800207 if (!tree->buffer)
208 tree->object.parsed = 0;
Jim Meyeringcc216822007-12-21 11:56:32 +0100209 if (!tree->object.parsed) {
Martin Koeglerd0b8c9e2008-02-03 22:22:39 +0100210 if (parse_tree_buffer(tree, buffer, size))
211 return NULL;
Stefan Beller8e92e8f2013-07-18 00:09:42 +0200212 *eaten_p = 1;
Jim Meyeringcc216822007-12-21 11:56:32 +0100213 }
Junio C Hamano9f613dd2006-09-15 13:30:02 -0700214 }
Nicolas Pitre21666f12007-02-26 14:55:59 -0500215 } else if (type == OBJ_COMMIT) {
Stefan Beller108ed1a2018-06-28 18:22:18 -0700216 struct commit *commit = lookup_commit(r, oid);
Jim Meyeringcc216822007-12-21 11:56:32 +0100217 if (commit) {
Stefan Beller108ed1a2018-06-28 18:22:18 -0700218 if (parse_commit_buffer(r, commit, buffer, size, 1))
Martin Koeglerd0b8c9e2008-02-03 22:22:39 +0100219 return NULL;
Stefan Beller108ed1a2018-06-28 18:22:18 -0700220 if (!get_cached_commit_buffer(r, commit, NULL)) {
221 set_commit_buffer(r, commit, buffer, size);
Stefan Beller8e92e8f2013-07-18 00:09:42 +0200222 *eaten_p = 1;
Jim Meyeringcc216822007-12-21 11:56:32 +0100223 }
224 obj = &commit->object;
Junio C Hamano9f613dd2006-09-15 13:30:02 -0700225 }
Nicolas Pitre21666f12007-02-26 14:55:59 -0500226 } else if (type == OBJ_TAG) {
Stefan Beller108ed1a2018-06-28 18:22:18 -0700227 struct tag *tag = lookup_tag(r, oid);
Jim Meyeringcc216822007-12-21 11:56:32 +0100228 if (tag) {
Stefan Beller108ed1a2018-06-28 18:22:18 -0700229 if (parse_tag_buffer(r, tag, buffer, size))
Martin Koeglerd0b8c9e2008-02-03 22:22:39 +0100230 return NULL;
Jim Meyeringcc216822007-12-21 11:56:32 +0100231 obj = &tag->object;
232 }
Junio C Hamano9f613dd2006-09-15 13:30:02 -0700233 } else {
Nguyễn Thái Ngọc Duy42246582018-07-21 09:49:33 +0200234 warning(_("object %s has unknown type id %d"), oid_to_hex(oid), type);
Junio C Hamano9f613dd2006-09-15 13:30:02 -0700235 obj = NULL;
236 }
Junio C Hamano9f613dd2006-09-15 13:30:02 -0700237 return obj;
238}
239
brian m. carlsonc251c832017-05-06 22:10:38 +0000240struct object *parse_object_or_die(const struct object_id *oid,
Jeff King75a95492013-03-17 04:22:36 -0400241 const char *name)
242{
Stefan Beller109cd762018-06-28 18:21:51 -0700243 struct object *o = parse_object(the_repository, oid);
Jeff King75a95492013-03-17 04:22:36 -0400244 if (o)
245 return o;
246
brian m. carlsonc251c832017-05-06 22:10:38 +0000247 die(_("unable to parse object: %s"), name ? name : oid_to_hex(oid));
Jeff King75a95492013-03-17 04:22:36 -0400248}
249
Stefan Beller8e4b0b62018-06-28 18:22:19 -0700250struct object *parse_object(struct repository *r, const struct object_id *oid)
Daniel Barkalowe9eefa62005-04-28 07:46:33 -0700251{
Junio C Hamanoc4584ae2005-06-27 03:33:33 -0700252 unsigned long size;
Nicolas Pitre21666f12007-02-26 14:55:59 -0500253 enum object_type type;
Junio C Hamano9f613dd2006-09-15 13:30:02 -0700254 int eaten;
Stefan Beller8e4b0b62018-06-28 18:22:19 -0700255 const struct object_id *repl = lookup_replace_object(r, oid);
Jeff Kingccdc6032012-01-05 16:00:01 -0500256 void *buffer;
257 struct object *obj;
Junio C Hamano9f613dd2006-09-15 13:30:02 -0700258
Stefan Beller8e4b0b62018-06-28 18:22:19 -0700259 obj = lookup_object(r, oid->hash);
Jeff Kingccdc6032012-01-05 16:00:01 -0500260 if (obj && obj->parsed)
261 return obj;
262
Stefan Bellerd1a69022018-11-13 16:12:49 -0800263 if ((obj && obj->type == OBJ_BLOB && repo_has_object_file(r, oid)) ||
264 (!obj && repo_has_object_file(r, oid) &&
Stefan Beller8e4b0b62018-06-28 18:22:19 -0700265 oid_object_info(r, oid, NULL) == OBJ_BLOB)) {
brian m. carlsonb383a132018-03-12 02:27:54 +0000266 if (check_object_signature(repl, NULL, 0, NULL) < 0) {
Jeff King01f8d592019-01-07 03:40:34 -0500267 error(_("hash mismatch %s"), oid_to_hex(oid));
Nguyễn Thái Ngọc Duy090ea122012-03-07 17:54:18 +0700268 return NULL;
269 }
Stefan Beller8e4b0b62018-06-28 18:22:19 -0700270 parse_blob_buffer(lookup_blob(r, oid), NULL, 0);
271 return lookup_object(r, oid->hash);
Nguyễn Thái Ngọc Duy090ea122012-03-07 17:54:18 +0700272 }
273
Stefan Bellerd1a69022018-11-13 16:12:49 -0800274 buffer = repo_read_object_file(r, oid, &type, &size);
Junio C Hamanoc4584ae2005-06-27 03:33:33 -0700275 if (buffer) {
brian m. carlsonb383a132018-03-12 02:27:54 +0000276 if (check_object_signature(repl, buffer, size, 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
Michael Haggerty31faeb22013-05-25 11:08:14 +0200311/*
312 * A zero-length string to which object_array_entry::name can be
313 * initialized without requiring a malloc/free.
314 */
315static char object_array_slopbuf[1];
316
Jeff King9e0c3c42014-10-15 18:42:57 -0400317void add_object_array_with_path(struct object *obj, const char *name,
318 struct object_array *array,
319 unsigned mode, const char *path)
Martin Koeglere5709a42007-04-22 18:43:58 +0200320{
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700321 unsigned nr = array->nr;
322 unsigned alloc = array->alloc;
323 struct object_array_entry *objects = array->objects;
Michael Haggerty31faeb22013-05-25 11:08:14 +0200324 struct object_array_entry *entry;
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700325
326 if (nr >= alloc) {
327 alloc = (alloc + 32) * 2;
René Scharfe2756ca42014-09-16 20:56:57 +0200328 REALLOC_ARRAY(objects, alloc);
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700329 array->alloc = alloc;
330 array->objects = objects;
331 }
Michael Haggerty31faeb22013-05-25 11:08:14 +0200332 entry = &objects[nr];
333 entry->item = obj;
334 if (!name)
335 entry->name = NULL;
336 else if (!*name)
337 /* Use our own empty string instead of allocating one: */
338 entry->name = object_array_slopbuf;
339 else
340 entry->name = xstrdup(name);
341 entry->mode = mode;
Jeff King9e0c3c42014-10-15 18:42:57 -0400342 if (path)
343 entry->path = xstrdup(path);
344 else
345 entry->path = NULL;
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700346 array->nr = ++nr;
347}
Junio C Hamanob2a6d1c2009-01-17 22:27:08 -0800348
Michael J Gruberafa15f32013-05-10 17:10:16 +0200349void add_object_array(struct object *obj, const char *name, struct object_array *array)
350{
Jeff King189a1222014-10-18 22:03:19 -0400351 add_object_array_with_path(obj, name, array, S_IFINVALID, NULL);
Michael J Gruberafa15f32013-05-10 17:10:16 +0200352}
353
Jeff King68f49232014-10-15 18:34:19 -0400354/*
355 * Free all memory associated with an entry; the result is
356 * in an unspecified state and should not be examined.
357 */
358static void object_array_release_entry(struct object_array_entry *ent)
359{
360 if (ent->name != object_array_slopbuf)
361 free(ent->name);
Jeff King9e0c3c42014-10-15 18:42:57 -0400362 free(ent->path);
Jeff King68f49232014-10-15 18:34:19 -0400363}
364
Martin Ågren71992032017-09-23 01:34:53 +0200365struct object *object_array_pop(struct object_array *array)
366{
367 struct object *ret;
368
369 if (!array->nr)
370 return NULL;
371
372 ret = array->objects[array->nr - 1].item;
373 object_array_release_entry(&array->objects[array->nr - 1]);
374 array->nr--;
375 return ret;
376}
377
Michael Haggertyaeb4a512013-05-25 11:08:08 +0200378void object_array_filter(struct object_array *array,
379 object_array_each_func_t want, void *cb_data)
380{
381 unsigned nr = array->nr, src, dst;
382 struct object_array_entry *objects = array->objects;
383
384 for (src = dst = 0; src < nr; src++) {
385 if (want(&objects[src], cb_data)) {
386 if (src != dst)
387 objects[dst] = objects[src];
388 dst++;
Michael Haggerty31faeb22013-05-25 11:08:14 +0200389 } else {
Jeff King68f49232014-10-15 18:34:19 -0400390 object_array_release_entry(&objects[src]);
Michael Haggertyaeb4a512013-05-25 11:08:08 +0200391 }
392 }
393 array->nr = dst;
394}
395
Jeff King46be8232014-10-15 18:34:34 -0400396void object_array_clear(struct object_array *array)
397{
398 int i;
399 for (i = 0; i < array->nr; i++)
400 object_array_release_entry(&array->objects[i]);
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +0000401 FREE_AND_NULL(array->objects);
Jeff King46be8232014-10-15 18:34:34 -0400402 array->nr = array->alloc = 0;
403}
404
Michael Haggerty15065102013-05-25 11:08:10 +0200405/*
406 * Return true iff array already contains an entry with name.
407 */
408static int contains_name(struct object_array *array, const char *name)
409{
410 unsigned nr = array->nr, i;
411 struct object_array_entry *object = array->objects;
412
413 for (i = 0; i < nr; i++, object++)
414 if (!strcmp(object->name, name))
415 return 1;
416 return 0;
417}
418
Junio C Hamanob2a6d1c2009-01-17 22:27:08 -0800419void object_array_remove_duplicates(struct object_array *array)
420{
Michael Haggerty15065102013-05-25 11:08:10 +0200421 unsigned nr = array->nr, src;
Junio C Hamanob2a6d1c2009-01-17 22:27:08 -0800422 struct object_array_entry *objects = array->objects;
423
Michael Haggerty15065102013-05-25 11:08:10 +0200424 array->nr = 0;
425 for (src = 0; src < nr; src++) {
426 if (!contains_name(array, objects[src].name)) {
427 if (src != array->nr)
428 objects[array->nr] = objects[src];
429 array->nr++;
Michael Haggerty31faeb22013-05-25 11:08:14 +0200430 } else {
Jeff King68f49232014-10-15 18:34:19 -0400431 object_array_release_entry(&objects[src]);
Junio C Hamanob2a6d1c2009-01-17 22:27:08 -0800432 }
Junio C Hamanob2a6d1c2009-01-17 22:27:08 -0800433 }
434}
Heiko Voigtbcc0a3e2012-03-29 09:21:21 +0200435
436void clear_object_flags(unsigned flags)
437{
438 int i;
439
Stefan Beller99bf1152018-05-08 12:37:24 -0700440 for (i=0; i < the_repository->parsed_objects->obj_hash_size; i++) {
441 struct object *obj = the_repository->parsed_objects->obj_hash[i];
Heiko Voigtbcc0a3e2012-03-29 09:21:21 +0200442 if (obj)
443 obj->flags &= ~flags;
444 }
445}
René Scharfe4ad315f2017-12-25 18:44:58 +0100446
447void clear_commit_marks_all(unsigned int flags)
448{
449 int i;
450
Stefan Beller99bf1152018-05-08 12:37:24 -0700451 for (i = 0; i < the_repository->parsed_objects->obj_hash_size; i++) {
452 struct object *obj = the_repository->parsed_objects->obj_hash[i];
René Scharfe4ad315f2017-12-25 18:44:58 +0100453 if (obj && obj->type == OBJ_COMMIT)
454 obj->flags &= ~flags;
455 }
456}
Stefan Beller90c62152018-03-23 18:20:55 +0100457
Stefan Beller99bf1152018-05-08 12:37:24 -0700458struct parsed_object_pool *parsed_object_pool_new(void)
459{
460 struct parsed_object_pool *o = xmalloc(sizeof(*o));
461 memset(o, 0, sizeof(*o));
Stefan Beller14ba97f2018-05-15 14:48:42 -0700462
463 o->blob_state = allocate_alloc_state();
464 o->tree_state = allocate_alloc_state();
465 o->commit_state = allocate_alloc_state();
466 o->tag_state = allocate_alloc_state();
467 o->object_state = allocate_alloc_state();
468
Stefan Bellereee45022018-05-17 15:51:52 -0700469 o->is_shallow = -1;
470 o->shallow_stat = xcalloc(1, sizeof(*o->shallow_stat));
471
Stefan Beller65ea9d42018-06-28 18:22:15 -0700472 o->buffer_slab = allocate_commit_buffer_slab();
473
Stefan Beller99bf1152018-05-08 12:37:24 -0700474 return o;
475}
476
Stefan Beller90c62152018-03-23 18:20:55 +0100477struct raw_object_store *raw_object_store_new(void)
478{
479 struct raw_object_store *o = xmalloc(sizeof(*o));
480
481 memset(o, 0, sizeof(*o));
Stefan Bellera80d72d2018-03-23 18:20:59 +0100482 INIT_LIST_HEAD(&o->packed_git_mru);
Stefan Beller90c62152018-03-23 18:20:55 +0100483 return o;
484}
Stefan Beller97501e92018-03-23 18:20:58 +0100485
Jeff Kingf0eaf632018-11-12 09:50:39 -0500486static void free_object_directory(struct object_directory *odb)
Stefan Beller97501e92018-03-23 18:20:58 +0100487{
Jeff Kingf0eaf632018-11-12 09:50:39 -0500488 free(odb->path);
René Scharfed4e19e52019-01-06 17:45:39 +0100489 odb_clear_loose_cache(odb);
Jeff King263db402018-11-12 09:48:47 -0500490 free(odb);
Stefan Beller97501e92018-03-23 18:20:58 +0100491}
492
Jeff Kingf0eaf632018-11-12 09:50:39 -0500493static void free_object_directories(struct raw_object_store *o)
Stefan Beller97501e92018-03-23 18:20:58 +0100494{
Jeff Kingf0eaf632018-11-12 09:50:39 -0500495 while (o->odb) {
Jeff King263db402018-11-12 09:48:47 -0500496 struct object_directory *next;
Stefan Beller97501e92018-03-23 18:20:58 +0100497
Jeff Kingf0eaf632018-11-12 09:50:39 -0500498 next = o->odb->next;
499 free_object_directory(o->odb);
500 o->odb = next;
Stefan Beller97501e92018-03-23 18:20:58 +0100501 }
502}
503
Stefan Beller90c62152018-03-23 18:20:55 +0100504void raw_object_store_clear(struct raw_object_store *o)
505{
Stefan Beller90c62152018-03-23 18:20:55 +0100506 FREE_AND_NULL(o->alternate_db);
Stefan Beller7a1dc602018-05-17 11:29:57 -0700507
508 oidmap_free(o->replace_map, 1);
Stefan Bellerd5873072018-05-09 16:40:58 -0700509 FREE_AND_NULL(o->replace_map);
Stefan Beller97501e92018-03-23 18:20:58 +0100510
Jonathan Tan85277502018-07-11 15:42:41 -0700511 free_commit_graph(o->commit_graph);
512 o->commit_graph = NULL;
513 o->commit_graph_attempted = 0;
514
Jeff Kingf0eaf632018-11-12 09:50:39 -0500515 free_object_directories(o);
516 o->odb_tail = NULL;
517 o->loaded_alternates = 0;
Stefan Bellera80d72d2018-03-23 18:20:59 +0100518
519 INIT_LIST_HEAD(&o->packed_git_mru);
Stefan Bellerd0b59862018-03-23 18:21:00 +0100520 close_all_packs(o);
521 o->packed_git = NULL;
Stefan Beller90c62152018-03-23 18:20:55 +0100522}
Stefan Beller99bf1152018-05-08 12:37:24 -0700523
524void parsed_object_pool_clear(struct parsed_object_pool *o)
525{
526 /*
Stefan Beller99bf1152018-05-08 12:37:24 -0700527 * As objects are allocated in slabs (see alloc.c), we do
528 * not need to free each object, but each slab instead.
Stefan Beller14ba97f2018-05-15 14:48:42 -0700529 *
530 * Before doing so, we need to free any additional memory
531 * the objects may hold.
Stefan Beller99bf1152018-05-08 12:37:24 -0700532 */
Stefan Beller14ba97f2018-05-15 14:48:42 -0700533 unsigned i;
534
535 for (i = 0; i < o->obj_hash_size; i++) {
536 struct object *obj = o->obj_hash[i];
537
538 if (!obj)
539 continue;
540
541 if (obj->type == OBJ_TREE)
542 free_tree_buffer((struct tree*)obj);
543 else if (obj->type == OBJ_COMMIT)
Stefan Beller6a7895f2018-12-14 16:09:40 -0800544 release_commit_memory(o, (struct commit*)obj);
Stefan Beller14ba97f2018-05-15 14:48:42 -0700545 else if (obj->type == OBJ_TAG)
546 release_tag_memory((struct tag*)obj);
547 }
548
549 FREE_AND_NULL(o->obj_hash);
550 o->obj_hash_size = 0;
551
Stefan Beller65ea9d42018-06-28 18:22:15 -0700552 free_commit_buffer_slab(o->buffer_slab);
553 o->buffer_slab = NULL;
554
Stefan Beller14ba97f2018-05-15 14:48:42 -0700555 clear_alloc_state(o->blob_state);
556 clear_alloc_state(o->tree_state);
557 clear_alloc_state(o->commit_state);
558 clear_alloc_state(o->tag_state);
559 clear_alloc_state(o->object_state);
Josh Steadmon96b07102019-02-07 12:05:54 -0800560 stat_validity_clear(o->shallow_stat);
Stefan Beller14ba97f2018-05-15 14:48:42 -0700561 FREE_AND_NULL(o->blob_state);
562 FREE_AND_NULL(o->tree_state);
563 FREE_AND_NULL(o->commit_state);
564 FREE_AND_NULL(o->tag_state);
565 FREE_AND_NULL(o->object_state);
Josh Steadmon96b07102019-02-07 12:05:54 -0800566 FREE_AND_NULL(o->shallow_stat);
Stefan Beller99bf1152018-05-08 12:37:24 -0700567}