🌐 AI搜索 & 代理 主页
blob: 3ca92c4c4def46af10556dbe9b3f48774b9a4a35 [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
Linus Torvalds0556a112006-06-30 18:20:3346static unsigned int hash_obj(struct object *obj, unsigned int n)
47{
Dan McGeeb867d322009-05-12 01:17:3848 unsigned int hash;
49 memcpy(&hash, obj->sha1, sizeof(unsigned int));
Linus Torvalds0556a112006-06-30 18:20:3350 return hash % n;
51}
52
53static void insert_obj_hash(struct object *obj, struct object **hash, unsigned int size)
54{
Dan McGee91fe2f92009-05-19 04:34:0255 unsigned int j = hash_obj(obj, size);
Linus Torvalds0556a112006-06-30 18:20:3356
57 while (hash[j]) {
58 j++;
59 if (j >= size)
60 j = 0;
61 }
62 hash[j] = obj;
63}
64
Dan McGee91fe2f92009-05-19 04:34:0265static unsigned int hashtable_index(const unsigned char *sha1)
Johannes Schindelin070879c2006-02-12 01:57:5766{
Junio C Hamano2b796362006-02-12 02:51:1967 unsigned int i;
68 memcpy(&i, sha1, sizeof(unsigned int));
Dan McGee91fe2f92009-05-19 04:34:0269 return i % obj_hash_size;
Daniel Barkalow175785e2005-04-18 18:39:4870}
71
Jason McMullan5d6ccf52005-06-03 15:05:3972struct object *lookup_object(const unsigned char *sha1)
Daniel Barkalow175785e2005-04-18 18:39:4873{
Dan McGee91fe2f92009-05-19 04:34:0274 unsigned int i;
Linus Torvalds0556a112006-06-30 18:20:3375 struct object *obj;
76
77 if (!obj_hash)
78 return NULL;
79
80 i = hashtable_index(sha1);
81 while ((obj = obj_hash[i]) != NULL) {
David Rientjesa89fccd2006-08-17 18:54:5782 if (!hashcmp(sha1, obj->sha1))
Linus Torvalds0556a112006-06-30 18:20:3383 break;
84 i++;
85 if (i == obj_hash_size)
86 i = 0;
87 }
88 return obj;
89}
90
91static void grow_object_hash(void)
92{
93 int i;
94 int new_hash_size = obj_hash_size < 32 ? 32 : 2 * obj_hash_size;
95 struct object **new_hash;
96
Jonas Fonsecab3c952f2006-08-28 00:26:0797 new_hash = xcalloc(new_hash_size, sizeof(struct object *));
Linus Torvalds0556a112006-06-30 18:20:3398 for (i = 0; i < obj_hash_size; i++) {
99 struct object *obj = obj_hash[i];
100 if (!obj)
101 continue;
102 insert_obj_hash(obj, new_hash, new_hash_size);
103 }
104 free(obj_hash);
105 obj_hash = new_hash;
106 obj_hash_size = new_hash_size;
Daniel Barkalow175785e2005-04-18 18:39:48107}
108
Linus Torvalds100c5f32007-04-17 05:11:43109void *create_object(const unsigned char *sha1, int type, void *o)
Daniel Barkalow175785e2005-04-18 18:39:48110{
Linus Torvalds100c5f32007-04-17 05:11:43111 struct object *obj = o;
112
Daniel Barkalow175785e2005-04-18 18:39:48113 obj->parsed = 0;
Daniel Barkalow175785e2005-04-18 18:39:48114 obj->used = 0;
Linus Torvalds100c5f32007-04-17 05:11:43115 obj->type = type;
Linus Torvalds0556a112006-06-30 18:20:33116 obj->flags = 0;
Shawn Pearcee7024962006-08-23 06:49:00117 hashcpy(obj->sha1, sha1);
Daniel Barkalow175785e2005-04-18 18:39:48118
Linus Torvalds0556a112006-06-30 18:20:33119 if (obj_hash_size - 1 <= nr_objs * 2)
120 grow_object_hash();
Johannes Schindelin070879c2006-02-12 01:57:57121
Linus Torvalds0556a112006-06-30 18:20:33122 insert_obj_hash(obj, obj_hash, obj_hash_size);
Daniel Barkalow175785e2005-04-18 18:39:48123 nr_objs++;
Linus Torvalds100c5f32007-04-17 05:11:43124 return obj;
Daniel Barkalow175785e2005-04-18 18:39:48125}
126
barkalow@iabervon.org66e481b2005-08-02 23:45:48127struct object *lookup_unknown_object(const unsigned char *sha1)
128{
129 struct object *obj = lookup_object(sha1);
Linus Torvalds100c5f32007-04-17 05:11:43130 if (!obj)
131 obj = create_object(sha1, OBJ_NONE, alloc_object_node());
barkalow@iabervon.org66e481b2005-08-02 23:45:48132 return obj;
133}
134
Nicolas Pitre21666f12007-02-26 19:55:59135struct 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:02136{
137 struct object *obj;
138 int eaten = 0;
139
Jim Meyeringcc216822007-12-21 10:56:32140 obj = NULL;
Nicolas Pitre21666f12007-02-26 19:55:59141 if (type == OBJ_BLOB) {
Junio C Hamano9f613dd2006-09-15 20:30:02142 struct blob *blob = lookup_blob(sha1);
Jim Meyeringcc216822007-12-21 10:56:32143 if (blob) {
Martin Koeglerd0b8c9e2008-02-03 21:22:39144 if (parse_blob_buffer(blob, buffer, size))
145 return NULL;
Jim Meyeringcc216822007-12-21 10:56:32146 obj = &blob->object;
147 }
Nicolas Pitre21666f12007-02-26 19:55:59148 } else if (type == OBJ_TREE) {
Junio C Hamano9f613dd2006-09-15 20:30:02149 struct tree *tree = lookup_tree(sha1);
Jim Meyeringcc216822007-12-21 10:56:32150 if (tree) {
151 obj = &tree->object;
152 if (!tree->object.parsed) {
Martin Koeglerd0b8c9e2008-02-03 21:22:39153 if (parse_tree_buffer(tree, buffer, size))
154 return NULL;
Jim Meyeringcc216822007-12-21 10:56:32155 eaten = 1;
156 }
Junio C Hamano9f613dd2006-09-15 20:30:02157 }
Nicolas Pitre21666f12007-02-26 19:55:59158 } else if (type == OBJ_COMMIT) {
Junio C Hamano9f613dd2006-09-15 20:30:02159 struct commit *commit = lookup_commit(sha1);
Jim Meyeringcc216822007-12-21 10:56:32160 if (commit) {
Martin Koeglerd0b8c9e2008-02-03 21:22:39161 if (parse_commit_buffer(commit, buffer, size))
162 return NULL;
Jim Meyeringcc216822007-12-21 10:56:32163 if (!commit->buffer) {
164 commit->buffer = buffer;
165 eaten = 1;
166 }
167 obj = &commit->object;
Junio C Hamano9f613dd2006-09-15 20:30:02168 }
Nicolas Pitre21666f12007-02-26 19:55:59169 } else if (type == OBJ_TAG) {
Junio C Hamano9f613dd2006-09-15 20:30:02170 struct tag *tag = lookup_tag(sha1);
Jim Meyeringcc216822007-12-21 10:56:32171 if (tag) {
Martin Koeglerd0b8c9e2008-02-03 21:22:39172 if (parse_tag_buffer(tag, buffer, size))
173 return NULL;
Jim Meyeringcc216822007-12-21 10:56:32174 obj = &tag->object;
175 }
Junio C Hamano9f613dd2006-09-15 20:30:02176 } else {
Sam Vilaine2ac7cb2007-06-06 10:25:17177 warning("object %s has unknown type id %d\n", sha1_to_hex(sha1), type);
Junio C Hamano9f613dd2006-09-15 20:30:02178 obj = NULL;
179 }
Sam Vilaine2ac7cb2007-06-06 10:25:17180 if (obj && obj->type == OBJ_NONE)
181 obj->type = type;
Junio C Hamano9f613dd2006-09-15 20:30:02182 *eaten_p = eaten;
183 return obj;
184}
185
Jason McMullan5d6ccf52005-06-03 15:05:39186struct object *parse_object(const unsigned char *sha1)
Daniel Barkalowe9eefa62005-04-28 14:46:33187{
Junio C Hamanoc4584ae2005-06-27 10:33:33188 unsigned long size;
Nicolas Pitre21666f12007-02-26 19:55:59189 enum object_type type;
Junio C Hamano9f613dd2006-09-15 20:30:02190 int eaten;
Christian Couder0e87c362009-01-23 09:07:10191 const unsigned char *repl;
192 void *buffer = read_sha1_file_repl(sha1, &type, &size, &repl);
Junio C Hamano9f613dd2006-09-15 20:30:02193
Junio C Hamanoc4584ae2005-06-27 10:33:33194 if (buffer) {
Nicolas Pitrebd2c39f2005-05-06 17:48:34195 struct object *obj;
Christian Couder0e87c362009-01-23 09:07:10196 if (check_sha1_signature(repl, buffer, size, typename(type)) < 0) {
Carlos Rica0b1f1132007-05-25 01:46:22197 free(buffer);
Christian Couder0e87c362009-01-23 09:07:10198 error("sha1 mismatch %s\n", sha1_to_hex(repl));
Linus Torvaldsacdeec62007-03-20 17:05:20199 return NULL;
200 }
Junio C Hamano9f613dd2006-09-15 20:30:02201
Christian Couder0e87c362009-01-23 09:07:10202 obj = parse_object_buffer(repl, type, size, buffer, &eaten);
Junio C Hamano9f613dd2006-09-15 20:30:02203 if (!eaten)
204 free(buffer);
Nicolas Pitrebd2c39f2005-05-06 17:48:34205 return obj;
Daniel Barkalowe9eefa62005-04-28 14:46:33206 }
207 return NULL;
208}
barkalow@iabervon.org66e481b2005-08-02 23:45:48209
210struct object_list *object_list_insert(struct object *item,
211 struct object_list **list_p)
212{
213 struct object_list *new_list = xmalloc(sizeof(struct object_list));
214 new_list->item = item;
215 new_list->next = *list_p;
216 *list_p = new_list;
217 return new_list;
218}
219
barkalow@iabervon.org66e481b2005-08-02 23:45:48220int object_list_contains(struct object_list *list, struct object *obj)
221{
222 while (list) {
223 if (list->item == obj)
224 return 1;
225 list = list->next;
226 }
227 return 0;
228}
Linus Torvalds1f1e8952006-06-20 00:42:35229
230void add_object_array(struct object *obj, const char *name, struct object_array *array)
231{
Martin Koeglere5709a42007-04-22 16:43:58232 add_object_array_with_mode(obj, name, array, S_IFINVALID);
233}
234
235void add_object_array_with_mode(struct object *obj, const char *name, struct object_array *array, unsigned mode)
236{
Linus Torvalds1f1e8952006-06-20 00:42:35237 unsigned nr = array->nr;
238 unsigned alloc = array->alloc;
239 struct object_array_entry *objects = array->objects;
240
241 if (nr >= alloc) {
242 alloc = (alloc + 32) * 2;
243 objects = xrealloc(objects, alloc * sizeof(*objects));
244 array->alloc = alloc;
245 array->objects = objects;
246 }
247 objects[nr].item = obj;
248 objects[nr].name = name;
Martin Koeglere5709a42007-04-22 16:43:58249 objects[nr].mode = mode;
Linus Torvalds1f1e8952006-06-20 00:42:35250 array->nr = ++nr;
251}
Junio C Hamanob2a6d1c2009-01-18 06:27:08252
253void object_array_remove_duplicates(struct object_array *array)
254{
255 int ref, src, dst;
256 struct object_array_entry *objects = array->objects;
257
258 for (ref = 0; ref < array->nr - 1; ref++) {
259 for (src = ref + 1, dst = src;
260 src < array->nr;
261 src++) {
262 if (!strcmp(objects[ref].name, objects[src].name))
263 continue;
264 if (src != dst)
265 objects[dst] = objects[src];
266 dst++;
267 }
268 array->nr = dst;
269 }
270}