Junio C Hamano | 8f1d2e6 | 2006-01-07 01:33:54 -0800 | [diff] [blame] | 1 | #include "cache.h" |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 2 | #include "object.h" |
Daniel Barkalow | e9eefa6 | 2005-04-28 07:46:33 -0700 | [diff] [blame] | 3 | #include "blob.h" |
| 4 | #include "tree.h" |
| 5 | #include "commit.h" |
Daniel Barkalow | e9eefa6 | 2005-04-28 07:46:33 -0700 | [diff] [blame] | 6 | #include "tag.h" |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 7 | |
| 8 | struct object **objs; |
Johannes Schindelin | 070879c | 2006-02-12 02:57:57 +0100 | [diff] [blame] | 9 | static int nr_objs; |
| 10 | int obj_allocs; |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 11 | |
Linus Torvalds | 8805cca | 2005-09-16 14:55:33 -0700 | [diff] [blame] | 12 | int track_object_refs = 1; |
| 13 | |
Johannes Schindelin | 070879c | 2006-02-12 02:57:57 +0100 | [diff] [blame] | 14 | static int hashtable_index(const unsigned char *sha1) |
| 15 | { |
Junio C Hamano | 2b79636 | 2006-02-11 18:51:19 -0800 | [diff] [blame] | 16 | unsigned int i; |
| 17 | memcpy(&i, sha1, sizeof(unsigned int)); |
Johannes Schindelin | 070879c | 2006-02-12 02:57:57 +0100 | [diff] [blame] | 18 | return (int)(i % obj_allocs); |
| 19 | } |
| 20 | |
Jason McMullan | 5d6ccf5 | 2005-06-03 11:05:39 -0400 | [diff] [blame] | 21 | static int find_object(const unsigned char *sha1) |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 22 | { |
Junio C Hamano | 2b79636 | 2006-02-11 18:51:19 -0800 | [diff] [blame] | 23 | int i; |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 24 | |
Johannes Schindelin | 070879c | 2006-02-12 02:57:57 +0100 | [diff] [blame] | 25 | if (!objs) |
| 26 | return -1; |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 27 | |
Junio C Hamano | 2b79636 | 2006-02-11 18:51:19 -0800 | [diff] [blame] | 28 | i = hashtable_index(sha1); |
Johannes Schindelin | 070879c | 2006-02-12 02:57:57 +0100 | [diff] [blame] | 29 | while (objs[i]) { |
| 30 | if (memcmp(sha1, objs[i]->sha1, 20) == 0) |
| 31 | return i; |
| 32 | i++; |
| 33 | if (i == obj_allocs) |
| 34 | i = 0; |
| 35 | } |
| 36 | return -1 - i; |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 37 | } |
| 38 | |
Jason McMullan | 5d6ccf5 | 2005-06-03 11:05:39 -0400 | [diff] [blame] | 39 | struct object *lookup_object(const unsigned char *sha1) |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 40 | { |
| 41 | int pos = find_object(sha1); |
| 42 | if (pos >= 0) |
| 43 | return objs[pos]; |
| 44 | return NULL; |
| 45 | } |
| 46 | |
Jason McMullan | 5d6ccf5 | 2005-06-03 11:05:39 -0400 | [diff] [blame] | 47 | void created_object(const unsigned char *sha1, struct object *obj) |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 48 | { |
Johannes Schindelin | 070879c | 2006-02-12 02:57:57 +0100 | [diff] [blame] | 49 | int pos; |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 50 | |
| 51 | obj->parsed = 0; |
| 52 | memcpy(obj->sha1, sha1, 20); |
| 53 | obj->type = NULL; |
| 54 | obj->refs = NULL; |
| 55 | obj->used = 0; |
| 56 | |
Johannes Schindelin | 070879c | 2006-02-12 02:57:57 +0100 | [diff] [blame] | 57 | if (obj_allocs - 1 <= nr_objs * 2) { |
| 58 | int i, count = obj_allocs; |
| 59 | obj_allocs = (obj_allocs < 32 ? 32 : 2 * obj_allocs); |
| 60 | objs = xrealloc(objs, obj_allocs * sizeof(struct object *)); |
| 61 | memset(objs + count, 0, (obj_allocs - count) |
| 62 | * sizeof(struct object *)); |
Linus Torvalds | d7ee090 | 2006-02-12 11:24:50 -0800 | [diff] [blame] | 63 | for (i = 0; i < obj_allocs; i++) |
Johannes Schindelin | 070879c | 2006-02-12 02:57:57 +0100 | [diff] [blame] | 64 | if (objs[i]) { |
| 65 | int j = find_object(objs[i]->sha1); |
| 66 | if (j != i) { |
| 67 | j = -1 - j; |
| 68 | objs[j] = objs[i]; |
| 69 | objs[i] = NULL; |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | pos = find_object(sha1); |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 75 | if (pos >= 0) |
| 76 | die("Inserting %s twice\n", sha1_to_hex(sha1)); |
| 77 | pos = -pos-1; |
| 78 | |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 79 | objs[pos] = obj; |
| 80 | nr_objs++; |
| 81 | } |
| 82 | |
Sergey Vlasov | 4a4e6fd | 2005-11-15 19:08:08 +0300 | [diff] [blame] | 83 | struct object_refs *alloc_object_refs(unsigned count) |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 84 | { |
Sergey Vlasov | 4a4e6fd | 2005-11-15 19:08:08 +0300 | [diff] [blame] | 85 | struct object_refs *refs; |
| 86 | size_t size = sizeof(*refs) + count*sizeof(struct object *); |
Linus Torvalds | 8805cca | 2005-09-16 14:55:33 -0700 | [diff] [blame] | 87 | |
Sergey Vlasov | 4a4e6fd | 2005-11-15 19:08:08 +0300 | [diff] [blame] | 88 | refs = xmalloc(size); |
| 89 | memset(refs, 0, size); |
| 90 | refs->count = count; |
| 91 | return refs; |
| 92 | } |
| 93 | |
| 94 | static int compare_object_pointers(const void *a, const void *b) |
| 95 | { |
| 96 | const struct object * const *pa = a; |
| 97 | const struct object * const *pb = b; |
Junio C Hamano | e23eff8 | 2005-12-06 13:41:48 -0800 | [diff] [blame] | 98 | if (*pa == *pb) |
| 99 | return 0; |
| 100 | else if (*pa < *pb) |
| 101 | return -1; |
| 102 | else |
| 103 | return 1; |
Sergey Vlasov | 4a4e6fd | 2005-11-15 19:08:08 +0300 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | void set_object_refs(struct object *obj, struct object_refs *refs) |
| 107 | { |
| 108 | unsigned int i, j; |
| 109 | |
| 110 | /* Do not install empty list of references */ |
| 111 | if (refs->count < 1) { |
| 112 | free(refs); |
Linus Torvalds | 8805cca | 2005-09-16 14:55:33 -0700 | [diff] [blame] | 113 | return; |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 114 | } |
| 115 | |
Sergey Vlasov | 4a4e6fd | 2005-11-15 19:08:08 +0300 | [diff] [blame] | 116 | /* Sort the list and filter out duplicates */ |
| 117 | qsort(refs->ref, refs->count, sizeof(refs->ref[0]), |
| 118 | compare_object_pointers); |
| 119 | for (i = j = 1; i < refs->count; i++) { |
| 120 | if (refs->ref[i] != refs->ref[i - 1]) |
| 121 | refs->ref[j++] = refs->ref[i]; |
| 122 | } |
| 123 | if (j < refs->count) { |
| 124 | /* Duplicates were found - reallocate list */ |
| 125 | size_t size = sizeof(*refs) + j*sizeof(struct object *); |
| 126 | refs->count = j; |
| 127 | refs = xrealloc(refs, size); |
| 128 | } |
| 129 | |
| 130 | for (i = 0; i < refs->count; i++) |
| 131 | refs->ref[i]->used = 1; |
| 132 | obj->refs = refs; |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | void mark_reachable(struct object *obj, unsigned int mask) |
| 136 | { |
Linus Torvalds | 8805cca | 2005-09-16 14:55:33 -0700 | [diff] [blame] | 137 | if (!track_object_refs) |
| 138 | die("cannot do reachability with object refs turned off"); |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 139 | /* If we've been here already, don't bother */ |
| 140 | if (obj->flags & mask) |
| 141 | return; |
| 142 | obj->flags |= mask; |
Sergey Vlasov | 4a4e6fd | 2005-11-15 19:08:08 +0300 | [diff] [blame] | 143 | if (obj->refs) { |
| 144 | const struct object_refs *refs = obj->refs; |
| 145 | unsigned i; |
| 146 | for (i = 0; i < refs->count; i++) |
| 147 | mark_reachable(refs->ref[i], mask); |
Daniel Barkalow | 175785e | 2005-04-18 11:39:48 -0700 | [diff] [blame] | 148 | } |
| 149 | } |
Daniel Barkalow | e9eefa6 | 2005-04-28 07:46:33 -0700 | [diff] [blame] | 150 | |
Daniel Barkalow | 89e4202 | 2005-06-21 20:35:10 -0400 | [diff] [blame] | 151 | struct object *lookup_object_type(const unsigned char *sha1, const char *type) |
| 152 | { |
barkalow@iabervon.org | 66e481b | 2005-08-02 19:45:48 -0400 | [diff] [blame] | 153 | if (!type) { |
| 154 | return lookup_unknown_object(sha1); |
| 155 | } else if (!strcmp(type, blob_type)) { |
Daniel Barkalow | 89e4202 | 2005-06-21 20:35:10 -0400 | [diff] [blame] | 156 | return &lookup_blob(sha1)->object; |
| 157 | } else if (!strcmp(type, tree_type)) { |
| 158 | return &lookup_tree(sha1)->object; |
| 159 | } else if (!strcmp(type, commit_type)) { |
| 160 | return &lookup_commit(sha1)->object; |
| 161 | } else if (!strcmp(type, tag_type)) { |
| 162 | return &lookup_tag(sha1)->object; |
| 163 | } else { |
| 164 | error("Unknown type %s", type); |
| 165 | return NULL; |
| 166 | } |
| 167 | } |
| 168 | |
barkalow@iabervon.org | 66e481b | 2005-08-02 19:45:48 -0400 | [diff] [blame] | 169 | union any_object { |
| 170 | struct object object; |
| 171 | struct commit commit; |
| 172 | struct tree tree; |
| 173 | struct blob blob; |
| 174 | struct tag tag; |
| 175 | }; |
| 176 | |
| 177 | struct object *lookup_unknown_object(const unsigned char *sha1) |
| 178 | { |
| 179 | struct object *obj = lookup_object(sha1); |
| 180 | if (!obj) { |
| 181 | union any_object *ret = xmalloc(sizeof(*ret)); |
| 182 | memset(ret, 0, sizeof(*ret)); |
| 183 | created_object(sha1, &ret->object); |
| 184 | ret->object.type = NULL; |
| 185 | return &ret->object; |
| 186 | } |
| 187 | return obj; |
| 188 | } |
| 189 | |
Jason McMullan | 5d6ccf5 | 2005-06-03 11:05:39 -0400 | [diff] [blame] | 190 | struct object *parse_object(const unsigned char *sha1) |
Daniel Barkalow | e9eefa6 | 2005-04-28 07:46:33 -0700 | [diff] [blame] | 191 | { |
Junio C Hamano | c4584ae | 2005-06-27 03:33:33 -0700 | [diff] [blame] | 192 | unsigned long size; |
| 193 | char type[20]; |
| 194 | void *buffer = read_sha1_file(sha1, type, &size); |
| 195 | if (buffer) { |
Nicolas Pitre | bd2c39f | 2005-05-06 13:48:34 -0400 | [diff] [blame] | 196 | struct object *obj; |
Junio C Hamano | c4584ae | 2005-06-27 03:33:33 -0700 | [diff] [blame] | 197 | if (check_sha1_signature(sha1, buffer, size, type) < 0) |
Daniel Barkalow | e9eefa6 | 2005-04-28 07:46:33 -0700 | [diff] [blame] | 198 | printf("sha1 mismatch %s\n", sha1_to_hex(sha1)); |
Junio C Hamano | c4584ae | 2005-06-27 03:33:33 -0700 | [diff] [blame] | 199 | if (!strcmp(type, "blob")) { |
Nicolas Pitre | bd2c39f | 2005-05-06 13:48:34 -0400 | [diff] [blame] | 200 | struct blob *blob = lookup_blob(sha1); |
| 201 | parse_blob_buffer(blob, buffer, size); |
| 202 | obj = &blob->object; |
Daniel Barkalow | e9eefa6 | 2005-04-28 07:46:33 -0700 | [diff] [blame] | 203 | } else if (!strcmp(type, "tree")) { |
Nicolas Pitre | bd2c39f | 2005-05-06 13:48:34 -0400 | [diff] [blame] | 204 | struct tree *tree = lookup_tree(sha1); |
| 205 | parse_tree_buffer(tree, buffer, size); |
| 206 | obj = &tree->object; |
Daniel Barkalow | e9eefa6 | 2005-04-28 07:46:33 -0700 | [diff] [blame] | 207 | } else if (!strcmp(type, "commit")) { |
Nicolas Pitre | bd2c39f | 2005-05-06 13:48:34 -0400 | [diff] [blame] | 208 | struct commit *commit = lookup_commit(sha1); |
| 209 | parse_commit_buffer(commit, buffer, size); |
Linus Torvalds | bd1e17e | 2005-05-25 19:26:28 -0700 | [diff] [blame] | 210 | if (!commit->buffer) { |
| 211 | commit->buffer = buffer; |
| 212 | buffer = NULL; |
| 213 | } |
Nicolas Pitre | bd2c39f | 2005-05-06 13:48:34 -0400 | [diff] [blame] | 214 | obj = &commit->object; |
Daniel Barkalow | e9eefa6 | 2005-04-28 07:46:33 -0700 | [diff] [blame] | 215 | } else if (!strcmp(type, "tag")) { |
Nicolas Pitre | bd2c39f | 2005-05-06 13:48:34 -0400 | [diff] [blame] | 216 | struct tag *tag = lookup_tag(sha1); |
| 217 | parse_tag_buffer(tag, buffer, size); |
| 218 | obj = &tag->object; |
Daniel Barkalow | e9eefa6 | 2005-04-28 07:46:33 -0700 | [diff] [blame] | 219 | } else { |
Nicolas Pitre | bd2c39f | 2005-05-06 13:48:34 -0400 | [diff] [blame] | 220 | obj = NULL; |
Daniel Barkalow | e9eefa6 | 2005-04-28 07:46:33 -0700 | [diff] [blame] | 221 | } |
Nicolas Pitre | bd2c39f | 2005-05-06 13:48:34 -0400 | [diff] [blame] | 222 | free(buffer); |
| 223 | return obj; |
Daniel Barkalow | e9eefa6 | 2005-04-28 07:46:33 -0700 | [diff] [blame] | 224 | } |
| 225 | return NULL; |
| 226 | } |
barkalow@iabervon.org | 66e481b | 2005-08-02 19:45:48 -0400 | [diff] [blame] | 227 | |
| 228 | struct 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)); |
| 232 | new_list->item = item; |
| 233 | new_list->next = *list_p; |
| 234 | *list_p = new_list; |
| 235 | return new_list; |
| 236 | } |
| 237 | |
Daniel Barkalow | 680bab3 | 2005-09-05 02:04:18 -0400 | [diff] [blame] | 238 | void object_list_append(struct object *item, |
| 239 | struct object_list **list_p) |
| 240 | { |
| 241 | while (*list_p) { |
| 242 | list_p = &((*list_p)->next); |
| 243 | } |
| 244 | *list_p = xmalloc(sizeof(struct object_list)); |
| 245 | (*list_p)->next = NULL; |
| 246 | (*list_p)->item = item; |
| 247 | } |
| 248 | |
barkalow@iabervon.org | 66e481b | 2005-08-02 19:45:48 -0400 | [diff] [blame] | 249 | unsigned object_list_length(struct object_list *list) |
| 250 | { |
| 251 | unsigned ret = 0; |
| 252 | while (list) { |
| 253 | list = list->next; |
| 254 | ret++; |
| 255 | } |
| 256 | return ret; |
| 257 | } |
| 258 | |
| 259 | int object_list_contains(struct object_list *list, struct object *obj) |
| 260 | { |
| 261 | while (list) { |
| 262 | if (list->item == obj) |
| 263 | return 1; |
| 264 | list = list->next; |
| 265 | } |
| 266 | return 0; |
| 267 | } |