]>
Commit | Line | Data |
---|---|---|
8f1d2e6f | 1 | #include "cache.h" |
175785e5 | 2 | #include "object.h" |
e9eefa67 DB |
3 | #include "blob.h" |
4 | #include "tree.h" | |
5 | #include "commit.h" | |
e9eefa67 | 6 | #include "tag.h" |
175785e5 | 7 | |
0556a11a LT |
8 | static struct object **obj_hash; |
9 | static int nr_objs, obj_hash_size; | |
fc046a75 LT |
10 | |
11 | unsigned int get_max_object_index(void) | |
12 | { | |
0556a11a | 13 | return obj_hash_size; |
fc046a75 LT |
14 | } |
15 | ||
16 | struct object *get_indexed_object(unsigned int idx) | |
17 | { | |
0556a11a | 18 | return obj_hash[idx]; |
fc046a75 | 19 | } |
175785e5 | 20 | |
df843662 NP |
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 */ | |
885a86ab LT |
27 | }; |
28 | ||
df843662 NP |
29 | const char *typename(unsigned int type) |
30 | { | |
31 | if (type >= ARRAY_SIZE(object_type_strings)) | |
32 | return NULL; | |
33 | return object_type_strings[type]; | |
34 | } | |
35 | ||
36 | int type_from_string(const char *str) | |
37 | { | |
38 | int i; | |
39 | ||
40 | for (i = 1; i < ARRAY_SIZE(object_type_strings); i++) | |
41 | if (!strcmp(str, object_type_strings[i])) | |
42 | return i; | |
43 | die("invalid object type \"%s\"", str); | |
44 | } | |
45 | ||
9f36c9b7 | 46 | static unsigned int hash_obj(const unsigned char *sha1, unsigned int n) |
0556a11a | 47 | { |
b867d324 | 48 | unsigned int hash; |
9f36c9b7 NP |
49 | memcpy(&hash, sha1, sizeof(unsigned int)); |
50 | /* Assumes power-of-2 hash sizes in grow_object_hash */ | |
51 | return hash & (n - 1); | |
0556a11a LT |
52 | } |
53 | ||
54 | static void insert_obj_hash(struct object *obj, struct object **hash, unsigned int size) | |
55 | { | |
9f36c9b7 | 56 | unsigned int j = hash_obj(obj->sha1, size); |
0556a11a LT |
57 | |
58 | while (hash[j]) { | |
59 | j++; | |
60 | if (j >= size) | |
61 | j = 0; | |
62 | } | |
63 | hash[j] = obj; | |
64 | } | |
65 | ||
0556a11a | 66 | struct object *lookup_object(const unsigned char *sha1) |
175785e5 | 67 | { |
9a414486 | 68 | unsigned int i, first; |
0556a11a | 69 | struct object *obj; |
175785e5 | 70 | |
0556a11a LT |
71 | if (!obj_hash) |
72 | return NULL; | |
175785e5 | 73 | |
9f36c9b7 | 74 | first = i = hash_obj(sha1, obj_hash_size); |
0556a11a | 75 | while ((obj = obj_hash[i]) != NULL) { |
a89fccd2 | 76 | if (!hashcmp(sha1, obj->sha1)) |
0556a11a | 77 | break; |
070879ca | 78 | i++; |
0556a11a | 79 | if (i == obj_hash_size) |
070879ca JS |
80 | i = 0; |
81 | } | |
9a414486 JK |
82 | if (obj && i != first) { |
83 | /* | |
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. | |
87 | */ | |
88 | struct object *tmp = obj_hash[i]; | |
89 | obj_hash[i] = obj_hash[first]; | |
90 | obj_hash[first] = tmp; | |
91 | } | |
0556a11a | 92 | return obj; |
175785e5 DB |
93 | } |
94 | ||
0556a11a | 95 | static void grow_object_hash(void) |
175785e5 | 96 | { |
0556a11a | 97 | int i; |
9f36c9b7 NP |
98 | /* |
99 | * Note that this size must always be power-of-2 to match hash_obj | |
100 | * above. | |
101 | */ | |
0556a11a LT |
102 | int new_hash_size = obj_hash_size < 32 ? 32 : 2 * obj_hash_size; |
103 | struct object **new_hash; | |
104 | ||
b3c952f8 | 105 | new_hash = xcalloc(new_hash_size, sizeof(struct object *)); |
0556a11a LT |
106 | for (i = 0; i < obj_hash_size; i++) { |
107 | struct object *obj = obj_hash[i]; | |
108 | if (!obj) | |
109 | continue; | |
110 | insert_obj_hash(obj, new_hash, new_hash_size); | |
111 | } | |
112 | free(obj_hash); | |
113 | obj_hash = new_hash; | |
114 | obj_hash_size = new_hash_size; | |
175785e5 DB |
115 | } |
116 | ||
100c5f3b | 117 | void *create_object(const unsigned char *sha1, int type, void *o) |
175785e5 | 118 | { |
100c5f3b LT |
119 | struct object *obj = o; |
120 | ||
175785e5 | 121 | obj->parsed = 0; |
175785e5 | 122 | obj->used = 0; |
100c5f3b | 123 | obj->type = type; |
0556a11a | 124 | obj->flags = 0; |
e702496e | 125 | hashcpy(obj->sha1, sha1); |
175785e5 | 126 | |
0556a11a LT |
127 | if (obj_hash_size - 1 <= nr_objs * 2) |
128 | grow_object_hash(); | |
175785e5 | 129 | |
0556a11a | 130 | insert_obj_hash(obj, obj_hash, obj_hash_size); |
175785e5 | 131 | nr_objs++; |
100c5f3b | 132 | return obj; |
175785e5 DB |
133 | } |
134 | ||
66e481b0 DB |
135 | struct object *lookup_unknown_object(const unsigned char *sha1) |
136 | { | |
137 | struct object *obj = lookup_object(sha1); | |
100c5f3b LT |
138 | if (!obj) |
139 | obj = create_object(sha1, OBJ_NONE, alloc_object_node()); | |
66e481b0 DB |
140 | return obj; |
141 | } | |
142 | ||
21666f1a | 143 | struct object *parse_object_buffer(const unsigned char *sha1, enum object_type type, unsigned long size, void *buffer, int *eaten_p) |
9f613ddd JH |
144 | { |
145 | struct object *obj; | |
8e92e8f2 | 146 | *eaten_p = 0; |
9f613ddd | 147 | |
cc216827 | 148 | obj = NULL; |
21666f1a | 149 | if (type == OBJ_BLOB) { |
9f613ddd | 150 | struct blob *blob = lookup_blob(sha1); |
cc216827 | 151 | if (blob) { |
d0b8c9e5 MK |
152 | if (parse_blob_buffer(blob, buffer, size)) |
153 | return NULL; | |
cc216827 JM |
154 | obj = &blob->object; |
155 | } | |
21666f1a | 156 | } else if (type == OBJ_TREE) { |
9f613ddd | 157 | struct tree *tree = lookup_tree(sha1); |
cc216827 JM |
158 | if (tree) { |
159 | obj = &tree->object; | |
68be2fea JH |
160 | if (!tree->buffer) |
161 | tree->object.parsed = 0; | |
cc216827 | 162 | if (!tree->object.parsed) { |
d0b8c9e5 MK |
163 | if (parse_tree_buffer(tree, buffer, size)) |
164 | return NULL; | |
8e92e8f2 | 165 | *eaten_p = 1; |
cc216827 | 166 | } |
9f613ddd | 167 | } |
21666f1a | 168 | } else if (type == OBJ_COMMIT) { |
9f613ddd | 169 | struct commit *commit = lookup_commit(sha1); |
cc216827 | 170 | if (commit) { |
d0b8c9e5 MK |
171 | if (parse_commit_buffer(commit, buffer, size)) |
172 | return NULL; | |
cc216827 JM |
173 | if (!commit->buffer) { |
174 | commit->buffer = buffer; | |
8e92e8f2 | 175 | *eaten_p = 1; |
cc216827 JM |
176 | } |
177 | obj = &commit->object; | |
9f613ddd | 178 | } |
21666f1a | 179 | } else if (type == OBJ_TAG) { |
9f613ddd | 180 | struct tag *tag = lookup_tag(sha1); |
cc216827 | 181 | if (tag) { |
d0b8c9e5 MK |
182 | if (parse_tag_buffer(tag, buffer, size)) |
183 | return NULL; | |
cc216827 JM |
184 | obj = &tag->object; |
185 | } | |
9f613ddd | 186 | } else { |
82247e9b | 187 | warning("object %s has unknown type id %d", sha1_to_hex(sha1), type); |
9f613ddd JH |
188 | obj = NULL; |
189 | } | |
e2ac7cb5 SV |
190 | if (obj && obj->type == OBJ_NONE) |
191 | obj->type = type; | |
9f613ddd JH |
192 | return obj; |
193 | } | |
194 | ||
75a95490 JK |
195 | struct object *parse_object_or_die(const unsigned char *sha1, |
196 | const char *name) | |
197 | { | |
198 | struct object *o = parse_object(sha1); | |
199 | if (o) | |
200 | return o; | |
201 | ||
202 | die(_("unable to parse object: %s"), name ? name : sha1_to_hex(sha1)); | |
203 | } | |
204 | ||
5d6ccf5c | 205 | struct object *parse_object(const unsigned char *sha1) |
e9eefa67 | 206 | { |
c4584ae3 | 207 | unsigned long size; |
21666f1a | 208 | enum object_type type; |
9f613ddd | 209 | int eaten; |
4bbf5a26 | 210 | const unsigned char *repl = lookup_replace_object(sha1); |
ccdc6037 JK |
211 | void *buffer; |
212 | struct object *obj; | |
213 | ||
214 | obj = lookup_object(sha1); | |
215 | if (obj && obj->parsed) | |
216 | return obj; | |
9f613ddd | 217 | |
090ea126 NTND |
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) { | |
82247e9b | 222 | error("sha1 mismatch %s", sha1_to_hex(repl)); |
090ea126 NTND |
223 | return NULL; |
224 | } | |
225 | parse_blob_buffer(lookup_blob(sha1), NULL, 0); | |
226 | return lookup_object(sha1); | |
227 | } | |
228 | ||
ccdc6037 | 229 | buffer = read_sha1_file(sha1, &type, &size); |
c4584ae3 | 230 | if (buffer) { |
0e87c367 | 231 | if (check_sha1_signature(repl, buffer, size, typename(type)) < 0) { |
0b1f1130 | 232 | free(buffer); |
82247e9b | 233 | error("sha1 mismatch %s", sha1_to_hex(repl)); |
acdeec62 LT |
234 | return NULL; |
235 | } | |
9f613ddd | 236 | |
2e3400c0 | 237 | obj = parse_object_buffer(sha1, type, size, buffer, &eaten); |
9f613ddd JH |
238 | if (!eaten) |
239 | free(buffer); | |
bd2c39f5 | 240 | return obj; |
e9eefa67 DB |
241 | } |
242 | return NULL; | |
243 | } | |
66e481b0 DB |
244 | |
245 | struct object_list *object_list_insert(struct object *item, | |
246 | struct object_list **list_p) | |
247 | { | |
248 | struct object_list *new_list = xmalloc(sizeof(struct object_list)); | |
55b4e9e4 JH |
249 | new_list->item = item; |
250 | new_list->next = *list_p; | |
251 | *list_p = new_list; | |
252 | return new_list; | |
66e481b0 DB |
253 | } |
254 | ||
66e481b0 DB |
255 | int object_list_contains(struct object_list *list, struct object *obj) |
256 | { | |
257 | while (list) { | |
258 | if (list->item == obj) | |
259 | return 1; | |
260 | list = list->next; | |
261 | } | |
262 | return 0; | |
263 | } | |
1f1e895f | 264 | |
31faeb20 MH |
265 | /* |
266 | * A zero-length string to which object_array_entry::name can be | |
267 | * initialized without requiring a malloc/free. | |
268 | */ | |
269 | static char object_array_slopbuf[1]; | |
270 | ||
4197361e JH |
271 | static void add_object_array_with_mode_context(struct object *obj, const char *name, |
272 | struct object_array *array, | |
273 | unsigned mode, | |
274 | struct object_context *context) | |
1f1e895f LT |
275 | { |
276 | unsigned nr = array->nr; | |
277 | unsigned alloc = array->alloc; | |
278 | struct object_array_entry *objects = array->objects; | |
31faeb20 | 279 | struct object_array_entry *entry; |
1f1e895f LT |
280 | |
281 | if (nr >= alloc) { | |
282 | alloc = (alloc + 32) * 2; | |
283 | objects = xrealloc(objects, alloc * sizeof(*objects)); | |
284 | array->alloc = alloc; | |
285 | array->objects = objects; | |
286 | } | |
31faeb20 MH |
287 | entry = &objects[nr]; |
288 | entry->item = obj; | |
289 | if (!name) | |
290 | entry->name = NULL; | |
291 | else if (!*name) | |
292 | /* Use our own empty string instead of allocating one: */ | |
293 | entry->name = object_array_slopbuf; | |
294 | else | |
295 | entry->name = xstrdup(name); | |
296 | entry->mode = mode; | |
4197361e | 297 | entry->context = context; |
1f1e895f LT |
298 | array->nr = ++nr; |
299 | } | |
b2a6d1c6 | 300 | |
afa15f3c MG |
301 | void add_object_array(struct object *obj, const char *name, struct object_array *array) |
302 | { | |
303 | add_object_array_with_mode(obj, name, array, S_IFINVALID); | |
304 | } | |
305 | ||
306 | void add_object_array_with_mode(struct object *obj, const char *name, struct object_array *array, unsigned mode) | |
307 | { | |
308 | add_object_array_with_mode_context(obj, name, array, mode, NULL); | |
309 | } | |
310 | ||
311 | void add_object_array_with_context(struct object *obj, const char *name, struct object_array *array, struct object_context *context) | |
312 | { | |
313 | if (context) | |
314 | add_object_array_with_mode_context(obj, name, array, context->mode, context); | |
315 | else | |
316 | add_object_array_with_mode_context(obj, name, array, S_IFINVALID, context); | |
317 | } | |
318 | ||
aeb4a51e MH |
319 | void object_array_filter(struct object_array *array, |
320 | object_array_each_func_t want, void *cb_data) | |
b2a6d1c6 | 321 | { |
aeb4a51e | 322 | unsigned nr = array->nr, src, dst; |
b2a6d1c6 JH |
323 | struct object_array_entry *objects = array->objects; |
324 | ||
aeb4a51e MH |
325 | for (src = dst = 0; src < nr; src++) { |
326 | if (want(&objects[src], cb_data)) { | |
b2a6d1c6 JH |
327 | if (src != dst) |
328 | objects[dst] = objects[src]; | |
329 | dst++; | |
31faeb20 MH |
330 | } else { |
331 | if (objects[src].name != object_array_slopbuf) | |
332 | free(objects[src].name); | |
aeb4a51e MH |
333 | } |
334 | } | |
335 | array->nr = dst; | |
336 | } | |
337 | ||
1506510c MH |
338 | /* |
339 | * Return true iff array already contains an entry with name. | |
340 | */ | |
341 | static int contains_name(struct object_array *array, const char *name) | |
342 | { | |
343 | unsigned nr = array->nr, i; | |
344 | struct object_array_entry *object = array->objects; | |
345 | ||
346 | for (i = 0; i < nr; i++, object++) | |
347 | if (!strcmp(object->name, name)) | |
348 | return 1; | |
349 | return 0; | |
350 | } | |
351 | ||
b2a6d1c6 JH |
352 | void object_array_remove_duplicates(struct object_array *array) |
353 | { | |
1506510c | 354 | unsigned nr = array->nr, src; |
b2a6d1c6 JH |
355 | struct object_array_entry *objects = array->objects; |
356 | ||
1506510c MH |
357 | array->nr = 0; |
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]; | |
362 | array->nr++; | |
31faeb20 MH |
363 | } else { |
364 | if (objects[src].name != object_array_slopbuf) | |
365 | free(objects[src].name); | |
b2a6d1c6 | 366 | } |
b2a6d1c6 JH |
367 | } |
368 | } | |
bcc0a3ea HV |
369 | |
370 | void clear_object_flags(unsigned flags) | |
371 | { | |
372 | int i; | |
373 | ||
374 | for (i=0; i < obj_hash_size; i++) { | |
375 | struct object *obj = obj_hash[i]; | |
376 | if (obj) | |
377 | obj->flags &= ~flags; | |
378 | } | |
379 | } |