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", "blob", "tree", "commit", "bad"
25 static unsigned int hash_obj(struct object
*obj
, unsigned int n
)
27 unsigned int hash
= *(unsigned int *)obj
->sha1
;
31 static void insert_obj_hash(struct object
*obj
, struct object
**hash
, unsigned int size
)
33 int j
= hash_obj(obj
, size
);
43 static int hashtable_index(const unsigned char *sha1
)
46 memcpy(&i
, sha1
, sizeof(unsigned int));
47 return (int)(i
% obj_hash_size
);
50 struct object
*lookup_object(const unsigned char *sha1
)
58 i
= hashtable_index(sha1
);
59 while ((obj
= obj_hash
[i
]) != NULL
) {
60 if (!memcmp(sha1
, obj
->sha1
, 20))
63 if (i
== obj_hash_size
)
69 static void grow_object_hash(void)
72 int new_hash_size
= obj_hash_size
< 32 ? 32 : 2 * obj_hash_size
;
73 struct object
**new_hash
;
75 new_hash
= calloc(new_hash_size
, sizeof(struct object
*));
76 for (i
= 0; i
< obj_hash_size
; i
++) {
77 struct object
*obj
= obj_hash
[i
];
80 insert_obj_hash(obj
, new_hash
, new_hash_size
);
84 obj_hash_size
= new_hash_size
;
87 void created_object(const unsigned char *sha1
, struct object
*obj
)
91 obj
->type
= TYPE_NONE
;
93 memcpy(obj
->sha1
, sha1
, 20);
95 if (obj_hash_size
- 1 <= nr_objs
* 2)
98 insert_obj_hash(obj
, obj_hash
, obj_hash_size
);
102 struct object
*lookup_object_type(const unsigned char *sha1
, const char *type
)
105 return lookup_unknown_object(sha1
);
106 } else if (!strcmp(type
, blob_type
)) {
107 return &lookup_blob(sha1
)->object
;
108 } else if (!strcmp(type
, tree_type
)) {
109 return &lookup_tree(sha1
)->object
;
110 } else if (!strcmp(type
, commit_type
)) {
111 return &lookup_commit(sha1
)->object
;
112 } else if (!strcmp(type
, tag_type
)) {
113 return &lookup_tag(sha1
)->object
;
115 error("Unknown type %s", type
);
121 struct object object
;
122 struct commit commit
;
128 struct object
*lookup_unknown_object(const unsigned char *sha1
)
130 struct object
*obj
= lookup_object(sha1
);
132 union any_object
*ret
= xcalloc(1, sizeof(*ret
));
133 created_object(sha1
, &ret
->object
);
134 ret
->object
.type
= TYPE_NONE
;
140 struct object
*parse_object(const unsigned char *sha1
)
144 void *buffer
= read_sha1_file(sha1
, type
, &size
);
147 if (check_sha1_signature(sha1
, buffer
, size
, type
) < 0)
148 printf("sha1 mismatch %s\n", sha1_to_hex(sha1
));
149 if (!strcmp(type
, blob_type
)) {
150 struct blob
*blob
= lookup_blob(sha1
);
151 parse_blob_buffer(blob
, buffer
, size
);
153 } else if (!strcmp(type
, tree_type
)) {
154 struct tree
*tree
= lookup_tree(sha1
);
156 if (!tree
->object
.parsed
) {
157 parse_tree_buffer(tree
, buffer
, size
);
160 } else if (!strcmp(type
, commit_type
)) {
161 struct commit
*commit
= lookup_commit(sha1
);
162 parse_commit_buffer(commit
, buffer
, size
);
163 if (!commit
->buffer
) {
164 commit
->buffer
= buffer
;
167 obj
= &commit
->object
;
168 } else if (!strcmp(type
, tag_type
)) {
169 struct tag
*tag
= lookup_tag(sha1
);
170 parse_tag_buffer(tag
, buffer
, size
);
181 struct object_list
*object_list_insert(struct object
*item
,
182 struct object_list
**list_p
)
184 struct object_list
*new_list
= xmalloc(sizeof(struct object_list
));
185 new_list
->item
= item
;
186 new_list
->next
= *list_p
;
191 void object_list_append(struct object
*item
,
192 struct object_list
**list_p
)
195 list_p
= &((*list_p
)->next
);
197 *list_p
= xmalloc(sizeof(struct object_list
));
198 (*list_p
)->next
= NULL
;
199 (*list_p
)->item
= item
;
202 unsigned object_list_length(struct object_list
*list
)
212 int object_list_contains(struct object_list
*list
, struct object
*obj
)
215 if (list
->item
== obj
)
222 void add_object_array(struct object
*obj
, const char *name
, struct object_array
*array
)
224 unsigned nr
= array
->nr
;
225 unsigned alloc
= array
->alloc
;
226 struct object_array_entry
*objects
= array
->objects
;
229 alloc
= (alloc
+ 32) * 2;
230 objects
= xrealloc(objects
, alloc
* sizeof(*objects
));
231 array
->alloc
= alloc
;
232 array
->objects
= objects
;
234 objects
[nr
].item
= obj
;
235 objects
[nr
].name
= name
;
239 void clear_object_marks(unsigned mark
)
243 for (i
= 0; i
< obj_hash_size
; i
++)
245 obj_hash
[i
]->flags
&= ~mark
;