🌐 AI搜索 & 代理 主页
blob: 584f7acb36b68d17678b3fd6251105241794ea48 [file] [log] [blame]
Junio C Hamano8f1d2e62006-01-07 09:33:541#include "cache.h"
Daniel Barkalow175785e2005-04-18 18:39:482#include "object.h"
Daniel Barkalowe9eefa62005-04-28 14:46:333#include "blob.h"
4#include "tree.h"
5#include "commit.h"
Daniel Barkalowe9eefa62005-04-28 14:46:336#include "tag.h"
Daniel Barkalow175785e2005-04-18 18:39:487
Linus Torvalds0556a112006-06-30 18:20:338static struct object **obj_hash;
9static int nr_objs, obj_hash_size;
Linus Torvaldsfc046a72006-06-30 04:38:5510
11unsigned int get_max_object_index(void)
12{
Linus Torvalds0556a112006-06-30 18:20:3313 return obj_hash_size;
Linus Torvaldsfc046a72006-06-30 04:38:5514}
15
16struct object *get_indexed_object(unsigned int idx)
17{
Linus Torvalds0556a112006-06-30 18:20:3318 return obj_hash[idx];
Linus Torvaldsfc046a72006-06-30 04:38:5519}
Daniel Barkalow175785e2005-04-18 18:39:4820
Nicolas Pitredf843662007-02-26 19:55:5821static 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 */
Linus Torvalds885a86a2006-06-14 23:45:1327};
28
Nicolas Pitredf843662007-02-26 19:55:5829const 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
36int 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
Nicolas Pitre9f36c9b2013-09-10 22:17:1246static unsigned int hash_obj(const unsigned char *sha1, unsigned int n)
Linus Torvalds0556a112006-06-30 18:20:3347{
Dan McGeeb867d322009-05-12 01:17:3848 unsigned int hash;
Nicolas Pitre9f36c9b2013-09-10 22:17:1249 memcpy(&hash, sha1, sizeof(unsigned int));
50 /* Assumes power-of-2 hash sizes in grow_object_hash */
51 return hash & (n - 1);
Linus Torvalds0556a112006-06-30 18:20:3352}
53
54static void insert_obj_hash(struct object *obj, struct object **hash, unsigned int size)
55{
Nicolas Pitre9f36c9b2013-09-10 22:17:1256 unsigned int j = hash_obj(obj->sha1, size);
Linus Torvalds0556a112006-06-30 18:20:3357
58 while (hash[j]) {
59 j++;
60 if (j >= size)
61 j = 0;
62 }
63 hash[j] = obj;
64}
65
Jason McMullan5d6ccf52005-06-03 15:05:3966struct object *lookup_object(const unsigned char *sha1)
Daniel Barkalow175785e2005-04-18 18:39:4867{
Jeff King9a414482013-05-01 20:34:5068 unsigned int i, first;
Linus Torvalds0556a112006-06-30 18:20:3369 struct object *obj;
70
71 if (!obj_hash)
72 return NULL;
73
Nicolas Pitre9f36c9b2013-09-10 22:17:1274 first = i = hash_obj(sha1, obj_hash_size);
Linus Torvalds0556a112006-06-30 18:20:3375 while ((obj = obj_hash[i]) != NULL) {
David Rientjesa89fccd2006-08-17 18:54:5776 if (!hashcmp(sha1, obj->sha1))
Linus Torvalds0556a112006-06-30 18:20:3377 break;
78 i++;
79 if (i == obj_hash_size)
80 i = 0;
81 }
Jeff King9a414482013-05-01 20:34:5082 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 }
Linus Torvalds0556a112006-06-30 18:20:3392 return obj;
93}
94
95static void grow_object_hash(void)
96{
97 int i;
Nicolas Pitre9f36c9b2013-09-10 22:17:1298 /*
99 * Note that this size must always be power-of-2 to match hash_obj
100 * above.
101 */
Linus Torvalds0556a112006-06-30 18:20:33102 int new_hash_size = obj_hash_size < 32 ? 32 : 2 * obj_hash_size;
103 struct object **new_hash;
104
Jonas Fonsecab3c952f2006-08-28 00:26:07105 new_hash = xcalloc(new_hash_size, sizeof(struct object *));
Linus Torvalds0556a112006-06-30 18:20:33106 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;
Daniel Barkalow175785e2005-04-18 18:39:48115}
116
Linus Torvalds100c5f32007-04-17 05:11:43117void *create_object(const unsigned char *sha1, int type, void *o)
Daniel Barkalow175785e2005-04-18 18:39:48118{
Linus Torvalds100c5f32007-04-17 05:11:43119 struct object *obj = o;
120
Daniel Barkalow175785e2005-04-18 18:39:48121 obj->parsed = 0;
Daniel Barkalow175785e2005-04-18 18:39:48122 obj->used = 0;
Linus Torvalds100c5f32007-04-17 05:11:43123 obj->type = type;
Linus Torvalds0556a112006-06-30 18:20:33124 obj->flags = 0;
Shawn Pearcee7024962006-08-23 06:49:00125 hashcpy(obj->sha1, sha1);
Daniel Barkalow175785e2005-04-18 18:39:48126
Linus Torvalds0556a112006-06-30 18:20:33127 if (obj_hash_size - 1 <= nr_objs * 2)
128 grow_object_hash();
Johannes Schindelin070879c2006-02-12 01:57:57129
Linus Torvalds0556a112006-06-30 18:20:33130 insert_obj_hash(obj, obj_hash, obj_hash_size);
Daniel Barkalow175785e2005-04-18 18:39:48131 nr_objs++;
Linus Torvalds100c5f32007-04-17 05:11:43132 return obj;
Daniel Barkalow175785e2005-04-18 18:39:48133}
134
barkalow@iabervon.org66e481b2005-08-02 23:45:48135struct object *lookup_unknown_object(const unsigned char *sha1)
136{
137 struct object *obj = lookup_object(sha1);
Linus Torvalds100c5f32007-04-17 05:11:43138 if (!obj)
139 obj = create_object(sha1, OBJ_NONE, alloc_object_node());
barkalow@iabervon.org66e481b2005-08-02 23:45:48140 return obj;
141}
142
Nicolas Pitre21666f12007-02-26 19:55:59143struct object *parse_object_buffer(const unsigned char *sha1, enum object_type type, unsigned long size, void *buffer, int *eaten_p)
Junio C Hamano9f613dd2006-09-15 20:30:02144{
145 struct object *obj;
Stefan Beller8e92e8f2013-07-17 22:09:42146 *eaten_p = 0;
Junio C Hamano9f613dd2006-09-15 20:30:02147
Jim Meyeringcc216822007-12-21 10:56:32148 obj = NULL;
Nicolas Pitre21666f12007-02-26 19:55:59149 if (type == OBJ_BLOB) {
Junio C Hamano9f613dd2006-09-15 20:30:02150 struct blob *blob = lookup_blob(sha1);
Jim Meyeringcc216822007-12-21 10:56:32151 if (blob) {
Martin Koeglerd0b8c9e2008-02-03 21:22:39152 if (parse_blob_buffer(blob, buffer, size))
153 return NULL;
Jim Meyeringcc216822007-12-21 10:56:32154 obj = &blob->object;
155 }
Nicolas Pitre21666f12007-02-26 19:55:59156 } else if (type == OBJ_TREE) {
Junio C Hamano9f613dd2006-09-15 20:30:02157 struct tree *tree = lookup_tree(sha1);
Jim Meyeringcc216822007-12-21 10:56:32158 if (tree) {
159 obj = &tree->object;
Junio C Hamano68be2fe2011-11-17 06:04:13160 if (!tree->buffer)
161 tree->object.parsed = 0;
Jim Meyeringcc216822007-12-21 10:56:32162 if (!tree->object.parsed) {
Martin Koeglerd0b8c9e2008-02-03 21:22:39163 if (parse_tree_buffer(tree, buffer, size))
164 return NULL;
Stefan Beller8e92e8f2013-07-17 22:09:42165 *eaten_p = 1;
Jim Meyeringcc216822007-12-21 10:56:32166 }
Junio C Hamano9f613dd2006-09-15 20:30:02167 }
Nicolas Pitre21666f12007-02-26 19:55:59168 } else if (type == OBJ_COMMIT) {
Junio C Hamano9f613dd2006-09-15 20:30:02169 struct commit *commit = lookup_commit(sha1);
Jim Meyeringcc216822007-12-21 10:56:32170 if (commit) {
Martin Koeglerd0b8c9e2008-02-03 21:22:39171 if (parse_commit_buffer(commit, buffer, size))
172 return NULL;
Jim Meyeringcc216822007-12-21 10:56:32173 if (!commit->buffer) {
174 commit->buffer = buffer;
Stefan Beller8e92e8f2013-07-17 22:09:42175 *eaten_p = 1;
Jim Meyeringcc216822007-12-21 10:56:32176 }
177 obj = &commit->object;
Junio C Hamano9f613dd2006-09-15 20:30:02178 }
Nicolas Pitre21666f12007-02-26 19:55:59179 } else if (type == OBJ_TAG) {
Junio C Hamano9f613dd2006-09-15 20:30:02180 struct tag *tag = lookup_tag(sha1);
Jim Meyeringcc216822007-12-21 10:56:32181 if (tag) {
Martin Koeglerd0b8c9e2008-02-03 21:22:39182 if (parse_tag_buffer(tag, buffer, size))
183 return NULL;
Jim Meyeringcc216822007-12-21 10:56:32184 obj = &tag->object;
185 }
Junio C Hamano9f613dd2006-09-15 20:30:02186 } else {
Pete Wyckoff82247e92012-04-30 00:28:45187 warning("object %s has unknown type id %d", sha1_to_hex(sha1), type);
Junio C Hamano9f613dd2006-09-15 20:30:02188 obj = NULL;
189 }
Sam Vilaine2ac7cb2007-06-06 10:25:17190 if (obj && obj->type == OBJ_NONE)
191 obj->type = type;
Junio C Hamano9f613dd2006-09-15 20:30:02192 return obj;
193}
194
Jeff King75a95492013-03-17 08:22:36195struct 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
Jason McMullan5d6ccf52005-06-03 15:05:39205struct object *parse_object(const unsigned char *sha1)
Daniel Barkalowe9eefa62005-04-28 14:46:33206{
Junio C Hamanoc4584ae2005-06-27 10:33:33207 unsigned long size;
Nicolas Pitre21666f12007-02-26 19:55:59208 enum object_type type;
Junio C Hamano9f613dd2006-09-15 20:30:02209 int eaten;
Junio C Hamano4bbf5a22011-05-15 19:54:52210 const unsigned char *repl = lookup_replace_object(sha1);
Jeff Kingccdc6032012-01-05 21:00:01211 void *buffer;
212 struct object *obj;
Junio C Hamano9f613dd2006-09-15 20:30:02213
Jeff Kingccdc6032012-01-05 21:00:01214 obj = lookup_object(sha1);
215 if (obj && obj->parsed)
216 return obj;
217
Nguyễn Thái Ngọc Duy090ea122012-03-07 10:54:18218 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) {
Pete Wyckoff82247e92012-04-30 00:28:45222 error("sha1 mismatch %s", sha1_to_hex(repl));
Nguyễn Thái Ngọc Duy090ea122012-03-07 10:54:18223 return NULL;
224 }
225 parse_blob_buffer(lookup_blob(sha1), NULL, 0);
226 return lookup_object(sha1);
227 }
228
Jeff Kingccdc6032012-01-05 21:00:01229 buffer = read_sha1_file(sha1, &type, &size);
Junio C Hamanoc4584ae2005-06-27 10:33:33230 if (buffer) {
Christian Couder0e87c362009-01-23 09:07:10231 if (check_sha1_signature(repl, buffer, size, typename(type)) < 0) {
Carlos Rica0b1f1132007-05-25 01:46:22232 free(buffer);
Pete Wyckoff82247e92012-04-30 00:28:45233 error("sha1 mismatch %s", sha1_to_hex(repl));
Linus Torvaldsacdeec62007-03-20 17:05:20234 return NULL;
235 }
Junio C Hamano9f613dd2006-09-15 20:30:02236
Nguyễn Thái Ngọc Duy2e3400c2010-09-03 20:51:53237 obj = parse_object_buffer(sha1, type, size, buffer, &eaten);
Junio C Hamano9f613dd2006-09-15 20:30:02238 if (!eaten)
239 free(buffer);
Nicolas Pitrebd2c39f2005-05-06 17:48:34240 return obj;
Daniel Barkalowe9eefa62005-04-28 14:46:33241 }
242 return NULL;
243}
barkalow@iabervon.org66e481b2005-08-02 23:45:48244
245struct 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));
Jared Hance55b4e9e2010-09-05 19:36:33249 new_list->item = item;
250 new_list->next = *list_p;
251 *list_p = new_list;
252 return new_list;
barkalow@iabervon.org66e481b2005-08-02 23:45:48253}
254
barkalow@iabervon.org66e481b2005-08-02 23:45:48255int 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}
Linus Torvalds1f1e8952006-06-20 00:42:35264
Michael Haggerty31faeb22013-05-25 09:08:14265/*
266 * A zero-length string to which object_array_entry::name can be
267 * initialized without requiring a malloc/free.
268 */
269static char object_array_slopbuf[1];
270
Junio C Hamano41973612013-10-23 20:21:30271static 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)
Martin Koeglere5709a42007-04-22 16:43:58275{
Linus Torvalds1f1e8952006-06-20 00:42:35276 unsigned nr = array->nr;
277 unsigned alloc = array->alloc;
278 struct object_array_entry *objects = array->objects;
Michael Haggerty31faeb22013-05-25 09:08:14279 struct object_array_entry *entry;
Linus Torvalds1f1e8952006-06-20 00:42:35280
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 }
Michael Haggerty31faeb22013-05-25 09:08:14287 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;
Junio C Hamano41973612013-10-23 20:21:30297 entry->context = context;
Linus Torvalds1f1e8952006-06-20 00:42:35298 array->nr = ++nr;
299}
Junio C Hamanob2a6d1c2009-01-18 06:27:08300
Michael J Gruberafa15f32013-05-10 15:10:16301void 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
306void 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
311void 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
Michael Haggertyaeb4a512013-05-25 09:08:08319void object_array_filter(struct object_array *array,
320 object_array_each_func_t want, void *cb_data)
321{
322 unsigned nr = array->nr, src, dst;
323 struct object_array_entry *objects = array->objects;
324
325 for (src = dst = 0; src < nr; src++) {
326 if (want(&objects[src], cb_data)) {
327 if (src != dst)
328 objects[dst] = objects[src];
329 dst++;
Michael Haggerty31faeb22013-05-25 09:08:14330 } else {
331 if (objects[src].name != object_array_slopbuf)
332 free(objects[src].name);
Michael Haggertyaeb4a512013-05-25 09:08:08333 }
334 }
335 array->nr = dst;
336}
337
Michael Haggerty15065102013-05-25 09:08:10338/*
339 * Return true iff array already contains an entry with name.
340 */
341static 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
Junio C Hamanob2a6d1c2009-01-18 06:27:08352void object_array_remove_duplicates(struct object_array *array)
353{
Michael Haggerty15065102013-05-25 09:08:10354 unsigned nr = array->nr, src;
Junio C Hamanob2a6d1c2009-01-18 06:27:08355 struct object_array_entry *objects = array->objects;
356
Michael Haggerty15065102013-05-25 09:08:10357 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++;
Michael Haggerty31faeb22013-05-25 09:08:14363 } else {
364 if (objects[src].name != object_array_slopbuf)
365 free(objects[src].name);
Junio C Hamanob2a6d1c2009-01-18 06:27:08366 }
Junio C Hamanob2a6d1c2009-01-18 06:27:08367 }
368}
Heiko Voigtbcc0a3e2012-03-29 07:21:21369
370void 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}