blob: 66cffaf6e516f181fa391ddab125fdc16cc71cb5 [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"
Daniel Barkalowe9eefa62005-04-28 07:46:33 -07004#include "blob.h"
5#include "tree.h"
6#include "commit.h"
Daniel Barkalowe9eefa62005-04-28 07:46:33 -07007#include "tag.h"
Stefan Beller90c62152018-03-23 18:20:55 +01008#include "object-store.h"
Stefan Bellerd0b59862018-03-23 18:21:00 +01009#include "packfile.h"
Daniel Barkalow175785e2005-04-18 11:39:48 -070010
Linus Torvalds0556a112006-06-30 11:20:33 -070011static struct object **obj_hash;
12static int nr_objs, obj_hash_size;
Linus Torvaldsfc046a72006-06-29 21:38:55 -070013
14unsigned int get_max_object_index(void)
15{
Linus Torvalds0556a112006-06-30 11:20:33 -070016 return obj_hash_size;
Linus Torvaldsfc046a72006-06-29 21:38:55 -070017}
18
19struct object *get_indexed_object(unsigned int idx)
20{
Linus Torvalds0556a112006-06-30 11:20:33 -070021 return obj_hash[idx];
Linus Torvaldsfc046a72006-06-29 21:38:55 -070022}
Daniel Barkalow175785e2005-04-18 11:39:48 -070023
Nicolas Pitredf843662007-02-26 14:55:58 -050024static const char *object_type_strings[] = {
25 NULL, /* OBJ_NONE = 0 */
26 "commit", /* OBJ_COMMIT = 1 */
27 "tree", /* OBJ_TREE = 2 */
28 "blob", /* OBJ_BLOB = 3 */
29 "tag", /* OBJ_TAG = 4 */
Linus Torvalds885a86a2006-06-14 16:45:13 -070030};
31
Brandon Williamsdebca9d2018-02-14 10:59:24 -080032const char *type_name(unsigned int type)
Nicolas Pitredf843662007-02-26 14:55:58 -050033{
34 if (type >= ARRAY_SIZE(object_type_strings))
35 return NULL;
36 return object_type_strings[type];
37}
38
Johannes Schindelinfe8e3b72014-09-10 15:52:44 +020039int type_from_string_gently(const char *str, ssize_t len, int gentle)
Nicolas Pitredf843662007-02-26 14:55:58 -050040{
41 int i;
42
Johannes Schindelinfe8e3b72014-09-10 15:52:44 +020043 if (len < 0)
44 len = strlen(str);
45
Nicolas Pitredf843662007-02-26 14:55:58 -050046 for (i = 1; i < ARRAY_SIZE(object_type_strings); i++)
Jeff Kingb7994af2015-04-17 10:52:48 -040047 if (!strncmp(str, object_type_strings[i], len) &&
48 object_type_strings[i][len] == '\0')
Nicolas Pitredf843662007-02-26 14:55:58 -050049 return i;
Johannes Schindelinfe8e3b72014-09-10 15:52:44 +020050
51 if (gentle)
52 return -1;
53
Nicolas Pitredf843662007-02-26 14:55:58 -050054 die("invalid object type \"%s\"", str);
55}
56
Michael Haggerty33bef7e2014-02-28 17:29:17 +010057/*
58 * Return a numerical hash value between 0 and n-1 for the object with
59 * the specified sha1. n must be a power of 2. Please note that the
60 * return value is *not* consistent across computer architectures.
61 */
Nicolas Pitre9f36c9b2013-09-10 18:17:12 -040062static unsigned int hash_obj(const unsigned char *sha1, unsigned int n)
Linus Torvalds0556a112006-06-30 11:20:33 -070063{
Karsten Blees039dc712014-07-03 00:20:20 +020064 return sha1hash(sha1) & (n - 1);
Linus Torvalds0556a112006-06-30 11:20:33 -070065}
66
Michael Haggerty33bef7e2014-02-28 17:29:17 +010067/*
68 * Insert obj into the hash table hash, which has length size (which
69 * must be a power of 2). On collisions, simply overflow to the next
70 * empty bucket.
71 */
Linus Torvalds0556a112006-06-30 11:20:33 -070072static void insert_obj_hash(struct object *obj, struct object **hash, unsigned int size)
73{
brian m. carlsoned1c9972015-11-10 02:22:29 +000074 unsigned int j = hash_obj(obj->oid.hash, size);
Linus Torvalds0556a112006-06-30 11:20:33 -070075
76 while (hash[j]) {
77 j++;
78 if (j >= size)
79 j = 0;
80 }
81 hash[j] = obj;
82}
83
Michael Haggerty33bef7e2014-02-28 17:29:17 +010084/*
85 * Look up the record for the given sha1 in the hash map stored in
86 * obj_hash. Return NULL if it was not found.
87 */
Jason McMullan5d6ccf52005-06-03 11:05:39 -040088struct object *lookup_object(const unsigned char *sha1)
Daniel Barkalow175785e2005-04-18 11:39:48 -070089{
Jeff King9a414482013-05-01 16:34:50 -040090 unsigned int i, first;
Linus Torvalds0556a112006-06-30 11:20:33 -070091 struct object *obj;
92
93 if (!obj_hash)
94 return NULL;
95
Nicolas Pitre9f36c9b2013-09-10 18:17:12 -040096 first = i = hash_obj(sha1, obj_hash_size);
Linus Torvalds0556a112006-06-30 11:20:33 -070097 while ((obj = obj_hash[i]) != NULL) {
brian m. carlsoned1c9972015-11-10 02:22:29 +000098 if (!hashcmp(sha1, obj->oid.hash))
Linus Torvalds0556a112006-06-30 11:20:33 -070099 break;
100 i++;
101 if (i == obj_hash_size)
102 i = 0;
103 }
Jeff King9a414482013-05-01 16:34:50 -0400104 if (obj && i != first) {
105 /*
106 * Move object to where we started to look for it so
107 * that we do not need to walk the hash table the next
108 * time we look for it.
109 */
René Scharfe35d803b2017-01-28 22:40:58 +0100110 SWAP(obj_hash[i], 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 */
Linus Torvalds0556a112006-06-30 11:20:33 -0700120static void grow_object_hash(void)
121{
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 */
Linus Torvalds0556a112006-06-30 11:20:33 -0700127 int new_hash_size = obj_hash_size < 32 ? 32 : 2 * obj_hash_size;
128 struct object **new_hash;
129
Jonas Fonsecab3c952f2006-08-28 02:26:07 +0200130 new_hash = xcalloc(new_hash_size, sizeof(struct object *));
Linus Torvalds0556a112006-06-30 11:20:33 -0700131 for (i = 0; i < obj_hash_size; i++) {
132 struct object *obj = obj_hash[i];
133 if (!obj)
134 continue;
135 insert_obj_hash(obj, new_hash, new_hash_size);
136 }
137 free(obj_hash);
138 obj_hash = new_hash;
139 obj_hash_size = new_hash_size;
Daniel Barkalow175785e2005-04-18 11:39:48 -0700140}
141
Jeff Kingd36f51c2014-07-13 02:41:55 -0400142void *create_object(const unsigned char *sha1, void *o)
Daniel Barkalow175785e2005-04-18 11:39:48 -0700143{
Linus Torvalds100c5f32007-04-16 22:11:43 -0700144 struct object *obj = o;
145
Daniel Barkalow175785e2005-04-18 11:39:48 -0700146 obj->parsed = 0;
Linus Torvalds0556a112006-06-30 11:20:33 -0700147 obj->flags = 0;
brian m. carlsoned1c9972015-11-10 02:22:29 +0000148 hashcpy(obj->oid.hash, sha1);
Daniel Barkalow175785e2005-04-18 11:39:48 -0700149
Linus Torvalds0556a112006-06-30 11:20:33 -0700150 if (obj_hash_size - 1 <= nr_objs * 2)
151 grow_object_hash();
Johannes Schindelin070879c2006-02-12 02:57:57 +0100152
Linus Torvalds0556a112006-06-30 11:20:33 -0700153 insert_obj_hash(obj, obj_hash, obj_hash_size);
Daniel Barkalow175785e2005-04-18 11:39:48 -0700154 nr_objs++;
Linus Torvalds100c5f32007-04-16 22:11:43 -0700155 return obj;
Daniel Barkalow175785e2005-04-18 11:39:48 -0700156}
157
Jeff King8ff226a2014-07-13 02:42:03 -0400158void *object_as_type(struct object *obj, enum object_type type, int quiet)
159{
160 if (obj->type == type)
161 return obj;
162 else if (obj->type == OBJ_NONE) {
Jeff Kingd66bebc2014-07-13 02:42:12 -0400163 if (type == OBJ_COMMIT)
164 ((struct commit *)obj)->index = alloc_commit_index();
Jeff King8ff226a2014-07-13 02:42:03 -0400165 obj->type = type;
166 return obj;
167 }
168 else {
169 if (!quiet)
170 error("object %s is a %s, not a %s",
brian m. carlsonf2fd0762015-11-10 02:22:28 +0000171 oid_to_hex(&obj->oid),
Brandon Williamsdebca9d2018-02-14 10:59:24 -0800172 type_name(obj->type), type_name(type));
Jeff King8ff226a2014-07-13 02:42:03 -0400173 return NULL;
174 }
175}
176
barkalow@iabervon.org66e481b2005-08-02 19:45:48 -0400177struct object *lookup_unknown_object(const unsigned char *sha1)
178{
179 struct object *obj = lookup_object(sha1);
Linus Torvalds100c5f32007-04-16 22:11:43 -0700180 if (!obj)
Jeff Kingd36f51c2014-07-13 02:41:55 -0400181 obj = create_object(sha1, alloc_object_node());
barkalow@iabervon.org66e481b2005-08-02 19:45:48 -0400182 return obj;
183}
184
brian m. carlsonc251c832017-05-06 22:10:38 +0000185struct object *parse_object_buffer(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 -0700186{
187 struct object *obj;
Stefan Beller8e92e8f2013-07-18 00:09:42 +0200188 *eaten_p = 0;
Junio C Hamano9f613dd2006-09-15 13:30:02 -0700189
Jim Meyeringcc216822007-12-21 11:56:32 +0100190 obj = NULL;
Nicolas Pitre21666f12007-02-26 14:55:59 -0500191 if (type == OBJ_BLOB) {
brian m. carlsonc251c832017-05-06 22:10:38 +0000192 struct blob *blob = lookup_blob(oid);
Jim Meyeringcc216822007-12-21 11:56:32 +0100193 if (blob) {
Martin Koeglerd0b8c9e2008-02-03 22:22:39 +0100194 if (parse_blob_buffer(blob, buffer, size))
195 return NULL;
Jim Meyeringcc216822007-12-21 11:56:32 +0100196 obj = &blob->object;
197 }
Nicolas Pitre21666f12007-02-26 14:55:59 -0500198 } else if (type == OBJ_TREE) {
brian m. carlsonc251c832017-05-06 22:10:38 +0000199 struct tree *tree = lookup_tree(oid);
Jim Meyeringcc216822007-12-21 11:56:32 +0100200 if (tree) {
201 obj = &tree->object;
Junio C Hamano68be2fe2011-11-16 22:04:13 -0800202 if (!tree->buffer)
203 tree->object.parsed = 0;
Jim Meyeringcc216822007-12-21 11:56:32 +0100204 if (!tree->object.parsed) {
Martin Koeglerd0b8c9e2008-02-03 22:22:39 +0100205 if (parse_tree_buffer(tree, buffer, size))
206 return NULL;
Stefan Beller8e92e8f2013-07-18 00:09:42 +0200207 *eaten_p = 1;
Jim Meyeringcc216822007-12-21 11:56:32 +0100208 }
Junio C Hamano9f613dd2006-09-15 13:30:02 -0700209 }
Nicolas Pitre21666f12007-02-26 14:55:59 -0500210 } else if (type == OBJ_COMMIT) {
brian m. carlsonc251c832017-05-06 22:10:38 +0000211 struct commit *commit = lookup_commit(oid);
Jim Meyeringcc216822007-12-21 11:56:32 +0100212 if (commit) {
Martin Koeglerd0b8c9e2008-02-03 22:22:39 +0100213 if (parse_commit_buffer(commit, buffer, size))
214 return NULL;
Jeff King8597ea32014-06-10 17:44:13 -0400215 if (!get_cached_commit_buffer(commit, NULL)) {
216 set_commit_buffer(commit, buffer, size);
Stefan Beller8e92e8f2013-07-18 00:09:42 +0200217 *eaten_p = 1;
Jim Meyeringcc216822007-12-21 11:56:32 +0100218 }
219 obj = &commit->object;
Junio C Hamano9f613dd2006-09-15 13:30:02 -0700220 }
Nicolas Pitre21666f12007-02-26 14:55:59 -0500221 } else if (type == OBJ_TAG) {
brian m. carlsonc251c832017-05-06 22:10:38 +0000222 struct tag *tag = lookup_tag(oid);
Jim Meyeringcc216822007-12-21 11:56:32 +0100223 if (tag) {
Martin Koeglerd0b8c9e2008-02-03 22:22:39 +0100224 if (parse_tag_buffer(tag, buffer, size))
225 return NULL;
Jim Meyeringcc216822007-12-21 11:56:32 +0100226 obj = &tag->object;
227 }
Junio C Hamano9f613dd2006-09-15 13:30:02 -0700228 } else {
brian m. carlsonc251c832017-05-06 22:10:38 +0000229 warning("object %s has unknown type id %d", oid_to_hex(oid), type);
Junio C Hamano9f613dd2006-09-15 13:30:02 -0700230 obj = NULL;
231 }
Junio C Hamano9f613dd2006-09-15 13:30:02 -0700232 return obj;
233}
234
brian m. carlsonc251c832017-05-06 22:10:38 +0000235struct object *parse_object_or_die(const struct object_id *oid,
Jeff King75a95492013-03-17 04:22:36 -0400236 const char *name)
237{
brian m. carlsonc251c832017-05-06 22:10:38 +0000238 struct object *o = parse_object(oid);
Jeff King75a95492013-03-17 04:22:36 -0400239 if (o)
240 return o;
241
brian m. carlsonc251c832017-05-06 22:10:38 +0000242 die(_("unable to parse object: %s"), name ? name : oid_to_hex(oid));
Jeff King75a95492013-03-17 04:22:36 -0400243}
244
brian m. carlsonc251c832017-05-06 22:10:38 +0000245struct object *parse_object(const struct object_id *oid)
Daniel Barkalowe9eefa62005-04-28 07:46:33 -0700246{
Junio C Hamanoc4584ae2005-06-27 03:33:33 -0700247 unsigned long size;
Nicolas Pitre21666f12007-02-26 14:55:59 -0500248 enum object_type type;
Junio C Hamano9f613dd2006-09-15 13:30:02 -0700249 int eaten;
Stefan Beller1f2e7ce2018-04-11 17:21:13 -0700250 const struct object_id *repl = lookup_replace_object(the_repository, oid);
Jeff Kingccdc6032012-01-05 16:00:01 -0500251 void *buffer;
252 struct object *obj;
Junio C Hamano9f613dd2006-09-15 13:30:02 -0700253
brian m. carlsonc251c832017-05-06 22:10:38 +0000254 obj = lookup_object(oid->hash);
Jeff Kingccdc6032012-01-05 16:00:01 -0500255 if (obj && obj->parsed)
256 return obj;
257
Jonathan Tandf11e192017-12-08 15:27:15 +0000258 if ((obj && obj->type == OBJ_BLOB && has_object_file(oid)) ||
brian m. carlsonc251c832017-05-06 22:10:38 +0000259 (!obj && has_object_file(oid) &&
brian m. carlsonabef9022018-03-12 02:27:46 +0000260 oid_object_info(oid, NULL) == OBJ_BLOB)) {
brian m. carlsonb383a132018-03-12 02:27:54 +0000261 if (check_object_signature(repl, NULL, 0, NULL) < 0) {
brian m. carlsonc251c832017-05-06 22:10:38 +0000262 error("sha1 mismatch %s", oid_to_hex(oid));
Nguyễn Thái Ngọc Duy090ea122012-03-07 17:54:18 +0700263 return NULL;
264 }
brian m. carlsonc251c832017-05-06 22:10:38 +0000265 parse_blob_buffer(lookup_blob(oid), NULL, 0);
266 return lookup_object(oid->hash);
Nguyễn Thái Ngọc Duy090ea122012-03-07 17:54:18 +0700267 }
268
brian m. carlsonb4f5aca2018-03-12 02:27:53 +0000269 buffer = read_object_file(oid, &type, &size);
Junio C Hamanoc4584ae2005-06-27 03:33:33 -0700270 if (buffer) {
brian m. carlsonb383a132018-03-12 02:27:54 +0000271 if (check_object_signature(repl, buffer, size, type_name(type)) < 0) {
Carlos Rica0b1f1132007-05-25 03:46:22 +0200272 free(buffer);
brian m. carlsonb383a132018-03-12 02:27:54 +0000273 error("sha1 mismatch %s", oid_to_hex(repl));
Linus Torvaldsacdeec62007-03-20 10:05:20 -0700274 return NULL;
275 }
Junio C Hamano9f613dd2006-09-15 13:30:02 -0700276
brian m. carlsonc251c832017-05-06 22:10:38 +0000277 obj = parse_object_buffer(oid, type, size, buffer, &eaten);
Junio C Hamano9f613dd2006-09-15 13:30:02 -0700278 if (!eaten)
279 free(buffer);
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400280 return obj;
Daniel Barkalowe9eefa62005-04-28 07:46:33 -0700281 }
282 return NULL;
283}
barkalow@iabervon.org66e481b2005-08-02 19:45:48 -0400284
285struct object_list *object_list_insert(struct object *item,
286 struct object_list **list_p)
287{
288 struct object_list *new_list = xmalloc(sizeof(struct object_list));
Jared Hance55b4e9e2010-09-05 15:36:33 -0400289 new_list->item = item;
290 new_list->next = *list_p;
291 *list_p = new_list;
292 return new_list;
barkalow@iabervon.org66e481b2005-08-02 19:45:48 -0400293}
294
barkalow@iabervon.org66e481b2005-08-02 19:45:48 -0400295int object_list_contains(struct object_list *list, struct object *obj)
296{
297 while (list) {
298 if (list->item == obj)
299 return 1;
300 list = list->next;
301 }
302 return 0;
303}
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700304
Michael Haggerty31faeb22013-05-25 11:08:14 +0200305/*
306 * A zero-length string to which object_array_entry::name can be
307 * initialized without requiring a malloc/free.
308 */
309static char object_array_slopbuf[1];
310
Jeff King9e0c3c42014-10-15 18:42:57 -0400311void add_object_array_with_path(struct object *obj, const char *name,
312 struct object_array *array,
313 unsigned mode, const char *path)
Martin Koeglere5709a42007-04-22 18:43:58 +0200314{
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700315 unsigned nr = array->nr;
316 unsigned alloc = array->alloc;
317 struct object_array_entry *objects = array->objects;
Michael Haggerty31faeb22013-05-25 11:08:14 +0200318 struct object_array_entry *entry;
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700319
320 if (nr >= alloc) {
321 alloc = (alloc + 32) * 2;
René Scharfe2756ca42014-09-16 20:56:57 +0200322 REALLOC_ARRAY(objects, alloc);
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700323 array->alloc = alloc;
324 array->objects = objects;
325 }
Michael Haggerty31faeb22013-05-25 11:08:14 +0200326 entry = &objects[nr];
327 entry->item = obj;
328 if (!name)
329 entry->name = NULL;
330 else if (!*name)
331 /* Use our own empty string instead of allocating one: */
332 entry->name = object_array_slopbuf;
333 else
334 entry->name = xstrdup(name);
335 entry->mode = mode;
Jeff King9e0c3c42014-10-15 18:42:57 -0400336 if (path)
337 entry->path = xstrdup(path);
338 else
339 entry->path = NULL;
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700340 array->nr = ++nr;
341}
Junio C Hamanob2a6d1c2009-01-17 22:27:08 -0800342
Michael J Gruberafa15f32013-05-10 17:10:16 +0200343void add_object_array(struct object *obj, const char *name, struct object_array *array)
344{
Jeff King189a1222014-10-18 22:03:19 -0400345 add_object_array_with_path(obj, name, array, S_IFINVALID, NULL);
Michael J Gruberafa15f32013-05-10 17:10:16 +0200346}
347
Jeff King68f49232014-10-15 18:34:19 -0400348/*
349 * Free all memory associated with an entry; the result is
350 * in an unspecified state and should not be examined.
351 */
352static void object_array_release_entry(struct object_array_entry *ent)
353{
354 if (ent->name != object_array_slopbuf)
355 free(ent->name);
Jeff King9e0c3c42014-10-15 18:42:57 -0400356 free(ent->path);
Jeff King68f49232014-10-15 18:34:19 -0400357}
358
Martin Ågren71992032017-09-23 01:34:53 +0200359struct object *object_array_pop(struct object_array *array)
360{
361 struct object *ret;
362
363 if (!array->nr)
364 return NULL;
365
366 ret = array->objects[array->nr - 1].item;
367 object_array_release_entry(&array->objects[array->nr - 1]);
368 array->nr--;
369 return ret;
370}
371
Michael Haggertyaeb4a512013-05-25 11:08:08 +0200372void object_array_filter(struct object_array *array,
373 object_array_each_func_t want, void *cb_data)
374{
375 unsigned nr = array->nr, src, dst;
376 struct object_array_entry *objects = array->objects;
377
378 for (src = dst = 0; src < nr; src++) {
379 if (want(&objects[src], cb_data)) {
380 if (src != dst)
381 objects[dst] = objects[src];
382 dst++;
Michael Haggerty31faeb22013-05-25 11:08:14 +0200383 } else {
Jeff King68f49232014-10-15 18:34:19 -0400384 object_array_release_entry(&objects[src]);
Michael Haggertyaeb4a512013-05-25 11:08:08 +0200385 }
386 }
387 array->nr = dst;
388}
389
Jeff King46be8232014-10-15 18:34:34 -0400390void object_array_clear(struct object_array *array)
391{
392 int i;
393 for (i = 0; i < array->nr; i++)
394 object_array_release_entry(&array->objects[i]);
Ævar Arnfjörð Bjarmason6a83d902017-06-15 23:15:46 +0000395 FREE_AND_NULL(array->objects);
Jeff King46be8232014-10-15 18:34:34 -0400396 array->nr = array->alloc = 0;
397}
398
Michael Haggerty15065102013-05-25 11:08:10 +0200399/*
400 * Return true iff array already contains an entry with name.
401 */
402static int contains_name(struct object_array *array, const char *name)
403{
404 unsigned nr = array->nr, i;
405 struct object_array_entry *object = array->objects;
406
407 for (i = 0; i < nr; i++, object++)
408 if (!strcmp(object->name, name))
409 return 1;
410 return 0;
411}
412
Junio C Hamanob2a6d1c2009-01-17 22:27:08 -0800413void object_array_remove_duplicates(struct object_array *array)
414{
Michael Haggerty15065102013-05-25 11:08:10 +0200415 unsigned nr = array->nr, src;
Junio C Hamanob2a6d1c2009-01-17 22:27:08 -0800416 struct object_array_entry *objects = array->objects;
417
Michael Haggerty15065102013-05-25 11:08:10 +0200418 array->nr = 0;
419 for (src = 0; src < nr; src++) {
420 if (!contains_name(array, objects[src].name)) {
421 if (src != array->nr)
422 objects[array->nr] = objects[src];
423 array->nr++;
Michael Haggerty31faeb22013-05-25 11:08:14 +0200424 } else {
Jeff King68f49232014-10-15 18:34:19 -0400425 object_array_release_entry(&objects[src]);
Junio C Hamanob2a6d1c2009-01-17 22:27:08 -0800426 }
Junio C Hamanob2a6d1c2009-01-17 22:27:08 -0800427 }
428}
Heiko Voigtbcc0a3e2012-03-29 09:21:21 +0200429
430void clear_object_flags(unsigned flags)
431{
432 int i;
433
434 for (i=0; i < obj_hash_size; i++) {
435 struct object *obj = obj_hash[i];
436 if (obj)
437 obj->flags &= ~flags;
438 }
439}
René Scharfe4ad315f2017-12-25 18:44:58 +0100440
441void clear_commit_marks_all(unsigned int flags)
442{
443 int i;
444
445 for (i = 0; i < obj_hash_size; i++) {
446 struct object *obj = obj_hash[i];
447 if (obj && obj->type == OBJ_COMMIT)
448 obj->flags &= ~flags;
449 }
450}
Stefan Beller90c62152018-03-23 18:20:55 +0100451
452struct raw_object_store *raw_object_store_new(void)
453{
454 struct raw_object_store *o = xmalloc(sizeof(*o));
455
456 memset(o, 0, sizeof(*o));
Stefan Bellera80d72d2018-03-23 18:20:59 +0100457 INIT_LIST_HEAD(&o->packed_git_mru);
Stefan Beller90c62152018-03-23 18:20:55 +0100458 return o;
459}
Stefan Beller97501e92018-03-23 18:20:58 +0100460
461static void free_alt_odb(struct alternate_object_database *alt)
462{
463 strbuf_release(&alt->scratch);
464 oid_array_clear(&alt->loose_objects_cache);
465 free(alt);
466}
467
468static void free_alt_odbs(struct raw_object_store *o)
469{
470 while (o->alt_odb_list) {
471 struct alternate_object_database *next;
472
473 next = o->alt_odb_list->next;
474 free_alt_odb(o->alt_odb_list);
475 o->alt_odb_list = next;
476 }
477}
478
Stefan Beller90c62152018-03-23 18:20:55 +0100479void raw_object_store_clear(struct raw_object_store *o)
480{
481 FREE_AND_NULL(o->objectdir);
482 FREE_AND_NULL(o->alternate_db);
Stefan Beller97501e92018-03-23 18:20:58 +0100483
484 free_alt_odbs(o);
485 o->alt_odb_tail = NULL;
Stefan Bellera80d72d2018-03-23 18:20:59 +0100486
487 INIT_LIST_HEAD(&o->packed_git_mru);
Stefan Bellerd0b59862018-03-23 18:21:00 +0100488 close_all_packs(o);
489 o->packed_git = NULL;
Stefan Beller90c62152018-03-23 18:20:55 +0100490}