1 #define USE_THE_REPOSITORY_VARIABLE
3 #include "git-compat-util.h"
7 #include "replace-object.h"
8 #include "object-file.h"
9 #include "object-store.h"
17 #include "commit-graph.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
))
42 return object_type_strings
[type
];
45 int type_from_string_gently(const char *str
, ssize_t len
, int gentle
)
52 for (i
= 1; i
< ARRAY_SIZE(object_type_strings
); i
++)
53 if (!xstrncmpz(object_type_strings
[i
], str
, len
))
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
77 static void insert_obj_hash(struct object
*obj
, struct object
**hash
, unsigned int size
)
79 unsigned int j
= hash_obj(&obj
->oid
, size
);
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
;
98 if (!r
->parsed_objects
->obj_hash
)
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
))
106 if (i
== r
->parsed_objects
->obj_hash_size
)
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
]);
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
126 static void grow_object_hash(struct repository
*r
)
130 * Note that this size must always be power-of-2 to match hash_obj
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
];
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
;
155 oidcpy(&obj
->oid
, oid
);
157 if (r
->parsed_objects
->obj_hash_size
- 1 <= r
->parsed_objects
->nr_objs
* 2)
160 insert_obj_hash(obj
, r
->parsed_objects
->obj_hash
,
161 r
->parsed_objects
->obj_hash_size
);
162 r
->parsed_objects
->nr_objs
++;
166 void *object_as_type(struct object
*obj
, enum object_type type
, int quiet
)
168 if (obj
->type
== type
)
170 else if (obj
->type
== OBJ_NONE
) {
171 if (type
== OBJ_COMMIT
)
172 init_commit_node((struct commit
*) obj
);
179 error(_("object %s is a %s, not a %s"),
180 oid_to_hex(&obj
->oid
),
181 type_name(obj
->type
), type_name(type
));
186 struct object
*lookup_unknown_object(struct repository
*r
, const struct object_id
*oid
)
188 struct object
*obj
= lookup_object(r
, oid
);
190 obj
= create_object(r
, oid
, alloc_object_node(r
));
194 struct object
*lookup_object_by_type(struct repository
*r
,
195 const struct object_id
*oid
,
196 enum object_type type
)
200 return (struct object
*)lookup_commit(r
, oid
);
202 return (struct object
*)lookup_tree(r
, oid
);
204 return (struct object
*)lookup_tag(r
, oid
);
206 return (struct object
*)lookup_blob(r
, oid
);
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))
224 if (o
->type
!= OBJ_TAG
)
227 o
= deref_tag_noverify(r
, o
);
231 oidcpy(oid
, &o
->oid
);
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
)
241 if (type
== OBJ_BLOB
) {
242 struct blob
*blob
= lookup_blob(r
, oid
);
244 parse_blob_buffer(blob
);
247 } else if (type
== OBJ_TREE
) {
248 struct tree
*tree
= lookup_tree(r
, oid
);
252 tree
->object
.parsed
= 0;
253 if (!tree
->object
.parsed
) {
254 if (parse_tree_buffer(tree
, buffer
, size
))
259 } else if (type
== OBJ_COMMIT
) {
260 struct commit
*commit
= lookup_commit(r
, oid
);
262 if (parse_commit_buffer(r
, commit
, buffer
, size
, 1))
264 if (save_commit_buffer
&&
265 !get_cached_commit_buffer(r
, commit
, NULL
)) {
266 set_commit_buffer(r
, commit
, buffer
, size
);
269 obj
= &commit
->object
;
271 } else if (type
== OBJ_TAG
) {
272 struct tag
*tag
= lookup_tag(r
, oid
);
274 if (parse_tag_buffer(r
, tag
, buffer
, size
))
279 warning(_("object %s has unknown type id %d"), oid_to_hex(oid
), type
);
285 struct object
*parse_object_or_die(const struct object_id
*oid
,
288 struct object
*o
= parse_object(the_repository
, oid
);
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
);
302 enum object_type type
;
304 const struct object_id
*repl
= lookup_replace_object(r
, oid
);
308 obj
= lookup_object(r
, oid
);
309 if (obj
&& obj
->parsed
)
313 struct commit
*commit
= lookup_commit_in_graph(r
, repl
);
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
));
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
);
342 check_object_signature(r
, repl
, buffer
, size
, type
) < 0) {
344 error(_("hash mismatch %s"), oid_to_hex(repl
));
348 obj
= parse_object_buffer(r
, oid
, type
, size
,
352 if (discard_tree
&& type
== OBJ_TREE
)
353 free_tree_buffer((struct tree
*)obj
);
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
;
374 int object_list_contains(struct object_list
*list
, struct object
*obj
)
377 if (list
->item
== obj
)
384 void object_list_free(struct object_list
**list
)
387 struct object_list
*p
= *list
;
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
;
415 alloc
= (alloc
+ 32) * 2;
416 REALLOC_ARRAY(objects
, alloc
);
417 array
->alloc
= alloc
;
418 array
->objects
= objects
;
420 entry
= &objects
[nr
];
425 /* Use our own empty string instead of allocating one: */
426 entry
->name
= object_array_slopbuf
;
428 entry
->name
= xstrdup(name
);
431 entry
->path
= xstrdup(path
);
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
)
453 struct object
*object_array_pop(struct object_array
*array
)
460 ret
= array
->objects
[array
->nr
- 1].item
;
461 object_array_release_entry(&array
->objects
[array
->nr
- 1]);
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
)) {
475 objects
[dst
] = objects
[src
];
478 object_array_release_entry(&objects
[src
]);
484 void object_array_clear(struct object_array
*array
)
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
))
508 void object_array_remove_duplicates(struct object_array
*array
)
510 unsigned nr
= array
->nr
, src
;
511 struct object_array_entry
*objects
= array
->objects
;
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
];
521 object_array_release_entry(&objects
[src
]);
526 void clear_object_flags(unsigned flags
)
530 for (i
=0; i
< the_repository
->parsed_objects
->obj_hash_size
; i
++) {
531 struct object
*obj
= the_repository
->parsed_objects
->obj_hash
[i
];
533 obj
->flags
&= ~flags
;
537 void repo_clear_commit_marks(struct repository
*r
, unsigned int flags
)
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();
560 CALLOC_ARRAY(o
->shallow_stat
, 1);
562 o
->buffer_slab
= allocate_commit_buffer_slab();
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
);
578 void free_object_directory(struct object_directory
*odb
)
581 odb_clear_loose_cache(odb
);
582 loose_object_map_clear(&odb
->loose_map
);
586 static void free_object_directories(struct raw_object_store
*o
)
589 struct object_directory
*next
;
592 free_object_directory(o
->odb
);
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
);
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.
633 for (i
= 0; i
< o
->obj_hash_size
; i
++) {
634 struct object
*obj
= o
->obj_hash
[i
];
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
);