blob: 49a864ce54e6ca0f21ad86aab27a422570d1bcec [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{
Dan McGeeb867d322009-05-11 20:17:38 -050048 unsigned int hash;
49 memcpy(&hash, obj->sha1, sizeof(unsigned int));
Linus Torvalds0556a112006-06-30 11:20:33 -070050 return hash % n;
51}
52
53static void insert_obj_hash(struct object *obj, struct object **hash, unsigned int size)
54{
Dan McGee91fe2f92009-05-18 23:34:02 -050055 unsigned int j = hash_obj(obj, size);
Linus Torvalds0556a112006-06-30 11:20:33 -070056
57 while (hash[j]) {
58 j++;
59 if (j >= size)
60 j = 0;
61 }
62 hash[j] = obj;
63}
64
Dan McGee91fe2f92009-05-18 23:34:02 -050065static unsigned int hashtable_index(const unsigned char *sha1)
Johannes Schindelin070879c2006-02-12 02:57:57 +010066{
Junio C Hamano2b796362006-02-11 18:51:19 -080067 unsigned int i;
68 memcpy(&i, sha1, sizeof(unsigned int));
Dan McGee91fe2f92009-05-18 23:34:02 -050069 return i % obj_hash_size;
Daniel Barkalow175785e2005-04-18 11:39:48 -070070}
71
Jason McMullan5d6ccf52005-06-03 11:05:39 -040072struct object *lookup_object(const unsigned char *sha1)
Daniel Barkalow175785e2005-04-18 11:39:48 -070073{
Dan McGee91fe2f92009-05-18 23:34:02 -050074 unsigned int i;
Linus Torvalds0556a112006-06-30 11:20:33 -070075 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 11:54:57 -070082 if (!hashcmp(sha1, obj->sha1))
Linus Torvalds0556a112006-06-30 11:20:33 -070083 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 02:26:07 +020097 new_hash = xcalloc(new_hash_size, sizeof(struct object *));
Linus Torvalds0556a112006-06-30 11:20:33 -070098 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 11:39:48 -0700107}
108
Linus Torvalds100c5f32007-04-16 22:11:43 -0700109void *create_object(const unsigned char *sha1, int type, void *o)
Daniel Barkalow175785e2005-04-18 11:39:48 -0700110{
Linus Torvalds100c5f32007-04-16 22:11:43 -0700111 struct object *obj = o;
112
Daniel Barkalow175785e2005-04-18 11:39:48 -0700113 obj->parsed = 0;
Daniel Barkalow175785e2005-04-18 11:39:48 -0700114 obj->used = 0;
Linus Torvalds100c5f32007-04-16 22:11:43 -0700115 obj->type = type;
Linus Torvalds0556a112006-06-30 11:20:33 -0700116 obj->flags = 0;
Shawn Pearcee7024962006-08-23 02:49:00 -0400117 hashcpy(obj->sha1, sha1);
Daniel Barkalow175785e2005-04-18 11:39:48 -0700118
Linus Torvalds0556a112006-06-30 11:20:33 -0700119 if (obj_hash_size - 1 <= nr_objs * 2)
120 grow_object_hash();
Johannes Schindelin070879c2006-02-12 02:57:57 +0100121
Linus Torvalds0556a112006-06-30 11:20:33 -0700122 insert_obj_hash(obj, obj_hash, obj_hash_size);
Daniel Barkalow175785e2005-04-18 11:39:48 -0700123 nr_objs++;
Linus Torvalds100c5f32007-04-16 22:11:43 -0700124 return obj;
Daniel Barkalow175785e2005-04-18 11:39:48 -0700125}
126
barkalow@iabervon.org66e481b2005-08-02 19:45:48 -0400127struct object *lookup_unknown_object(const unsigned char *sha1)
128{
129 struct object *obj = lookup_object(sha1);
Linus Torvalds100c5f32007-04-16 22:11:43 -0700130 if (!obj)
131 obj = create_object(sha1, OBJ_NONE, alloc_object_node());
barkalow@iabervon.org66e481b2005-08-02 19:45:48 -0400132 return obj;
133}
134
Nicolas Pitre21666f12007-02-26 14:55:59 -0500135struct 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 -0700136{
137 struct object *obj;
138 int eaten = 0;
139
Jim Meyeringcc216822007-12-21 11:56:32 +0100140 obj = NULL;
Nicolas Pitre21666f12007-02-26 14:55:59 -0500141 if (type == OBJ_BLOB) {
Junio C Hamano9f613dd2006-09-15 13:30:02 -0700142 struct blob *blob = lookup_blob(sha1);
Jim Meyeringcc216822007-12-21 11:56:32 +0100143 if (blob) {
Martin Koeglerd0b8c9e2008-02-03 22:22:39 +0100144 if (parse_blob_buffer(blob, buffer, size))
145 return NULL;
Jim Meyeringcc216822007-12-21 11:56:32 +0100146 obj = &blob->object;
147 }
Nicolas Pitre21666f12007-02-26 14:55:59 -0500148 } else if (type == OBJ_TREE) {
Junio C Hamano9f613dd2006-09-15 13:30:02 -0700149 struct tree *tree = lookup_tree(sha1);
Jim Meyeringcc216822007-12-21 11:56:32 +0100150 if (tree) {
151 obj = &tree->object;
Junio C Hamano68be2fe2011-11-16 22:04:13 -0800152 if (!tree->buffer)
153 tree->object.parsed = 0;
Jim Meyeringcc216822007-12-21 11:56:32 +0100154 if (!tree->object.parsed) {
Martin Koeglerd0b8c9e2008-02-03 22:22:39 +0100155 if (parse_tree_buffer(tree, buffer, size))
156 return NULL;
Jim Meyeringcc216822007-12-21 11:56:32 +0100157 eaten = 1;
158 }
Junio C Hamano9f613dd2006-09-15 13:30:02 -0700159 }
Nicolas Pitre21666f12007-02-26 14:55:59 -0500160 } else if (type == OBJ_COMMIT) {
Junio C Hamano9f613dd2006-09-15 13:30:02 -0700161 struct commit *commit = lookup_commit(sha1);
Jim Meyeringcc216822007-12-21 11:56:32 +0100162 if (commit) {
Martin Koeglerd0b8c9e2008-02-03 22:22:39 +0100163 if (parse_commit_buffer(commit, buffer, size))
164 return NULL;
Jim Meyeringcc216822007-12-21 11:56:32 +0100165 if (!commit->buffer) {
166 commit->buffer = buffer;
167 eaten = 1;
168 }
169 obj = &commit->object;
Junio C Hamano9f613dd2006-09-15 13:30:02 -0700170 }
Nicolas Pitre21666f12007-02-26 14:55:59 -0500171 } else if (type == OBJ_TAG) {
Junio C Hamano9f613dd2006-09-15 13:30:02 -0700172 struct tag *tag = lookup_tag(sha1);
Jim Meyeringcc216822007-12-21 11:56:32 +0100173 if (tag) {
Martin Koeglerd0b8c9e2008-02-03 22:22:39 +0100174 if (parse_tag_buffer(tag, buffer, size))
175 return NULL;
Jim Meyeringcc216822007-12-21 11:56:32 +0100176 obj = &tag->object;
177 }
Junio C Hamano9f613dd2006-09-15 13:30:02 -0700178 } else {
Sam Vilaine2ac7cb2007-06-06 22:25:17 +1200179 warning("object %s has unknown type id %d\n", sha1_to_hex(sha1), type);
Junio C Hamano9f613dd2006-09-15 13:30:02 -0700180 obj = NULL;
181 }
Sam Vilaine2ac7cb2007-06-06 22:25:17 +1200182 if (obj && obj->type == OBJ_NONE)
183 obj->type = type;
Junio C Hamano9f613dd2006-09-15 13:30:02 -0700184 *eaten_p = eaten;
185 return obj;
186}
187
Jason McMullan5d6ccf52005-06-03 11:05:39 -0400188struct object *parse_object(const unsigned char *sha1)
Daniel Barkalowe9eefa62005-04-28 07:46:33 -0700189{
Junio C Hamanoc4584ae2005-06-27 03:33:33 -0700190 unsigned long size;
Nicolas Pitre21666f12007-02-26 14:55:59 -0500191 enum object_type type;
Junio C Hamano9f613dd2006-09-15 13:30:02 -0700192 int eaten;
Junio C Hamano4bbf5a22011-05-15 12:54:52 -0700193 const unsigned char *repl = lookup_replace_object(sha1);
Jeff Kingccdc6032012-01-05 16:00:01 -0500194 void *buffer;
195 struct object *obj;
Junio C Hamano9f613dd2006-09-15 13:30:02 -0700196
Jeff Kingccdc6032012-01-05 16:00:01 -0500197 obj = lookup_object(sha1);
198 if (obj && obj->parsed)
199 return obj;
200
Nguyễn Thái Ngọc Duy090ea122012-03-07 17:54:18 +0700201 if ((obj && obj->type == OBJ_BLOB) ||
202 (!obj && has_sha1_file(sha1) &&
203 sha1_object_info(sha1, NULL) == OBJ_BLOB)) {
204 if (check_sha1_signature(repl, NULL, 0, NULL) < 0) {
205 error("sha1 mismatch %s\n", sha1_to_hex(repl));
206 return NULL;
207 }
208 parse_blob_buffer(lookup_blob(sha1), NULL, 0);
209 return lookup_object(sha1);
210 }
211
Jeff Kingccdc6032012-01-05 16:00:01 -0500212 buffer = read_sha1_file(sha1, &type, &size);
Junio C Hamanoc4584ae2005-06-27 03:33:33 -0700213 if (buffer) {
Christian Couder0e87c362009-01-23 10:07:10 +0100214 if (check_sha1_signature(repl, buffer, size, typename(type)) < 0) {
Carlos Rica0b1f1132007-05-25 03:46:22 +0200215 free(buffer);
Christian Couder0e87c362009-01-23 10:07:10 +0100216 error("sha1 mismatch %s\n", sha1_to_hex(repl));
Linus Torvaldsacdeec62007-03-20 10:05:20 -0700217 return NULL;
218 }
Junio C Hamano9f613dd2006-09-15 13:30:02 -0700219
Nguyễn Thái Ngọc Duy2e3400c2010-09-03 22:51:53 +0200220 obj = parse_object_buffer(sha1, type, size, buffer, &eaten);
Junio C Hamano9f613dd2006-09-15 13:30:02 -0700221 if (!eaten)
222 free(buffer);
Nicolas Pitrebd2c39f2005-05-06 13:48:34 -0400223 return obj;
Daniel Barkalowe9eefa62005-04-28 07:46:33 -0700224 }
225 return NULL;
226}
barkalow@iabervon.org66e481b2005-08-02 19:45:48 -0400227
228struct object_list *object_list_insert(struct object *item,
229 struct object_list **list_p)
230{
231 struct object_list *new_list = xmalloc(sizeof(struct object_list));
Jared Hance55b4e9e2010-09-05 15:36:33 -0400232 new_list->item = item;
233 new_list->next = *list_p;
234 *list_p = new_list;
235 return new_list;
barkalow@iabervon.org66e481b2005-08-02 19:45:48 -0400236}
237
barkalow@iabervon.org66e481b2005-08-02 19:45:48 -0400238int object_list_contains(struct object_list *list, struct object *obj)
239{
240 while (list) {
241 if (list->item == obj)
242 return 1;
243 list = list->next;
244 }
245 return 0;
246}
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700247
248void add_object_array(struct object *obj, const char *name, struct object_array *array)
249{
Martin Koeglere5709a42007-04-22 18:43:58 +0200250 add_object_array_with_mode(obj, name, array, S_IFINVALID);
251}
252
253void add_object_array_with_mode(struct object *obj, const char *name, struct object_array *array, unsigned mode)
254{
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700255 unsigned nr = array->nr;
256 unsigned alloc = array->alloc;
257 struct object_array_entry *objects = array->objects;
258
259 if (nr >= alloc) {
260 alloc = (alloc + 32) * 2;
261 objects = xrealloc(objects, alloc * sizeof(*objects));
262 array->alloc = alloc;
263 array->objects = objects;
264 }
265 objects[nr].item = obj;
266 objects[nr].name = name;
Martin Koeglere5709a42007-04-22 18:43:58 +0200267 objects[nr].mode = mode;
Linus Torvalds1f1e8952006-06-19 17:42:35 -0700268 array->nr = ++nr;
269}
Junio C Hamanob2a6d1c2009-01-17 22:27:08 -0800270
271void object_array_remove_duplicates(struct object_array *array)
272{
Jonathan Nieder97a20ee2010-04-19 03:03:40 -0500273 unsigned int ref, src, dst;
Junio C Hamanob2a6d1c2009-01-17 22:27:08 -0800274 struct object_array_entry *objects = array->objects;
275
Jonathan Nieder97a20ee2010-04-19 03:03:40 -0500276 for (ref = 0; ref + 1 < array->nr; ref++) {
Junio C Hamanob2a6d1c2009-01-17 22:27:08 -0800277 for (src = ref + 1, dst = src;
278 src < array->nr;
279 src++) {
280 if (!strcmp(objects[ref].name, objects[src].name))
281 continue;
282 if (src != dst)
283 objects[dst] = objects[src];
284 dst++;
285 }
286 array->nr = dst;
287 }
288}
Heiko Voigtbcc0a3e2012-03-29 09:21:21 +0200289
290void clear_object_flags(unsigned flags)
291{
292 int i;
293
294 for (i=0; i < obj_hash_size; i++) {
295 struct object *obj = obj_hash[i];
296 if (obj)
297 obj->flags &= ~flags;
298 }
299}