| Junio C Hamano | 8f1d2e6 | 2006-01-07 09:33:54 | [diff] [blame] | 1 | #include "cache.h" |
| Daniel Barkalow | 175785e | 2005-04-18 18:39:48 | [diff] [blame] | 2 | #include "object.h" |
| Daniel Barkalow | e9eefa6 | 2005-04-28 14:46:33 | [diff] [blame] | 3 | #include "blob.h" |
| 4 | #include "tree.h" |
| 5 | #include "commit.h" |
| Daniel Barkalow | e9eefa6 | 2005-04-28 14:46:33 | [diff] [blame] | 6 | #include "tag.h" |
| Daniel Barkalow | 175785e | 2005-04-18 18:39:48 | [diff] [blame] | 7 | |
| Linus Torvalds | 0556a11 | 2006-06-30 18:20:33 | [diff] [blame] | 8 | static struct object **obj_hash; |
| 9 | static int nr_objs, obj_hash_size; |
| Linus Torvalds | fc046a7 | 2006-06-30 04:38:55 | [diff] [blame] | 10 | |
| 11 | unsigned int get_max_object_index(void) |
| 12 | { |
| Linus Torvalds | 0556a11 | 2006-06-30 18:20:33 | [diff] [blame] | 13 | return obj_hash_size; |
| Linus Torvalds | fc046a7 | 2006-06-30 04:38:55 | [diff] [blame] | 14 | } |
| 15 | |
| 16 | struct object *get_indexed_object(unsigned int idx) |
| 17 | { |
| Linus Torvalds | 0556a11 | 2006-06-30 18:20:33 | [diff] [blame] | 18 | return obj_hash[idx]; |
| Linus Torvalds | fc046a7 | 2006-06-30 04:38:55 | [diff] [blame] | 19 | } |
| Daniel Barkalow | 175785e | 2005-04-18 18:39:48 | [diff] [blame] | 20 | |
| Nicolas Pitre | df84366 | 2007-02-26 19:55:58 | [diff] [blame] | 21 | static 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 Torvalds | 885a86a | 2006-06-14 23:45:13 | [diff] [blame] | 27 | }; |
| 28 | |
| Nicolas Pitre | df84366 | 2007-02-26 19:55:58 | [diff] [blame] | 29 | const 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 | |
| 36 | int 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 Torvalds | 0556a11 | 2006-06-30 18:20:33 | [diff] [blame] | 46 | static 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 | |
| 52 | static 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 Schindelin | 070879c | 2006-02-12 01:57:57 | [diff] [blame] | 64 | static int hashtable_index(const unsigned char *sha1) |
| 65 | { |
| Junio C Hamano | 2b79636 | 2006-02-12 02:51:19 | [diff] [blame] | 66 | unsigned int i; |
| 67 | memcpy(&i, sha1, sizeof(unsigned int)); |
| Linus Torvalds | 0556a11 | 2006-06-30 18:20:33 | [diff] [blame] | 68 | return (int)(i % obj_hash_size); |
| Daniel Barkalow | 175785e | 2005-04-18 18:39:48 | [diff] [blame] | 69 | } |
| 70 | |
| Jason McMullan | 5d6ccf5 | 2005-06-03 15:05:39 | [diff] [blame] | 71 | struct object *lookup_object(const unsigned char *sha1) |
| Daniel Barkalow | 175785e | 2005-04-18 18:39:48 | [diff] [blame] | 72 | { |
| Linus Torvalds | 0556a11 | 2006-06-30 18:20:33 | [diff] [blame] | 73 | 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 Rientjes | a89fccd | 2006-08-17 18:54:57 | [diff] [blame] | 81 | if (!hashcmp(sha1, obj->sha1)) |
| Linus Torvalds | 0556a11 | 2006-06-30 18:20:33 | [diff] [blame] | 82 | break; |
| 83 | i++; |
| 84 | if (i == obj_hash_size) |
| 85 | i = 0; |
| 86 | } |
| 87 | return obj; |
| 88 | } |
| 89 | |
| 90 | static 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 Fonseca | b3c952f | 2006-08-28 00:26:07 | [diff] [blame] | 96 | new_hash = xcalloc(new_hash_size, sizeof(struct object *)); |
| Linus Torvalds | 0556a11 | 2006-06-30 18:20:33 | [diff] [blame] | 97 | 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 Barkalow | 175785e | 2005-04-18 18:39:48 | [diff] [blame] | 106 | } |
| 107 | |
| Linus Torvalds | 100c5f3 | 2007-04-17 05:11:43 | [diff] [blame] | 108 | void *create_object(const unsigned char *sha1, int type, void *o) |
| Daniel Barkalow | 175785e | 2005-04-18 18:39:48 | [diff] [blame] | 109 | { |
| Linus Torvalds | 100c5f3 | 2007-04-17 05:11:43 | [diff] [blame] | 110 | struct object *obj = o; |
| 111 | |
| Daniel Barkalow | 175785e | 2005-04-18 18:39:48 | [diff] [blame] | 112 | obj->parsed = 0; |
| Daniel Barkalow | 175785e | 2005-04-18 18:39:48 | [diff] [blame] | 113 | obj->used = 0; |
| Linus Torvalds | 100c5f3 | 2007-04-17 05:11:43 | [diff] [blame] | 114 | obj->type = type; |
| Linus Torvalds | 0556a11 | 2006-06-30 18:20:33 | [diff] [blame] | 115 | obj->flags = 0; |
| Shawn Pearce | e702496 | 2006-08-23 06:49:00 | [diff] [blame] | 116 | hashcpy(obj->sha1, sha1); |
| Daniel Barkalow | 175785e | 2005-04-18 18:39:48 | [diff] [blame] | 117 | |
| Linus Torvalds | 0556a11 | 2006-06-30 18:20:33 | [diff] [blame] | 118 | if (obj_hash_size - 1 <= nr_objs * 2) |
| 119 | grow_object_hash(); |
| Johannes Schindelin | 070879c | 2006-02-12 01:57:57 | [diff] [blame] | 120 | |
| Linus Torvalds | 0556a11 | 2006-06-30 18:20:33 | [diff] [blame] | 121 | insert_obj_hash(obj, obj_hash, obj_hash_size); |
| Daniel Barkalow | 175785e | 2005-04-18 18:39:48 | [diff] [blame] | 122 | nr_objs++; |
| Linus Torvalds | 100c5f3 | 2007-04-17 05:11:43 | [diff] [blame] | 123 | return obj; |
| Daniel Barkalow | 175785e | 2005-04-18 18:39:48 | [diff] [blame] | 124 | } |
| 125 | |
| barkalow@iabervon.org | 66e481b | 2005-08-02 23:45:48 | [diff] [blame] | 126 | struct object *lookup_unknown_object(const unsigned char *sha1) |
| 127 | { |
| 128 | struct object *obj = lookup_object(sha1); |
| Linus Torvalds | 100c5f3 | 2007-04-17 05:11:43 | [diff] [blame] | 129 | if (!obj) |
| 130 | obj = create_object(sha1, OBJ_NONE, alloc_object_node()); |
| barkalow@iabervon.org | 66e481b | 2005-08-02 23:45:48 | [diff] [blame] | 131 | return obj; |
| 132 | } |
| 133 | |
| Nicolas Pitre | 21666f1 | 2007-02-26 19:55:59 | [diff] [blame] | 134 | struct object *parse_object_buffer(const unsigned char *sha1, enum object_type type, unsigned long size, void *buffer, int *eaten_p) |
| Junio C Hamano | 9f613dd | 2006-09-15 20:30:02 | [diff] [blame] | 135 | { |
| 136 | struct object *obj; |
| 137 | int eaten = 0; |
| 138 | |
| Nicolas Pitre | 21666f1 | 2007-02-26 19:55:59 | [diff] [blame] | 139 | if (type == OBJ_BLOB) { |
| Junio C Hamano | 9f613dd | 2006-09-15 20:30:02 | [diff] [blame] | 140 | struct blob *blob = lookup_blob(sha1); |
| 141 | parse_blob_buffer(blob, buffer, size); |
| 142 | obj = &blob->object; |
| Nicolas Pitre | 21666f1 | 2007-02-26 19:55:59 | [diff] [blame] | 143 | } else if (type == OBJ_TREE) { |
| Junio C Hamano | 9f613dd | 2006-09-15 20:30:02 | [diff] [blame] | 144 | struct tree *tree = lookup_tree(sha1); |
| 145 | obj = &tree->object; |
| 146 | if (!tree->object.parsed) { |
| 147 | parse_tree_buffer(tree, buffer, size); |
| 148 | eaten = 1; |
| 149 | } |
| Nicolas Pitre | 21666f1 | 2007-02-26 19:55:59 | [diff] [blame] | 150 | } else if (type == OBJ_COMMIT) { |
| Junio C Hamano | 9f613dd | 2006-09-15 20:30:02 | [diff] [blame] | 151 | struct commit *commit = lookup_commit(sha1); |
| 152 | parse_commit_buffer(commit, buffer, size); |
| 153 | if (!commit->buffer) { |
| 154 | commit->buffer = buffer; |
| 155 | eaten = 1; |
| 156 | } |
| 157 | obj = &commit->object; |
| Nicolas Pitre | 21666f1 | 2007-02-26 19:55:59 | [diff] [blame] | 158 | } else if (type == OBJ_TAG) { |
| Junio C Hamano | 9f613dd | 2006-09-15 20:30:02 | [diff] [blame] | 159 | struct tag *tag = lookup_tag(sha1); |
| 160 | parse_tag_buffer(tag, buffer, size); |
| 161 | obj = &tag->object; |
| 162 | } else { |
| Sam Vilain | e2ac7cb | 2007-06-06 10:25:17 | [diff] [blame] | 163 | warning("object %s has unknown type id %d\n", sha1_to_hex(sha1), type); |
| Junio C Hamano | 9f613dd | 2006-09-15 20:30:02 | [diff] [blame] | 164 | obj = NULL; |
| 165 | } |
| Sam Vilain | e2ac7cb | 2007-06-06 10:25:17 | [diff] [blame] | 166 | if (obj && obj->type == OBJ_NONE) |
| 167 | obj->type = type; |
| Junio C Hamano | 9f613dd | 2006-09-15 20:30:02 | [diff] [blame] | 168 | *eaten_p = eaten; |
| 169 | return obj; |
| 170 | } |
| 171 | |
| Jason McMullan | 5d6ccf5 | 2005-06-03 15:05:39 | [diff] [blame] | 172 | struct object *parse_object(const unsigned char *sha1) |
| Daniel Barkalow | e9eefa6 | 2005-04-28 14:46:33 | [diff] [blame] | 173 | { |
| Junio C Hamano | c4584ae | 2005-06-27 10:33:33 | [diff] [blame] | 174 | unsigned long size; |
| Nicolas Pitre | 21666f1 | 2007-02-26 19:55:59 | [diff] [blame] | 175 | enum object_type type; |
| Junio C Hamano | 9f613dd | 2006-09-15 20:30:02 | [diff] [blame] | 176 | int eaten; |
| Nicolas Pitre | 21666f1 | 2007-02-26 19:55:59 | [diff] [blame] | 177 | void *buffer = read_sha1_file(sha1, &type, &size); |
| Junio C Hamano | 9f613dd | 2006-09-15 20:30:02 | [diff] [blame] | 178 | |
| Junio C Hamano | c4584ae | 2005-06-27 10:33:33 | [diff] [blame] | 179 | if (buffer) { |
| Nicolas Pitre | bd2c39f | 2005-05-06 17:48:34 | [diff] [blame] | 180 | struct object *obj; |
| Linus Torvalds | acdeec6 | 2007-03-20 17:05:20 | [diff] [blame] | 181 | if (check_sha1_signature(sha1, buffer, size, typename(type)) < 0) { |
| Carlos Rica | 0b1f113 | 2007-05-25 01:46:22 | [diff] [blame] | 182 | free(buffer); |
| Linus Torvalds | acdeec6 | 2007-03-20 17:05:20 | [diff] [blame] | 183 | error("sha1 mismatch %s\n", sha1_to_hex(sha1)); |
| 184 | return NULL; |
| 185 | } |
| Junio C Hamano | 9f613dd | 2006-09-15 20:30:02 | [diff] [blame] | 186 | |
| 187 | obj = parse_object_buffer(sha1, type, size, buffer, &eaten); |
| 188 | if (!eaten) |
| 189 | free(buffer); |
| Nicolas Pitre | bd2c39f | 2005-05-06 17:48:34 | [diff] [blame] | 190 | return obj; |
| Daniel Barkalow | e9eefa6 | 2005-04-28 14:46:33 | [diff] [blame] | 191 | } |
| 192 | return NULL; |
| 193 | } |
| barkalow@iabervon.org | 66e481b | 2005-08-02 23:45:48 | [diff] [blame] | 194 | |
| 195 | struct object_list *object_list_insert(struct object *item, |
| 196 | struct object_list **list_p) |
| 197 | { |
| 198 | struct object_list *new_list = xmalloc(sizeof(struct object_list)); |
| 199 | new_list->item = item; |
| 200 | new_list->next = *list_p; |
| 201 | *list_p = new_list; |
| 202 | return new_list; |
| 203 | } |
| 204 | |
| Daniel Barkalow | 680bab3 | 2005-09-05 06:04:18 | [diff] [blame] | 205 | void object_list_append(struct object *item, |
| 206 | struct object_list **list_p) |
| 207 | { |
| 208 | while (*list_p) { |
| 209 | list_p = &((*list_p)->next); |
| 210 | } |
| 211 | *list_p = xmalloc(sizeof(struct object_list)); |
| 212 | (*list_p)->next = NULL; |
| 213 | (*list_p)->item = item; |
| 214 | } |
| 215 | |
| barkalow@iabervon.org | 66e481b | 2005-08-02 23:45:48 | [diff] [blame] | 216 | unsigned object_list_length(struct object_list *list) |
| 217 | { |
| 218 | unsigned ret = 0; |
| 219 | while (list) { |
| 220 | list = list->next; |
| 221 | ret++; |
| 222 | } |
| 223 | return ret; |
| 224 | } |
| 225 | |
| 226 | int object_list_contains(struct object_list *list, struct object *obj) |
| 227 | { |
| 228 | while (list) { |
| 229 | if (list->item == obj) |
| 230 | return 1; |
| 231 | list = list->next; |
| 232 | } |
| 233 | return 0; |
| 234 | } |
| Linus Torvalds | 1f1e895 | 2006-06-20 00:42:35 | [diff] [blame] | 235 | |
| 236 | void add_object_array(struct object *obj, const char *name, struct object_array *array) |
| 237 | { |
| Martin Koegler | e5709a4 | 2007-04-22 16:43:58 | [diff] [blame] | 238 | add_object_array_with_mode(obj, name, array, S_IFINVALID); |
| 239 | } |
| 240 | |
| 241 | void add_object_array_with_mode(struct object *obj, const char *name, struct object_array *array, unsigned mode) |
| 242 | { |
| Linus Torvalds | 1f1e895 | 2006-06-20 00:42:35 | [diff] [blame] | 243 | unsigned nr = array->nr; |
| 244 | unsigned alloc = array->alloc; |
| 245 | struct object_array_entry *objects = array->objects; |
| 246 | |
| 247 | if (nr >= alloc) { |
| 248 | alloc = (alloc + 32) * 2; |
| 249 | objects = xrealloc(objects, alloc * sizeof(*objects)); |
| 250 | array->alloc = alloc; |
| 251 | array->objects = objects; |
| 252 | } |
| 253 | objects[nr].item = obj; |
| 254 | objects[nr].name = name; |
| Martin Koegler | e5709a4 | 2007-04-22 16:43:58 | [diff] [blame] | 255 | objects[nr].mode = mode; |
| Linus Torvalds | 1f1e895 | 2006-06-20 00:42:35 | [diff] [blame] | 256 | array->nr = ++nr; |
| 257 | } |