merge-recursive: honor diff.algorithm
[git/gitster.git] / object.c
blob0c0fcb76c0c80a874d70f66bbfdbddbf6ce19d19
1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "git-compat-util.h"
4 #include "gettext.h"
5 #include "hex.h"
6 #include "object.h"
7 #include "replace-object.h"
8 #include "object-file.h"
9 #include "object-store.h"
10 #include "blob.h"
11 #include "statinfo.h"
12 #include "tree.h"
13 #include "commit.h"
14 #include "tag.h"
15 #include "alloc.h"
16 #include "packfile.h"
17 #include "commit-graph.h"
18 #include "loose.h"
20 unsigned int get_max_object_index(void)
22 return the_repository->parsed_objects->obj_hash_size;
25 struct object *get_indexed_object(unsigned int idx)
27 return the_repository->parsed_objects->obj_hash[idx];
30 static const char *object_type_strings[] = {
31 NULL, /* OBJ_NONE = 0 */
32 "commit", /* OBJ_COMMIT = 1 */
33 "tree", /* OBJ_TREE = 2 */
34 "blob", /* OBJ_BLOB = 3 */
35 "tag", /* OBJ_TAG = 4 */
38 const char *type_name(unsigned int type)
40 if (type >= ARRAY_SIZE(object_type_strings))
41 return NULL;
42 return object_type_strings[type];
45 int type_from_string_gently(const char *str, ssize_t len, int gentle)
47 int i;
49 if (len < 0)
50 len = strlen(str);
52 for (i = 1; i < ARRAY_SIZE(object_type_strings); i++)
53 if (!xstrncmpz(object_type_strings[i], str, len))
54 return i;
56 if (gentle)
57 return -1;
59 die(_("invalid object type \"%s\""), str);
63 * Return a numerical hash value between 0 and n-1 for the object with
64 * the specified sha1. n must be a power of 2. Please note that the
65 * return value is *not* consistent across computer architectures.
67 static unsigned int hash_obj(const struct object_id *oid, unsigned int n)
69 return oidhash(oid) & (n - 1);
73 * Insert obj into the hash table hash, which has length size (which
74 * must be a power of 2). On collisions, simply overflow to the next
75 * empty bucket.
77 static void insert_obj_hash(struct object *obj, struct object **hash, unsigned int size)
79 unsigned int j = hash_obj(&obj->oid, size);
81 while (hash[j]) {
82 j++;
83 if (j >= size)
84 j = 0;
86 hash[j] = obj;
90 * Look up the record for the given sha1 in the hash map stored in
91 * obj_hash. Return NULL if it was not found.
93 struct object *lookup_object(struct repository *r, const struct object_id *oid)
95 unsigned int i, first;
96 struct object *obj;
98 if (!r->parsed_objects->obj_hash)
99 return NULL;
101 first = i = hash_obj(oid, r->parsed_objects->obj_hash_size);
102 while ((obj = r->parsed_objects->obj_hash[i]) != NULL) {
103 if (oideq(oid, &obj->oid))
104 break;
105 i++;
106 if (i == r->parsed_objects->obj_hash_size)
107 i = 0;
109 if (obj && i != first) {
111 * Move object to where we started to look for it so
112 * that we do not need to walk the hash table the next
113 * time we look for it.
115 SWAP(r->parsed_objects->obj_hash[i],
116 r->parsed_objects->obj_hash[first]);
118 return obj;
122 * Increase the size of the hash map stored in obj_hash to the next
123 * power of 2 (but at least 32). Copy the existing values to the new
124 * hash map.
126 static void grow_object_hash(struct repository *r)
128 int i;
130 * Note that this size must always be power-of-2 to match hash_obj
131 * above.
133 int new_hash_size = r->parsed_objects->obj_hash_size < 32 ? 32 : 2 * r->parsed_objects->obj_hash_size;
134 struct object **new_hash;
136 CALLOC_ARRAY(new_hash, new_hash_size);
137 for (i = 0; i < r->parsed_objects->obj_hash_size; i++) {
138 struct object *obj = r->parsed_objects->obj_hash[i];
140 if (!obj)
141 continue;
142 insert_obj_hash(obj, new_hash, new_hash_size);
144 free(r->parsed_objects->obj_hash);
145 r->parsed_objects->obj_hash = new_hash;
146 r->parsed_objects->obj_hash_size = new_hash_size;
149 void *create_object(struct repository *r, const struct object_id *oid, void *o)
151 struct object *obj = o;
153 obj->parsed = 0;
154 obj->flags = 0;
155 oidcpy(&obj->oid, oid);
157 if (r->parsed_objects->obj_hash_size - 1 <= r->parsed_objects->nr_objs * 2)
158 grow_object_hash(r);
160 insert_obj_hash(obj, r->parsed_objects->obj_hash,
161 r->parsed_objects->obj_hash_size);
162 r->parsed_objects->nr_objs++;
163 return obj;
166 void *object_as_type(struct object *obj, enum object_type type, int quiet)
168 if (obj->type == type)
169 return obj;
170 else if (obj->type == OBJ_NONE) {
171 if (type == OBJ_COMMIT)
172 init_commit_node((struct commit *) obj);
173 else
174 obj->type = type;
175 return obj;
177 else {
178 if (!quiet)
179 error(_("object %s is a %s, not a %s"),
180 oid_to_hex(&obj->oid),
181 type_name(obj->type), type_name(type));
182 return NULL;
186 struct object *lookup_unknown_object(struct repository *r, const struct object_id *oid)
188 struct object *obj = lookup_object(r, oid);
189 if (!obj)
190 obj = create_object(r, oid, alloc_object_node(r));
191 return obj;
194 struct object *lookup_object_by_type(struct repository *r,
195 const struct object_id *oid,
196 enum object_type type)
198 switch (type) {
199 case OBJ_COMMIT:
200 return (struct object *)lookup_commit(r, oid);
201 case OBJ_TREE:
202 return (struct object *)lookup_tree(r, oid);
203 case OBJ_TAG:
204 return (struct object *)lookup_tag(r, oid);
205 case OBJ_BLOB:
206 return (struct object *)lookup_blob(r, oid);
207 default:
208 BUG("unknown object type %d", type);
212 enum peel_status peel_object(struct repository *r,
213 const struct object_id *name,
214 struct object_id *oid)
216 struct object *o = lookup_unknown_object(r, name);
218 if (o->type == OBJ_NONE) {
219 int type = oid_object_info(r, name, NULL);
220 if (type < 0 || !object_as_type(o, type, 0))
221 return PEEL_INVALID;
224 if (o->type != OBJ_TAG)
225 return PEEL_NON_TAG;
227 o = deref_tag_noverify(r, o);
228 if (!o)
229 return PEEL_INVALID;
231 oidcpy(oid, &o->oid);
232 return PEEL_PEELED;
235 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)
237 struct object *obj;
238 *eaten_p = 0;
240 obj = NULL;
241 if (type == OBJ_BLOB) {
242 struct blob *blob = lookup_blob(r, oid);
243 if (blob) {
244 parse_blob_buffer(blob);
245 obj = &blob->object;
247 } else if (type == OBJ_TREE) {
248 struct tree *tree = lookup_tree(r, oid);
249 if (tree) {
250 obj = &tree->object;
251 if (!tree->buffer)
252 tree->object.parsed = 0;
253 if (!tree->object.parsed) {
254 if (parse_tree_buffer(tree, buffer, size))
255 return NULL;
256 *eaten_p = 1;
259 } else if (type == OBJ_COMMIT) {
260 struct commit *commit = lookup_commit(r, oid);
261 if (commit) {
262 if (parse_commit_buffer(r, commit, buffer, size, 1))
263 return NULL;
264 if (save_commit_buffer &&
265 !get_cached_commit_buffer(r, commit, NULL)) {
266 set_commit_buffer(r, commit, buffer, size);
267 *eaten_p = 1;
269 obj = &commit->object;
271 } else if (type == OBJ_TAG) {
272 struct tag *tag = lookup_tag(r, oid);
273 if (tag) {
274 if (parse_tag_buffer(r, tag, buffer, size))
275 return NULL;
276 obj = &tag->object;
278 } else {
279 warning(_("object %s has unknown type id %d"), oid_to_hex(oid), type);
280 obj = NULL;
282 return obj;
285 struct object *parse_object_or_die(const struct object_id *oid,
286 const char *name)
288 struct object *o = parse_object(the_repository, oid);
289 if (o)
290 return o;
292 die(_("unable to parse object: %s"), name ? name : oid_to_hex(oid));
295 struct object *parse_object_with_flags(struct repository *r,
296 const struct object_id *oid,
297 enum parse_object_flags flags)
299 int skip_hash = !!(flags & PARSE_OBJECT_SKIP_HASH_CHECK);
300 int discard_tree = !!(flags & PARSE_OBJECT_DISCARD_TREE);
301 unsigned long size;
302 enum object_type type;
303 int eaten;
304 const struct object_id *repl = lookup_replace_object(r, oid);
305 void *buffer;
306 struct object *obj;
308 obj = lookup_object(r, oid);
309 if (obj && obj->parsed)
310 return obj;
312 if (skip_hash) {
313 struct commit *commit = lookup_commit_in_graph(r, repl);
314 if (commit)
315 return &commit->object;
318 if ((!obj || obj->type == OBJ_BLOB) &&
319 oid_object_info(r, oid, NULL) == OBJ_BLOB) {
320 if (!skip_hash && stream_object_signature(r, repl) < 0) {
321 error(_("hash mismatch %s"), oid_to_hex(oid));
322 return NULL;
324 parse_blob_buffer(lookup_blob(r, oid));
325 return lookup_object(r, oid);
329 * If the caller does not care about the tree buffer and does not
330 * care about checking the hash, we can simply verify that we
331 * have the on-disk object with the correct type.
333 if (skip_hash && discard_tree &&
334 (!obj || obj->type == OBJ_TREE) &&
335 oid_object_info(r, oid, NULL) == OBJ_TREE) {
336 return &lookup_tree(r, oid)->object;
339 buffer = repo_read_object_file(r, oid, &type, &size);
340 if (buffer) {
341 if (!skip_hash &&
342 check_object_signature(r, repl, buffer, size, type) < 0) {
343 free(buffer);
344 error(_("hash mismatch %s"), oid_to_hex(repl));
345 return NULL;
348 obj = parse_object_buffer(r, oid, type, size,
349 buffer, &eaten);
350 if (!eaten)
351 free(buffer);
352 if (discard_tree && type == OBJ_TREE)
353 free_tree_buffer((struct tree *)obj);
354 return obj;
356 return NULL;
359 struct object *parse_object(struct repository *r, const struct object_id *oid)
361 return parse_object_with_flags(r, oid, 0);
364 struct object_list *object_list_insert(struct object *item,
365 struct object_list **list_p)
367 struct object_list *new_list = xmalloc(sizeof(struct object_list));
368 new_list->item = item;
369 new_list->next = *list_p;
370 *list_p = new_list;
371 return new_list;
374 int object_list_contains(struct object_list *list, struct object *obj)
376 while (list) {
377 if (list->item == obj)
378 return 1;
379 list = list->next;
381 return 0;
384 void object_list_free(struct object_list **list)
386 while (*list) {
387 struct object_list *p = *list;
388 *list = p->next;
389 free(p);
394 * A zero-length string to which object_array_entry::name can be
395 * initialized without requiring a malloc/free.
397 static char object_array_slopbuf[1];
399 void object_array_init(struct object_array *array)
401 struct object_array blank = OBJECT_ARRAY_INIT;
402 memcpy(array, &blank, sizeof(*array));
405 void add_object_array_with_path(struct object *obj, const char *name,
406 struct object_array *array,
407 unsigned mode, const char *path)
409 unsigned nr = array->nr;
410 unsigned alloc = array->alloc;
411 struct object_array_entry *objects = array->objects;
412 struct object_array_entry *entry;
414 if (nr >= alloc) {
415 alloc = (alloc + 32) * 2;
416 REALLOC_ARRAY(objects, alloc);
417 array->alloc = alloc;
418 array->objects = objects;
420 entry = &objects[nr];
421 entry->item = obj;
422 if (!name)
423 entry->name = NULL;
424 else if (!*name)
425 /* Use our own empty string instead of allocating one: */
426 entry->name = object_array_slopbuf;
427 else
428 entry->name = xstrdup(name);
429 entry->mode = mode;
430 if (path)
431 entry->path = xstrdup(path);
432 else
433 entry->path = NULL;
434 array->nr = ++nr;
437 void add_object_array(struct object *obj, const char *name, struct object_array *array)
439 add_object_array_with_path(obj, name, array, S_IFINVALID, NULL);
443 * Free all memory associated with an entry; the result is
444 * in an unspecified state and should not be examined.
446 static void object_array_release_entry(struct object_array_entry *ent)
448 if (ent->name != object_array_slopbuf)
449 free(ent->name);
450 free(ent->path);
453 struct object *object_array_pop(struct object_array *array)
455 struct object *ret;
457 if (!array->nr)
458 return NULL;
460 ret = array->objects[array->nr - 1].item;
461 object_array_release_entry(&array->objects[array->nr - 1]);
462 array->nr--;
463 return ret;
466 void object_array_filter(struct object_array *array,
467 object_array_each_func_t want, void *cb_data)
469 unsigned nr = array->nr, src, dst;
470 struct object_array_entry *objects = array->objects;
472 for (src = dst = 0; src < nr; src++) {
473 if (want(&objects[src], cb_data)) {
474 if (src != dst)
475 objects[dst] = objects[src];
476 dst++;
477 } else {
478 object_array_release_entry(&objects[src]);
481 array->nr = dst;
484 void object_array_clear(struct object_array *array)
486 int i;
487 for (i = 0; i < array->nr; i++)
488 object_array_release_entry(&array->objects[i]);
489 FREE_AND_NULL(array->objects);
490 array->nr = array->alloc = 0;
494 * Return true if array already contains an entry.
496 static int contains_object(struct object_array *array,
497 const struct object *item, const char *name)
499 unsigned nr = array->nr, i;
500 struct object_array_entry *object = array->objects;
502 for (i = 0; i < nr; i++, object++)
503 if (item == object->item && !strcmp(object->name, name))
504 return 1;
505 return 0;
508 void object_array_remove_duplicates(struct object_array *array)
510 unsigned nr = array->nr, src;
511 struct object_array_entry *objects = array->objects;
513 array->nr = 0;
514 for (src = 0; src < nr; src++) {
515 if (!contains_object(array, objects[src].item,
516 objects[src].name)) {
517 if (src != array->nr)
518 objects[array->nr] = objects[src];
519 array->nr++;
520 } else {
521 object_array_release_entry(&objects[src]);
526 void clear_object_flags(unsigned flags)
528 int i;
530 for (i=0; i < the_repository->parsed_objects->obj_hash_size; i++) {
531 struct object *obj = the_repository->parsed_objects->obj_hash[i];
532 if (obj)
533 obj->flags &= ~flags;
537 void repo_clear_commit_marks(struct repository *r, unsigned int flags)
539 int i;
541 for (i = 0; i < r->parsed_objects->obj_hash_size; i++) {
542 struct object *obj = r->parsed_objects->obj_hash[i];
543 if (obj && obj->type == OBJ_COMMIT)
544 obj->flags &= ~flags;
548 struct parsed_object_pool *parsed_object_pool_new(void)
550 struct parsed_object_pool *o = xmalloc(sizeof(*o));
551 memset(o, 0, sizeof(*o));
553 o->blob_state = allocate_alloc_state();
554 o->tree_state = allocate_alloc_state();
555 o->commit_state = allocate_alloc_state();
556 o->tag_state = allocate_alloc_state();
557 o->object_state = allocate_alloc_state();
559 o->is_shallow = -1;
560 CALLOC_ARRAY(o->shallow_stat, 1);
562 o->buffer_slab = allocate_commit_buffer_slab();
564 return o;
567 struct raw_object_store *raw_object_store_new(void)
569 struct raw_object_store *o = xmalloc(sizeof(*o));
571 memset(o, 0, sizeof(*o));
572 INIT_LIST_HEAD(&o->packed_git_mru);
573 hashmap_init(&o->pack_map, pack_map_entry_cmp, NULL, 0);
574 pthread_mutex_init(&o->replace_mutex, NULL);
575 return o;
578 void free_object_directory(struct object_directory *odb)
580 free(odb->path);
581 odb_clear_loose_cache(odb);
582 loose_object_map_clear(&odb->loose_map);
583 free(odb);
586 static void free_object_directories(struct raw_object_store *o)
588 while (o->odb) {
589 struct object_directory *next;
591 next = o->odb->next;
592 free_object_directory(o->odb);
593 o->odb = next;
595 kh_destroy_odb_path_map(o->odb_by_path);
596 o->odb_by_path = NULL;
599 void raw_object_store_clear(struct raw_object_store *o)
601 FREE_AND_NULL(o->alternate_db);
603 oidmap_free(o->replace_map, 1);
604 FREE_AND_NULL(o->replace_map);
605 pthread_mutex_destroy(&o->replace_mutex);
607 free_commit_graph(o->commit_graph);
608 o->commit_graph = NULL;
609 o->commit_graph_attempted = 0;
611 free_object_directories(o);
612 o->odb_tail = NULL;
613 o->loaded_alternates = 0;
615 INIT_LIST_HEAD(&o->packed_git_mru);
616 close_object_store(o);
617 o->packed_git = NULL;
619 hashmap_clear(&o->pack_map);
622 void parsed_object_pool_clear(struct parsed_object_pool *o)
625 * As objects are allocated in slabs (see alloc.c), we do
626 * not need to free each object, but each slab instead.
628 * Before doing so, we need to free any additional memory
629 * the objects may hold.
631 unsigned i;
633 for (i = 0; i < o->obj_hash_size; i++) {
634 struct object *obj = o->obj_hash[i];
636 if (!obj)
637 continue;
639 if (obj->type == OBJ_TREE)
640 free_tree_buffer((struct tree*)obj);
641 else if (obj->type == OBJ_COMMIT)
642 release_commit_memory(o, (struct commit*)obj);
643 else if (obj->type == OBJ_TAG)
644 release_tag_memory((struct tag*)obj);
647 FREE_AND_NULL(o->obj_hash);
648 o->obj_hash_size = 0;
650 free_commit_buffer_slab(o->buffer_slab);
651 o->buffer_slab = NULL;
653 clear_alloc_state(o->blob_state);
654 clear_alloc_state(o->tree_state);
655 clear_alloc_state(o->commit_state);
656 clear_alloc_state(o->tag_state);
657 clear_alloc_state(o->object_state);
658 stat_validity_clear(o->shallow_stat);
659 FREE_AND_NULL(o->blob_state);
660 FREE_AND_NULL(o->tree_state);
661 FREE_AND_NULL(o->commit_state);
662 FREE_AND_NULL(o->tag_state);
663 FREE_AND_NULL(o->object_state);
664 FREE_AND_NULL(o->shallow_stat);