blob: 344087de4de5184fcbfb599277180cec8f39c474 [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
René Scharfeca56dad2021-03-13 17:17:22 +0100130 CALLOC_ARRAY(new_hash, new_hash_size);
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
Abhishek Kumar6da43d92020-06-17 14:44:08 +0530160void *object_as_type(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)
Abhishek Kumar6da43d92020-06-17 14:44:08 +0530166 init_commit_node((struct commit *) obj);
SZEDER Gábor4468d442019-01-27 14:08:32 +0100167 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 King45a187c2021-04-13 03:16:36 -0400180struct object *lookup_unknown_object(struct repository *r, const struct object_id *oid)
barkalow@iabervon.org66e481b2005-08-02 19:45:48 -0400181{
Jeff King45a187c2021-04-13 03:16:36 -0400182 struct object *obj = lookup_object(r, oid);
Linus Torvalds100c5f32007-04-16 22:11:43 -0700183 if (!obj)
Jeff King45a187c2021-04-13 03:16:36 -0400184 obj = create_object(r, oid, alloc_object_node(r));
barkalow@iabervon.org66e481b2005-08-02 19:45:48 -0400185 return obj;
186}
187
Jeff King74630642021-06-22 12:06:41 -0400188struct object *lookup_object_by_type(struct repository *r,
189 const struct object_id *oid,
190 enum object_type type)
191{
192 switch (type) {
193 case OBJ_COMMIT:
194 return (struct object *)lookup_commit(r, oid);
195 case OBJ_TREE:
196 return (struct object *)lookup_tree(r, oid);
197 case OBJ_TAG:
198 return (struct object *)lookup_tag(r, oid);
199 case OBJ_BLOB:
200 return (struct object *)lookup_blob(r, oid);
201 default:
Ævar Arnfjörð Bjarmasoneafd6e72021-12-07 12:05:54 +0100202 BUG("unknown object type %d", type);
Jeff King74630642021-06-22 12:06:41 -0400203 }
204}
205
Stefan Beller108ed1a2018-06-28 18:22:18 -0700206struct 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 -0700207{
208 struct object *obj;
Stefan Beller8e92e8f2013-07-18 00:09:42 +0200209 *eaten_p = 0;
Junio C Hamano9f613dd2006-09-15 13:30:02 -0700210
Jim Meyeringcc216822007-12-21 11:56:32 +0100211 obj = NULL;
Nicolas Pitre21666f12007-02-26 14:55:59 -0500212 if (type == OBJ_BLOB) {
Stefan Beller108ed1a2018-06-28 18:22:18 -0700213 struct blob *blob = lookup_blob(r, oid);
Jim Meyeringcc216822007-12-21 11:56:32 +0100214 if (blob) {
Jeff Kingc1166ca2022-12-13 06:11:57 -0500215 parse_blob_buffer(blob);
Jim Meyeringcc216822007-12-21 11:56:32 +0100216 obj = &blob->object;
217 }
Nicolas Pitre21666f12007-02-26 14:55:59 -0500218 } else if (type == OBJ_TREE) {
Stefan Beller108ed1a2018-06-28 18:22:18 -0700219 struct tree *tree = lookup_tree(r, oid);
Jim Meyeringcc216822007-12-21 11:56:32 +0100220 if (tree) {
221 obj = &tree->object;
Junio C Hamano68be2fe2011-11-16 22:04:13 -0800222 if (!tree->buffer)
223 tree->object.parsed = 0;
Jim Meyeringcc216822007-12-21 11:56:32 +0100224 if (!tree->object.parsed) {
Martin Koeglerd0b8c9e2008-02-03 22:22:39 +0100225 if (parse_tree_buffer(tree, buffer, size))
226 return NULL;
Stefan Beller8e92e8f2013-07-18 00:09:42 +0200227 *eaten_p = 1;
Jim Meyeringcc216822007-12-21 11:56:32 +0100228 }
Junio C Hamano9f613dd2006-09-15 13:30:02 -0700229 }
Nicolas Pitre21666f12007-02-26 14:55:59 -0500230 } else if (type == OBJ_COMMIT) {
Stefan Beller108ed1a2018-06-28 18:22:18 -0700231 struct commit *commit = lookup_commit(r, oid);
Jim Meyeringcc216822007-12-21 11:56:32 +0100232 if (commit) {
Stefan Beller108ed1a2018-06-28 18:22:18 -0700233 if (parse_commit_buffer(r, commit, buffer, size, 1))
Martin Koeglerd0b8c9e2008-02-03 22:22:39 +0100234 return NULL;
Jeff King51b27742022-09-22 06:15:38 -0400235 if (save_commit_buffer &&
236 !get_cached_commit_buffer(r, commit, NULL)) {
Stefan Beller108ed1a2018-06-28 18:22:18 -0700237 set_commit_buffer(r, commit, buffer, size);
Stefan Beller8e92e8f2013-07-18 00:09:42 +0200238 *eaten_p = 1;
Jim Meyeringcc216822007-12-21 11:56:32 +0100239 }
240 obj = &commit->object;
Junio C Hamano9f613dd2006-09-15 13:30:02 -0700241 }
Nicolas Pitre21666f12007-02-26 14:55:59 -0500242 } else if (type == OBJ_TAG) {
Stefan Beller108ed1a2018-06-28 18:22:18 -0700243 struct tag *tag = lookup_tag(r, oid);
Jim Meyeringcc216822007-12-21 11:56:32 +0100244 if (tag) {
Stefan Beller108ed1a2018-06-28 18:22:18 -0700245 if (parse_tag_buffer(r, tag, buffer, size))
Martin Koeglerd0b8c9e2008-02-03 22:22:39 +0100246 return NULL;
Jim Meyeringcc216822007-12-21 11:56:32 +0100247 obj = &tag->object;
248 }
Junio C Hamano9f613dd2006-09-15 13:30:02 -0700249 } else {
Nguyễn Thái Ngọc Duy42246582018-07-21 09:49:33 +0200250 warning(_("object %s has unknown type id %d"), oid_to_hex(oid), type);
Junio C Hamano9f613dd2006-09-15 13:30:02 -0700251 obj = NULL;
252 }
Junio C Hamano9f613dd2006-09-15 13:30:02 -0700253 return obj;
254}
255
brian m. carlsonc251c832017-05-06 22:10:38 +0000256struct object *parse_object_or_die(const struct object_id *oid,
Jeff King75a95492013-03-17 04:22:36 -0400257 const char *name)
258{
Stefan Beller109cd762018-06-28 18:21:51 -0700259 struct object *o = parse_object(the_repository, oid);
Jeff King75a95492013-03-17 04:22:36 -0400260 if (o)
261 return o;
262
brian m. carlsonc251c832017-05-06 22:10:38 +0000263 die(_("unable to parse object: %s"), name ? name : oid_to_hex(oid));
Jeff King75a95492013-03-17 04:22:36 -0400264}
265
Jeff Kingc868d8e2022-09-06 19:01:34 -0400266struct object *parse_object_with_flags(struct repository *r,
267 const struct object_id *oid,
268 enum parse_object_flags flags)
Daniel Barkalowe9eefa62005-04-28 07:46:33 -0700269{
Jeff Kingc868d8e2022-09-06 19:01:34 -0400270 int skip_hash = !!(flags & PARSE_OBJECT_SKIP_HASH_CHECK);
Junio C Hamanoc4584ae2005-06-27 03:33:33 -0700271 unsigned long size;
Nicolas Pitre21666f12007-02-26 14:55:59 -0500272 enum object_type type;
Junio C Hamano9f613dd2006-09-15 13:30:02 -0700273 int eaten;
Stefan Beller8e4b0b62018-06-28 18:22:19 -0700274 const struct object_id *repl = lookup_replace_object(r, oid);
Jeff Kingccdc6032012-01-05 16:00:01 -0500275 void *buffer;
276 struct object *obj;
Junio C Hamano9f613dd2006-09-15 13:30:02 -0700277
Jeff Kingd0229ab2019-06-20 03:41:14 -0400278 obj = lookup_object(r, oid);
Jeff Kingccdc6032012-01-05 16:00:01 -0500279 if (obj && obj->parsed)
280 return obj;
281
Jeff King9a8c3c42022-09-06 19:06:25 -0400282 if (skip_hash) {
283 struct commit *commit = lookup_commit_in_graph(r, repl);
284 if (commit)
285 return &commit->object;
286 }
287
Ævar Arnfjörð Bjarmason40286ca2022-11-21 14:26:55 -0500288 if ((!obj || obj->type == OBJ_BLOB) &&
Jeff King8db2dad2022-11-17 17:41:16 -0500289 oid_object_info(r, oid, NULL) == OBJ_BLOB) {
Jeff Kingc868d8e2022-09-06 19:01:34 -0400290 if (!skip_hash && stream_object_signature(r, repl) < 0) {
Jeff King01f8d592019-01-07 03:40:34 -0500291 error(_("hash mismatch %s"), oid_to_hex(oid));
Nguyễn Thái Ngọc Duy090ea122012-03-07 17:54:18 +0700292 return NULL;
293 }
Jeff Kingc1166ca2022-12-13 06:11:57 -0500294 parse_blob_buffer(lookup_blob(r, oid));
Jeff Kingd0229ab2019-06-20 03:41:14 -0400295 return lookup_object(r, oid);
Nguyễn Thái Ngọc Duy090ea122012-03-07 17:54:18 +0700296 }
297
Stefan Bellerd1a69022018-11-13 16:12:49 -0800298 buffer = repo_read_object_file(r, oid, &type, &size);
Junio C Hamanoc4584ae2005-06-27 03:33:33 -0700299 if (buffer) {
Jeff Kingc868d8e2022-09-06 19:01:34 -0400300 if (!skip_hash &&
301 check_object_signature(r, repl, buffer, size, type) < 0) {
Carlos Rica0b1f1132007-05-25 03:46:22 +0200302 free(buffer);
Jeff King01f8d592019-01-07 03:40:34 -0500303 error(_("hash mismatch %s"), oid_to_hex(repl));
Linus Torvaldsacdeec62007-03-20 10:05:20 -0700304 return NULL;
305 }
Junio C Hamano9f613dd2006-09-15 13:30:02 -0700306
Stefan Beller8e4b0b62018-06-28 18:22:19 -0700307 obj = parse_object_buffer(r, oid, type, size,
Stefan Beller1ec5bfd2018-06-28 18:21:53 -0700308 buffer, &eaten);
Junio C Hamano9f613dd2006-09-15 13:30:02 -0700309 if (!eaten)
310 free(buffer);
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400311 return obj;
Daniel Barkalowe9eefa62005-04-28 07:46:33 -0700312 }
313 return NULL;
314}
barkalow@iabervon.org66e481b2005-08-02 19:45:48 -0400315
Jeff Kingc868d8e2022-09-06 19:01:34 -0400316struct object *parse_object(struct repository *r, const struct object_id *oid)
317{
318 return parse_object_with_flags(r, oid, 0);
319}
320
barkalow@iabervon.org66e481b2005-08-02 19:45:48 -0400321struct object_list *object_list_insert(struct object *item,
322 struct object_list **list_p)
323{
324 struct object_list *new_list = xmalloc(sizeof(struct object_list));
Jared Hance55b4e9e2010-09-05 15:36:33 -0400325 new_list->item = item;
326 new_list->next = *list_p;
327 *list_p = new_list;
328 return new_list;
barkalow@iabervon.org66e481b2005-08-02 19:45:48 -0400329}
330
barkalow@iabervon.org66e481b2005-08-02 19:45:48 -0400331int object_list_contains(struct object_list *list, struct object *obj)
332{
333 while (list) {
334 if (list->item == obj)
335 return 1;
336 list = list->next;
337 }
338 return 0;
339}
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700340
Jeff Kingacac50d2020-02-12 21:16:33 -0500341void object_list_free(struct object_list **list)
342{
343 while (*list) {
344 struct object_list *p = *list;
345 *list = p->next;
346 free(p);
347 }
348}
349
Michael Haggerty31faeb22013-05-25 11:08:14 +0200350/*
351 * A zero-length string to which object_array_entry::name can be
352 * initialized without requiring a malloc/free.
353 */
354static char object_array_slopbuf[1];
355
Jeff King9e0c3c42014-10-15 18:42:57 -0400356void add_object_array_with_path(struct object *obj, const char *name,
357 struct object_array *array,
358 unsigned mode, const char *path)
Martin Koeglere5709a42007-04-22 18:43:58 +0200359{
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700360 unsigned nr = array->nr;
361 unsigned alloc = array->alloc;
362 struct object_array_entry *objects = array->objects;
Michael Haggerty31faeb22013-05-25 11:08:14 +0200363 struct object_array_entry *entry;
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700364
365 if (nr >= alloc) {
366 alloc = (alloc + 32) * 2;
René Scharfe2756ca42014-09-16 20:56:57 +0200367 REALLOC_ARRAY(objects, alloc);
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700368 array->alloc = alloc;
369 array->objects = objects;
370 }
Michael Haggerty31faeb22013-05-25 11:08:14 +0200371 entry = &objects[nr];
372 entry->item = obj;
373 if (!name)
374 entry->name = NULL;
375 else if (!*name)
376 /* Use our own empty string instead of allocating one: */
377 entry->name = object_array_slopbuf;
378 else
379 entry->name = xstrdup(name);
380 entry->mode = mode;
Jeff King9e0c3c42014-10-15 18:42:57 -0400381 if (path)
382 entry->path = xstrdup(path);
383 else
384 entry->path = NULL;
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700385 array->nr = ++nr;
386}
Junio C Hamanob2a6d1c2009-01-17 22:27:08 -0800387
Michael J Gruberafa15f32013-05-10 17:10:16 +0200388void add_object_array(struct object *obj, const char *name, struct object_array *array)
389{
Jeff King189a1222014-10-18 22:03:19 -0400390 add_object_array_with_path(obj, name, array, S_IFINVALID, NULL);
Michael J Gruberafa15f32013-05-10 17:10:16 +0200391}
392
Jeff King68f49232014-10-15 18:34:19 -0400393/*
394 * Free all memory associated with an entry; the result is
395 * in an unspecified state and should not be examined.
396 */
397static void object_array_release_entry(struct object_array_entry *ent)
398{
399 if (ent->name != object_array_slopbuf)
400 free(ent->name);
Jeff King9e0c3c42014-10-15 18:42:57 -0400401 free(ent->path);
Jeff King68f49232014-10-15 18:34:19 -0400402}
403
Martin Ågren71992032017-09-23 01:34:53 +0200404struct object *object_array_pop(struct object_array *array)
405{
406 struct object *ret;
407
408 if (!array->nr)
409 return NULL;
410
411 ret = array->objects[array->nr - 1].item;
412 object_array_release_entry(&array->objects[array->nr - 1]);
413 array->nr--;
414 return ret;
415}
416
Michael Haggertyaeb4a512013-05-25 11:08:08 +0200417void object_array_filter(struct object_array *array,
418 object_array_each_func_t want, void *cb_data)
419{
420 unsigned nr = array->nr, src, dst;
421 struct object_array_entry *objects = array->objects;
422
423 for (src = dst = 0; src < nr; src++) {
424 if (want(&objects[src], cb_data)) {
425 if (src != dst)
426 objects[dst] = objects[src];
427 dst++;
Michael Haggerty31faeb22013-05-25 11:08:14 +0200428 } else {
Jeff King68f49232014-10-15 18:34:19 -0400429 object_array_release_entry(&objects[src]);
Michael Haggertyaeb4a512013-05-25 11:08:08 +0200430 }
431 }
432 array->nr = dst;
433}
434
Jeff King46be8232014-10-15 18:34:34 -0400435void object_array_clear(struct object_array *array)
436{
437 int i;
438 for (i = 0; i < array->nr; i++)
439 object_array_release_entry(&array->objects[i]);
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +0000440 FREE_AND_NULL(array->objects);
Jeff King46be8232014-10-15 18:34:34 -0400441 array->nr = array->alloc = 0;
442}
443
Michael Haggerty15065102013-05-25 11:08:10 +0200444/*
Jiang Xince1d6d92021-01-11 21:27:02 -0500445 * Return true if array already contains an entry.
Michael Haggerty15065102013-05-25 11:08:10 +0200446 */
Jiang Xince1d6d92021-01-11 21:27:02 -0500447static int contains_object(struct object_array *array,
448 const struct object *item, const char *name)
Michael Haggerty15065102013-05-25 11:08:10 +0200449{
450 unsigned nr = array->nr, i;
451 struct object_array_entry *object = array->objects;
452
453 for (i = 0; i < nr; i++, object++)
Jiang Xince1d6d92021-01-11 21:27:02 -0500454 if (item == object->item && !strcmp(object->name, name))
Michael Haggerty15065102013-05-25 11:08:10 +0200455 return 1;
456 return 0;
457}
458
Junio C Hamanob2a6d1c2009-01-17 22:27:08 -0800459void object_array_remove_duplicates(struct object_array *array)
460{
Michael Haggerty15065102013-05-25 11:08:10 +0200461 unsigned nr = array->nr, src;
Junio C Hamanob2a6d1c2009-01-17 22:27:08 -0800462 struct object_array_entry *objects = array->objects;
463
Michael Haggerty15065102013-05-25 11:08:10 +0200464 array->nr = 0;
465 for (src = 0; src < nr; src++) {
Jiang Xince1d6d92021-01-11 21:27:02 -0500466 if (!contains_object(array, objects[src].item,
467 objects[src].name)) {
Michael Haggerty15065102013-05-25 11:08:10 +0200468 if (src != array->nr)
469 objects[array->nr] = objects[src];
470 array->nr++;
Michael Haggerty31faeb22013-05-25 11:08:14 +0200471 } else {
Jeff King68f49232014-10-15 18:34:19 -0400472 object_array_release_entry(&objects[src]);
Junio C Hamanob2a6d1c2009-01-17 22:27:08 -0800473 }
Junio C Hamanob2a6d1c2009-01-17 22:27:08 -0800474 }
475}
Heiko Voigtbcc0a3e2012-03-29 09:21:21 +0200476
477void clear_object_flags(unsigned flags)
478{
479 int i;
480
Stefan Beller99bf1152018-05-08 12:37:24 -0700481 for (i=0; i < the_repository->parsed_objects->obj_hash_size; i++) {
482 struct object *obj = the_repository->parsed_objects->obj_hash[i];
Heiko Voigtbcc0a3e2012-03-29 09:21:21 +0200483 if (obj)
484 obj->flags &= ~flags;
485 }
486}
René Scharfe4ad315f2017-12-25 18:44:58 +0100487
René Scharfecd888842020-10-31 13:46:08 +0100488void repo_clear_commit_marks(struct repository *r, unsigned int flags)
René Scharfe4ad315f2017-12-25 18:44:58 +0100489{
490 int i;
491
René Scharfecd888842020-10-31 13:46:08 +0100492 for (i = 0; i < r->parsed_objects->obj_hash_size; i++) {
493 struct object *obj = r->parsed_objects->obj_hash[i];
René Scharfe4ad315f2017-12-25 18:44:58 +0100494 if (obj && obj->type == OBJ_COMMIT)
495 obj->flags &= ~flags;
496 }
497}
Stefan Beller90c62152018-03-23 18:20:55 +0100498
Stefan Beller99bf1152018-05-08 12:37:24 -0700499struct parsed_object_pool *parsed_object_pool_new(void)
500{
501 struct parsed_object_pool *o = xmalloc(sizeof(*o));
502 memset(o, 0, sizeof(*o));
Stefan Beller14ba97f2018-05-15 14:48:42 -0700503
504 o->blob_state = allocate_alloc_state();
505 o->tree_state = allocate_alloc_state();
506 o->commit_state = allocate_alloc_state();
507 o->tag_state = allocate_alloc_state();
508 o->object_state = allocate_alloc_state();
509
Stefan Bellereee45022018-05-17 15:51:52 -0700510 o->is_shallow = -1;
René Scharfeca56dad2021-03-13 17:17:22 +0100511 CALLOC_ARRAY(o->shallow_stat, 1);
Stefan Bellereee45022018-05-17 15:51:52 -0700512
Stefan Beller65ea9d42018-06-28 18:22:15 -0700513 o->buffer_slab = allocate_commit_buffer_slab();
514
Stefan Beller99bf1152018-05-08 12:37:24 -0700515 return o;
516}
517
Stefan Beller90c62152018-03-23 18:20:55 +0100518struct raw_object_store *raw_object_store_new(void)
519{
520 struct raw_object_store *o = xmalloc(sizeof(*o));
521
522 memset(o, 0, sizeof(*o));
Stefan Bellera80d72d2018-03-23 18:20:59 +0100523 INIT_LIST_HEAD(&o->packed_git_mru);
Colin Stolleyec485402019-11-27 16:24:53 -0600524 hashmap_init(&o->pack_map, pack_map_entry_cmp, NULL, 0);
Matheus Tavaresb1fc9da2020-01-15 23:39:52 -0300525 pthread_mutex_init(&o->replace_mutex, NULL);
Stefan Beller90c62152018-03-23 18:20:55 +0100526 return o;
527}
Stefan Beller97501e92018-03-23 18:20:58 +0100528
Neeraj Singhb3cecf42021-12-06 22:05:04 +0000529void free_object_directory(struct object_directory *odb)
Stefan Beller97501e92018-03-23 18:20:58 +0100530{
Jeff Kingf0eaf632018-11-12 09:50:39 -0500531 free(odb->path);
René Scharfed4e19e52019-01-06 17:45:39 +0100532 odb_clear_loose_cache(odb);
Jeff King263db402018-11-12 09:48:47 -0500533 free(odb);
Stefan Beller97501e92018-03-23 18:20:58 +0100534}
535
Jeff Kingf0eaf632018-11-12 09:50:39 -0500536static void free_object_directories(struct raw_object_store *o)
Stefan Beller97501e92018-03-23 18:20:58 +0100537{
Jeff Kingf0eaf632018-11-12 09:50:39 -0500538 while (o->odb) {
Jeff King263db402018-11-12 09:48:47 -0500539 struct object_directory *next;
Stefan Beller97501e92018-03-23 18:20:58 +0100540
Jeff Kingf0eaf632018-11-12 09:50:39 -0500541 next = o->odb->next;
542 free_object_directory(o->odb);
543 o->odb = next;
Stefan Beller97501e92018-03-23 18:20:58 +0100544 }
Eric Wongcf2dc1c2021-07-07 23:10:15 +0000545 kh_destroy_odb_path_map(o->odb_by_path);
546 o->odb_by_path = NULL;
Stefan Beller97501e92018-03-23 18:20:58 +0100547}
548
Stefan Beller90c62152018-03-23 18:20:55 +0100549void raw_object_store_clear(struct raw_object_store *o)
550{
Stefan Beller90c62152018-03-23 18:20:55 +0100551 FREE_AND_NULL(o->alternate_db);
Stefan Beller7a1dc602018-05-17 11:29:57 -0700552
553 oidmap_free(o->replace_map, 1);
Stefan Bellerd5873072018-05-09 16:40:58 -0700554 FREE_AND_NULL(o->replace_map);
Matheus Tavaresb1fc9da2020-01-15 23:39:52 -0300555 pthread_mutex_destroy(&o->replace_mutex);
Stefan Beller97501e92018-03-23 18:20:58 +0100556
Jonathan Tan85277502018-07-11 15:42:41 -0700557 free_commit_graph(o->commit_graph);
558 o->commit_graph = NULL;
559 o->commit_graph_attempted = 0;
560
Jeff Kingf0eaf632018-11-12 09:50:39 -0500561 free_object_directories(o);
562 o->odb_tail = NULL;
563 o->loaded_alternates = 0;
Stefan Bellera80d72d2018-03-23 18:20:59 +0100564
565 INIT_LIST_HEAD(&o->packed_git_mru);
Derrick Stolee2d511cf2019-05-17 11:41:49 -0700566 close_object_store(o);
Stefan Bellerd0b59862018-03-23 18:21:00 +0100567 o->packed_git = NULL;
Colin Stolleyec485402019-11-27 16:24:53 -0600568
Elijah Newren6da1a252020-11-02 18:55:05 +0000569 hashmap_clear(&o->pack_map);
Stefan Beller90c62152018-03-23 18:20:55 +0100570}
Stefan Beller99bf1152018-05-08 12:37:24 -0700571
572void parsed_object_pool_clear(struct parsed_object_pool *o)
573{
574 /*
Stefan Beller99bf1152018-05-08 12:37:24 -0700575 * As objects are allocated in slabs (see alloc.c), we do
576 * not need to free each object, but each slab instead.
Stefan Beller14ba97f2018-05-15 14:48:42 -0700577 *
578 * Before doing so, we need to free any additional memory
579 * the objects may hold.
Stefan Beller99bf1152018-05-08 12:37:24 -0700580 */
Stefan Beller14ba97f2018-05-15 14:48:42 -0700581 unsigned i;
582
583 for (i = 0; i < o->obj_hash_size; i++) {
584 struct object *obj = o->obj_hash[i];
585
586 if (!obj)
587 continue;
588
589 if (obj->type == OBJ_TREE)
590 free_tree_buffer((struct tree*)obj);
591 else if (obj->type == OBJ_COMMIT)
Stefan Beller6a7895f2018-12-14 16:09:40 -0800592 release_commit_memory(o, (struct commit*)obj);
Stefan Beller14ba97f2018-05-15 14:48:42 -0700593 else if (obj->type == OBJ_TAG)
594 release_tag_memory((struct tag*)obj);
595 }
596
597 FREE_AND_NULL(o->obj_hash);
598 o->obj_hash_size = 0;
599
Stefan Beller65ea9d42018-06-28 18:22:15 -0700600 free_commit_buffer_slab(o->buffer_slab);
601 o->buffer_slab = NULL;
602
Stefan Beller14ba97f2018-05-15 14:48:42 -0700603 clear_alloc_state(o->blob_state);
604 clear_alloc_state(o->tree_state);
605 clear_alloc_state(o->commit_state);
606 clear_alloc_state(o->tag_state);
607 clear_alloc_state(o->object_state);
Josh Steadmon96b07102019-02-07 12:05:54 -0800608 stat_validity_clear(o->shallow_stat);
Stefan Beller14ba97f2018-05-15 14:48:42 -0700609 FREE_AND_NULL(o->blob_state);
610 FREE_AND_NULL(o->tree_state);
611 FREE_AND_NULL(o->commit_state);
612 FREE_AND_NULL(o->tag_state);
613 FREE_AND_NULL(o->object_state);
Josh Steadmon96b07102019-02-07 12:05:54 -0800614 FREE_AND_NULL(o->shallow_stat);
Stefan Beller99bf1152018-05-08 12:37:24 -0700615}