blob: 8a74eb85e94603458e0fe7eafbc58a40a3380b10 [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) {
Martin Koeglerd0b8c9e2008-02-03 22:22:39 +0100215 if (parse_blob_buffer(blob, buffer, size))
216 return NULL;
Jim Meyeringcc216822007-12-21 11:56:32 +0100217 obj = &blob->object;
218 }
Nicolas Pitre21666f12007-02-26 14:55:59 -0500219 } else if (type == OBJ_TREE) {
Stefan Beller108ed1a2018-06-28 18:22:18 -0700220 struct tree *tree = lookup_tree(r, oid);
Jim Meyeringcc216822007-12-21 11:56:32 +0100221 if (tree) {
222 obj = &tree->object;
Junio C Hamano68be2fe2011-11-16 22:04:13 -0800223 if (!tree->buffer)
224 tree->object.parsed = 0;
Jim Meyeringcc216822007-12-21 11:56:32 +0100225 if (!tree->object.parsed) {
Martin Koeglerd0b8c9e2008-02-03 22:22:39 +0100226 if (parse_tree_buffer(tree, buffer, size))
227 return NULL;
Stefan Beller8e92e8f2013-07-18 00:09:42 +0200228 *eaten_p = 1;
Jim Meyeringcc216822007-12-21 11:56:32 +0100229 }
Junio C Hamano9f613dd2006-09-15 13:30:02 -0700230 }
Nicolas Pitre21666f12007-02-26 14:55:59 -0500231 } else if (type == OBJ_COMMIT) {
Stefan Beller108ed1a2018-06-28 18:22:18 -0700232 struct commit *commit = lookup_commit(r, oid);
Jim Meyeringcc216822007-12-21 11:56:32 +0100233 if (commit) {
Stefan Beller108ed1a2018-06-28 18:22:18 -0700234 if (parse_commit_buffer(r, commit, buffer, size, 1))
Martin Koeglerd0b8c9e2008-02-03 22:22:39 +0100235 return NULL;
Jeff King51b27742022-09-22 06:15:38 -0400236 if (save_commit_buffer &&
237 !get_cached_commit_buffer(r, commit, NULL)) {
Stefan Beller108ed1a2018-06-28 18:22:18 -0700238 set_commit_buffer(r, commit, buffer, size);
Stefan Beller8e92e8f2013-07-18 00:09:42 +0200239 *eaten_p = 1;
Jim Meyeringcc216822007-12-21 11:56:32 +0100240 }
241 obj = &commit->object;
Junio C Hamano9f613dd2006-09-15 13:30:02 -0700242 }
Nicolas Pitre21666f12007-02-26 14:55:59 -0500243 } else if (type == OBJ_TAG) {
Stefan Beller108ed1a2018-06-28 18:22:18 -0700244 struct tag *tag = lookup_tag(r, oid);
Jim Meyeringcc216822007-12-21 11:56:32 +0100245 if (tag) {
Stefan Beller108ed1a2018-06-28 18:22:18 -0700246 if (parse_tag_buffer(r, tag, buffer, size))
Martin Koeglerd0b8c9e2008-02-03 22:22:39 +0100247 return NULL;
Jim Meyeringcc216822007-12-21 11:56:32 +0100248 obj = &tag->object;
249 }
Junio C Hamano9f613dd2006-09-15 13:30:02 -0700250 } else {
Nguyễn Thái Ngọc Duy42246582018-07-21 09:49:33 +0200251 warning(_("object %s has unknown type id %d"), oid_to_hex(oid), type);
Junio C Hamano9f613dd2006-09-15 13:30:02 -0700252 obj = NULL;
253 }
Junio C Hamano9f613dd2006-09-15 13:30:02 -0700254 return obj;
255}
256
brian m. carlsonc251c832017-05-06 22:10:38 +0000257struct object *parse_object_or_die(const struct object_id *oid,
Jeff King75a95492013-03-17 04:22:36 -0400258 const char *name)
259{
Stefan Beller109cd762018-06-28 18:21:51 -0700260 struct object *o = parse_object(the_repository, oid);
Jeff King75a95492013-03-17 04:22:36 -0400261 if (o)
262 return o;
263
brian m. carlsonc251c832017-05-06 22:10:38 +0000264 die(_("unable to parse object: %s"), name ? name : oid_to_hex(oid));
Jeff King75a95492013-03-17 04:22:36 -0400265}
266
Jeff Kingc868d8e2022-09-06 19:01:34 -0400267struct object *parse_object_with_flags(struct repository *r,
268 const struct object_id *oid,
269 enum parse_object_flags flags)
Daniel Barkalowe9eefa62005-04-28 07:46:33 -0700270{
Jeff Kingc868d8e2022-09-06 19:01:34 -0400271 int skip_hash = !!(flags & PARSE_OBJECT_SKIP_HASH_CHECK);
Junio C Hamanoc4584ae2005-06-27 03:33:33 -0700272 unsigned long size;
Nicolas Pitre21666f12007-02-26 14:55:59 -0500273 enum object_type type;
Junio C Hamano9f613dd2006-09-15 13:30:02 -0700274 int eaten;
Stefan Beller8e4b0b62018-06-28 18:22:19 -0700275 const struct object_id *repl = lookup_replace_object(r, oid);
Jeff Kingccdc6032012-01-05 16:00:01 -0500276 void *buffer;
277 struct object *obj;
Junio C Hamano9f613dd2006-09-15 13:30:02 -0700278
Jeff Kingd0229ab2019-06-20 03:41:14 -0400279 obj = lookup_object(r, oid);
Jeff Kingccdc6032012-01-05 16:00:01 -0500280 if (obj && obj->parsed)
281 return obj;
282
Jeff King9a8c3c42022-09-06 19:06:25 -0400283 if (skip_hash) {
284 struct commit *commit = lookup_commit_in_graph(r, repl);
285 if (commit)
286 return &commit->object;
287 }
288
Stefan Bellerd1a69022018-11-13 16:12:49 -0800289 if ((obj && obj->type == OBJ_BLOB && repo_has_object_file(r, oid)) ||
290 (!obj && repo_has_object_file(r, oid) &&
Stefan Beller8e4b0b62018-06-28 18:22:19 -0700291 oid_object_info(r, oid, NULL) == OBJ_BLOB)) {
Jeff Kingc868d8e2022-09-06 19:01:34 -0400292 if (!skip_hash && stream_object_signature(r, repl) < 0) {
Jeff King01f8d592019-01-07 03:40:34 -0500293 error(_("hash mismatch %s"), oid_to_hex(oid));
Nguyễn Thái Ngọc Duy090ea122012-03-07 17:54:18 +0700294 return NULL;
295 }
Stefan Beller8e4b0b62018-06-28 18:22:19 -0700296 parse_blob_buffer(lookup_blob(r, oid), NULL, 0);
Jeff Kingd0229ab2019-06-20 03:41:14 -0400297 return lookup_object(r, oid);
Nguyễn Thái Ngọc Duy090ea122012-03-07 17:54:18 +0700298 }
299
Stefan Bellerd1a69022018-11-13 16:12:49 -0800300 buffer = repo_read_object_file(r, oid, &type, &size);
Junio C Hamanoc4584ae2005-06-27 03:33:33 -0700301 if (buffer) {
Jeff Kingc868d8e2022-09-06 19:01:34 -0400302 if (!skip_hash &&
303 check_object_signature(r, repl, buffer, size, type) < 0) {
Carlos Rica0b1f1132007-05-25 03:46:22 +0200304 free(buffer);
Jeff King01f8d592019-01-07 03:40:34 -0500305 error(_("hash mismatch %s"), oid_to_hex(repl));
Linus Torvaldsacdeec62007-03-20 10:05:20 -0700306 return NULL;
307 }
Junio C Hamano9f613dd2006-09-15 13:30:02 -0700308
Stefan Beller8e4b0b62018-06-28 18:22:19 -0700309 obj = parse_object_buffer(r, oid, type, size,
Stefan Beller1ec5bfd2018-06-28 18:21:53 -0700310 buffer, &eaten);
Junio C Hamano9f613dd2006-09-15 13:30:02 -0700311 if (!eaten)
312 free(buffer);
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400313 return obj;
Daniel Barkalowe9eefa62005-04-28 07:46:33 -0700314 }
315 return NULL;
316}
barkalow@iabervon.org66e481b2005-08-02 19:45:48 -0400317
Jeff Kingc868d8e2022-09-06 19:01:34 -0400318struct object *parse_object(struct repository *r, const struct object_id *oid)
319{
320 return parse_object_with_flags(r, oid, 0);
321}
322
barkalow@iabervon.org66e481b2005-08-02 19:45:48 -0400323struct object_list *object_list_insert(struct object *item,
324 struct object_list **list_p)
325{
326 struct object_list *new_list = xmalloc(sizeof(struct object_list));
Jared Hance55b4e9e2010-09-05 15:36:33 -0400327 new_list->item = item;
328 new_list->next = *list_p;
329 *list_p = new_list;
330 return new_list;
barkalow@iabervon.org66e481b2005-08-02 19:45:48 -0400331}
332
barkalow@iabervon.org66e481b2005-08-02 19:45:48 -0400333int object_list_contains(struct object_list *list, struct object *obj)
334{
335 while (list) {
336 if (list->item == obj)
337 return 1;
338 list = list->next;
339 }
340 return 0;
341}
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700342
Jeff Kingacac50d2020-02-12 21:16:33 -0500343void object_list_free(struct object_list **list)
344{
345 while (*list) {
346 struct object_list *p = *list;
347 *list = p->next;
348 free(p);
349 }
350}
351
Michael Haggerty31faeb22013-05-25 11:08:14 +0200352/*
353 * A zero-length string to which object_array_entry::name can be
354 * initialized without requiring a malloc/free.
355 */
356static char object_array_slopbuf[1];
357
Jeff King9e0c3c42014-10-15 18:42:57 -0400358void add_object_array_with_path(struct object *obj, const char *name,
359 struct object_array *array,
360 unsigned mode, const char *path)
Martin Koeglere5709a42007-04-22 18:43:58 +0200361{
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700362 unsigned nr = array->nr;
363 unsigned alloc = array->alloc;
364 struct object_array_entry *objects = array->objects;
Michael Haggerty31faeb22013-05-25 11:08:14 +0200365 struct object_array_entry *entry;
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700366
367 if (nr >= alloc) {
368 alloc = (alloc + 32) * 2;
René Scharfe2756ca42014-09-16 20:56:57 +0200369 REALLOC_ARRAY(objects, alloc);
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700370 array->alloc = alloc;
371 array->objects = objects;
372 }
Michael Haggerty31faeb22013-05-25 11:08:14 +0200373 entry = &objects[nr];
374 entry->item = obj;
375 if (!name)
376 entry->name = NULL;
377 else if (!*name)
378 /* Use our own empty string instead of allocating one: */
379 entry->name = object_array_slopbuf;
380 else
381 entry->name = xstrdup(name);
382 entry->mode = mode;
Jeff King9e0c3c42014-10-15 18:42:57 -0400383 if (path)
384 entry->path = xstrdup(path);
385 else
386 entry->path = NULL;
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700387 array->nr = ++nr;
388}
Junio C Hamanob2a6d1c2009-01-17 22:27:08 -0800389
Michael J Gruberafa15f32013-05-10 17:10:16 +0200390void add_object_array(struct object *obj, const char *name, struct object_array *array)
391{
Jeff King189a1222014-10-18 22:03:19 -0400392 add_object_array_with_path(obj, name, array, S_IFINVALID, NULL);
Michael J Gruberafa15f32013-05-10 17:10:16 +0200393}
394
Jeff King68f49232014-10-15 18:34:19 -0400395/*
396 * Free all memory associated with an entry; the result is
397 * in an unspecified state and should not be examined.
398 */
399static void object_array_release_entry(struct object_array_entry *ent)
400{
401 if (ent->name != object_array_slopbuf)
402 free(ent->name);
Jeff King9e0c3c42014-10-15 18:42:57 -0400403 free(ent->path);
Jeff King68f49232014-10-15 18:34:19 -0400404}
405
Martin Ågren71992032017-09-23 01:34:53 +0200406struct object *object_array_pop(struct object_array *array)
407{
408 struct object *ret;
409
410 if (!array->nr)
411 return NULL;
412
413 ret = array->objects[array->nr - 1].item;
414 object_array_release_entry(&array->objects[array->nr - 1]);
415 array->nr--;
416 return ret;
417}
418
Michael Haggertyaeb4a512013-05-25 11:08:08 +0200419void object_array_filter(struct object_array *array,
420 object_array_each_func_t want, void *cb_data)
421{
422 unsigned nr = array->nr, src, dst;
423 struct object_array_entry *objects = array->objects;
424
425 for (src = dst = 0; src < nr; src++) {
426 if (want(&objects[src], cb_data)) {
427 if (src != dst)
428 objects[dst] = objects[src];
429 dst++;
Michael Haggerty31faeb22013-05-25 11:08:14 +0200430 } else {
Jeff King68f49232014-10-15 18:34:19 -0400431 object_array_release_entry(&objects[src]);
Michael Haggertyaeb4a512013-05-25 11:08:08 +0200432 }
433 }
434 array->nr = dst;
435}
436
Jeff King46be8232014-10-15 18:34:34 -0400437void object_array_clear(struct object_array *array)
438{
439 int i;
440 for (i = 0; i < array->nr; i++)
441 object_array_release_entry(&array->objects[i]);
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +0000442 FREE_AND_NULL(array->objects);
Jeff King46be8232014-10-15 18:34:34 -0400443 array->nr = array->alloc = 0;
444}
445
Michael Haggerty15065102013-05-25 11:08:10 +0200446/*
Jiang Xince1d6d92021-01-11 21:27:02 -0500447 * Return true if array already contains an entry.
Michael Haggerty15065102013-05-25 11:08:10 +0200448 */
Jiang Xince1d6d92021-01-11 21:27:02 -0500449static int contains_object(struct object_array *array,
450 const struct object *item, const char *name)
Michael Haggerty15065102013-05-25 11:08:10 +0200451{
452 unsigned nr = array->nr, i;
453 struct object_array_entry *object = array->objects;
454
455 for (i = 0; i < nr; i++, object++)
Jiang Xince1d6d92021-01-11 21:27:02 -0500456 if (item == object->item && !strcmp(object->name, name))
Michael Haggerty15065102013-05-25 11:08:10 +0200457 return 1;
458 return 0;
459}
460
Junio C Hamanob2a6d1c2009-01-17 22:27:08 -0800461void object_array_remove_duplicates(struct object_array *array)
462{
Michael Haggerty15065102013-05-25 11:08:10 +0200463 unsigned nr = array->nr, src;
Junio C Hamanob2a6d1c2009-01-17 22:27:08 -0800464 struct object_array_entry *objects = array->objects;
465
Michael Haggerty15065102013-05-25 11:08:10 +0200466 array->nr = 0;
467 for (src = 0; src < nr; src++) {
Jiang Xince1d6d92021-01-11 21:27:02 -0500468 if (!contains_object(array, objects[src].item,
469 objects[src].name)) {
Michael Haggerty15065102013-05-25 11:08:10 +0200470 if (src != array->nr)
471 objects[array->nr] = objects[src];
472 array->nr++;
Michael Haggerty31faeb22013-05-25 11:08:14 +0200473 } else {
Jeff King68f49232014-10-15 18:34:19 -0400474 object_array_release_entry(&objects[src]);
Junio C Hamanob2a6d1c2009-01-17 22:27:08 -0800475 }
Junio C Hamanob2a6d1c2009-01-17 22:27:08 -0800476 }
477}
Heiko Voigtbcc0a3e2012-03-29 09:21:21 +0200478
479void clear_object_flags(unsigned flags)
480{
481 int i;
482
Stefan Beller99bf1152018-05-08 12:37:24 -0700483 for (i=0; i < the_repository->parsed_objects->obj_hash_size; i++) {
484 struct object *obj = the_repository->parsed_objects->obj_hash[i];
Heiko Voigtbcc0a3e2012-03-29 09:21:21 +0200485 if (obj)
486 obj->flags &= ~flags;
487 }
488}
René Scharfe4ad315f2017-12-25 18:44:58 +0100489
René Scharfecd888842020-10-31 13:46:08 +0100490void repo_clear_commit_marks(struct repository *r, unsigned int flags)
René Scharfe4ad315f2017-12-25 18:44:58 +0100491{
492 int i;
493
René Scharfecd888842020-10-31 13:46:08 +0100494 for (i = 0; i < r->parsed_objects->obj_hash_size; i++) {
495 struct object *obj = r->parsed_objects->obj_hash[i];
René Scharfe4ad315f2017-12-25 18:44:58 +0100496 if (obj && obj->type == OBJ_COMMIT)
497 obj->flags &= ~flags;
498 }
499}
Stefan Beller90c62152018-03-23 18:20:55 +0100500
Stefan Beller99bf1152018-05-08 12:37:24 -0700501struct parsed_object_pool *parsed_object_pool_new(void)
502{
503 struct parsed_object_pool *o = xmalloc(sizeof(*o));
504 memset(o, 0, sizeof(*o));
Stefan Beller14ba97f2018-05-15 14:48:42 -0700505
506 o->blob_state = allocate_alloc_state();
507 o->tree_state = allocate_alloc_state();
508 o->commit_state = allocate_alloc_state();
509 o->tag_state = allocate_alloc_state();
510 o->object_state = allocate_alloc_state();
511
Stefan Bellereee45022018-05-17 15:51:52 -0700512 o->is_shallow = -1;
René Scharfeca56dad2021-03-13 17:17:22 +0100513 CALLOC_ARRAY(o->shallow_stat, 1);
Stefan Bellereee45022018-05-17 15:51:52 -0700514
Stefan Beller65ea9d42018-06-28 18:22:15 -0700515 o->buffer_slab = allocate_commit_buffer_slab();
516
Stefan Beller99bf1152018-05-08 12:37:24 -0700517 return o;
518}
519
Stefan Beller90c62152018-03-23 18:20:55 +0100520struct raw_object_store *raw_object_store_new(void)
521{
522 struct raw_object_store *o = xmalloc(sizeof(*o));
523
524 memset(o, 0, sizeof(*o));
Stefan Bellera80d72d2018-03-23 18:20:59 +0100525 INIT_LIST_HEAD(&o->packed_git_mru);
Colin Stolleyec485402019-11-27 16:24:53 -0600526 hashmap_init(&o->pack_map, pack_map_entry_cmp, NULL, 0);
Matheus Tavaresb1fc9da2020-01-15 23:39:52 -0300527 pthread_mutex_init(&o->replace_mutex, NULL);
Stefan Beller90c62152018-03-23 18:20:55 +0100528 return o;
529}
Stefan Beller97501e92018-03-23 18:20:58 +0100530
Neeraj Singhb3cecf42021-12-06 22:05:04 +0000531void free_object_directory(struct object_directory *odb)
Stefan Beller97501e92018-03-23 18:20:58 +0100532{
Jeff Kingf0eaf632018-11-12 09:50:39 -0500533 free(odb->path);
René Scharfed4e19e52019-01-06 17:45:39 +0100534 odb_clear_loose_cache(odb);
Jeff King263db402018-11-12 09:48:47 -0500535 free(odb);
Stefan Beller97501e92018-03-23 18:20:58 +0100536}
537
Jeff Kingf0eaf632018-11-12 09:50:39 -0500538static void free_object_directories(struct raw_object_store *o)
Stefan Beller97501e92018-03-23 18:20:58 +0100539{
Jeff Kingf0eaf632018-11-12 09:50:39 -0500540 while (o->odb) {
Jeff King263db402018-11-12 09:48:47 -0500541 struct object_directory *next;
Stefan Beller97501e92018-03-23 18:20:58 +0100542
Jeff Kingf0eaf632018-11-12 09:50:39 -0500543 next = o->odb->next;
544 free_object_directory(o->odb);
545 o->odb = next;
Stefan Beller97501e92018-03-23 18:20:58 +0100546 }
Eric Wongcf2dc1c2021-07-07 23:10:15 +0000547 kh_destroy_odb_path_map(o->odb_by_path);
548 o->odb_by_path = NULL;
Stefan Beller97501e92018-03-23 18:20:58 +0100549}
550
Stefan Beller90c62152018-03-23 18:20:55 +0100551void raw_object_store_clear(struct raw_object_store *o)
552{
Stefan Beller90c62152018-03-23 18:20:55 +0100553 FREE_AND_NULL(o->alternate_db);
Stefan Beller7a1dc602018-05-17 11:29:57 -0700554
555 oidmap_free(o->replace_map, 1);
Stefan Bellerd5873072018-05-09 16:40:58 -0700556 FREE_AND_NULL(o->replace_map);
Matheus Tavaresb1fc9da2020-01-15 23:39:52 -0300557 pthread_mutex_destroy(&o->replace_mutex);
Stefan Beller97501e92018-03-23 18:20:58 +0100558
Jonathan Tan85277502018-07-11 15:42:41 -0700559 free_commit_graph(o->commit_graph);
560 o->commit_graph = NULL;
561 o->commit_graph_attempted = 0;
562
Jeff Kingf0eaf632018-11-12 09:50:39 -0500563 free_object_directories(o);
564 o->odb_tail = NULL;
565 o->loaded_alternates = 0;
Stefan Bellera80d72d2018-03-23 18:20:59 +0100566
567 INIT_LIST_HEAD(&o->packed_git_mru);
Derrick Stolee2d511cf2019-05-17 11:41:49 -0700568 close_object_store(o);
Stefan Bellerd0b59862018-03-23 18:21:00 +0100569 o->packed_git = NULL;
Colin Stolleyec485402019-11-27 16:24:53 -0600570
Elijah Newren6da1a252020-11-02 18:55:05 +0000571 hashmap_clear(&o->pack_map);
Stefan Beller90c62152018-03-23 18:20:55 +0100572}
Stefan Beller99bf1152018-05-08 12:37:24 -0700573
574void parsed_object_pool_clear(struct parsed_object_pool *o)
575{
576 /*
Stefan Beller99bf1152018-05-08 12:37:24 -0700577 * As objects are allocated in slabs (see alloc.c), we do
578 * not need to free each object, but each slab instead.
Stefan Beller14ba97f2018-05-15 14:48:42 -0700579 *
580 * Before doing so, we need to free any additional memory
581 * the objects may hold.
Stefan Beller99bf1152018-05-08 12:37:24 -0700582 */
Stefan Beller14ba97f2018-05-15 14:48:42 -0700583 unsigned i;
584
585 for (i = 0; i < o->obj_hash_size; i++) {
586 struct object *obj = o->obj_hash[i];
587
588 if (!obj)
589 continue;
590
591 if (obj->type == OBJ_TREE)
592 free_tree_buffer((struct tree*)obj);
593 else if (obj->type == OBJ_COMMIT)
Stefan Beller6a7895f2018-12-14 16:09:40 -0800594 release_commit_memory(o, (struct commit*)obj);
Stefan Beller14ba97f2018-05-15 14:48:42 -0700595 else if (obj->type == OBJ_TAG)
596 release_tag_memory((struct tag*)obj);
597 }
598
599 FREE_AND_NULL(o->obj_hash);
600 o->obj_hash_size = 0;
601
Stefan Beller65ea9d42018-06-28 18:22:15 -0700602 free_commit_buffer_slab(o->buffer_slab);
603 o->buffer_slab = NULL;
604
Stefan Beller14ba97f2018-05-15 14:48:42 -0700605 clear_alloc_state(o->blob_state);
606 clear_alloc_state(o->tree_state);
607 clear_alloc_state(o->commit_state);
608 clear_alloc_state(o->tag_state);
609 clear_alloc_state(o->object_state);
Josh Steadmon96b07102019-02-07 12:05:54 -0800610 stat_validity_clear(o->shallow_stat);
Stefan Beller14ba97f2018-05-15 14:48:42 -0700611 FREE_AND_NULL(o->blob_state);
612 FREE_AND_NULL(o->tree_state);
613 FREE_AND_NULL(o->commit_state);
614 FREE_AND_NULL(o->tag_state);
615 FREE_AND_NULL(o->object_state);
Josh Steadmon96b07102019-02-07 12:05:54 -0800616 FREE_AND_NULL(o->shallow_stat);
Stefan Beller99bf1152018-05-08 12:37:24 -0700617}