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)
48 unsigned int hash = *(unsigned int *)obj->sha1;
52 static void insert_obj_hash(struct object *obj, struct object **hash, unsigned int size)
54 int j = hash_obj(obj, size);
64 static int hashtable_index(const unsigned char *sha1)
67 memcpy(&i, sha1, sizeof(unsigned int));
68 return (int)(i % obj_hash_size);
71 struct object *lookup_object(const unsigned char *sha1)
79 i = hashtable_index(sha1);
80 while ((obj = obj_hash[i]) != NULL) {
81 if (!hashcmp(sha1, obj->sha1))
84 if (i == obj_hash_size)
90 static void grow_object_hash(void)
93 int new_hash_size = obj_hash_size < 32 ? 32 : 2 * obj_hash_size;
94 struct object **new_hash;
96 new_hash = xcalloc(new_hash_size, sizeof(struct object *));
97 for (i = 0; i < obj_hash_size; i++) {
98 struct object *obj = obj_hash[i];
101 insert_obj_hash(obj, new_hash, new_hash_size);
105 obj_hash_size = new_hash_size;
108 void created_object(const unsigned char *sha1, struct object *obj)
112 obj->type = OBJ_NONE;
114 hashcpy(obj->sha1, sha1);
116 if (obj_hash_size - 1 <= nr_objs * 2)
119 insert_obj_hash(obj, obj_hash, obj_hash_size);
123 struct object *lookup_object_type(const unsigned char *sha1, const char *type)
126 return lookup_unknown_object(sha1);
127 } else if (!strcmp(type, blob_type)) {
128 return &lookup_blob(sha1)->object;
129 } else if (!strcmp(type, tree_type)) {
130 return &lookup_tree(sha1)->object;
131 } else if (!strcmp(type, commit_type)) {
132 return &lookup_commit(sha1)->object;
133 } else if (!strcmp(type, tag_type)) {
134 return &lookup_tag(sha1)->object;
136 error("Unknown type %s", type);
142 struct object object;
143 struct commit commit;
149 struct object *lookup_unknown_object(const unsigned char *sha1)
151 struct object *obj = lookup_object(sha1);
153 union any_object *ret = xcalloc(1, sizeof(*ret));
154 created_object(sha1, &ret->object);
155 ret->object.type = OBJ_NONE;
161 struct object *parse_object_buffer(const unsigned char *sha1, const char *type, unsigned long size, void *buffer, int *eaten_p)
166 if (!strcmp(type, blob_type)) {
167 struct blob *blob = lookup_blob(sha1);
168 parse_blob_buffer(blob, buffer, size);
170 } else if (!strcmp(type, tree_type)) {
171 struct tree *tree = lookup_tree(sha1);
173 if (!tree->object.parsed) {
174 parse_tree_buffer(tree, buffer, size);
177 } else if (!strcmp(type, commit_type)) {
178 struct commit *commit = lookup_commit(sha1);
179 parse_commit_buffer(commit, buffer, size);
180 if (!commit->buffer) {
181 commit->buffer = buffer;
184 obj = &commit->object;
185 } else if (!strcmp(type, tag_type)) {
186 struct tag *tag = lookup_tag(sha1);
187 parse_tag_buffer(tag, buffer, size);
196 struct object *parse_object(const unsigned char *sha1)
201 void *buffer = read_sha1_file(sha1, type, &size);
205 if (check_sha1_signature(sha1, buffer, size, type) < 0)
206 printf("sha1 mismatch %s\n", sha1_to_hex(sha1));
208 obj = parse_object_buffer(sha1, type, size, buffer, &eaten);
216 struct object_list *object_list_insert(struct object *item,
217 struct object_list **list_p)
219 struct object_list *new_list = xmalloc(sizeof(struct object_list));
220 new_list->item = item;
221 new_list->next = *list_p;
226 void object_list_append(struct object *item,
227 struct object_list **list_p)
230 list_p = &((*list_p)->next);
232 *list_p = xmalloc(sizeof(struct object_list));
233 (*list_p)->next = NULL;
234 (*list_p)->item = item;
237 unsigned object_list_length(struct object_list *list)
247 int object_list_contains(struct object_list *list, struct object *obj)
250 if (list->item == obj)
257 void add_object_array(struct object *obj, const char *name, struct object_array *array)
259 unsigned nr = array->nr;
260 unsigned alloc = array->alloc;
261 struct object_array_entry *objects = array->objects;
264 alloc = (alloc + 32) * 2;
265 objects = xrealloc(objects, alloc * sizeof(*objects));
266 array->alloc = alloc;
267 array->objects = objects;
269 objects[nr].item = obj;
270 objects[nr].name = name;