🌐 AI搜索 & 代理 主页
blob: 2c61e4c86217e633d2e28acd0b3ae654584ede7d [file] [log] [blame]
Elijah Newrenbc5c5ec2023-05-16 06:33:571#include "git-compat-util.h"
Elijah Newrenf394e092023-03-21 06:25:542#include "gettext.h"
Elijah Newren41771fa2023-02-24 00:09:273#include "hex.h"
Daniel Barkalow175785e2005-04-18 18:39:484#include "object.h"
Stefan Beller47f351e2018-04-12 00:21:065#include "replace-object.h"
Elijah Newren87bed172023-04-11 07:41:536#include "object-file.h"
Stefan Bellercbd53a22018-05-15 23:42:157#include "object-store.h"
Daniel Barkalowe9eefa62005-04-28 14:46:338#include "blob.h"
Elijah Newren90cbae92023-05-16 06:33:489#include "statinfo.h"
Daniel Barkalowe9eefa62005-04-28 14:46:3310#include "tree.h"
11#include "commit.h"
Daniel Barkalowe9eefa62005-04-28 14:46:3312#include "tag.h"
Stefan Beller14ba97f2018-05-15 21:48:4213#include "alloc.h"
Stefan Bellerd0b59862018-03-23 17:21:0014#include "packfile.h"
Jonathan Tan85277502018-07-11 22:42:4115#include "commit-graph.h"
Daniel Barkalow175785e2005-04-18 18:39:4816
Linus Torvaldsfc046a72006-06-30 04:38:5517unsigned int get_max_object_index(void)
18{
Stefan Beller99bf1152018-05-08 19:37:2419 return the_repository->parsed_objects->obj_hash_size;
Linus Torvaldsfc046a72006-06-30 04:38:5520}
21
22struct object *get_indexed_object(unsigned int idx)
23{
Stefan Beller99bf1152018-05-08 19:37:2424 return the_repository->parsed_objects->obj_hash[idx];
Linus Torvaldsfc046a72006-06-30 04:38:5525}
Daniel Barkalow175785e2005-04-18 18:39:4826
Nicolas Pitredf843662007-02-26 19:55:5827static const char *object_type_strings[] = {
28 NULL, /* OBJ_NONE = 0 */
29 "commit", /* OBJ_COMMIT = 1 */
30 "tree", /* OBJ_TREE = 2 */
31 "blob", /* OBJ_BLOB = 3 */
32 "tag", /* OBJ_TAG = 4 */
Linus Torvalds885a86a2006-06-14 23:45:1333};
34
Brandon Williamsdebca9d2018-02-14 18:59:2435const char *type_name(unsigned int type)
Nicolas Pitredf843662007-02-26 19:55:5836{
37 if (type >= ARRAY_SIZE(object_type_strings))
38 return NULL;
39 return object_type_strings[type];
40}
41
Johannes Schindelinfe8e3b72014-09-10 13:52:4442int type_from_string_gently(const char *str, ssize_t len, int gentle)
Nicolas Pitredf843662007-02-26 19:55:5843{
44 int i;
45
Johannes Schindelinfe8e3b72014-09-10 13:52:4446 if (len < 0)
47 len = strlen(str);
48
Nicolas Pitredf843662007-02-26 19:55:5849 for (i = 1; i < ARRAY_SIZE(object_type_strings); i++)
Jeff Kingb7994af2015-04-17 14:52:4850 if (!strncmp(str, object_type_strings[i], len) &&
51 object_type_strings[i][len] == '\0')
Nicolas Pitredf843662007-02-26 19:55:5852 return i;
Johannes Schindelinfe8e3b72014-09-10 13:52:4453
54 if (gentle)
55 return -1;
56
Nguyễn Thái Ngọc Duy42246582018-07-21 07:49:3357 die(_("invalid object type \"%s\""), str);
Nicolas Pitredf843662007-02-26 19:55:5858}
59
Michael Haggerty33bef7e2014-02-28 16:29:1760/*
61 * Return a numerical hash value between 0 and n-1 for the object with
62 * the specified sha1. n must be a power of 2. Please note that the
63 * return value is *not* consistent across computer architectures.
64 */
Jeff King46931d32019-06-20 07:41:1765static unsigned int hash_obj(const struct object_id *oid, unsigned int n)
Linus Torvalds0556a112006-06-30 18:20:3366{
Jeff Kingd40abc82019-06-20 07:41:4967 return oidhash(oid) & (n - 1);
Linus Torvalds0556a112006-06-30 18:20:3368}
69
Michael Haggerty33bef7e2014-02-28 16:29:1770/*
71 * Insert obj into the hash table hash, which has length size (which
72 * must be a power of 2). On collisions, simply overflow to the next
73 * empty bucket.
74 */
Linus Torvalds0556a112006-06-30 18:20:3375static void insert_obj_hash(struct object *obj, struct object **hash, unsigned int size)
76{
Jeff King46931d32019-06-20 07:41:1777 unsigned int j = hash_obj(&obj->oid, size);
Linus Torvalds0556a112006-06-30 18:20:3378
79 while (hash[j]) {
80 j++;
81 if (j >= size)
82 j = 0;
83 }
84 hash[j] = obj;
85}
86
Michael Haggerty33bef7e2014-02-28 16:29:1787/*
88 * Look up the record for the given sha1 in the hash map stored in
89 * obj_hash. Return NULL if it was not found.
90 */
Jeff Kingd0229ab2019-06-20 07:41:1491struct object *lookup_object(struct repository *r, const struct object_id *oid)
Daniel Barkalow175785e2005-04-18 18:39:4892{
Jeff King9a414482013-05-01 20:34:5093 unsigned int i, first;
Linus Torvalds0556a112006-06-30 18:20:3394 struct object *obj;
95
Stefan Beller94c09a72018-06-29 01:22:0796 if (!r->parsed_objects->obj_hash)
Linus Torvalds0556a112006-06-30 18:20:3397 return NULL;
98
Jeff King46931d32019-06-20 07:41:1799 first = i = hash_obj(oid, r->parsed_objects->obj_hash_size);
Stefan Beller94c09a72018-06-29 01:22:07100 while ((obj = r->parsed_objects->obj_hash[i]) != NULL) {
Jeff Kingd0229ab2019-06-20 07:41:14101 if (oideq(oid, &obj->oid))
Linus Torvalds0556a112006-06-30 18:20:33102 break;
103 i++;
Stefan Beller94c09a72018-06-29 01:22:07104 if (i == r->parsed_objects->obj_hash_size)
Linus Torvalds0556a112006-06-30 18:20:33105 i = 0;
106 }
Jeff King9a414482013-05-01 20:34:50107 if (obj && i != first) {
108 /*
109 * Move object to where we started to look for it so
110 * that we do not need to walk the hash table the next
111 * time we look for it.
112 */
Stefan Beller94c09a72018-06-29 01:22:07113 SWAP(r->parsed_objects->obj_hash[i],
114 r->parsed_objects->obj_hash[first]);
Jeff King9a414482013-05-01 20:34:50115 }
Linus Torvalds0556a112006-06-30 18:20:33116 return obj;
117}
118
Michael Haggerty33bef7e2014-02-28 16:29:17119/*
120 * Increase the size of the hash map stored in obj_hash to the next
121 * power of 2 (but at least 32). Copy the existing values to the new
122 * hash map.
123 */
Stefan Beller346a8172018-05-08 19:37:34124static void grow_object_hash(struct repository *r)
Linus Torvalds0556a112006-06-30 18:20:33125{
126 int i;
Nicolas Pitre9f36c9b2013-09-10 22:17:12127 /*
128 * Note that this size must always be power-of-2 to match hash_obj
129 * above.
130 */
Stefan Beller346a8172018-05-08 19:37:34131 int new_hash_size = r->parsed_objects->obj_hash_size < 32 ? 32 : 2 * r->parsed_objects->obj_hash_size;
Linus Torvalds0556a112006-06-30 18:20:33132 struct object **new_hash;
133
René Scharfeca56dad2021-03-13 16:17:22134 CALLOC_ARRAY(new_hash, new_hash_size);
Stefan Beller346a8172018-05-08 19:37:34135 for (i = 0; i < r->parsed_objects->obj_hash_size; i++) {
136 struct object *obj = r->parsed_objects->obj_hash[i];
137
Linus Torvalds0556a112006-06-30 18:20:33138 if (!obj)
139 continue;
140 insert_obj_hash(obj, new_hash, new_hash_size);
141 }
Stefan Beller346a8172018-05-08 19:37:34142 free(r->parsed_objects->obj_hash);
143 r->parsed_objects->obj_hash = new_hash;
144 r->parsed_objects->obj_hash_size = new_hash_size;
Daniel Barkalow175785e2005-04-18 18:39:48145}
146
Jeff Kinga3785092019-06-20 07:41:21147void *create_object(struct repository *r, const struct object_id *oid, void *o)
Daniel Barkalow175785e2005-04-18 18:39:48148{
Linus Torvalds100c5f32007-04-17 05:11:43149 struct object *obj = o;
150
Daniel Barkalow175785e2005-04-18 18:39:48151 obj->parsed = 0;
Linus Torvalds0556a112006-06-30 18:20:33152 obj->flags = 0;
Jeff Kinga3785092019-06-20 07:41:21153 oidcpy(&obj->oid, oid);
Daniel Barkalow175785e2005-04-18 18:39:48154
Stefan Beller341e45e2018-05-08 19:37:35155 if (r->parsed_objects->obj_hash_size - 1 <= r->parsed_objects->nr_objs * 2)
156 grow_object_hash(r);
Johannes Schindelin070879c2006-02-12 01:57:57157
Stefan Beller341e45e2018-05-08 19:37:35158 insert_obj_hash(obj, r->parsed_objects->obj_hash,
159 r->parsed_objects->obj_hash_size);
160 r->parsed_objects->nr_objs++;
Linus Torvalds100c5f32007-04-17 05:11:43161 return obj;
Daniel Barkalow175785e2005-04-18 18:39:48162}
163
Abhishek Kumar6da43d92020-06-17 09:14:08164void *object_as_type(struct object *obj, enum object_type type, int quiet)
Jeff King8ff226a2014-07-13 06:42:03165{
166 if (obj->type == type)
167 return obj;
168 else if (obj->type == OBJ_NONE) {
Jeff Kingd66bebc2014-07-13 06:42:12169 if (type == OBJ_COMMIT)
Abhishek Kumar6da43d92020-06-17 09:14:08170 init_commit_node((struct commit *) obj);
SZEDER Gábor4468d442019-01-27 13:08:32171 else
172 obj->type = type;
Jeff King8ff226a2014-07-13 06:42:03173 return obj;
174 }
175 else {
176 if (!quiet)
Nguyễn Thái Ngọc Duy42246582018-07-21 07:49:33177 error(_("object %s is a %s, not a %s"),
brian m. carlsonf2fd0762015-11-10 02:22:28178 oid_to_hex(&obj->oid),
Brandon Williamsdebca9d2018-02-14 18:59:24179 type_name(obj->type), type_name(type));
Jeff King8ff226a2014-07-13 06:42:03180 return NULL;
181 }
182}
183
Jeff King45a187c2021-04-13 07:16:36184struct object *lookup_unknown_object(struct repository *r, const struct object_id *oid)
barkalow@iabervon.org66e481b2005-08-02 23:45:48185{
Jeff King45a187c2021-04-13 07:16:36186 struct object *obj = lookup_object(r, oid);
Linus Torvalds100c5f32007-04-17 05:11:43187 if (!obj)
Jeff King45a187c2021-04-13 07:16:36188 obj = create_object(r, oid, alloc_object_node(r));
barkalow@iabervon.org66e481b2005-08-02 23:45:48189 return obj;
190}
191
Jeff King74630642021-06-22 16:06:41192struct object *lookup_object_by_type(struct repository *r,
193 const struct object_id *oid,
194 enum object_type type)
195{
196 switch (type) {
197 case OBJ_COMMIT:
198 return (struct object *)lookup_commit(r, oid);
199 case OBJ_TREE:
200 return (struct object *)lookup_tree(r, oid);
201 case OBJ_TAG:
202 return (struct object *)lookup_tag(r, oid);
203 case OBJ_BLOB:
204 return (struct object *)lookup_blob(r, oid);
205 default:
Ævar Arnfjörð Bjarmasoneafd6e72021-12-07 11:05:54206 BUG("unknown object type %d", type);
Jeff King74630642021-06-22 16:06:41207 }
208}
209
Stefan Beller108ed1a2018-06-29 01:22:18210struct 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 20:30:02211{
212 struct object *obj;
Stefan Beller8e92e8f2013-07-17 22:09:42213 *eaten_p = 0;
Junio C Hamano9f613dd2006-09-15 20:30:02214
Jim Meyeringcc216822007-12-21 10:56:32215 obj = NULL;
Nicolas Pitre21666f12007-02-26 19:55:59216 if (type == OBJ_BLOB) {
Stefan Beller108ed1a2018-06-29 01:22:18217 struct blob *blob = lookup_blob(r, oid);
Jim Meyeringcc216822007-12-21 10:56:32218 if (blob) {
Jeff Kingc1166ca2022-12-13 11:11:57219 parse_blob_buffer(blob);
Jim Meyeringcc216822007-12-21 10:56:32220 obj = &blob->object;
221 }
Nicolas Pitre21666f12007-02-26 19:55:59222 } else if (type == OBJ_TREE) {
Stefan Beller108ed1a2018-06-29 01:22:18223 struct tree *tree = lookup_tree(r, oid);
Jim Meyeringcc216822007-12-21 10:56:32224 if (tree) {
225 obj = &tree->object;
Junio C Hamano68be2fe2011-11-17 06:04:13226 if (!tree->buffer)
227 tree->object.parsed = 0;
Jim Meyeringcc216822007-12-21 10:56:32228 if (!tree->object.parsed) {
Martin Koeglerd0b8c9e2008-02-03 21:22:39229 if (parse_tree_buffer(tree, buffer, size))
230 return NULL;
Stefan Beller8e92e8f2013-07-17 22:09:42231 *eaten_p = 1;
Jim Meyeringcc216822007-12-21 10:56:32232 }
Junio C Hamano9f613dd2006-09-15 20:30:02233 }
Nicolas Pitre21666f12007-02-26 19:55:59234 } else if (type == OBJ_COMMIT) {
Stefan Beller108ed1a2018-06-29 01:22:18235 struct commit *commit = lookup_commit(r, oid);
Jim Meyeringcc216822007-12-21 10:56:32236 if (commit) {
Stefan Beller108ed1a2018-06-29 01:22:18237 if (parse_commit_buffer(r, commit, buffer, size, 1))
Martin Koeglerd0b8c9e2008-02-03 21:22:39238 return NULL;
Jeff King51b27742022-09-22 10:15:38239 if (save_commit_buffer &&
240 !get_cached_commit_buffer(r, commit, NULL)) {
Stefan Beller108ed1a2018-06-29 01:22:18241 set_commit_buffer(r, commit, buffer, size);
Stefan Beller8e92e8f2013-07-17 22:09:42242 *eaten_p = 1;
Jim Meyeringcc216822007-12-21 10:56:32243 }
244 obj = &commit->object;
Junio C Hamano9f613dd2006-09-15 20:30:02245 }
Nicolas Pitre21666f12007-02-26 19:55:59246 } else if (type == OBJ_TAG) {
Stefan Beller108ed1a2018-06-29 01:22:18247 struct tag *tag = lookup_tag(r, oid);
Jim Meyeringcc216822007-12-21 10:56:32248 if (tag) {
Stefan Beller108ed1a2018-06-29 01:22:18249 if (parse_tag_buffer(r, tag, buffer, size))
Martin Koeglerd0b8c9e2008-02-03 21:22:39250 return NULL;
Jim Meyeringcc216822007-12-21 10:56:32251 obj = &tag->object;
252 }
Junio C Hamano9f613dd2006-09-15 20:30:02253 } else {
Nguyễn Thái Ngọc Duy42246582018-07-21 07:49:33254 warning(_("object %s has unknown type id %d"), oid_to_hex(oid), type);
Junio C Hamano9f613dd2006-09-15 20:30:02255 obj = NULL;
256 }
Junio C Hamano9f613dd2006-09-15 20:30:02257 return obj;
258}
259
brian m. carlsonc251c832017-05-06 22:10:38260struct object *parse_object_or_die(const struct object_id *oid,
Jeff King75a95492013-03-17 08:22:36261 const char *name)
262{
Stefan Beller109cd762018-06-29 01:21:51263 struct object *o = parse_object(the_repository, oid);
Jeff King75a95492013-03-17 08:22:36264 if (o)
265 return o;
266
brian m. carlsonc251c832017-05-06 22:10:38267 die(_("unable to parse object: %s"), name ? name : oid_to_hex(oid));
Jeff King75a95492013-03-17 08:22:36268}
269
Jeff Kingc868d8e2022-09-06 23:01:34270struct object *parse_object_with_flags(struct repository *r,
271 const struct object_id *oid,
272 enum parse_object_flags flags)
Daniel Barkalowe9eefa62005-04-28 14:46:33273{
Jeff Kingc868d8e2022-09-06 23:01:34274 int skip_hash = !!(flags & PARSE_OBJECT_SKIP_HASH_CHECK);
Junio C Hamanoc4584ae2005-06-27 10:33:33275 unsigned long size;
Nicolas Pitre21666f12007-02-26 19:55:59276 enum object_type type;
Junio C Hamano9f613dd2006-09-15 20:30:02277 int eaten;
Stefan Beller8e4b0b62018-06-29 01:22:19278 const struct object_id *repl = lookup_replace_object(r, oid);
Jeff Kingccdc6032012-01-05 21:00:01279 void *buffer;
280 struct object *obj;
Junio C Hamano9f613dd2006-09-15 20:30:02281
Jeff Kingd0229ab2019-06-20 07:41:14282 obj = lookup_object(r, oid);
Jeff Kingccdc6032012-01-05 21:00:01283 if (obj && obj->parsed)
284 return obj;
285
Jeff King9a8c3c42022-09-06 23:06:25286 if (skip_hash) {
287 struct commit *commit = lookup_commit_in_graph(r, repl);
288 if (commit)
289 return &commit->object;
290 }
291
Ævar Arnfjörð Bjarmason40286ca2022-11-21 19:26:55292 if ((!obj || obj->type == OBJ_BLOB) &&
Jeff King8db2dad2022-11-17 22:41:16293 oid_object_info(r, oid, NULL) == OBJ_BLOB) {
Jeff Kingc868d8e2022-09-06 23:01:34294 if (!skip_hash && stream_object_signature(r, repl) < 0) {
Jeff King01f8d592019-01-07 08:40:34295 error(_("hash mismatch %s"), oid_to_hex(oid));
Nguyễn Thái Ngọc Duy090ea122012-03-07 10:54:18296 return NULL;
297 }
Jeff Kingc1166ca2022-12-13 11:11:57298 parse_blob_buffer(lookup_blob(r, oid));
Jeff Kingd0229ab2019-06-20 07:41:14299 return lookup_object(r, oid);
Nguyễn Thái Ngọc Duy090ea122012-03-07 10:54:18300 }
301
Stefan Bellerd1a69022018-11-14 00:12:49302 buffer = repo_read_object_file(r, oid, &type, &size);
Junio C Hamanoc4584ae2005-06-27 10:33:33303 if (buffer) {
Jeff Kingc868d8e2022-09-06 23:01:34304 if (!skip_hash &&
305 check_object_signature(r, repl, buffer, size, type) < 0) {
Carlos Rica0b1f1132007-05-25 01:46:22306 free(buffer);
Jeff King01f8d592019-01-07 08:40:34307 error(_("hash mismatch %s"), oid_to_hex(repl));
Linus Torvaldsacdeec62007-03-20 17:05:20308 return NULL;
309 }
Junio C Hamano9f613dd2006-09-15 20:30:02310
Stefan Beller8e4b0b62018-06-29 01:22:19311 obj = parse_object_buffer(r, oid, type, size,
Stefan Beller1ec5bfd2018-06-29 01:21:53312 buffer, &eaten);
Junio C Hamano9f613dd2006-09-15 20:30:02313 if (!eaten)
314 free(buffer);
Nicolas Pitrebd2c39f2005-05-06 17:48:34315 return obj;
Daniel Barkalowe9eefa62005-04-28 14:46:33316 }
317 return NULL;
318}
barkalow@iabervon.org66e481b2005-08-02 23:45:48319
Jeff Kingc868d8e2022-09-06 23:01:34320struct object *parse_object(struct repository *r, const struct object_id *oid)
321{
322 return parse_object_with_flags(r, oid, 0);
323}
324
barkalow@iabervon.org66e481b2005-08-02 23:45:48325struct object_list *object_list_insert(struct object *item,
326 struct object_list **list_p)
327{
328 struct object_list *new_list = xmalloc(sizeof(struct object_list));
Jared Hance55b4e9e2010-09-05 19:36:33329 new_list->item = item;
330 new_list->next = *list_p;
331 *list_p = new_list;
332 return new_list;
barkalow@iabervon.org66e481b2005-08-02 23:45:48333}
334
barkalow@iabervon.org66e481b2005-08-02 23:45:48335int object_list_contains(struct object_list *list, struct object *obj)
336{
337 while (list) {
338 if (list->item == obj)
339 return 1;
340 list = list->next;
341 }
342 return 0;
343}
Linus Torvalds1f1e8952006-06-20 00:42:35344
Jeff Kingacac50d2020-02-13 02:16:33345void object_list_free(struct object_list **list)
346{
347 while (*list) {
348 struct object_list *p = *list;
349 *list = p->next;
350 free(p);
351 }
352}
353
Michael Haggerty31faeb22013-05-25 09:08:14354/*
355 * A zero-length string to which object_array_entry::name can be
356 * initialized without requiring a malloc/free.
357 */
358static char object_array_slopbuf[1];
359
Taylor Blaufe903552023-05-08 17:38:06360void object_array_init(struct object_array *array)
361{
362 struct object_array blank = OBJECT_ARRAY_INIT;
363 memcpy(array, &blank, sizeof(*array));
364}
365
Jeff King9e0c3c42014-10-15 22:42:57366void add_object_array_with_path(struct object *obj, const char *name,
367 struct object_array *array,
368 unsigned mode, const char *path)
Martin Koeglere5709a42007-04-22 16:43:58369{
Linus Torvalds1f1e8952006-06-20 00:42:35370 unsigned nr = array->nr;
371 unsigned alloc = array->alloc;
372 struct object_array_entry *objects = array->objects;
Michael Haggerty31faeb22013-05-25 09:08:14373 struct object_array_entry *entry;
Linus Torvalds1f1e8952006-06-20 00:42:35374
375 if (nr >= alloc) {
376 alloc = (alloc + 32) * 2;
René Scharfe2756ca42014-09-16 18:56:57377 REALLOC_ARRAY(objects, alloc);
Linus Torvalds1f1e8952006-06-20 00:42:35378 array->alloc = alloc;
379 array->objects = objects;
380 }
Michael Haggerty31faeb22013-05-25 09:08:14381 entry = &objects[nr];
382 entry->item = obj;
383 if (!name)
384 entry->name = NULL;
385 else if (!*name)
386 /* Use our own empty string instead of allocating one: */
387 entry->name = object_array_slopbuf;
388 else
389 entry->name = xstrdup(name);
390 entry->mode = mode;
Jeff King9e0c3c42014-10-15 22:42:57391 if (path)
392 entry->path = xstrdup(path);
393 else
394 entry->path = NULL;
Linus Torvalds1f1e8952006-06-20 00:42:35395 array->nr = ++nr;
396}
Junio C Hamanob2a6d1c2009-01-18 06:27:08397
Michael J Gruberafa15f32013-05-10 15:10:16398void add_object_array(struct object *obj, const char *name, struct object_array *array)
399{
Jeff King189a1222014-10-19 02:03:19400 add_object_array_with_path(obj, name, array, S_IFINVALID, NULL);
Michael J Gruberafa15f32013-05-10 15:10:16401}
402
Jeff King68f49232014-10-15 22:34:19403/*
404 * Free all memory associated with an entry; the result is
405 * in an unspecified state and should not be examined.
406 */
407static void object_array_release_entry(struct object_array_entry *ent)
408{
409 if (ent->name != object_array_slopbuf)
410 free(ent->name);
Jeff King9e0c3c42014-10-15 22:42:57411 free(ent->path);
Jeff King68f49232014-10-15 22:34:19412}
413
Martin Ågren71992032017-09-22 23:34:53414struct object *object_array_pop(struct object_array *array)
415{
416 struct object *ret;
417
418 if (!array->nr)
419 return NULL;
420
421 ret = array->objects[array->nr - 1].item;
422 object_array_release_entry(&array->objects[array->nr - 1]);
423 array->nr--;
424 return ret;
425}
426
Michael Haggertyaeb4a512013-05-25 09:08:08427void object_array_filter(struct object_array *array,
428 object_array_each_func_t want, void *cb_data)
429{
430 unsigned nr = array->nr, src, dst;
431 struct object_array_entry *objects = array->objects;
432
433 for (src = dst = 0; src < nr; src++) {
434 if (want(&objects[src], cb_data)) {
435 if (src != dst)
436 objects[dst] = objects[src];
437 dst++;
Michael Haggerty31faeb22013-05-25 09:08:14438 } else {
Jeff King68f49232014-10-15 22:34:19439 object_array_release_entry(&objects[src]);
Michael Haggertyaeb4a512013-05-25 09:08:08440 }
441 }
442 array->nr = dst;
443}
444
Jeff King46be8232014-10-15 22:34:34445void object_array_clear(struct object_array *array)
446{
447 int i;
448 for (i = 0; i < array->nr; i++)
449 object_array_release_entry(&array->objects[i]);
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46450 FREE_AND_NULL(array->objects);
Jeff King46be8232014-10-15 22:34:34451 array->nr = array->alloc = 0;
452}
453
Michael Haggerty15065102013-05-25 09:08:10454/*
Jiang Xince1d6d92021-01-12 02:27:02455 * Return true if array already contains an entry.
Michael Haggerty15065102013-05-25 09:08:10456 */
Jiang Xince1d6d92021-01-12 02:27:02457static int contains_object(struct object_array *array,
458 const struct object *item, const char *name)
Michael Haggerty15065102013-05-25 09:08:10459{
460 unsigned nr = array->nr, i;
461 struct object_array_entry *object = array->objects;
462
463 for (i = 0; i < nr; i++, object++)
Jiang Xince1d6d92021-01-12 02:27:02464 if (item == object->item && !strcmp(object->name, name))
Michael Haggerty15065102013-05-25 09:08:10465 return 1;
466 return 0;
467}
468
Junio C Hamanob2a6d1c2009-01-18 06:27:08469void object_array_remove_duplicates(struct object_array *array)
470{
Michael Haggerty15065102013-05-25 09:08:10471 unsigned nr = array->nr, src;
Junio C Hamanob2a6d1c2009-01-18 06:27:08472 struct object_array_entry *objects = array->objects;
473
Michael Haggerty15065102013-05-25 09:08:10474 array->nr = 0;
475 for (src = 0; src < nr; src++) {
Jiang Xince1d6d92021-01-12 02:27:02476 if (!contains_object(array, objects[src].item,
477 objects[src].name)) {
Michael Haggerty15065102013-05-25 09:08:10478 if (src != array->nr)
479 objects[array->nr] = objects[src];
480 array->nr++;
Michael Haggerty31faeb22013-05-25 09:08:14481 } else {
Jeff King68f49232014-10-15 22:34:19482 object_array_release_entry(&objects[src]);
Junio C Hamanob2a6d1c2009-01-18 06:27:08483 }
Junio C Hamanob2a6d1c2009-01-18 06:27:08484 }
485}
Heiko Voigtbcc0a3e2012-03-29 07:21:21486
487void clear_object_flags(unsigned flags)
488{
489 int i;
490
Stefan Beller99bf1152018-05-08 19:37:24491 for (i=0; i < the_repository->parsed_objects->obj_hash_size; i++) {
492 struct object *obj = the_repository->parsed_objects->obj_hash[i];
Heiko Voigtbcc0a3e2012-03-29 07:21:21493 if (obj)
494 obj->flags &= ~flags;
495 }
496}
René Scharfe4ad315f2017-12-25 17:44:58497
René Scharfecd888842020-10-31 12:46:08498void repo_clear_commit_marks(struct repository *r, unsigned int flags)
René Scharfe4ad315f2017-12-25 17:44:58499{
500 int i;
501
René Scharfecd888842020-10-31 12:46:08502 for (i = 0; i < r->parsed_objects->obj_hash_size; i++) {
503 struct object *obj = r->parsed_objects->obj_hash[i];
René Scharfe4ad315f2017-12-25 17:44:58504 if (obj && obj->type == OBJ_COMMIT)
505 obj->flags &= ~flags;
506 }
507}
Stefan Beller90c62152018-03-23 17:20:55508
Stefan Beller99bf1152018-05-08 19:37:24509struct parsed_object_pool *parsed_object_pool_new(void)
510{
511 struct parsed_object_pool *o = xmalloc(sizeof(*o));
512 memset(o, 0, sizeof(*o));
Stefan Beller14ba97f2018-05-15 21:48:42513
514 o->blob_state = allocate_alloc_state();
515 o->tree_state = allocate_alloc_state();
516 o->commit_state = allocate_alloc_state();
517 o->tag_state = allocate_alloc_state();
518 o->object_state = allocate_alloc_state();
519
Stefan Bellereee45022018-05-17 22:51:52520 o->is_shallow = -1;
René Scharfeca56dad2021-03-13 16:17:22521 CALLOC_ARRAY(o->shallow_stat, 1);
Stefan Bellereee45022018-05-17 22:51:52522
Stefan Beller65ea9d42018-06-29 01:22:15523 o->buffer_slab = allocate_commit_buffer_slab();
524
Stefan Beller99bf1152018-05-08 19:37:24525 return o;
526}
527
Stefan Beller90c62152018-03-23 17:20:55528struct raw_object_store *raw_object_store_new(void)
529{
530 struct raw_object_store *o = xmalloc(sizeof(*o));
531
532 memset(o, 0, sizeof(*o));
Stefan Bellera80d72d2018-03-23 17:20:59533 INIT_LIST_HEAD(&o->packed_git_mru);
Colin Stolleyec485402019-11-27 22:24:53534 hashmap_init(&o->pack_map, pack_map_entry_cmp, NULL, 0);
Matheus Tavaresb1fc9da2020-01-16 02:39:52535 pthread_mutex_init(&o->replace_mutex, NULL);
Stefan Beller90c62152018-03-23 17:20:55536 return o;
537}
Stefan Beller97501e92018-03-23 17:20:58538
Neeraj Singhb3cecf42021-12-06 22:05:04539void free_object_directory(struct object_directory *odb)
Stefan Beller97501e92018-03-23 17:20:58540{
Jeff Kingf0eaf632018-11-12 14:50:39541 free(odb->path);
René Scharfed4e19e52019-01-06 16:45:39542 odb_clear_loose_cache(odb);
Jeff King263db402018-11-12 14:48:47543 free(odb);
Stefan Beller97501e92018-03-23 17:20:58544}
545
Jeff Kingf0eaf632018-11-12 14:50:39546static void free_object_directories(struct raw_object_store *o)
Stefan Beller97501e92018-03-23 17:20:58547{
Jeff Kingf0eaf632018-11-12 14:50:39548 while (o->odb) {
Jeff King263db402018-11-12 14:48:47549 struct object_directory *next;
Stefan Beller97501e92018-03-23 17:20:58550
Jeff Kingf0eaf632018-11-12 14:50:39551 next = o->odb->next;
552 free_object_directory(o->odb);
553 o->odb = next;
Stefan Beller97501e92018-03-23 17:20:58554 }
Eric Wongcf2dc1c2021-07-07 23:10:15555 kh_destroy_odb_path_map(o->odb_by_path);
556 o->odb_by_path = NULL;
Stefan Beller97501e92018-03-23 17:20:58557}
558
Stefan Beller90c62152018-03-23 17:20:55559void raw_object_store_clear(struct raw_object_store *o)
560{
Stefan Beller90c62152018-03-23 17:20:55561 FREE_AND_NULL(o->alternate_db);
Stefan Beller7a1dc602018-05-17 18:29:57562
563 oidmap_free(o->replace_map, 1);
Stefan Bellerd5873072018-05-09 23:40:58564 FREE_AND_NULL(o->replace_map);
Matheus Tavaresb1fc9da2020-01-16 02:39:52565 pthread_mutex_destroy(&o->replace_mutex);
Stefan Beller97501e92018-03-23 17:20:58566
Jonathan Tan85277502018-07-11 22:42:41567 free_commit_graph(o->commit_graph);
568 o->commit_graph = NULL;
569 o->commit_graph_attempted = 0;
570
Jeff Kingf0eaf632018-11-12 14:50:39571 free_object_directories(o);
572 o->odb_tail = NULL;
573 o->loaded_alternates = 0;
Stefan Bellera80d72d2018-03-23 17:20:59574
575 INIT_LIST_HEAD(&o->packed_git_mru);
Derrick Stolee2d511cf2019-05-17 18:41:49576 close_object_store(o);
Stefan Bellerd0b59862018-03-23 17:21:00577 o->packed_git = NULL;
Colin Stolleyec485402019-11-27 22:24:53578
Elijah Newren6da1a252020-11-02 18:55:05579 hashmap_clear(&o->pack_map);
Stefan Beller90c62152018-03-23 17:20:55580}
Stefan Beller99bf1152018-05-08 19:37:24581
582void parsed_object_pool_clear(struct parsed_object_pool *o)
583{
584 /*
Stefan Beller99bf1152018-05-08 19:37:24585 * As objects are allocated in slabs (see alloc.c), we do
586 * not need to free each object, but each slab instead.
Stefan Beller14ba97f2018-05-15 21:48:42587 *
588 * Before doing so, we need to free any additional memory
589 * the objects may hold.
Stefan Beller99bf1152018-05-08 19:37:24590 */
Stefan Beller14ba97f2018-05-15 21:48:42591 unsigned i;
592
593 for (i = 0; i < o->obj_hash_size; i++) {
594 struct object *obj = o->obj_hash[i];
595
596 if (!obj)
597 continue;
598
599 if (obj->type == OBJ_TREE)
600 free_tree_buffer((struct tree*)obj);
601 else if (obj->type == OBJ_COMMIT)
Stefan Beller6a7895f2018-12-15 00:09:40602 release_commit_memory(o, (struct commit*)obj);
Stefan Beller14ba97f2018-05-15 21:48:42603 else if (obj->type == OBJ_TAG)
604 release_tag_memory((struct tag*)obj);
605 }
606
607 FREE_AND_NULL(o->obj_hash);
608 o->obj_hash_size = 0;
609
Stefan Beller65ea9d42018-06-29 01:22:15610 free_commit_buffer_slab(o->buffer_slab);
611 o->buffer_slab = NULL;
612
Stefan Beller14ba97f2018-05-15 21:48:42613 clear_alloc_state(o->blob_state);
614 clear_alloc_state(o->tree_state);
615 clear_alloc_state(o->commit_state);
616 clear_alloc_state(o->tag_state);
617 clear_alloc_state(o->object_state);
Josh Steadmon96b07102019-02-07 20:05:54618 stat_validity_clear(o->shallow_stat);
Stefan Beller14ba97f2018-05-15 21:48:42619 FREE_AND_NULL(o->blob_state);
620 FREE_AND_NULL(o->tree_state);
621 FREE_AND_NULL(o->commit_state);
622 FREE_AND_NULL(o->tag_state);
623 FREE_AND_NULL(o->object_state);
Josh Steadmon96b07102019-02-07 20:05:54624 FREE_AND_NULL(o->shallow_stat);
Stefan Beller99bf1152018-05-08 19:37:24625}