blob: 50b6528001fe4bafdfe70126dc2078860c3d1969 [file] [log] [blame]
Junio C Hamano8f1d2e62006-01-07 01:33:54 -08001#include "cache.h"
Daniel Barkalow175785e2005-04-18 11:39:48 -07002#include "object.h"
Daniel Barkalowe9eefa62005-04-28 07:46:33 -07003#include "blob.h"
4#include "tree.h"
5#include "commit.h"
Daniel Barkalowe9eefa62005-04-28 07:46:33 -07006#include "tag.h"
Daniel Barkalow175785e2005-04-18 11:39:48 -07007
Linus Torvalds0556a112006-06-30 11:20:33 -07008static struct object **obj_hash;
9static int nr_objs, obj_hash_size;
Linus Torvaldsfc046a72006-06-29 21:38:55 -070010
11unsigned int get_max_object_index(void)
12{
Linus Torvalds0556a112006-06-30 11:20:33 -070013 return obj_hash_size;
Linus Torvaldsfc046a72006-06-29 21:38:55 -070014}
15
16struct object *get_indexed_object(unsigned int idx)
17{
Linus Torvalds0556a112006-06-30 11:20:33 -070018 return obj_hash[idx];
Linus Torvaldsfc046a72006-06-29 21:38:55 -070019}
Daniel Barkalow175785e2005-04-18 11:39:48 -070020
Nicolas Pitredf843662007-02-26 14:55:58 -050021static 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 16:45:13 -070027};
28
Nicolas Pitredf843662007-02-26 14:55:58 -050029const 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 11:20:33 -070046static unsigned int hash_obj(struct object *obj, unsigned int n)
47{
48 unsigned int hash = *(unsigned int *)obj->sha1;
49 return hash % n;
50}
51
52static void insert_obj_hash(struct object *obj, struct object **hash, unsigned int size)
53{
54 int j = hash_obj(obj, size);
55
56 while (hash[j]) {
57 j++;
58 if (j >= size)
59 j = 0;
60 }
61 hash[j] = obj;
62}
63
Johannes Schindelin070879c2006-02-12 02:57:57 +010064static int hashtable_index(const unsigned char *sha1)
65{
Junio C Hamano2b796362006-02-11 18:51:19 -080066 unsigned int i;
67 memcpy(&i, sha1, sizeof(unsigned int));
Linus Torvalds0556a112006-06-30 11:20:33 -070068 return (int)(i % obj_hash_size);
Daniel Barkalow175785e2005-04-18 11:39:48 -070069}
70
Jason McMullan5d6ccf52005-06-03 11:05:39 -040071struct object *lookup_object(const unsigned char *sha1)
Daniel Barkalow175785e2005-04-18 11:39:48 -070072{
Linus Torvalds0556a112006-06-30 11:20:33 -070073 int i;
74 struct object *obj;
75
76 if (!obj_hash)
77 return NULL;
78
79 i = hashtable_index(sha1);
80 while ((obj = obj_hash[i]) != NULL) {
David Rientjesa89fccd2006-08-17 11:54:57 -070081 if (!hashcmp(sha1, obj->sha1))
Linus Torvalds0556a112006-06-30 11:20:33 -070082 break;
83 i++;
84 if (i == obj_hash_size)
85 i = 0;
86 }
87 return obj;
88}
89
90static void grow_object_hash(void)
91{
92 int i;
93 int new_hash_size = obj_hash_size < 32 ? 32 : 2 * obj_hash_size;
94 struct object **new_hash;
95
Jonas Fonsecab3c952f2006-08-28 02:26:07 +020096 new_hash = xcalloc(new_hash_size, sizeof(struct object *));
Linus Torvalds0556a112006-06-30 11:20:33 -070097 for (i = 0; i < obj_hash_size; i++) {
98 struct object *obj = obj_hash[i];
99 if (!obj)
100 continue;
101 insert_obj_hash(obj, new_hash, new_hash_size);
102 }
103 free(obj_hash);
104 obj_hash = new_hash;
105 obj_hash_size = new_hash_size;
Daniel Barkalow175785e2005-04-18 11:39:48 -0700106}
107
Linus Torvalds100c5f32007-04-16 22:11:43 -0700108void *create_object(const unsigned char *sha1, int type, void *o)
Daniel Barkalow175785e2005-04-18 11:39:48 -0700109{
Linus Torvalds100c5f32007-04-16 22:11:43 -0700110 struct object *obj = o;
111
Daniel Barkalow175785e2005-04-18 11:39:48 -0700112 obj->parsed = 0;
Daniel Barkalow175785e2005-04-18 11:39:48 -0700113 obj->used = 0;
Linus Torvalds100c5f32007-04-16 22:11:43 -0700114 obj->type = type;
Linus Torvalds0556a112006-06-30 11:20:33 -0700115 obj->flags = 0;
Shawn Pearcee7024962006-08-23 02:49:00 -0400116 hashcpy(obj->sha1, sha1);
Daniel Barkalow175785e2005-04-18 11:39:48 -0700117
Linus Torvalds0556a112006-06-30 11:20:33 -0700118 if (obj_hash_size - 1 <= nr_objs * 2)
119 grow_object_hash();
Johannes Schindelin070879c2006-02-12 02:57:57 +0100120
Linus Torvalds0556a112006-06-30 11:20:33 -0700121 insert_obj_hash(obj, obj_hash, obj_hash_size);
Daniel Barkalow175785e2005-04-18 11:39:48 -0700122 nr_objs++;
Linus Torvalds100c5f32007-04-16 22:11:43 -0700123 return obj;
Daniel Barkalow175785e2005-04-18 11:39:48 -0700124}
125
barkalow@iabervon.org66e481b2005-08-02 19:45:48 -0400126struct object *lookup_unknown_object(const unsigned char *sha1)
127{
128 struct object *obj = lookup_object(sha1);
Linus Torvalds100c5f32007-04-16 22:11:43 -0700129 if (!obj)
130 obj = create_object(sha1, OBJ_NONE, alloc_object_node());
barkalow@iabervon.org66e481b2005-08-02 19:45:48 -0400131 return obj;
132}
133
Nicolas Pitre21666f12007-02-26 14:55:59 -0500134struct 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 13:30:02 -0700135{
136 struct object *obj;
137 int eaten = 0;
138
Jim Meyeringcc216822007-12-21 11:56:32 +0100139 obj = NULL;
Nicolas Pitre21666f12007-02-26 14:55:59 -0500140 if (type == OBJ_BLOB) {
Junio C Hamano9f613dd2006-09-15 13:30:02 -0700141 struct blob *blob = lookup_blob(sha1);
Jim Meyeringcc216822007-12-21 11:56:32 +0100142 if (blob) {
Martin Koeglerd0b8c9e2008-02-03 22:22:39 +0100143 if (parse_blob_buffer(blob, buffer, size))
144 return NULL;
Jim Meyeringcc216822007-12-21 11:56:32 +0100145 obj = &blob->object;
146 }
Nicolas Pitre21666f12007-02-26 14:55:59 -0500147 } else if (type == OBJ_TREE) {
Junio C Hamano9f613dd2006-09-15 13:30:02 -0700148 struct tree *tree = lookup_tree(sha1);
Jim Meyeringcc216822007-12-21 11:56:32 +0100149 if (tree) {
150 obj = &tree->object;
151 if (!tree->object.parsed) {
Martin Koeglerd0b8c9e2008-02-03 22:22:39 +0100152 if (parse_tree_buffer(tree, buffer, size))
153 return NULL;
Jim Meyeringcc216822007-12-21 11:56:32 +0100154 eaten = 1;
155 }
Junio C Hamano9f613dd2006-09-15 13:30:02 -0700156 }
Nicolas Pitre21666f12007-02-26 14:55:59 -0500157 } else if (type == OBJ_COMMIT) {
Junio C Hamano9f613dd2006-09-15 13:30:02 -0700158 struct commit *commit = lookup_commit(sha1);
Jim Meyeringcc216822007-12-21 11:56:32 +0100159 if (commit) {
Martin Koeglerd0b8c9e2008-02-03 22:22:39 +0100160 if (parse_commit_buffer(commit, buffer, size))
161 return NULL;
Jim Meyeringcc216822007-12-21 11:56:32 +0100162 if (!commit->buffer) {
163 commit->buffer = buffer;
164 eaten = 1;
165 }
166 obj = &commit->object;
Junio C Hamano9f613dd2006-09-15 13:30:02 -0700167 }
Nicolas Pitre21666f12007-02-26 14:55:59 -0500168 } else if (type == OBJ_TAG) {
Junio C Hamano9f613dd2006-09-15 13:30:02 -0700169 struct tag *tag = lookup_tag(sha1);
Jim Meyeringcc216822007-12-21 11:56:32 +0100170 if (tag) {
Martin Koeglerd0b8c9e2008-02-03 22:22:39 +0100171 if (parse_tag_buffer(tag, buffer, size))
172 return NULL;
Jim Meyeringcc216822007-12-21 11:56:32 +0100173 obj = &tag->object;
174 }
Junio C Hamano9f613dd2006-09-15 13:30:02 -0700175 } else {
Sam Vilaine2ac7cb2007-06-06 22:25:17 +1200176 warning("object %s has unknown type id %d\n", sha1_to_hex(sha1), type);
Junio C Hamano9f613dd2006-09-15 13:30:02 -0700177 obj = NULL;
178 }
Sam Vilaine2ac7cb2007-06-06 22:25:17 +1200179 if (obj && obj->type == OBJ_NONE)
180 obj->type = type;
Junio C Hamano9f613dd2006-09-15 13:30:02 -0700181 *eaten_p = eaten;
182 return obj;
183}
184
Jason McMullan5d6ccf52005-06-03 11:05:39 -0400185struct object *parse_object(const unsigned char *sha1)
Daniel Barkalowe9eefa62005-04-28 07:46:33 -0700186{
Junio C Hamanoc4584ae2005-06-27 03:33:33 -0700187 unsigned long size;
Nicolas Pitre21666f12007-02-26 14:55:59 -0500188 enum object_type type;
Junio C Hamano9f613dd2006-09-15 13:30:02 -0700189 int eaten;
Nicolas Pitre21666f12007-02-26 14:55:59 -0500190 void *buffer = read_sha1_file(sha1, &type, &size);
Junio C Hamano9f613dd2006-09-15 13:30:02 -0700191
Junio C Hamanoc4584ae2005-06-27 03:33:33 -0700192 if (buffer) {
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400193 struct object *obj;
Linus Torvaldsacdeec62007-03-20 10:05:20 -0700194 if (check_sha1_signature(sha1, buffer, size, typename(type)) < 0) {
Carlos Rica0b1f1132007-05-25 03:46:22 +0200195 free(buffer);
Linus Torvaldsacdeec62007-03-20 10:05:20 -0700196 error("sha1 mismatch %s\n", sha1_to_hex(sha1));
197 return NULL;
198 }
Junio C Hamano9f613dd2006-09-15 13:30:02 -0700199
200 obj = parse_object_buffer(sha1, type, size, buffer, &eaten);
201 if (!eaten)
202 free(buffer);
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400203 return obj;
Daniel Barkalowe9eefa62005-04-28 07:46:33 -0700204 }
205 return NULL;
206}
barkalow@iabervon.org66e481b2005-08-02 19:45:48 -0400207
208struct object_list *object_list_insert(struct object *item,
209 struct object_list **list_p)
210{
211 struct object_list *new_list = xmalloc(sizeof(struct object_list));
212 new_list->item = item;
213 new_list->next = *list_p;
214 *list_p = new_list;
215 return new_list;
216}
217
Daniel Barkalow680bab32005-09-05 02:04:18 -0400218void object_list_append(struct object *item,
219 struct object_list **list_p)
220{
221 while (*list_p) {
222 list_p = &((*list_p)->next);
223 }
224 *list_p = xmalloc(sizeof(struct object_list));
225 (*list_p)->next = NULL;
226 (*list_p)->item = item;
227}
228
barkalow@iabervon.org66e481b2005-08-02 19:45:48 -0400229unsigned object_list_length(struct object_list *list)
230{
231 unsigned ret = 0;
232 while (list) {
233 list = list->next;
234 ret++;
235 }
236 return ret;
237}
238
239int object_list_contains(struct object_list *list, struct object *obj)
240{
241 while (list) {
242 if (list->item == obj)
243 return 1;
244 list = list->next;
245 }
246 return 0;
247}
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700248
249void add_object_array(struct object *obj, const char *name, struct object_array *array)
250{
Martin Koeglere5709a42007-04-22 18:43:58 +0200251 add_object_array_with_mode(obj, name, array, S_IFINVALID);
252}
253
254void add_object_array_with_mode(struct object *obj, const char *name, struct object_array *array, unsigned mode)
255{
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700256 unsigned nr = array->nr;
257 unsigned alloc = array->alloc;
258 struct object_array_entry *objects = array->objects;
259
260 if (nr >= alloc) {
261 alloc = (alloc + 32) * 2;
262 objects = xrealloc(objects, alloc * sizeof(*objects));
263 array->alloc = alloc;
264 array->objects = objects;
265 }
266 objects[nr].item = obj;
267 objects[nr].name = name;
Martin Koeglere5709a42007-04-22 18:43:58 +0200268 objects[nr].mode = mode;
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700269 array->nr = ++nr;
270}