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(const unsigned char *sha1
, unsigned int n
)
49 memcpy(&hash
, sha1
, sizeof(unsigned int));
50 /* Assumes power-of-2 hash sizes in grow_object_hash */
51 return hash
& (n
- 1);
54 static void insert_obj_hash(struct object
*obj
, struct object
**hash
, unsigned int size
)
56 unsigned int j
= hash_obj(obj
->sha1
, size
);
66 struct object
*lookup_object(const unsigned char *sha1
)
68 unsigned int i
, first
;
74 first
= i
= hash_obj(sha1
, obj_hash_size
);
75 while ((obj
= obj_hash
[i
]) != NULL
) {
76 if (!hashcmp(sha1
, obj
->sha1
))
79 if (i
== obj_hash_size
)
82 if (obj
&& i
!= first
) {
84 * Move object to where we started to look for it so
85 * that we do not need to walk the hash table the next
86 * time we look for it.
88 struct object
*tmp
= obj_hash
[i
];
89 obj_hash
[i
] = obj_hash
[first
];
90 obj_hash
[first
] = tmp
;
95 static void grow_object_hash(void)
99 * Note that this size must always be power-of-2 to match hash_obj
102 int new_hash_size
= obj_hash_size
< 32 ? 32 : 2 * obj_hash_size
;
103 struct object
**new_hash
;
105 new_hash
= xcalloc(new_hash_size
, sizeof(struct object
*));
106 for (i
= 0; i
< obj_hash_size
; i
++) {
107 struct object
*obj
= obj_hash
[i
];
110 insert_obj_hash(obj
, new_hash
, new_hash_size
);
114 obj_hash_size
= new_hash_size
;
117 void *create_object(const unsigned char *sha1
, int type
, void *o
)
119 struct object
*obj
= o
;
125 hashcpy(obj
->sha1
, sha1
);
127 if (obj_hash_size
- 1 <= nr_objs
* 2)
130 insert_obj_hash(obj
, obj_hash
, obj_hash_size
);
135 struct object
*lookup_unknown_object(const unsigned char *sha1
)
137 struct object
*obj
= lookup_object(sha1
);
139 obj
= create_object(sha1
, OBJ_NONE
, alloc_object_node());
143 struct object
*parse_object_buffer(const unsigned char *sha1
, enum object_type type
, unsigned long size
, void *buffer
, int *eaten_p
)
149 if (type
== OBJ_BLOB
) {
150 struct blob
*blob
= lookup_blob(sha1
);
152 if (parse_blob_buffer(blob
, buffer
, size
))
156 } else if (type
== OBJ_TREE
) {
157 struct tree
*tree
= lookup_tree(sha1
);
161 tree
->object
.parsed
= 0;
162 if (!tree
->object
.parsed
) {
163 if (parse_tree_buffer(tree
, buffer
, size
))
168 } else if (type
== OBJ_COMMIT
) {
169 struct commit
*commit
= lookup_commit(sha1
);
171 if (parse_commit_buffer(commit
, buffer
, size
))
173 if (!commit
->buffer
) {
174 commit
->buffer
= buffer
;
177 obj
= &commit
->object
;
179 } else if (type
== OBJ_TAG
) {
180 struct tag
*tag
= lookup_tag(sha1
);
182 if (parse_tag_buffer(tag
, buffer
, size
))
187 warning("object %s has unknown type id %d", sha1_to_hex(sha1
), type
);
190 if (obj
&& obj
->type
== OBJ_NONE
)
195 struct object
*parse_object_or_die(const unsigned char *sha1
,
198 struct object
*o
= parse_object(sha1
);
202 die(_("unable to parse object: %s"), name
? name
: sha1_to_hex(sha1
));
205 struct object
*parse_object(const unsigned char *sha1
)
208 enum object_type type
;
210 const unsigned char *repl
= lookup_replace_object(sha1
);
214 obj
= lookup_object(sha1
);
215 if (obj
&& obj
->parsed
)
218 if ((obj
&& obj
->type
== OBJ_BLOB
) ||
219 (!obj
&& has_sha1_file(sha1
) &&
220 sha1_object_info(sha1
, NULL
) == OBJ_BLOB
)) {
221 if (check_sha1_signature(repl
, NULL
, 0, NULL
) < 0) {
222 error("sha1 mismatch %s", sha1_to_hex(repl
));
225 parse_blob_buffer(lookup_blob(sha1
), NULL
, 0);
226 return lookup_object(sha1
);
229 buffer
= read_sha1_file(sha1
, &type
, &size
);
231 if (check_sha1_signature(repl
, buffer
, size
, typename(type
)) < 0) {
233 error("sha1 mismatch %s", sha1_to_hex(repl
));
237 obj
= parse_object_buffer(sha1
, type
, size
, buffer
, &eaten
);
245 struct object_list
*object_list_insert(struct object
*item
,
246 struct object_list
**list_p
)
248 struct object_list
*new_list
= xmalloc(sizeof(struct object_list
));
249 new_list
->item
= item
;
250 new_list
->next
= *list_p
;
255 int object_list_contains(struct object_list
*list
, struct object
*obj
)
258 if (list
->item
== obj
)
266 * A zero-length string to which object_array_entry::name can be
267 * initialized without requiring a malloc/free.
269 static char object_array_slopbuf
[1];
271 static void add_object_array_with_mode_context(struct object
*obj
, const char *name
,
272 struct object_array
*array
,
274 struct object_context
*context
)
276 unsigned nr
= array
->nr
;
277 unsigned alloc
= array
->alloc
;
278 struct object_array_entry
*objects
= array
->objects
;
279 struct object_array_entry
*entry
;
282 alloc
= (alloc
+ 32) * 2;
283 objects
= xrealloc(objects
, alloc
* sizeof(*objects
));
284 array
->alloc
= alloc
;
285 array
->objects
= objects
;
287 entry
= &objects
[nr
];
292 /* Use our own empty string instead of allocating one: */
293 entry
->name
= object_array_slopbuf
;
295 entry
->name
= xstrdup(name
);
297 entry
->context
= context
;
301 void add_object_array(struct object
*obj
, const char *name
, struct object_array
*array
)
303 add_object_array_with_mode(obj
, name
, array
, S_IFINVALID
);
306 void add_object_array_with_mode(struct object
*obj
, const char *name
, struct object_array
*array
, unsigned mode
)
308 add_object_array_with_mode_context(obj
, name
, array
, mode
, NULL
);
311 void add_object_array_with_context(struct object
*obj
, const char *name
, struct object_array
*array
, struct object_context
*context
)
314 add_object_array_with_mode_context(obj
, name
, array
, context
->mode
, context
);
316 add_object_array_with_mode_context(obj
, name
, array
, S_IFINVALID
, context
);
319 void object_array_filter(struct object_array
*array
,
320 object_array_each_func_t want
, void *cb_data
)
322 unsigned nr
= array
->nr
, src
, dst
;
323 struct object_array_entry
*objects
= array
->objects
;
325 for (src
= dst
= 0; src
< nr
; src
++) {
326 if (want(&objects
[src
], cb_data
)) {
328 objects
[dst
] = objects
[src
];
331 if (objects
[src
].name
!= object_array_slopbuf
)
332 free(objects
[src
].name
);
339 * Return true iff array already contains an entry with name.
341 static int contains_name(struct object_array
*array
, const char *name
)
343 unsigned nr
= array
->nr
, i
;
344 struct object_array_entry
*object
= array
->objects
;
346 for (i
= 0; i
< nr
; i
++, object
++)
347 if (!strcmp(object
->name
, name
))
352 void object_array_remove_duplicates(struct object_array
*array
)
354 unsigned nr
= array
->nr
, src
;
355 struct object_array_entry
*objects
= array
->objects
;
358 for (src
= 0; src
< nr
; src
++) {
359 if (!contains_name(array
, objects
[src
].name
)) {
360 if (src
!= array
->nr
)
361 objects
[array
->nr
] = objects
[src
];
364 if (objects
[src
].name
!= object_array_slopbuf
)
365 free(objects
[src
].name
);
370 void clear_object_flags(unsigned flags
)
374 for (i
=0; i
< obj_hash_size
; i
++) {
375 struct object
*obj
= obj_hash
[i
];
377 obj
->flags
&= ~flags
;