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 const char *type_names
[] = {
22 "none", "commit", "tree", "blob", "tag",
23 "bad type 5", "bad type 6", "delta", "bad",
26 static unsigned int hash_obj(struct object
*obj
, unsigned int n
)
28 unsigned int hash
= *(unsigned int *)obj
->sha1
;
32 static void insert_obj_hash(struct object
*obj
, struct object
**hash
, unsigned int size
)
34 int j
= hash_obj(obj
, size
);
44 static int hashtable_index(const unsigned char *sha1
)
47 memcpy(&i
, sha1
, sizeof(unsigned int));
48 return (int)(i
% obj_hash_size
);
51 struct object
*lookup_object(const unsigned char *sha1
)
59 i
= hashtable_index(sha1
);
60 while ((obj
= obj_hash
[i
]) != NULL
) {
61 if (!hashcmp(sha1
, obj
->sha1
))
64 if (i
== obj_hash_size
)
70 static void grow_object_hash(void)
73 int new_hash_size
= obj_hash_size
< 32 ? 32 : 2 * obj_hash_size
;
74 struct object
**new_hash
;
76 new_hash
= xcalloc(new_hash_size
, sizeof(struct object
*));
77 for (i
= 0; i
< obj_hash_size
; i
++) {
78 struct object
*obj
= obj_hash
[i
];
81 insert_obj_hash(obj
, new_hash
, new_hash_size
);
85 obj_hash_size
= new_hash_size
;
88 void created_object(const unsigned char *sha1
, struct object
*obj
)
94 hashcpy(obj
->sha1
, sha1
);
96 if (obj_hash_size
- 1 <= nr_objs
* 2)
99 insert_obj_hash(obj
, obj_hash
, obj_hash_size
);
103 struct object
*lookup_object_type(const unsigned char *sha1
, const char *type
)
106 return lookup_unknown_object(sha1
);
107 } else if (!strcmp(type
, blob_type
)) {
108 return &lookup_blob(sha1
)->object
;
109 } else if (!strcmp(type
, tree_type
)) {
110 return &lookup_tree(sha1
)->object
;
111 } else if (!strcmp(type
, commit_type
)) {
112 return &lookup_commit(sha1
)->object
;
113 } else if (!strcmp(type
, tag_type
)) {
114 return &lookup_tag(sha1
)->object
;
116 error("Unknown type %s", type
);
122 struct object object
;
123 struct commit commit
;
129 struct object
*lookup_unknown_object(const unsigned char *sha1
)
131 struct object
*obj
= lookup_object(sha1
);
133 union any_object
*ret
= xcalloc(1, sizeof(*ret
));
134 created_object(sha1
, &ret
->object
);
135 ret
->object
.type
= OBJ_NONE
;
141 struct object
*parse_object_buffer(const unsigned char *sha1
, const char *type
, unsigned long size
, void *buffer
, int *eaten_p
)
146 if (!strcmp(type
, blob_type
)) {
147 struct blob
*blob
= lookup_blob(sha1
);
148 parse_blob_buffer(blob
, buffer
, size
);
150 } else if (!strcmp(type
, tree_type
)) {
151 struct tree
*tree
= lookup_tree(sha1
);
153 if (!tree
->object
.parsed
) {
154 parse_tree_buffer(tree
, buffer
, size
);
157 } else if (!strcmp(type
, commit_type
)) {
158 struct commit
*commit
= lookup_commit(sha1
);
159 parse_commit_buffer(commit
, buffer
, size
);
160 if (!commit
->buffer
) {
161 commit
->buffer
= buffer
;
164 obj
= &commit
->object
;
165 } else if (!strcmp(type
, tag_type
)) {
166 struct tag
*tag
= lookup_tag(sha1
);
167 parse_tag_buffer(tag
, buffer
, size
);
176 struct object
*parse_object(const unsigned char *sha1
)
181 void *buffer
= read_sha1_file(sha1
, type
, &size
);
185 if (check_sha1_signature(sha1
, buffer
, size
, type
) < 0)
186 printf("sha1 mismatch %s\n", sha1_to_hex(sha1
));
188 obj
= parse_object_buffer(sha1
, type
, size
, buffer
, &eaten
);
196 struct object_list
*object_list_insert(struct object
*item
,
197 struct object_list
**list_p
)
199 struct object_list
*new_list
= xmalloc(sizeof(struct object_list
));
200 new_list
->item
= item
;
201 new_list
->next
= *list_p
;
206 void object_list_append(struct object
*item
,
207 struct object_list
**list_p
)
210 list_p
= &((*list_p
)->next
);
212 *list_p
= xmalloc(sizeof(struct object_list
));
213 (*list_p
)->next
= NULL
;
214 (*list_p
)->item
= item
;
217 unsigned object_list_length(struct object_list
*list
)
227 int object_list_contains(struct object_list
*list
, struct object
*obj
)
230 if (list
->item
== obj
)
237 void add_object_array(struct object
*obj
, const char *name
, struct object_array
*array
)
239 unsigned nr
= array
->nr
;
240 unsigned alloc
= array
->alloc
;
241 struct object_array_entry
*objects
= array
->objects
;
244 alloc
= (alloc
+ 32) * 2;
245 objects
= xrealloc(objects
, alloc
* sizeof(*objects
));
246 array
->alloc
= alloc
;
247 array
->objects
= objects
;
249 objects
[nr
].item
= obj
;
250 objects
[nr
].name
= name
;