1 #define USE_THE_REPOSITORY_VARIABLE
2 #define DISABLE_SIGN_COMPARE_WARNINGS
4 #include "git-compat-util.h"
8 #include "replace-object.h"
9 #include "object-file.h"
10 #include "object-store.h"
18 #include "commit-graph.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
))
43 return object_type_strings
[type
];
46 int type_from_string_gently(const char *str
, ssize_t len
, int gentle
)
53 for (i
= 1; i
< ARRAY_SIZE(object_type_strings
); i
++)
54 if (!xstrncmpz(object_type_strings
[i
], str
, len
))
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
78 static void insert_obj_hash(struct object
*obj
, struct object
**hash
, unsigned int size
)
80 unsigned int j
= hash_obj(&obj
->oid
, size
);
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
;
99 if (!r
->parsed_objects
->obj_hash
)
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
))
107 if (i
== r
->parsed_objects
->obj_hash_size
)
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
]);
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
127 static void grow_object_hash(struct repository
*r
)
131 * Note that this size must always be power-of-2 to match hash_obj
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
];
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
;
156 oidcpy(&obj
->oid
, oid
);
158 if (r
->parsed_objects
->obj_hash_size
- 1 <= r
->parsed_objects
->nr_objs
* 2)
161 insert_obj_hash(obj
, r
->parsed_objects
->obj_hash
,
162 r
->parsed_objects
->obj_hash_size
);
163 r
->parsed_objects
->nr_objs
++;
167 void *object_as_type(struct object
*obj
, enum object_type type
, int quiet
)
169 if (obj
->type
== type
)
171 else if (obj
->type
== OBJ_NONE
) {
172 if (type
== OBJ_COMMIT
)
173 init_commit_node((struct commit
*) obj
);
180 error(_("object %s is a %s, not a %s"),
181 oid_to_hex(&obj
->oid
),
182 type_name(obj
->type
), type_name(type
));
187 struct object
*lookup_unknown_object(struct repository
*r
, const struct object_id
*oid
)
189 struct object
*obj
= lookup_object(r
, oid
);
191 obj
= create_object(r
, oid
, alloc_object_node(r
));
195 struct object
*lookup_object_by_type(struct repository
*r
,
196 const struct object_id
*oid
,
197 enum object_type type
)
201 return (struct object
*)lookup_commit(r
, oid
);
203 return (struct object
*)lookup_tree(r
, oid
);
205 return (struct object
*)lookup_tag(r
, oid
);
207 return (struct object
*)lookup_blob(r
, oid
);
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))
225 if (o
->type
!= OBJ_TAG
)
228 o
= deref_tag_noverify(r
, o
);
232 oidcpy(oid
, &o
->oid
);
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
)
242 if (type
== OBJ_BLOB
) {
243 struct blob
*blob
= lookup_blob(r
, oid
);
245 parse_blob_buffer(blob
);
248 } else if (type
== OBJ_TREE
) {
249 struct tree
*tree
= lookup_tree(r
, oid
);
253 tree
->object
.parsed
= 0;
254 if (!tree
->object
.parsed
) {
255 if (parse_tree_buffer(tree
, buffer
, size
))
260 } else if (type
== OBJ_COMMIT
) {
261 struct commit
*commit
= lookup_commit(r
, oid
);
263 if (parse_commit_buffer(r
, commit
, buffer
, size
, 1))
265 if (save_commit_buffer
&&
266 !get_cached_commit_buffer(r
, commit
, NULL
)) {
267 set_commit_buffer(r
, commit
, buffer
, size
);
270 obj
= &commit
->object
;
272 } else if (type
== OBJ_TAG
) {
273 struct tag
*tag
= lookup_tag(r
, oid
);
275 if (parse_tag_buffer(r
, tag
, buffer
, size
))
280 warning(_("object %s has unknown type id %d"), oid_to_hex(oid
), type
);
286 struct object
*parse_object_or_die(const struct object_id
*oid
,
289 struct object
*o
= parse_object(the_repository
, oid
);
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
);
303 enum object_type type
;
305 const struct object_id
*repl
= lookup_replace_object(r
, oid
);
309 obj
= lookup_object(r
, oid
);
310 if (obj
&& obj
->parsed
)
314 struct commit
*commit
= lookup_commit_in_graph(r
, repl
);
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
));
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
);
343 check_object_signature(r
, repl
, buffer
, size
, type
) < 0) {
345 error(_("hash mismatch %s"), oid_to_hex(repl
));
349 obj
= parse_object_buffer(r
, oid
, type
, size
,
353 if (discard_tree
&& type
== OBJ_TREE
)
354 free_tree_buffer((struct tree
*)obj
);
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
;
375 int object_list_contains(struct object_list
*list
, struct object
*obj
)
378 if (list
->item
== obj
)
385 void object_list_free(struct object_list
**list
)
388 struct object_list
*p
= *list
;
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
;
416 alloc
= (alloc
+ 32) * 2;
417 REALLOC_ARRAY(objects
, alloc
);
418 array
->alloc
= alloc
;
419 array
->objects
= objects
;
421 entry
= &objects
[nr
];
426 /* Use our own empty string instead of allocating one: */
427 entry
->name
= object_array_slopbuf
;
429 entry
->name
= xstrdup(name
);
432 entry
->path
= xstrdup(path
);
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
)
454 struct object
*object_array_pop(struct object_array
*array
)
461 ret
= array
->objects
[array
->nr
- 1].item
;
462 object_array_release_entry(&array
->objects
[array
->nr
- 1]);
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
)) {
476 objects
[dst
] = objects
[src
];
479 object_array_release_entry(&objects
[src
]);
485 void object_array_clear(struct object_array
*array
)
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
))
509 void object_array_remove_duplicates(struct object_array
*array
)
511 unsigned nr
= array
->nr
, src
;
512 struct object_array_entry
*objects
= array
->objects
;
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
];
522 object_array_release_entry(&objects
[src
]);
527 void clear_object_flags(unsigned flags
)
531 for (i
=0; i
< the_repository
->parsed_objects
->obj_hash_size
; i
++) {
532 struct object
*obj
= the_repository
->parsed_objects
->obj_hash
[i
];
534 obj
->flags
&= ~flags
;
538 void repo_clear_commit_marks(struct repository
*r
, unsigned int flags
)
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
));
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();
562 CALLOC_ARRAY(o
->shallow_stat
, 1);
564 o
->buffer_slab
= allocate_commit_buffer_slab();
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
);
580 void free_object_directory(struct object_directory
*odb
)
583 odb_clear_loose_cache(odb
);
584 loose_object_map_clear(&odb
->loose_map
);
588 static void free_object_directories(struct raw_object_store
*o
)
591 struct object_directory
*next
;
594 free_object_directory(o
->odb
);
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
);
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
) {
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
);
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.
654 for (i
= 0; i
< o
->obj_hash_size
; i
++) {
655 struct object
*obj
= o
->obj_hash
[i
];
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
);