blob: 3b8b8c55c9a8e7e2961d9911f1ec4fd70730f07f [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)) {
brian m. carlsonb383a132018-03-12 02:27:54 +0000265 if (check_object_signature(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) {
brian m. carlsonb383a132018-03-12 02:27:54 +0000275 if (check_object_signature(repl, buffer, size, type_name(type)) < 0) {
Carlos Rica0b1f1132007-05-25 03:46:22 +0200276 free(buffer);
Jeff King01f8d592019-01-07 03:40:34 -0500277 error(_("hash mismatch %s"), oid_to_hex(repl));
Linus Torvaldsacdeec62007-03-20 10:05:20 -0700278 return NULL;
279 }
Junio C Hamano9f613dd2006-09-15 13:30:02 -0700280
Stefan Beller8e4b0b62018-06-28 18:22:19 -0700281 obj = parse_object_buffer(r, oid, type, size,
Stefan Beller1ec5bfd2018-06-28 18:21:53 -0700282 buffer, &eaten);
Junio C Hamano9f613dd2006-09-15 13:30:02 -0700283 if (!eaten)
284 free(buffer);
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400285 return obj;
Daniel Barkalowe9eefa62005-04-28 07:46:33 -0700286 }
287 return NULL;
288}
barkalow@iabervon.org66e481b2005-08-02 19:45:48 -0400289
290struct object_list *object_list_insert(struct object *item,
291 struct object_list **list_p)
292{
293 struct object_list *new_list = xmalloc(sizeof(struct object_list));
Jared Hance55b4e9e2010-09-05 15:36:33 -0400294 new_list->item = item;
295 new_list->next = *list_p;
296 *list_p = new_list;
297 return new_list;
barkalow@iabervon.org66e481b2005-08-02 19:45:48 -0400298}
299
barkalow@iabervon.org66e481b2005-08-02 19:45:48 -0400300int object_list_contains(struct object_list *list, struct object *obj)
301{
302 while (list) {
303 if (list->item == obj)
304 return 1;
305 list = list->next;
306 }
307 return 0;
308}
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700309
Michael Haggerty31faeb22013-05-25 11:08:14 +0200310/*
311 * A zero-length string to which object_array_entry::name can be
312 * initialized without requiring a malloc/free.
313 */
314static char object_array_slopbuf[1];
315
Jeff King9e0c3c42014-10-15 18:42:57 -0400316void add_object_array_with_path(struct object *obj, const char *name,
317 struct object_array *array,
318 unsigned mode, const char *path)
Martin Koeglere5709a42007-04-22 18:43:58 +0200319{
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700320 unsigned nr = array->nr;
321 unsigned alloc = array->alloc;
322 struct object_array_entry *objects = array->objects;
Michael Haggerty31faeb22013-05-25 11:08:14 +0200323 struct object_array_entry *entry;
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700324
325 if (nr >= alloc) {
326 alloc = (alloc + 32) * 2;
René Scharfe2756ca42014-09-16 20:56:57 +0200327 REALLOC_ARRAY(objects, alloc);
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700328 array->alloc = alloc;
329 array->objects = objects;
330 }
Michael Haggerty31faeb22013-05-25 11:08:14 +0200331 entry = &objects[nr];
332 entry->item = obj;
333 if (!name)
334 entry->name = NULL;
335 else if (!*name)
336 /* Use our own empty string instead of allocating one: */
337 entry->name = object_array_slopbuf;
338 else
339 entry->name = xstrdup(name);
340 entry->mode = mode;
Jeff King9e0c3c42014-10-15 18:42:57 -0400341 if (path)
342 entry->path = xstrdup(path);
343 else
344 entry->path = NULL;
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700345 array->nr = ++nr;
346}
Junio C Hamanob2a6d1c2009-01-17 22:27:08 -0800347
Michael J Gruberafa15f32013-05-10 17:10:16 +0200348void add_object_array(struct object *obj, const char *name, struct object_array *array)
349{
Jeff King189a1222014-10-18 22:03:19 -0400350 add_object_array_with_path(obj, name, array, S_IFINVALID, NULL);
Michael J Gruberafa15f32013-05-10 17:10:16 +0200351}
352
Jeff King68f49232014-10-15 18:34:19 -0400353/*
354 * Free all memory associated with an entry; the result is
355 * in an unspecified state and should not be examined.
356 */
357static void object_array_release_entry(struct object_array_entry *ent)
358{
359 if (ent->name != object_array_slopbuf)
360 free(ent->name);
Jeff King9e0c3c42014-10-15 18:42:57 -0400361 free(ent->path);
Jeff King68f49232014-10-15 18:34:19 -0400362}
363
Martin Ågren71992032017-09-23 01:34:53 +0200364struct object *object_array_pop(struct object_array *array)
365{
366 struct object *ret;
367
368 if (!array->nr)
369 return NULL;
370
371 ret = array->objects[array->nr - 1].item;
372 object_array_release_entry(&array->objects[array->nr - 1]);
373 array->nr--;
374 return ret;
375}
376
Michael Haggertyaeb4a512013-05-25 11:08:08 +0200377void object_array_filter(struct object_array *array,
378 object_array_each_func_t want, void *cb_data)
379{
380 unsigned nr = array->nr, src, dst;
381 struct object_array_entry *objects = array->objects;
382
383 for (src = dst = 0; src < nr; src++) {
384 if (want(&objects[src], cb_data)) {
385 if (src != dst)
386 objects[dst] = objects[src];
387 dst++;
Michael Haggerty31faeb22013-05-25 11:08:14 +0200388 } else {
Jeff King68f49232014-10-15 18:34:19 -0400389 object_array_release_entry(&objects[src]);
Michael Haggertyaeb4a512013-05-25 11:08:08 +0200390 }
391 }
392 array->nr = dst;
393}
394
Jeff King46be8232014-10-15 18:34:34 -0400395void object_array_clear(struct object_array *array)
396{
397 int i;
398 for (i = 0; i < array->nr; i++)
399 object_array_release_entry(&array->objects[i]);
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +0000400 FREE_AND_NULL(array->objects);
Jeff King46be8232014-10-15 18:34:34 -0400401 array->nr = array->alloc = 0;
402}
403
Michael Haggerty15065102013-05-25 11:08:10 +0200404/*
405 * Return true iff array already contains an entry with name.
406 */
407static int contains_name(struct object_array *array, const char *name)
408{
409 unsigned nr = array->nr, i;
410 struct object_array_entry *object = array->objects;
411
412 for (i = 0; i < nr; i++, object++)
413 if (!strcmp(object->name, name))
414 return 1;
415 return 0;
416}
417
Junio C Hamanob2a6d1c2009-01-17 22:27:08 -0800418void object_array_remove_duplicates(struct object_array *array)
419{
Michael Haggerty15065102013-05-25 11:08:10 +0200420 unsigned nr = array->nr, src;
Junio C Hamanob2a6d1c2009-01-17 22:27:08 -0800421 struct object_array_entry *objects = array->objects;
422
Michael Haggerty15065102013-05-25 11:08:10 +0200423 array->nr = 0;
424 for (src = 0; src < nr; src++) {
425 if (!contains_name(array, objects[src].name)) {
426 if (src != array->nr)
427 objects[array->nr] = objects[src];
428 array->nr++;
Michael Haggerty31faeb22013-05-25 11:08:14 +0200429 } else {
Jeff King68f49232014-10-15 18:34:19 -0400430 object_array_release_entry(&objects[src]);
Junio C Hamanob2a6d1c2009-01-17 22:27:08 -0800431 }
Junio C Hamanob2a6d1c2009-01-17 22:27:08 -0800432 }
433}
Heiko Voigtbcc0a3e2012-03-29 09:21:21 +0200434
435void clear_object_flags(unsigned flags)
436{
437 int i;
438
Stefan Beller99bf1152018-05-08 12:37:24 -0700439 for (i=0; i < the_repository->parsed_objects->obj_hash_size; i++) {
440 struct object *obj = the_repository->parsed_objects->obj_hash[i];
Heiko Voigtbcc0a3e2012-03-29 09:21:21 +0200441 if (obj)
442 obj->flags &= ~flags;
443 }
444}
René Scharfe4ad315f2017-12-25 18:44:58 +0100445
446void clear_commit_marks_all(unsigned int flags)
447{
448 int i;
449
Stefan Beller99bf1152018-05-08 12:37:24 -0700450 for (i = 0; i < the_repository->parsed_objects->obj_hash_size; i++) {
451 struct object *obj = the_repository->parsed_objects->obj_hash[i];
René Scharfe4ad315f2017-12-25 18:44:58 +0100452 if (obj && obj->type == OBJ_COMMIT)
453 obj->flags &= ~flags;
454 }
455}
Stefan Beller90c62152018-03-23 18:20:55 +0100456
Stefan Beller99bf1152018-05-08 12:37:24 -0700457struct parsed_object_pool *parsed_object_pool_new(void)
458{
459 struct parsed_object_pool *o = xmalloc(sizeof(*o));
460 memset(o, 0, sizeof(*o));
Stefan Beller14ba97f2018-05-15 14:48:42 -0700461
462 o->blob_state = allocate_alloc_state();
463 o->tree_state = allocate_alloc_state();
464 o->commit_state = allocate_alloc_state();
465 o->tag_state = allocate_alloc_state();
466 o->object_state = allocate_alloc_state();
467
Stefan Bellereee45022018-05-17 15:51:52 -0700468 o->is_shallow = -1;
469 o->shallow_stat = xcalloc(1, sizeof(*o->shallow_stat));
470
Stefan Beller65ea9d42018-06-28 18:22:15 -0700471 o->buffer_slab = allocate_commit_buffer_slab();
472
Stefan Beller99bf1152018-05-08 12:37:24 -0700473 return o;
474}
475
Stefan Beller90c62152018-03-23 18:20:55 +0100476struct raw_object_store *raw_object_store_new(void)
477{
478 struct raw_object_store *o = xmalloc(sizeof(*o));
479
480 memset(o, 0, sizeof(*o));
Stefan Bellera80d72d2018-03-23 18:20:59 +0100481 INIT_LIST_HEAD(&o->packed_git_mru);
Stefan Beller90c62152018-03-23 18:20:55 +0100482 return o;
483}
Stefan Beller97501e92018-03-23 18:20:58 +0100484
Jeff Kingf0eaf632018-11-12 09:50:39 -0500485static void free_object_directory(struct object_directory *odb)
Stefan Beller97501e92018-03-23 18:20:58 +0100486{
Jeff Kingf0eaf632018-11-12 09:50:39 -0500487 free(odb->path);
René Scharfed4e19e52019-01-06 17:45:39 +0100488 odb_clear_loose_cache(odb);
Jeff King263db402018-11-12 09:48:47 -0500489 free(odb);
Stefan Beller97501e92018-03-23 18:20:58 +0100490}
491
Jeff Kingf0eaf632018-11-12 09:50:39 -0500492static void free_object_directories(struct raw_object_store *o)
Stefan Beller97501e92018-03-23 18:20:58 +0100493{
Jeff Kingf0eaf632018-11-12 09:50:39 -0500494 while (o->odb) {
Jeff King263db402018-11-12 09:48:47 -0500495 struct object_directory *next;
Stefan Beller97501e92018-03-23 18:20:58 +0100496
Jeff Kingf0eaf632018-11-12 09:50:39 -0500497 next = o->odb->next;
498 free_object_directory(o->odb);
499 o->odb = next;
Stefan Beller97501e92018-03-23 18:20:58 +0100500 }
501}
502
Stefan Beller90c62152018-03-23 18:20:55 +0100503void raw_object_store_clear(struct raw_object_store *o)
504{
Stefan Beller90c62152018-03-23 18:20:55 +0100505 FREE_AND_NULL(o->alternate_db);
Stefan Beller7a1dc602018-05-17 11:29:57 -0700506
507 oidmap_free(o->replace_map, 1);
Stefan Bellerd5873072018-05-09 16:40:58 -0700508 FREE_AND_NULL(o->replace_map);
Stefan Beller97501e92018-03-23 18:20:58 +0100509
Jonathan Tan85277502018-07-11 15:42:41 -0700510 free_commit_graph(o->commit_graph);
511 o->commit_graph = NULL;
512 o->commit_graph_attempted = 0;
513
Jeff Kingf0eaf632018-11-12 09:50:39 -0500514 free_object_directories(o);
515 o->odb_tail = NULL;
516 o->loaded_alternates = 0;
Stefan Bellera80d72d2018-03-23 18:20:59 +0100517
518 INIT_LIST_HEAD(&o->packed_git_mru);
Derrick Stolee2d511cf2019-05-17 11:41:49 -0700519 close_object_store(o);
Stefan Bellerd0b59862018-03-23 18:21:00 +0100520 o->packed_git = NULL;
Stefan Beller90c62152018-03-23 18:20:55 +0100521}
Stefan Beller99bf1152018-05-08 12:37:24 -0700522
523void parsed_object_pool_clear(struct parsed_object_pool *o)
524{
525 /*
Stefan Beller99bf1152018-05-08 12:37:24 -0700526 * As objects are allocated in slabs (see alloc.c), we do
527 * not need to free each object, but each slab instead.
Stefan Beller14ba97f2018-05-15 14:48:42 -0700528 *
529 * Before doing so, we need to free any additional memory
530 * the objects may hold.
Stefan Beller99bf1152018-05-08 12:37:24 -0700531 */
Stefan Beller14ba97f2018-05-15 14:48:42 -0700532 unsigned i;
533
534 for (i = 0; i < o->obj_hash_size; i++) {
535 struct object *obj = o->obj_hash[i];
536
537 if (!obj)
538 continue;
539
540 if (obj->type == OBJ_TREE)
541 free_tree_buffer((struct tree*)obj);
542 else if (obj->type == OBJ_COMMIT)
Stefan Beller6a7895f2018-12-14 16:09:40 -0800543 release_commit_memory(o, (struct commit*)obj);
Stefan Beller14ba97f2018-05-15 14:48:42 -0700544 else if (obj->type == OBJ_TAG)
545 release_tag_memory((struct tag*)obj);
546 }
547
548 FREE_AND_NULL(o->obj_hash);
549 o->obj_hash_size = 0;
550
Stefan Beller65ea9d42018-06-28 18:22:15 -0700551 free_commit_buffer_slab(o->buffer_slab);
552 o->buffer_slab = NULL;
553
Stefan Beller14ba97f2018-05-15 14:48:42 -0700554 clear_alloc_state(o->blob_state);
555 clear_alloc_state(o->tree_state);
556 clear_alloc_state(o->commit_state);
557 clear_alloc_state(o->tag_state);
558 clear_alloc_state(o->object_state);
Josh Steadmon96b07102019-02-07 12:05:54 -0800559 stat_validity_clear(o->shallow_stat);
Stefan Beller14ba97f2018-05-15 14:48:42 -0700560 FREE_AND_NULL(o->blob_state);
561 FREE_AND_NULL(o->tree_state);
562 FREE_AND_NULL(o->commit_state);
563 FREE_AND_NULL(o->tag_state);
564 FREE_AND_NULL(o->object_state);
Josh Steadmon96b07102019-02-07 12:05:54 -0800565 FREE_AND_NULL(o->shallow_stat);
Stefan Beller99bf1152018-05-08 12:37:24 -0700566}