The seventh batch
[git.git] / object.c
blob100bf9b8d12beb1bdaa0c222e51e920471d891c7
1 #define USE_THE_REPOSITORY_VARIABLE
2 #define DISABLE_SIGN_COMPARE_WARNINGS
4 #include "git-compat-util.h"
5 #include "gettext.h"
6 #include "hex.h"
7 #include "object.h"
8 #include "replace-object.h"
9 #include "object-file.h"
10 #include "object-store.h"
11 #include "blob.h"
12 #include "statinfo.h"
13 #include "tree.h"
14 #include "commit.h"
15 #include "tag.h"
16 #include "alloc.h"
17 #include "packfile.h"
18 #include "commit-graph.h"
19 #include "loose.h"
21 unsigned int get_max_object_index(void)
23 return the_repository->parsed_objects->obj_hash_size;
26 struct object *get_indexed_object(unsigned int idx)
28 return the_repository->parsed_objects->obj_hash[idx];
31 static const char *object_type_strings[] = {
32 NULL, /* OBJ_NONE = 0 */
33 "commit", /* OBJ_COMMIT = 1 */
34 "tree", /* OBJ_TREE = 2 */
35 "blob", /* OBJ_BLOB = 3 */
36 "tag", /* OBJ_TAG = 4 */
39 const char *type_name(unsigned int type)
41 if (type >= ARRAY_SIZE(object_type_strings))
42 return NULL;
43 return object_type_strings[type];
46 int type_from_string_gently(const char *str, ssize_t len, int gentle)
48 int i;
50 if (len < 0)
51 len = strlen(str);
53 for (i = 1; i < ARRAY_SIZE(object_type_strings); i++)
54 if (!xstrncmpz(object_type_strings[i], str, len))
55 return i;
57 if (gentle)
58 return -1;
60 die(_("invalid object type \"%s\""), str);
64 * Return a numerical hash value between 0 and n-1 for the object with
65 * the specified sha1. n must be a power of 2. Please note that the
66 * return value is *not* consistent across computer architectures.
68 static unsigned int hash_obj(const struct object_id *oid, unsigned int n)
70 return oidhash(oid) & (n - 1);
74 * Insert obj into the hash table hash, which has length size (which
75 * must be a power of 2). On collisions, simply overflow to the next
76 * empty bucket.
78 static void insert_obj_hash(struct object *obj, struct object **hash, unsigned int size)
80 unsigned int j = hash_obj(&obj->oid, size);
82 while (hash[j]) {
83 j++;
84 if (j >= size)
85 j = 0;
87 hash[j] = obj;
91 * Look up the record for the given sha1 in the hash map stored in
92 * obj_hash. Return NULL if it was not found.
94 struct object *lookup_object(struct repository *r, const struct object_id *oid)
96 unsigned int i, first;
97 struct object *obj;
99 if (!r->parsed_objects->obj_hash)
100 return NULL;
102 first = i = hash_obj(oid, r->parsed_objects->obj_hash_size);
103 while ((obj = r->parsed_objects->obj_hash[i]) != NULL) {
104 if (oideq(oid, &obj->oid))
105 break;
106 i++;
107 if (i == r->parsed_objects->obj_hash_size)
108 i = 0;
110 if (obj && i != first) {
112 * Move object to where we started to look for it so
113 * that we do not need to walk the hash table the next
114 * time we look for it.
116 SWAP(r->parsed_objects->obj_hash[i],
117 r->parsed_objects->obj_hash[first]);
119 return obj;
123 * Increase the size of the hash map stored in obj_hash to the next
124 * power of 2 (but at least 32). Copy the existing values to the new
125 * hash map.
127 static void grow_object_hash(struct repository *r)
129 int i;
131 * Note that this size must always be power-of-2 to match hash_obj
132 * above.
134 int new_hash_size = r->parsed_objects->obj_hash_size < 32 ? 32 : 2 * r->parsed_objects->obj_hash_size;
135 struct object **new_hash;
137 CALLOC_ARRAY(new_hash, new_hash_size);
138 for (i = 0; i < r->parsed_objects->obj_hash_size; i++) {
139 struct object *obj = r->parsed_objects->obj_hash[i];
141 if (!obj)
142 continue;
143 insert_obj_hash(obj, new_hash, new_hash_size);
145 free(r->parsed_objects->obj_hash);
146 r->parsed_objects->obj_hash = new_hash;
147 r->parsed_objects->obj_hash_size = new_hash_size;
150 void *create_object(struct repository *r, const struct object_id *oid, void *o)
152 struct object *obj = o;
154 obj->parsed = 0;
155 obj->flags = 0;
156 oidcpy(&obj->oid, oid);
158 if (r->parsed_objects->obj_hash_size - 1 <= r->parsed_objects->nr_objs * 2)
159 grow_object_hash(r);
161 insert_obj_hash(obj, r->parsed_objects->obj_hash,
162 r->parsed_objects->obj_hash_size);
163 r->parsed_objects->nr_objs++;
164 return obj;
167 void *object_as_type(struct object *obj, enum object_type type, int quiet)
169 if (obj->type == type)
170 return obj;
171 else if (obj->type == OBJ_NONE) {
172 if (type == OBJ_COMMIT)
173 init_commit_node((struct commit *) obj);
174 else
175 obj->type = type;
176 return obj;
178 else {
179 if (!quiet)
180 error(_("object %s is a %s, not a %s"),
181 oid_to_hex(&obj->oid),
182 type_name(obj->type), type_name(type));
183 return NULL;
187 struct object *lookup_unknown_object(struct repository *r, const struct object_id *oid)
189 struct object *obj = lookup_object(r, oid);
190 if (!obj)
191 obj = create_object(r, oid, alloc_object_node(r));
192 return obj;
195 struct object *lookup_object_by_type(struct repository *r,
196 const struct object_id *oid,
197 enum object_type type)
199 switch (type) {
200 case OBJ_COMMIT:
201 return (struct object *)lookup_commit(r, oid);
202 case OBJ_TREE:
203 return (struct object *)lookup_tree(r, oid);
204 case OBJ_TAG:
205 return (struct object *)lookup_tag(r, oid);
206 case OBJ_BLOB:
207 return (struct object *)lookup_blob(r, oid);
208 default:
209 BUG("unknown object type %d", type);
213 enum peel_status peel_object(struct repository *r,
214 const struct object_id *name,
215 struct object_id *oid)
217 struct object *o = lookup_unknown_object(r, name);
219 if (o->type == OBJ_NONE) {
220 int type = oid_object_info(r, name, NULL);
221 if (type < 0 || !object_as_type(o, type, 0))
222 return PEEL_INVALID;
225 if (o->type != OBJ_TAG)
226 return PEEL_NON_TAG;
228 o = deref_tag_noverify(r, o);
229 if (!o)
230 return PEEL_INVALID;
232 oidcpy(oid, &o->oid);
233 return PEEL_PEELED;
236 struct object *parse_object_buffer(struct repository *r, const struct object_id *oid, enum object_type type, unsigned long size, void *buffer, int *eaten_p)
238 struct object *obj;
239 *eaten_p = 0;
241 obj = NULL;
242 if (type == OBJ_BLOB) {
243 struct blob *blob = lookup_blob(r, oid);
244 if (blob) {
245 parse_blob_buffer(blob);
246 obj = &blob->object;
248 } else if (type == OBJ_TREE) {
249 struct tree *tree = lookup_tree(r, oid);
250 if (tree) {
251 obj = &tree->object;
252 if (!tree->buffer)
253 tree->object.parsed = 0;
254 if (!tree->object.parsed) {
255 if (parse_tree_buffer(tree, buffer, size))
256 return NULL;
257 *eaten_p = 1;
260 } else if (type == OBJ_COMMIT) {
261 struct commit *commit = lookup_commit(r, oid);
262 if (commit) {
263 if (parse_commit_buffer(r, commit, buffer, size, 1))
264 return NULL;
265 if (save_commit_buffer &&
266 !get_cached_commit_buffer(r, commit, NULL)) {
267 set_commit_buffer(r, commit, buffer, size);
268 *eaten_p = 1;
270 obj = &commit->object;
272 } else if (type == OBJ_TAG) {
273 struct tag *tag = lookup_tag(r, oid);
274 if (tag) {
275 if (parse_tag_buffer(r, tag, buffer, size))
276 return NULL;
277 obj = &tag->object;
279 } else {
280 warning(_("object %s has unknown type id %d"), oid_to_hex(oid), type);
281 obj = NULL;
283 return obj;
286 struct object *parse_object_or_die(const struct object_id *oid,
287 const char *name)
289 struct object *o = parse_object(the_repository, oid);
290 if (o)
291 return o;
293 die(_("unable to parse object: %s"), name ? name : oid_to_hex(oid));
296 struct object *parse_object_with_flags(struct repository *r,
297 const struct object_id *oid,
298 enum parse_object_flags flags)
300 int skip_hash = !!(flags & PARSE_OBJECT_SKIP_HASH_CHECK);
301 int discard_tree = !!(flags & PARSE_OBJECT_DISCARD_TREE);
302 unsigned long size;
303 enum object_type type;
304 int eaten;
305 const struct object_id *repl = lookup_replace_object(r, oid);
306 void *buffer;
307 struct object *obj;
309 obj = lookup_object(r, oid);
310 if (obj && obj->parsed)
311 return obj;
313 if (skip_hash) {
314 struct commit *commit = lookup_commit_in_graph(r, repl);
315 if (commit)
316 return &commit->object;
319 if ((!obj || obj->type == OBJ_BLOB) &&
320 oid_object_info(r, oid, NULL) == OBJ_BLOB) {
321 if (!skip_hash && stream_object_signature(r, repl) < 0) {
322 error(_("hash mismatch %s"), oid_to_hex(oid));
323 return NULL;
325 parse_blob_buffer(lookup_blob(r, oid));
326 return lookup_object(r, oid);
330 * If the caller does not care about the tree buffer and does not
331 * care about checking the hash, we can simply verify that we
332 * have the on-disk object with the correct type.
334 if (skip_hash && discard_tree &&
335 (!obj || obj->type == OBJ_TREE) &&
336 oid_object_info(r, oid, NULL) == OBJ_TREE) {
337 return &lookup_tree(r, oid)->object;
340 buffer = repo_read_object_file(r, oid, &type, &size);
341 if (buffer) {
342 if (!skip_hash &&
343 check_object_signature(r, repl, buffer, size, type) < 0) {
344 free(buffer);
345 error(_("hash mismatch %s"), oid_to_hex(repl));
346 return NULL;
349 obj = parse_object_buffer(r, oid, type, size,
350 buffer, &eaten);
351 if (!eaten)
352 free(buffer);
353 if (discard_tree && type == OBJ_TREE)
354 free_tree_buffer((struct tree *)obj);
355 return obj;
357 return NULL;
360 struct object *parse_object(struct repository *r, const struct object_id *oid)
362 return parse_object_with_flags(r, oid, 0);
365 struct object_list *object_list_insert(struct object *item,
366 struct object_list **list_p)
368 struct object_list *new_list = xmalloc(sizeof(struct object_list));
369 new_list->item = item;
370 new_list->next = *list_p;
371 *list_p = new_list;
372 return new_list;
375 int object_list_contains(struct object_list *list, struct object *obj)
377 while (list) {
378 if (list->item == obj)
379 return 1;
380 list = list->next;
382 return 0;
385 void object_list_free(struct object_list **list)
387 while (*list) {
388 struct object_list *p = *list;
389 *list = p->next;
390 free(p);
395 * A zero-length string to which object_array_entry::name can be
396 * initialized without requiring a malloc/free.
398 static char object_array_slopbuf[1];
400 void object_array_init(struct object_array *array)
402 struct object_array blank = OBJECT_ARRAY_INIT;
403 memcpy(array, &blank, sizeof(*array));
406 void add_object_array_with_path(struct object *obj, const char *name,
407 struct object_array *array,
408 unsigned mode, const char *path)
410 unsigned nr = array->nr;
411 unsigned alloc = array->alloc;
412 struct object_array_entry *objects = array->objects;
413 struct object_array_entry *entry;
415 if (nr >= alloc) {
416 alloc = (alloc + 32) * 2;
417 REALLOC_ARRAY(objects, alloc);
418 array->alloc = alloc;
419 array->objects = objects;
421 entry = &objects[nr];
422 entry->item = obj;
423 if (!name)
424 entry->name = NULL;
425 else if (!*name)
426 /* Use our own empty string instead of allocating one: */
427 entry->name = object_array_slopbuf;
428 else
429 entry->name = xstrdup(name);
430 entry->mode = mode;
431 if (path)
432 entry->path = xstrdup(path);
433 else
434 entry->path = NULL;
435 array->nr = ++nr;
438 void add_object_array(struct object *obj, const char *name, struct object_array *array)
440 add_object_array_with_path(obj, name, array, S_IFINVALID, NULL);
444 * Free all memory associated with an entry; the result is
445 * in an unspecified state and should not be examined.
447 static void object_array_release_entry(struct object_array_entry *ent)
449 if (ent->name != object_array_slopbuf)
450 free(ent->name);
451 free(ent->path);
454 struct object *object_array_pop(struct object_array *array)
456 struct object *ret;
458 if (!array->nr)
459 return NULL;
461 ret = array->objects[array->nr - 1].item;
462 object_array_release_entry(&array->objects[array->nr - 1]);
463 array->nr--;
464 return ret;
467 void object_array_filter(struct object_array *array,
468 object_array_each_func_t want, void *cb_data)
470 unsigned nr = array->nr, src, dst;
471 struct object_array_entry *objects = array->objects;
473 for (src = dst = 0; src < nr; src++) {
474 if (want(&objects[src], cb_data)) {
475 if (src != dst)
476 objects[dst] = objects[src];
477 dst++;
478 } else {
479 object_array_release_entry(&objects[src]);
482 array->nr = dst;
485 void object_array_clear(struct object_array *array)
487 int i;
488 for (i = 0; i < array->nr; i++)
489 object_array_release_entry(&array->objects[i]);
490 FREE_AND_NULL(array->objects);
491 array->nr = array->alloc = 0;
495 * Return true if array already contains an entry.
497 static int contains_object(struct object_array *array,
498 const struct object *item, const char *name)
500 unsigned nr = array->nr, i;
501 struct object_array_entry *object = array->objects;
503 for (i = 0; i < nr; i++, object++)
504 if (item == object->item && !strcmp(object->name, name))
505 return 1;
506 return 0;
509 void object_array_remove_duplicates(struct object_array *array)
511 unsigned nr = array->nr, src;
512 struct object_array_entry *objects = array->objects;
514 array->nr = 0;
515 for (src = 0; src < nr; src++) {
516 if (!contains_object(array, objects[src].item,
517 objects[src].name)) {
518 if (src != array->nr)
519 objects[array->nr] = objects[src];
520 array->nr++;
521 } else {
522 object_array_release_entry(&objects[src]);
527 void clear_object_flags(unsigned flags)
529 int i;
531 for (i=0; i < the_repository->parsed_objects->obj_hash_size; i++) {
532 struct object *obj = the_repository->parsed_objects->obj_hash[i];
533 if (obj)
534 obj->flags &= ~flags;
538 void repo_clear_commit_marks(struct repository *r, unsigned int flags)
540 int i;
542 for (i = 0; i < r->parsed_objects->obj_hash_size; i++) {
543 struct object *obj = r->parsed_objects->obj_hash[i];
544 if (obj && obj->type == OBJ_COMMIT)
545 obj->flags &= ~flags;
549 struct parsed_object_pool *parsed_object_pool_new(struct repository *repo)
551 struct parsed_object_pool *o = xmalloc(sizeof(*o));
552 memset(o, 0, sizeof(*o));
554 o->repo = repo;
555 o->blob_state = allocate_alloc_state();
556 o->tree_state = allocate_alloc_state();
557 o->commit_state = allocate_alloc_state();
558 o->tag_state = allocate_alloc_state();
559 o->object_state = allocate_alloc_state();
561 o->is_shallow = -1;
562 CALLOC_ARRAY(o->shallow_stat, 1);
564 o->buffer_slab = allocate_commit_buffer_slab();
566 return o;
569 struct raw_object_store *raw_object_store_new(void)
571 struct raw_object_store *o = xmalloc(sizeof(*o));
573 memset(o, 0, sizeof(*o));
574 INIT_LIST_HEAD(&o->packed_git_mru);
575 hashmap_init(&o->pack_map, pack_map_entry_cmp, NULL, 0);
576 pthread_mutex_init(&o->replace_mutex, NULL);
577 return o;
580 void free_object_directory(struct object_directory *odb)
582 free(odb->path);
583 odb_clear_loose_cache(odb);
584 loose_object_map_clear(&odb->loose_map);
585 free(odb);
588 static void free_object_directories(struct raw_object_store *o)
590 while (o->odb) {
591 struct object_directory *next;
593 next = o->odb->next;
594 free_object_directory(o->odb);
595 o->odb = next;
597 kh_destroy_odb_path_map(o->odb_by_path);
598 o->odb_by_path = NULL;
601 void raw_object_store_clear(struct raw_object_store *o)
603 FREE_AND_NULL(o->alternate_db);
605 oidmap_free(o->replace_map, 1);
606 FREE_AND_NULL(o->replace_map);
607 pthread_mutex_destroy(&o->replace_mutex);
609 free_commit_graph(o->commit_graph);
610 o->commit_graph = NULL;
611 o->commit_graph_attempted = 0;
613 free_object_directories(o);
614 o->odb_tail = NULL;
615 o->loaded_alternates = 0;
617 INIT_LIST_HEAD(&o->packed_git_mru);
618 close_object_store(o);
621 * `close_object_store()` only closes the packfiles, but doesn't free
622 * them. We thus have to do this manually.
624 for (struct packed_git *p = o->packed_git, *next; p; p = next) {
625 next = p->next;
626 free(p);
628 o->packed_git = NULL;
630 hashmap_clear(&o->pack_map);
633 void parsed_object_pool_reset_commit_grafts(struct parsed_object_pool *o)
635 for (int i = 0; i < o->grafts_nr; i++) {
636 unparse_commit(o->repo, &o->grafts[i]->oid);
637 free(o->grafts[i]);
639 o->grafts_nr = 0;
640 o->commit_graft_prepared = 0;
643 void parsed_object_pool_clear(struct parsed_object_pool *o)
646 * As objects are allocated in slabs (see alloc.c), we do
647 * not need to free each object, but each slab instead.
649 * Before doing so, we need to free any additional memory
650 * the objects may hold.
652 unsigned i;
654 for (i = 0; i < o->obj_hash_size; i++) {
655 struct object *obj = o->obj_hash[i];
657 if (!obj)
658 continue;
660 if (obj->type == OBJ_TREE)
661 free_tree_buffer((struct tree*)obj);
662 else if (obj->type == OBJ_COMMIT)
663 release_commit_memory(o, (struct commit*)obj);
664 else if (obj->type == OBJ_TAG)
665 release_tag_memory((struct tag*)obj);
668 FREE_AND_NULL(o->obj_hash);
669 o->obj_hash_size = 0;
671 free_commit_buffer_slab(o->buffer_slab);
672 o->buffer_slab = NULL;
674 parsed_object_pool_reset_commit_grafts(o);
675 clear_alloc_state(o->blob_state);
676 clear_alloc_state(o->tree_state);
677 clear_alloc_state(o->commit_state);
678 clear_alloc_state(o->tag_state);
679 clear_alloc_state(o->object_state);
680 stat_validity_clear(o->shallow_stat);
681 FREE_AND_NULL(o->blob_state);
682 FREE_AND_NULL(o->tree_state);
683 FREE_AND_NULL(o->commit_state);
684 FREE_AND_NULL(o->tag_state);
685 FREE_AND_NULL(o->object_state);
686 FREE_AND_NULL(o->shallow_stat);