🌐 AI搜索 & 代理 主页
blob: 23d6c9671904cfe273fc16de8a4d2db56b91c3b4 [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
Johannes Schindelinfe8e3b72014-09-10 13:52:4436int type_from_string_gently(const char *str, ssize_t len, int gentle)
Nicolas Pitredf843662007-02-26 19:55:5837{
38 int i;
39
Johannes Schindelinfe8e3b72014-09-10 13:52:4440 if (len < 0)
41 len = strlen(str);
42
Nicolas Pitredf843662007-02-26 19:55:5843 for (i = 1; i < ARRAY_SIZE(object_type_strings); i++)
Johannes Schindelinfe8e3b72014-09-10 13:52:4444 if (!strncmp(str, object_type_strings[i], len))
Nicolas Pitredf843662007-02-26 19:55:5845 return i;
Johannes Schindelinfe8e3b72014-09-10 13:52:4446
47 if (gentle)
48 return -1;
49
Nicolas Pitredf843662007-02-26 19:55:5850 die("invalid object type \"%s\"", str);
51}
52
Michael Haggerty33bef7e2014-02-28 16:29:1753/*
54 * Return a numerical hash value between 0 and n-1 for the object with
55 * the specified sha1. n must be a power of 2. Please note that the
56 * return value is *not* consistent across computer architectures.
57 */
Nicolas Pitre9f36c9b2013-09-10 22:17:1258static unsigned int hash_obj(const unsigned char *sha1, unsigned int n)
Linus Torvalds0556a112006-06-30 18:20:3359{
Karsten Blees039dc712014-07-02 22:20:2060 return sha1hash(sha1) & (n - 1);
Linus Torvalds0556a112006-06-30 18:20:3361}
62
Michael Haggerty33bef7e2014-02-28 16:29:1763/*
64 * Insert obj into the hash table hash, which has length size (which
65 * must be a power of 2). On collisions, simply overflow to the next
66 * empty bucket.
67 */
Linus Torvalds0556a112006-06-30 18:20:3368static void insert_obj_hash(struct object *obj, struct object **hash, unsigned int size)
69{
Nicolas Pitre9f36c9b2013-09-10 22:17:1270 unsigned int j = hash_obj(obj->sha1, size);
Linus Torvalds0556a112006-06-30 18:20:3371
72 while (hash[j]) {
73 j++;
74 if (j >= size)
75 j = 0;
76 }
77 hash[j] = obj;
78}
79
Michael Haggerty33bef7e2014-02-28 16:29:1780/*
81 * Look up the record for the given sha1 in the hash map stored in
82 * obj_hash. Return NULL if it was not found.
83 */
Jason McMullan5d6ccf52005-06-03 15:05:3984struct object *lookup_object(const unsigned char *sha1)
Daniel Barkalow175785e2005-04-18 18:39:4885{
Jeff King9a414482013-05-01 20:34:5086 unsigned int i, first;
Linus Torvalds0556a112006-06-30 18:20:3387 struct object *obj;
88
89 if (!obj_hash)
90 return NULL;
91
Nicolas Pitre9f36c9b2013-09-10 22:17:1292 first = i = hash_obj(sha1, obj_hash_size);
Linus Torvalds0556a112006-06-30 18:20:3393 while ((obj = obj_hash[i]) != NULL) {
David Rientjesa89fccd2006-08-17 18:54:5794 if (!hashcmp(sha1, obj->sha1))
Linus Torvalds0556a112006-06-30 18:20:3395 break;
96 i++;
97 if (i == obj_hash_size)
98 i = 0;
99 }
Jeff King9a414482013-05-01 20:34:50100 if (obj && i != first) {
101 /*
102 * Move object to where we started to look for it so
103 * that we do not need to walk the hash table the next
104 * time we look for it.
105 */
106 struct object *tmp = obj_hash[i];
107 obj_hash[i] = obj_hash[first];
108 obj_hash[first] = tmp;
109 }
Linus Torvalds0556a112006-06-30 18:20:33110 return obj;
111}
112
Michael Haggerty33bef7e2014-02-28 16:29:17113/*
114 * Increase the size of the hash map stored in obj_hash to the next
115 * power of 2 (but at least 32). Copy the existing values to the new
116 * hash map.
117 */
Linus Torvalds0556a112006-06-30 18:20:33118static void grow_object_hash(void)
119{
120 int i;
Nicolas Pitre9f36c9b2013-09-10 22:17:12121 /*
122 * Note that this size must always be power-of-2 to match hash_obj
123 * above.
124 */
Linus Torvalds0556a112006-06-30 18:20:33125 int new_hash_size = obj_hash_size < 32 ? 32 : 2 * obj_hash_size;
126 struct object **new_hash;
127
Jonas Fonsecab3c952f2006-08-28 00:26:07128 new_hash = xcalloc(new_hash_size, sizeof(struct object *));
Linus Torvalds0556a112006-06-30 18:20:33129 for (i = 0; i < obj_hash_size; i++) {
130 struct object *obj = obj_hash[i];
131 if (!obj)
132 continue;
133 insert_obj_hash(obj, new_hash, new_hash_size);
134 }
135 free(obj_hash);
136 obj_hash = new_hash;
137 obj_hash_size = new_hash_size;
Daniel Barkalow175785e2005-04-18 18:39:48138}
139
Jeff Kingd36f51c2014-07-13 06:41:55140void *create_object(const unsigned char *sha1, void *o)
Daniel Barkalow175785e2005-04-18 18:39:48141{
Linus Torvalds100c5f32007-04-17 05:11:43142 struct object *obj = o;
143
Daniel Barkalow175785e2005-04-18 18:39:48144 obj->parsed = 0;
Daniel Barkalow175785e2005-04-18 18:39:48145 obj->used = 0;
Linus Torvalds0556a112006-06-30 18:20:33146 obj->flags = 0;
Shawn Pearcee7024962006-08-23 06:49:00147 hashcpy(obj->sha1, sha1);
Daniel Barkalow175785e2005-04-18 18:39:48148
Linus Torvalds0556a112006-06-30 18:20:33149 if (obj_hash_size - 1 <= nr_objs * 2)
150 grow_object_hash();
Johannes Schindelin070879c2006-02-12 01:57:57151
Linus Torvalds0556a112006-06-30 18:20:33152 insert_obj_hash(obj, obj_hash, obj_hash_size);
Daniel Barkalow175785e2005-04-18 18:39:48153 nr_objs++;
Linus Torvalds100c5f32007-04-17 05:11:43154 return obj;
Daniel Barkalow175785e2005-04-18 18:39:48155}
156
Jeff King8ff226a2014-07-13 06:42:03157void *object_as_type(struct object *obj, enum object_type type, int quiet)
158{
159 if (obj->type == type)
160 return obj;
161 else if (obj->type == OBJ_NONE) {
Jeff Kingd66bebc2014-07-13 06:42:12162 if (type == OBJ_COMMIT)
163 ((struct commit *)obj)->index = alloc_commit_index();
Jeff King8ff226a2014-07-13 06:42:03164 obj->type = type;
165 return obj;
166 }
167 else {
168 if (!quiet)
169 error("object %s is a %s, not a %s",
170 sha1_to_hex(obj->sha1),
171 typename(obj->type), typename(type));
172 return NULL;
173 }
174}
175
barkalow@iabervon.org66e481b2005-08-02 23:45:48176struct object *lookup_unknown_object(const unsigned char *sha1)
177{
178 struct object *obj = lookup_object(sha1);
Linus Torvalds100c5f32007-04-17 05:11:43179 if (!obj)
Jeff Kingd36f51c2014-07-13 06:41:55180 obj = create_object(sha1, alloc_object_node());
barkalow@iabervon.org66e481b2005-08-02 23:45:48181 return obj;
182}
183
Nicolas Pitre21666f12007-02-26 19:55:59184struct 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:02185{
186 struct object *obj;
Stefan Beller8e92e8f2013-07-17 22:09:42187 *eaten_p = 0;
Junio C Hamano9f613dd2006-09-15 20:30:02188
Jim Meyeringcc216822007-12-21 10:56:32189 obj = NULL;
Nicolas Pitre21666f12007-02-26 19:55:59190 if (type == OBJ_BLOB) {
Junio C Hamano9f613dd2006-09-15 20:30:02191 struct blob *blob = lookup_blob(sha1);
Jim Meyeringcc216822007-12-21 10:56:32192 if (blob) {
Martin Koeglerd0b8c9e2008-02-03 21:22:39193 if (parse_blob_buffer(blob, buffer, size))
194 return NULL;
Jim Meyeringcc216822007-12-21 10:56:32195 obj = &blob->object;
196 }
Nicolas Pitre21666f12007-02-26 19:55:59197 } else if (type == OBJ_TREE) {
Junio C Hamano9f613dd2006-09-15 20:30:02198 struct tree *tree = lookup_tree(sha1);
Jim Meyeringcc216822007-12-21 10:56:32199 if (tree) {
200 obj = &tree->object;
Junio C Hamano68be2fe2011-11-17 06:04:13201 if (!tree->buffer)
202 tree->object.parsed = 0;
Jim Meyeringcc216822007-12-21 10:56:32203 if (!tree->object.parsed) {
Martin Koeglerd0b8c9e2008-02-03 21:22:39204 if (parse_tree_buffer(tree, buffer, size))
205 return NULL;
Stefan Beller8e92e8f2013-07-17 22:09:42206 *eaten_p = 1;
Jim Meyeringcc216822007-12-21 10:56:32207 }
Junio C Hamano9f613dd2006-09-15 20:30:02208 }
Nicolas Pitre21666f12007-02-26 19:55:59209 } else if (type == OBJ_COMMIT) {
Junio C Hamano9f613dd2006-09-15 20:30:02210 struct commit *commit = lookup_commit(sha1);
Jim Meyeringcc216822007-12-21 10:56:32211 if (commit) {
Martin Koeglerd0b8c9e2008-02-03 21:22:39212 if (parse_commit_buffer(commit, buffer, size))
213 return NULL;
Jeff King8597ea32014-06-10 21:44:13214 if (!get_cached_commit_buffer(commit, NULL)) {
215 set_commit_buffer(commit, buffer, size);
Stefan Beller8e92e8f2013-07-17 22:09:42216 *eaten_p = 1;
Jim Meyeringcc216822007-12-21 10:56:32217 }
218 obj = &commit->object;
Junio C Hamano9f613dd2006-09-15 20:30:02219 }
Nicolas Pitre21666f12007-02-26 19:55:59220 } else if (type == OBJ_TAG) {
Junio C Hamano9f613dd2006-09-15 20:30:02221 struct tag *tag = lookup_tag(sha1);
Jim Meyeringcc216822007-12-21 10:56:32222 if (tag) {
Martin Koeglerd0b8c9e2008-02-03 21:22:39223 if (parse_tag_buffer(tag, buffer, size))
224 return NULL;
Jim Meyeringcc216822007-12-21 10:56:32225 obj = &tag->object;
226 }
Junio C Hamano9f613dd2006-09-15 20:30:02227 } else {
Pete Wyckoff82247e92012-04-30 00:28:45228 warning("object %s has unknown type id %d", sha1_to_hex(sha1), type);
Junio C Hamano9f613dd2006-09-15 20:30:02229 obj = NULL;
230 }
Junio C Hamano9f613dd2006-09-15 20:30:02231 return obj;
232}
233
Jeff King75a95492013-03-17 08:22:36234struct object *parse_object_or_die(const unsigned char *sha1,
235 const char *name)
236{
237 struct object *o = parse_object(sha1);
238 if (o)
239 return o;
240
241 die(_("unable to parse object: %s"), name ? name : sha1_to_hex(sha1));
242}
243
Jason McMullan5d6ccf52005-06-03 15:05:39244struct object *parse_object(const unsigned char *sha1)
Daniel Barkalowe9eefa62005-04-28 14:46:33245{
Junio C Hamanoc4584ae2005-06-27 10:33:33246 unsigned long size;
Nicolas Pitre21666f12007-02-26 19:55:59247 enum object_type type;
Junio C Hamano9f613dd2006-09-15 20:30:02248 int eaten;
Junio C Hamano4bbf5a22011-05-15 19:54:52249 const unsigned char *repl = lookup_replace_object(sha1);
Jeff Kingccdc6032012-01-05 21:00:01250 void *buffer;
251 struct object *obj;
Junio C Hamano9f613dd2006-09-15 20:30:02252
Jeff Kingccdc6032012-01-05 21:00:01253 obj = lookup_object(sha1);
254 if (obj && obj->parsed)
255 return obj;
256
Nguyễn Thái Ngọc Duy090ea122012-03-07 10:54:18257 if ((obj && obj->type == OBJ_BLOB) ||
258 (!obj && has_sha1_file(sha1) &&
259 sha1_object_info(sha1, NULL) == OBJ_BLOB)) {
260 if (check_sha1_signature(repl, NULL, 0, NULL) < 0) {
Pete Wyckoff82247e92012-04-30 00:28:45261 error("sha1 mismatch %s", sha1_to_hex(repl));
Nguyễn Thái Ngọc Duy090ea122012-03-07 10:54:18262 return NULL;
263 }
264 parse_blob_buffer(lookup_blob(sha1), NULL, 0);
265 return lookup_object(sha1);
266 }
267
Jeff Kingccdc6032012-01-05 21:00:01268 buffer = read_sha1_file(sha1, &type, &size);
Junio C Hamanoc4584ae2005-06-27 10:33:33269 if (buffer) {
Christian Couder0e87c362009-01-23 09:07:10270 if (check_sha1_signature(repl, buffer, size, typename(type)) < 0) {
Carlos Rica0b1f1132007-05-25 01:46:22271 free(buffer);
Pete Wyckoff82247e92012-04-30 00:28:45272 error("sha1 mismatch %s", sha1_to_hex(repl));
Linus Torvaldsacdeec62007-03-20 17:05:20273 return NULL;
274 }
Junio C Hamano9f613dd2006-09-15 20:30:02275
Nguyễn Thái Ngọc Duy2e3400c2010-09-03 20:51:53276 obj = parse_object_buffer(sha1, type, size, buffer, &eaten);
Junio C Hamano9f613dd2006-09-15 20:30:02277 if (!eaten)
278 free(buffer);
Nicolas Pitrebd2c39f2005-05-06 17:48:34279 return obj;
Daniel Barkalowe9eefa62005-04-28 14:46:33280 }
281 return NULL;
282}
barkalow@iabervon.org66e481b2005-08-02 23:45:48283
284struct object_list *object_list_insert(struct object *item,
285 struct object_list **list_p)
286{
287 struct object_list *new_list = xmalloc(sizeof(struct object_list));
Jared Hance55b4e9e2010-09-05 19:36:33288 new_list->item = item;
289 new_list->next = *list_p;
290 *list_p = new_list;
291 return new_list;
barkalow@iabervon.org66e481b2005-08-02 23:45:48292}
293
barkalow@iabervon.org66e481b2005-08-02 23:45:48294int object_list_contains(struct object_list *list, struct object *obj)
295{
296 while (list) {
297 if (list->item == obj)
298 return 1;
299 list = list->next;
300 }
301 return 0;
302}
Linus Torvalds1f1e8952006-06-20 00:42:35303
Michael Haggerty31faeb22013-05-25 09:08:14304/*
305 * A zero-length string to which object_array_entry::name can be
306 * initialized without requiring a malloc/free.
307 */
308static char object_array_slopbuf[1];
309
Jeff King9e0c3c42014-10-15 22:42:57310void add_object_array_with_path(struct object *obj, const char *name,
311 struct object_array *array,
312 unsigned mode, const char *path)
Martin Koeglere5709a42007-04-22 16:43:58313{
Linus Torvalds1f1e8952006-06-20 00:42:35314 unsigned nr = array->nr;
315 unsigned alloc = array->alloc;
316 struct object_array_entry *objects = array->objects;
Michael Haggerty31faeb22013-05-25 09:08:14317 struct object_array_entry *entry;
Linus Torvalds1f1e8952006-06-20 00:42:35318
319 if (nr >= alloc) {
320 alloc = (alloc + 32) * 2;
René Scharfe2756ca42014-09-16 18:56:57321 REALLOC_ARRAY(objects, alloc);
Linus Torvalds1f1e8952006-06-20 00:42:35322 array->alloc = alloc;
323 array->objects = objects;
324 }
Michael Haggerty31faeb22013-05-25 09:08:14325 entry = &objects[nr];
326 entry->item = obj;
327 if (!name)
328 entry->name = NULL;
329 else if (!*name)
330 /* Use our own empty string instead of allocating one: */
331 entry->name = object_array_slopbuf;
332 else
333 entry->name = xstrdup(name);
334 entry->mode = mode;
Jeff King9e0c3c42014-10-15 22:42:57335 if (path)
336 entry->path = xstrdup(path);
337 else
338 entry->path = NULL;
Linus Torvalds1f1e8952006-06-20 00:42:35339 array->nr = ++nr;
340}
Junio C Hamanob2a6d1c2009-01-18 06:27:08341
Michael J Gruberafa15f32013-05-10 15:10:16342void add_object_array(struct object *obj, const char *name, struct object_array *array)
343{
Jeff King189a1222014-10-19 02:03:19344 add_object_array_with_path(obj, name, array, S_IFINVALID, NULL);
Michael J Gruberafa15f32013-05-10 15:10:16345}
346
Jeff King68f49232014-10-15 22:34:19347/*
348 * Free all memory associated with an entry; the result is
349 * in an unspecified state and should not be examined.
350 */
351static void object_array_release_entry(struct object_array_entry *ent)
352{
353 if (ent->name != object_array_slopbuf)
354 free(ent->name);
Jeff King9e0c3c42014-10-15 22:42:57355 free(ent->path);
Jeff King68f49232014-10-15 22:34:19356}
357
Michael Haggertyaeb4a512013-05-25 09:08:08358void object_array_filter(struct object_array *array,
359 object_array_each_func_t want, void *cb_data)
360{
361 unsigned nr = array->nr, src, dst;
362 struct object_array_entry *objects = array->objects;
363
364 for (src = dst = 0; src < nr; src++) {
365 if (want(&objects[src], cb_data)) {
366 if (src != dst)
367 objects[dst] = objects[src];
368 dst++;
Michael Haggerty31faeb22013-05-25 09:08:14369 } else {
Jeff King68f49232014-10-15 22:34:19370 object_array_release_entry(&objects[src]);
Michael Haggertyaeb4a512013-05-25 09:08:08371 }
372 }
373 array->nr = dst;
374}
375
Jeff King46be8232014-10-15 22:34:34376void object_array_clear(struct object_array *array)
377{
378 int i;
379 for (i = 0; i < array->nr; i++)
380 object_array_release_entry(&array->objects[i]);
381 free(array->objects);
382 array->objects = NULL;
383 array->nr = array->alloc = 0;
384}
385
Michael Haggerty15065102013-05-25 09:08:10386/*
387 * Return true iff array already contains an entry with name.
388 */
389static int contains_name(struct object_array *array, const char *name)
390{
391 unsigned nr = array->nr, i;
392 struct object_array_entry *object = array->objects;
393
394 for (i = 0; i < nr; i++, object++)
395 if (!strcmp(object->name, name))
396 return 1;
397 return 0;
398}
399
Junio C Hamanob2a6d1c2009-01-18 06:27:08400void object_array_remove_duplicates(struct object_array *array)
401{
Michael Haggerty15065102013-05-25 09:08:10402 unsigned nr = array->nr, src;
Junio C Hamanob2a6d1c2009-01-18 06:27:08403 struct object_array_entry *objects = array->objects;
404
Michael Haggerty15065102013-05-25 09:08:10405 array->nr = 0;
406 for (src = 0; src < nr; src++) {
407 if (!contains_name(array, objects[src].name)) {
408 if (src != array->nr)
409 objects[array->nr] = objects[src];
410 array->nr++;
Michael Haggerty31faeb22013-05-25 09:08:14411 } else {
Jeff King68f49232014-10-15 22:34:19412 object_array_release_entry(&objects[src]);
Junio C Hamanob2a6d1c2009-01-18 06:27:08413 }
Junio C Hamanob2a6d1c2009-01-18 06:27:08414 }
415}
Heiko Voigtbcc0a3e2012-03-29 07:21:21416
417void clear_object_flags(unsigned flags)
418{
419 int i;
420
421 for (i=0; i < obj_hash_size; i++) {
422 struct object *obj = obj_hash[i];
423 if (obj)
424 obj->flags &= ~flags;
425 }
426}