8 static struct object **obj_hash;
9 static int nr_objs, obj_hash_size;
11 unsigned int get_max_object_index(void)
16 struct object *get_indexed_object(unsigned int idx)
21 static const char *object_type_strings[] = {
22 NULL, /* OBJ_NONE = 0 */
23 "commit", /* OBJ_COMMIT = 1 */
24 "tree", /* OBJ_TREE = 2 */
25 "blob", /* OBJ_BLOB = 3 */
26 "tag", /* OBJ_TAG = 4 */
29 const char *typename(unsigned int type)
31 if (type >= ARRAY_SIZE(object_type_strings))
33 return object_type_strings[type];
36 int type_from_string(const char *str)
40 for (i = 1; i < ARRAY_SIZE(object_type_strings); i++)
41 if (!strcmp(str, object_type_strings[i]))
43 die("invalid object type \"%s\"", str);
46 static unsigned int hash_obj(struct object *obj, unsigned int n)
49 memcpy(&hash, obj->sha1, sizeof(unsigned int));
53 static void insert_obj_hash(struct object *obj, struct object **hash, unsigned int size)
55 unsigned int j = hash_obj(obj, size);
65 static unsigned int hashtable_index(const unsigned char *sha1)
68 memcpy(&i, sha1, sizeof(unsigned int));
69 return i % obj_hash_size;
72 struct object *lookup_object(const unsigned char *sha1)
74 unsigned int i, first;
80 first = i = hashtable_index(sha1);
81 while ((obj = obj_hash[i]) != NULL) {
82 if (!hashcmp(sha1, obj->sha1))
85 if (i == obj_hash_size)
88 if (obj && i != first) {
90 * Move object to where we started to look for it so
91 * that we do not need to walk the hash table the next
92 * time we look for it.
94 struct object *tmp = obj_hash[i];
95 obj_hash[i] = obj_hash[first];
96 obj_hash[first] = tmp;
101 static void grow_object_hash(void)
104 int new_hash_size = obj_hash_size < 32 ? 32 : 2 * obj_hash_size;
105 struct object **new_hash;
107 new_hash = xcalloc(new_hash_size, sizeof(struct object *));
108 for (i = 0; i < obj_hash_size; i++) {
109 struct object *obj = obj_hash[i];
112 insert_obj_hash(obj, new_hash, new_hash_size);
116 obj_hash_size = new_hash_size;
119 void *create_object(const unsigned char *sha1, int type, void *o)
121 struct object *obj = o;
127 hashcpy(obj->sha1, sha1);
129 if (obj_hash_size - 1 <= nr_objs * 2)
132 insert_obj_hash(obj, obj_hash, obj_hash_size);
137 struct object *lookup_unknown_object(const unsigned char *sha1)
139 struct object *obj = lookup_object(sha1);
141 obj = create_object(sha1, OBJ_NONE, alloc_object_node());
145 struct object *parse_object_buffer(const unsigned char *sha1, enum object_type type, unsigned long size, void *buffer, int *eaten_p)
151 if (type == OBJ_BLOB) {
152 struct blob *blob = lookup_blob(sha1);
154 if (parse_blob_buffer(blob, buffer, size))
158 } else if (type == OBJ_TREE) {
159 struct tree *tree = lookup_tree(sha1);
163 tree->object.parsed = 0;
164 if (!tree->object.parsed) {
165 if (parse_tree_buffer(tree, buffer, size))
170 } else if (type == OBJ_COMMIT) {
171 struct commit *commit = lookup_commit(sha1);
173 if (parse_commit_buffer(commit, buffer, size))
175 if (!commit->buffer) {
176 commit->buffer = buffer;
179 obj = &commit->object;
181 } else if (type == OBJ_TAG) {
182 struct tag *tag = lookup_tag(sha1);
184 if (parse_tag_buffer(tag, buffer, size))
189 warning("object %s has unknown type id %d", sha1_to_hex(sha1), type);
192 if (obj && obj->type == OBJ_NONE)
197 struct object *parse_object_or_die(const unsigned char *sha1,
200 struct object *o = parse_object(sha1);
204 die(_("unable to parse object: %s"), name ? name : sha1_to_hex(sha1));
207 struct object *parse_object(const unsigned char *sha1)
210 enum object_type type;
212 const unsigned char *repl = lookup_replace_object(sha1);
216 obj = lookup_object(sha1);
217 if (obj && obj->parsed)
220 if ((obj && obj->type == OBJ_BLOB) ||
221 (!obj && has_sha1_file(sha1) &&
222 sha1_object_info(sha1, NULL) == OBJ_BLOB)) {
223 if (check_sha1_signature(repl, NULL, 0, NULL) < 0) {
224 error("sha1 mismatch %s", sha1_to_hex(repl));
227 parse_blob_buffer(lookup_blob(sha1), NULL, 0);
228 return lookup_object(sha1);
231 buffer = read_sha1_file(sha1, &type, &size);
233 if (check_sha1_signature(repl, buffer, size, typename(type)) < 0) {
235 error("sha1 mismatch %s", sha1_to_hex(repl));
239 obj = parse_object_buffer(sha1, type, size, buffer, &eaten);
247 struct object_list *object_list_insert(struct object *item,
248 struct object_list **list_p)
250 struct object_list *new_list = xmalloc(sizeof(struct object_list));
251 new_list->item = item;
252 new_list->next = *list_p;
257 int object_list_contains(struct object_list *list, struct object *obj)
260 if (list->item == obj)
267 void add_object_array(struct object *obj, const char *name, struct object_array *array)
269 add_object_array_with_mode(obj, name, array, S_IFINVALID);
273 * A zero-length string to which object_array_entry::name can be
274 * initialized without requiring a malloc/free.
276 static char object_array_slopbuf[1];
278 void add_object_array_with_mode(struct object *obj, const char *name, struct object_array *array, unsigned mode)
280 unsigned nr = array->nr;
281 unsigned alloc = array->alloc;
282 struct object_array_entry *objects = array->objects;
283 struct object_array_entry *entry;
286 alloc = (alloc + 32) * 2;
287 objects = xrealloc(objects, alloc * sizeof(*objects));
288 array->alloc = alloc;
289 array->objects = objects;
291 entry = &objects[nr];
296 /* Use our own empty string instead of allocating one: */
297 entry->name = object_array_slopbuf;
299 entry->name = xstrdup(name);
304 void object_array_filter(struct object_array *array,
305 object_array_each_func_t want, void *cb_data)
307 unsigned nr = array->nr, src, dst;
308 struct object_array_entry *objects = array->objects;
310 for (src = dst = 0; src < nr; src++) {
311 if (want(&objects[src], cb_data)) {
313 objects[dst] = objects[src];
316 if (objects[src].name != object_array_slopbuf)
317 free(objects[src].name);
324 * Return true iff array already contains an entry with name.
326 static int contains_name(struct object_array *array, const char *name)
328 unsigned nr = array->nr, i;
329 struct object_array_entry *object = array->objects;
331 for (i = 0; i < nr; i++, object++)
332 if (!strcmp(object->name, name))
337 void object_array_remove_duplicates(struct object_array *array)
339 unsigned nr = array->nr, src;
340 struct object_array_entry *objects = array->objects;
343 for (src = 0; src < nr; src++) {
344 if (!contains_name(array, objects[src].name)) {
345 if (src != array->nr)
346 objects[array->nr] = objects[src];
349 if (objects[src].name != object_array_slopbuf)
350 free(objects[src].name);
355 void clear_object_flags(unsigned flags)
359 for (i=0; i < obj_hash_size; i++) {
360 struct object *obj = obj_hash[i];
362 obj->flags &= ~flags;