8 static struct object
**objs
;
9 static int nr_objs
, obj_allocs
;
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 int hashtable_index(const unsigned char *sha1
)
28 memcpy(&i
, sha1
, sizeof(unsigned int));
29 return (int)(i
% obj_allocs
);
32 static int find_object(const unsigned char *sha1
)
39 i
= hashtable_index(sha1
);
41 if (memcmp(sha1
, objs
[i
]->sha1
, 20) == 0)
50 struct object
*lookup_object(const unsigned char *sha1
)
52 int pos
= find_object(sha1
);
58 void created_object(const unsigned char *sha1
, struct object
*obj
)
63 memcpy(obj
->sha1
, sha1
, 20);
64 obj
->type
= TYPE_NONE
;
67 if (obj_allocs
- 1 <= nr_objs
* 2) {
68 int i
, count
= obj_allocs
;
69 obj_allocs
= (obj_allocs
< 32 ? 32 : 2 * obj_allocs
);
70 objs
= xrealloc(objs
, obj_allocs
* sizeof(struct object
*));
71 memset(objs
+ count
, 0, (obj_allocs
- count
)
72 * sizeof(struct object
*));
73 for (i
= 0; i
< obj_allocs
; i
++)
75 int j
= find_object(objs
[i
]->sha1
);
84 pos
= find_object(sha1
);
86 die("Inserting %s twice\n", sha1_to_hex(sha1
));
93 struct object
*lookup_object_type(const unsigned char *sha1
, const char *type
)
96 return lookup_unknown_object(sha1
);
97 } else if (!strcmp(type
, blob_type
)) {
98 return &lookup_blob(sha1
)->object
;
99 } else if (!strcmp(type
, tree_type
)) {
100 return &lookup_tree(sha1
)->object
;
101 } else if (!strcmp(type
, commit_type
)) {
102 return &lookup_commit(sha1
)->object
;
103 } else if (!strcmp(type
, tag_type
)) {
104 return &lookup_tag(sha1
)->object
;
106 error("Unknown type %s", type
);
112 struct object object
;
113 struct commit commit
;
119 struct object
*lookup_unknown_object(const unsigned char *sha1
)
121 struct object
*obj
= lookup_object(sha1
);
123 union any_object
*ret
= xcalloc(1, sizeof(*ret
));
124 created_object(sha1
, &ret
->object
);
125 ret
->object
.type
= TYPE_NONE
;
131 struct object
*parse_object(const unsigned char *sha1
)
135 void *buffer
= read_sha1_file(sha1
, type
, &size
);
138 if (check_sha1_signature(sha1
, buffer
, size
, type
) < 0)
139 printf("sha1 mismatch %s\n", sha1_to_hex(sha1
));
140 if (!strcmp(type
, blob_type
)) {
141 struct blob
*blob
= lookup_blob(sha1
);
142 parse_blob_buffer(blob
, buffer
, size
);
144 } else if (!strcmp(type
, tree_type
)) {
145 struct tree
*tree
= lookup_tree(sha1
);
147 if (!tree
->object
.parsed
) {
148 parse_tree_buffer(tree
, buffer
, size
);
151 } else if (!strcmp(type
, commit_type
)) {
152 struct commit
*commit
= lookup_commit(sha1
);
153 parse_commit_buffer(commit
, buffer
, size
);
154 if (!commit
->buffer
) {
155 commit
->buffer
= buffer
;
158 obj
= &commit
->object
;
159 } else if (!strcmp(type
, tag_type
)) {
160 struct tag
*tag
= lookup_tag(sha1
);
161 parse_tag_buffer(tag
, buffer
, size
);
172 struct object_list
*object_list_insert(struct object
*item
,
173 struct object_list
**list_p
)
175 struct object_list
*new_list
= xmalloc(sizeof(struct object_list
));
176 new_list
->item
= item
;
177 new_list
->next
= *list_p
;
182 void object_list_append(struct object
*item
,
183 struct object_list
**list_p
)
186 list_p
= &((*list_p
)->next
);
188 *list_p
= xmalloc(sizeof(struct object_list
));
189 (*list_p
)->next
= NULL
;
190 (*list_p
)->item
= item
;
193 unsigned object_list_length(struct object_list
*list
)
203 int object_list_contains(struct object_list
*list
, struct object
*obj
)
206 if (list
->item
== obj
)
213 void add_object_array(struct object
*obj
, const char *name
, struct object_array
*array
)
215 unsigned nr
= array
->nr
;
216 unsigned alloc
= array
->alloc
;
217 struct object_array_entry
*objects
= array
->objects
;
220 alloc
= (alloc
+ 32) * 2;
221 objects
= xrealloc(objects
, alloc
* sizeof(*objects
));
222 array
->alloc
= alloc
;
223 array
->objects
= objects
;
225 objects
[nr
].item
= obj
;
226 objects
[nr
].name
= name
;