| 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 | |
| Linus Torvalds | 885a86a | 2006-06-14 23:45:13 | [diff] [blame] | 21 | const char *type_names[] = { |
| Linus Torvalds | 1974632 | 2006-07-12 03:45:31 | [diff] [blame] | 22 | "none", "commit", "tree", "blob", "tag", |
| 23 | "bad type 5", "bad type 6", "delta", "bad", |
| Linus Torvalds | 885a86a | 2006-06-14 23:45:13 | [diff] [blame] | 24 | }; |
| 25 | |
| Linus Torvalds | 0556a11 | 2006-06-30 18:20:33 | [diff] [blame] | 26 | static unsigned int hash_obj(struct object *obj, unsigned int n) |
| 27 | { |
| 28 | unsigned int hash = *(unsigned int *)obj->sha1; |
| 29 | return hash % n; |
| 30 | } |
| 31 | |
| 32 | static void insert_obj_hash(struct object *obj, struct object **hash, unsigned int size) |
| 33 | { |
| 34 | int j = hash_obj(obj, size); |
| 35 | |
| 36 | while (hash[j]) { |
| 37 | j++; |
| 38 | if (j >= size) |
| 39 | j = 0; |
| 40 | } |
| 41 | hash[j] = obj; |
| 42 | } |
| 43 | |
| Johannes Schindelin | 070879c | 2006-02-12 01:57:57 | [diff] [blame] | 44 | static int hashtable_index(const unsigned char *sha1) |
| 45 | { |
| Junio C Hamano | 2b79636 | 2006-02-12 02:51:19 | [diff] [blame] | 46 | unsigned int i; |
| 47 | memcpy(&i, sha1, sizeof(unsigned int)); |
| Linus Torvalds | 0556a11 | 2006-06-30 18:20:33 | [diff] [blame] | 48 | return (int)(i % obj_hash_size); |
| Daniel Barkalow | 175785e | 2005-04-18 18:39:48 | [diff] [blame] | 49 | } |
| 50 | |
| Jason McMullan | 5d6ccf5 | 2005-06-03 15:05:39 | [diff] [blame] | 51 | struct object *lookup_object(const unsigned char *sha1) |
| Daniel Barkalow | 175785e | 2005-04-18 18:39:48 | [diff] [blame] | 52 | { |
| Linus Torvalds | 0556a11 | 2006-06-30 18:20:33 | [diff] [blame] | 53 | int i; |
| 54 | struct object *obj; |
| 55 | |
| 56 | if (!obj_hash) |
| 57 | return NULL; |
| 58 | |
| 59 | i = hashtable_index(sha1); |
| 60 | while ((obj = obj_hash[i]) != NULL) { |
| David Rientjes | a89fccd | 2006-08-17 18:54:57 | [diff] [blame] | 61 | if (!hashcmp(sha1, obj->sha1)) |
| Linus Torvalds | 0556a11 | 2006-06-30 18:20:33 | [diff] [blame] | 62 | break; |
| 63 | i++; |
| 64 | if (i == obj_hash_size) |
| 65 | i = 0; |
| 66 | } |
| 67 | return obj; |
| 68 | } |
| 69 | |
| 70 | static void grow_object_hash(void) |
| 71 | { |
| 72 | int i; |
| 73 | int new_hash_size = obj_hash_size < 32 ? 32 : 2 * obj_hash_size; |
| 74 | struct object **new_hash; |
| 75 | |
| Jonas Fonseca | b3c952f | 2006-08-28 00:26:07 | [diff] [blame] | 76 | new_hash = xcalloc(new_hash_size, sizeof(struct object *)); |
| Linus Torvalds | 0556a11 | 2006-06-30 18:20:33 | [diff] [blame] | 77 | for (i = 0; i < obj_hash_size; i++) { |
| 78 | struct object *obj = obj_hash[i]; |
| 79 | if (!obj) |
| 80 | continue; |
| 81 | insert_obj_hash(obj, new_hash, new_hash_size); |
| 82 | } |
| 83 | free(obj_hash); |
| 84 | obj_hash = new_hash; |
| 85 | obj_hash_size = new_hash_size; |
| Daniel Barkalow | 175785e | 2005-04-18 18:39:48 | [diff] [blame] | 86 | } |
| 87 | |
| Jason McMullan | 5d6ccf5 | 2005-06-03 15:05:39 | [diff] [blame] | 88 | void created_object(const unsigned char *sha1, struct object *obj) |
| Daniel Barkalow | 175785e | 2005-04-18 18:39:48 | [diff] [blame] | 89 | { |
| Daniel Barkalow | 175785e | 2005-04-18 18:39:48 | [diff] [blame] | 90 | obj->parsed = 0; |
| Daniel Barkalow | 175785e | 2005-04-18 18:39:48 | [diff] [blame] | 91 | obj->used = 0; |
| Linus Torvalds | 1974632 | 2006-07-12 03:45:31 | [diff] [blame] | 92 | obj->type = OBJ_NONE; |
| Linus Torvalds | 0556a11 | 2006-06-30 18:20:33 | [diff] [blame] | 93 | obj->flags = 0; |
| Shawn Pearce | e702496 | 2006-08-23 06:49:00 | [diff] [blame] | 94 | hashcpy(obj->sha1, sha1); |
| Daniel Barkalow | 175785e | 2005-04-18 18:39:48 | [diff] [blame] | 95 | |
| Linus Torvalds | 0556a11 | 2006-06-30 18:20:33 | [diff] [blame] | 96 | if (obj_hash_size - 1 <= nr_objs * 2) |
| 97 | grow_object_hash(); |
| Johannes Schindelin | 070879c | 2006-02-12 01:57:57 | [diff] [blame] | 98 | |
| Linus Torvalds | 0556a11 | 2006-06-30 18:20:33 | [diff] [blame] | 99 | insert_obj_hash(obj, obj_hash, obj_hash_size); |
| Daniel Barkalow | 175785e | 2005-04-18 18:39:48 | [diff] [blame] | 100 | nr_objs++; |
| 101 | } |
| 102 | |
| Daniel Barkalow | 89e4202 | 2005-06-22 00:35:10 | [diff] [blame] | 103 | struct object *lookup_object_type(const unsigned char *sha1, const char *type) |
| 104 | { |
| barkalow@iabervon.org | 66e481b | 2005-08-02 23:45:48 | [diff] [blame] | 105 | if (!type) { |
| 106 | return lookup_unknown_object(sha1); |
| 107 | } else if (!strcmp(type, blob_type)) { |
| Daniel Barkalow | 89e4202 | 2005-06-22 00:35:10 | [diff] [blame] | 108 | return &lookup_blob(sha1)->object; |
| 109 | } else if (!strcmp(type, tree_type)) { |
| 110 | return &lookup_tree(sha1)->object; |
| 111 | } else if (!strcmp(type, commit_type)) { |
| 112 | return &lookup_commit(sha1)->object; |
| 113 | } else if (!strcmp(type, tag_type)) { |
| 114 | return &lookup_tag(sha1)->object; |
| 115 | } else { |
| 116 | error("Unknown type %s", type); |
| 117 | return NULL; |
| 118 | } |
| 119 | } |
| 120 | |
| barkalow@iabervon.org | 66e481b | 2005-08-02 23:45:48 | [diff] [blame] | 121 | union any_object { |
| 122 | struct object object; |
| 123 | struct commit commit; |
| 124 | struct tree tree; |
| 125 | struct blob blob; |
| 126 | struct tag tag; |
| 127 | }; |
| 128 | |
| 129 | struct object *lookup_unknown_object(const unsigned char *sha1) |
| 130 | { |
| 131 | struct object *obj = lookup_object(sha1); |
| 132 | if (!obj) { |
| Peter Eriksen | 90321c1 | 2006-04-03 18:30:46 | [diff] [blame] | 133 | union any_object *ret = xcalloc(1, sizeof(*ret)); |
| barkalow@iabervon.org | 66e481b | 2005-08-02 23:45:48 | [diff] [blame] | 134 | created_object(sha1, &ret->object); |
| Linus Torvalds | 1974632 | 2006-07-12 03:45:31 | [diff] [blame] | 135 | ret->object.type = OBJ_NONE; |
| barkalow@iabervon.org | 66e481b | 2005-08-02 23:45:48 | [diff] [blame] | 136 | return &ret->object; |
| 137 | } |
| 138 | return obj; |
| 139 | } |
| 140 | |
| Junio C Hamano | 9f613dd | 2006-09-15 20:30:02 | [diff] [blame] | 141 | struct object *parse_object_buffer(const unsigned char *sha1, const char *type, unsigned long size, void *buffer, int *eaten_p) |
| 142 | { |
| 143 | struct object *obj; |
| 144 | int eaten = 0; |
| 145 | |
| 146 | if (!strcmp(type, blob_type)) { |
| 147 | struct blob *blob = lookup_blob(sha1); |
| 148 | parse_blob_buffer(blob, buffer, size); |
| 149 | obj = &blob->object; |
| 150 | } else if (!strcmp(type, tree_type)) { |
| 151 | struct tree *tree = lookup_tree(sha1); |
| 152 | obj = &tree->object; |
| 153 | if (!tree->object.parsed) { |
| 154 | parse_tree_buffer(tree, buffer, size); |
| 155 | eaten = 1; |
| 156 | } |
| 157 | } else if (!strcmp(type, commit_type)) { |
| 158 | struct commit *commit = lookup_commit(sha1); |
| 159 | parse_commit_buffer(commit, buffer, size); |
| 160 | if (!commit->buffer) { |
| 161 | commit->buffer = buffer; |
| 162 | eaten = 1; |
| 163 | } |
| 164 | obj = &commit->object; |
| 165 | } else if (!strcmp(type, tag_type)) { |
| 166 | struct tag *tag = lookup_tag(sha1); |
| 167 | parse_tag_buffer(tag, buffer, size); |
| 168 | obj = &tag->object; |
| 169 | } else { |
| 170 | obj = NULL; |
| 171 | } |
| 172 | *eaten_p = eaten; |
| 173 | return obj; |
| 174 | } |
| 175 | |
| Jason McMullan | 5d6ccf5 | 2005-06-03 15:05:39 | [diff] [blame] | 176 | struct object *parse_object(const unsigned char *sha1) |
| Daniel Barkalow | e9eefa6 | 2005-04-28 14:46:33 | [diff] [blame] | 177 | { |
| Junio C Hamano | c4584ae | 2005-06-27 10:33:33 | [diff] [blame] | 178 | unsigned long size; |
| 179 | char type[20]; |
| Junio C Hamano | 9f613dd | 2006-09-15 20:30:02 | [diff] [blame] | 180 | int eaten; |
| Junio C Hamano | c4584ae | 2005-06-27 10:33:33 | [diff] [blame] | 181 | void *buffer = read_sha1_file(sha1, type, &size); |
| Junio C Hamano | 9f613dd | 2006-09-15 20:30:02 | [diff] [blame] | 182 | |
| Junio C Hamano | c4584ae | 2005-06-27 10:33:33 | [diff] [blame] | 183 | if (buffer) { |
| Nicolas Pitre | bd2c39f | 2005-05-06 17:48:34 | [diff] [blame] | 184 | struct object *obj; |
| Junio C Hamano | c4584ae | 2005-06-27 10:33:33 | [diff] [blame] | 185 | if (check_sha1_signature(sha1, buffer, size, type) < 0) |
| Daniel Barkalow | e9eefa6 | 2005-04-28 14:46:33 | [diff] [blame] | 186 | printf("sha1 mismatch %s\n", sha1_to_hex(sha1)); |
| Junio C Hamano | 9f613dd | 2006-09-15 20:30:02 | [diff] [blame] | 187 | |
| 188 | obj = parse_object_buffer(sha1, type, size, buffer, &eaten); |
| 189 | if (!eaten) |
| 190 | free(buffer); |
| Nicolas Pitre | bd2c39f | 2005-05-06 17:48:34 | [diff] [blame] | 191 | return obj; |
| Daniel Barkalow | e9eefa6 | 2005-04-28 14:46:33 | [diff] [blame] | 192 | } |
| 193 | return NULL; |
| 194 | } |
| barkalow@iabervon.org | 66e481b | 2005-08-02 23:45:48 | [diff] [blame] | 195 | |
| 196 | struct object_list *object_list_insert(struct object *item, |
| 197 | struct object_list **list_p) |
| 198 | { |
| 199 | struct object_list *new_list = xmalloc(sizeof(struct object_list)); |
| 200 | new_list->item = item; |
| 201 | new_list->next = *list_p; |
| 202 | *list_p = new_list; |
| 203 | return new_list; |
| 204 | } |
| 205 | |
| Daniel Barkalow | 680bab3 | 2005-09-05 06:04:18 | [diff] [blame] | 206 | void object_list_append(struct object *item, |
| 207 | struct object_list **list_p) |
| 208 | { |
| 209 | while (*list_p) { |
| 210 | list_p = &((*list_p)->next); |
| 211 | } |
| 212 | *list_p = xmalloc(sizeof(struct object_list)); |
| 213 | (*list_p)->next = NULL; |
| 214 | (*list_p)->item = item; |
| 215 | } |
| 216 | |
| barkalow@iabervon.org | 66e481b | 2005-08-02 23:45:48 | [diff] [blame] | 217 | unsigned object_list_length(struct object_list *list) |
| 218 | { |
| 219 | unsigned ret = 0; |
| 220 | while (list) { |
| 221 | list = list->next; |
| 222 | ret++; |
| 223 | } |
| 224 | return ret; |
| 225 | } |
| 226 | |
| 227 | int object_list_contains(struct object_list *list, struct object *obj) |
| 228 | { |
| 229 | while (list) { |
| 230 | if (list->item == obj) |
| 231 | return 1; |
| 232 | list = list->next; |
| 233 | } |
| 234 | return 0; |
| 235 | } |
| Linus Torvalds | 1f1e895 | 2006-06-20 00:42:35 | [diff] [blame] | 236 | |
| 237 | void add_object_array(struct object *obj, const char *name, struct object_array *array) |
| 238 | { |
| 239 | unsigned nr = array->nr; |
| 240 | unsigned alloc = array->alloc; |
| 241 | struct object_array_entry *objects = array->objects; |
| 242 | |
| 243 | if (nr >= alloc) { |
| 244 | alloc = (alloc + 32) * 2; |
| 245 | objects = xrealloc(objects, alloc * sizeof(*objects)); |
| 246 | array->alloc = alloc; |
| 247 | array->objects = objects; |
| 248 | } |
| 249 | objects[nr].item = obj; |
| 250 | objects[nr].name = name; |
| 251 | array->nr = ++nr; |
| 252 | } |