]>
Commit | Line | Data |
---|---|---|
8f1d2e6f | 1 | #include "cache.h" |
175785e5 | 2 | #include "object.h" |
e9eefa67 DB |
3 | #include "blob.h" |
4 | #include "tree.h" | |
5 | #include "commit.h" | |
e9eefa67 | 6 | #include "tag.h" |
175785e5 | 7 | |
0556a11a LT |
8 | static struct object **obj_hash; |
9 | static int nr_objs, obj_hash_size; | |
fc046a75 LT |
10 | |
11 | unsigned int get_max_object_index(void) | |
12 | { | |
0556a11a | 13 | return obj_hash_size; |
fc046a75 LT |
14 | } |
15 | ||
16 | struct object *get_indexed_object(unsigned int idx) | |
17 | { | |
0556a11a | 18 | return obj_hash[idx]; |
fc046a75 | 19 | } |
175785e5 | 20 | |
df843662 NP |
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 */ | |
885a86ab LT |
27 | }; |
28 | ||
df843662 NP |
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 | ||
0556a11a LT |
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 | ||
070879ca JS |
64 | static int hashtable_index(const unsigned char *sha1) |
65 | { | |
2b796360 JH |
66 | unsigned int i; |
67 | memcpy(&i, sha1, sizeof(unsigned int)); | |
0556a11a | 68 | return (int)(i % obj_hash_size); |
070879ca JS |
69 | } |
70 | ||
0556a11a | 71 | struct object *lookup_object(const unsigned char *sha1) |
175785e5 | 72 | { |
2b796360 | 73 | int i; |
0556a11a | 74 | struct object *obj; |
175785e5 | 75 | |
0556a11a LT |
76 | if (!obj_hash) |
77 | return NULL; | |
175785e5 | 78 | |
2b796360 | 79 | i = hashtable_index(sha1); |
0556a11a | 80 | while ((obj = obj_hash[i]) != NULL) { |
a89fccd2 | 81 | if (!hashcmp(sha1, obj->sha1)) |
0556a11a | 82 | break; |
070879ca | 83 | i++; |
0556a11a | 84 | if (i == obj_hash_size) |
070879ca JS |
85 | i = 0; |
86 | } | |
0556a11a | 87 | return obj; |
175785e5 DB |
88 | } |
89 | ||
0556a11a | 90 | static void grow_object_hash(void) |
175785e5 | 91 | { |
0556a11a LT |
92 | int i; |
93 | int new_hash_size = obj_hash_size < 32 ? 32 : 2 * obj_hash_size; | |
94 | struct object **new_hash; | |
95 | ||
b3c952f8 | 96 | new_hash = xcalloc(new_hash_size, sizeof(struct object *)); |
0556a11a LT |
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; | |
175785e5 DB |
106 | } |
107 | ||
100c5f3b | 108 | void *create_object(const unsigned char *sha1, int type, void *o) |
175785e5 | 109 | { |
100c5f3b LT |
110 | struct object *obj = o; |
111 | ||
175785e5 | 112 | obj->parsed = 0; |
175785e5 | 113 | obj->used = 0; |
100c5f3b | 114 | obj->type = type; |
0556a11a | 115 | obj->flags = 0; |
e702496e | 116 | hashcpy(obj->sha1, sha1); |
175785e5 | 117 | |
0556a11a LT |
118 | if (obj_hash_size - 1 <= nr_objs * 2) |
119 | grow_object_hash(); | |
175785e5 | 120 | |
0556a11a | 121 | insert_obj_hash(obj, obj_hash, obj_hash_size); |
175785e5 | 122 | nr_objs++; |
100c5f3b | 123 | return obj; |
175785e5 DB |
124 | } |
125 | ||
66e481b0 DB |
126 | struct object *lookup_unknown_object(const unsigned char *sha1) |
127 | { | |
128 | struct object *obj = lookup_object(sha1); | |
100c5f3b LT |
129 | if (!obj) |
130 | obj = create_object(sha1, OBJ_NONE, alloc_object_node()); | |
66e481b0 DB |
131 | return obj; |
132 | } | |
133 | ||
21666f1a | 134 | struct object *parse_object_buffer(const unsigned char *sha1, enum object_type type, unsigned long size, void *buffer, int *eaten_p) |
9f613ddd JH |
135 | { |
136 | struct object *obj; | |
137 | int eaten = 0; | |
138 | ||
cc216827 | 139 | obj = NULL; |
21666f1a | 140 | if (type == OBJ_BLOB) { |
9f613ddd | 141 | struct blob *blob = lookup_blob(sha1); |
cc216827 JM |
142 | if (blob) { |
143 | parse_blob_buffer(blob, buffer, size); | |
144 | obj = &blob->object; | |
145 | } | |
21666f1a | 146 | } else if (type == OBJ_TREE) { |
9f613ddd | 147 | struct tree *tree = lookup_tree(sha1); |
cc216827 JM |
148 | if (tree) { |
149 | obj = &tree->object; | |
150 | if (!tree->object.parsed) { | |
151 | parse_tree_buffer(tree, buffer, size); | |
152 | eaten = 1; | |
153 | } | |
9f613ddd | 154 | } |
21666f1a | 155 | } else if (type == OBJ_COMMIT) { |
9f613ddd | 156 | struct commit *commit = lookup_commit(sha1); |
cc216827 JM |
157 | if (commit) { |
158 | parse_commit_buffer(commit, buffer, size); | |
159 | if (!commit->buffer) { | |
160 | commit->buffer = buffer; | |
161 | eaten = 1; | |
162 | } | |
163 | obj = &commit->object; | |
9f613ddd | 164 | } |
21666f1a | 165 | } else if (type == OBJ_TAG) { |
9f613ddd | 166 | struct tag *tag = lookup_tag(sha1); |
cc216827 JM |
167 | if (tag) { |
168 | parse_tag_buffer(tag, buffer, size); | |
169 | obj = &tag->object; | |
170 | } | |
9f613ddd | 171 | } else { |
e2ac7cb5 | 172 | warning("object %s has unknown type id %d\n", sha1_to_hex(sha1), type); |
9f613ddd JH |
173 | obj = NULL; |
174 | } | |
e2ac7cb5 SV |
175 | if (obj && obj->type == OBJ_NONE) |
176 | obj->type = type; | |
9f613ddd JH |
177 | *eaten_p = eaten; |
178 | return obj; | |
179 | } | |
180 | ||
5d6ccf5c | 181 | struct object *parse_object(const unsigned char *sha1) |
e9eefa67 | 182 | { |
c4584ae3 | 183 | unsigned long size; |
21666f1a | 184 | enum object_type type; |
9f613ddd | 185 | int eaten; |
21666f1a | 186 | void *buffer = read_sha1_file(sha1, &type, &size); |
9f613ddd | 187 | |
c4584ae3 | 188 | if (buffer) { |
bd2c39f5 | 189 | struct object *obj; |
acdeec62 | 190 | if (check_sha1_signature(sha1, buffer, size, typename(type)) < 0) { |
0b1f1130 | 191 | free(buffer); |
acdeec62 LT |
192 | error("sha1 mismatch %s\n", sha1_to_hex(sha1)); |
193 | return NULL; | |
194 | } | |
9f613ddd JH |
195 | |
196 | obj = parse_object_buffer(sha1, type, size, buffer, &eaten); | |
197 | if (!eaten) | |
198 | free(buffer); | |
bd2c39f5 | 199 | return obj; |
e9eefa67 DB |
200 | } |
201 | return NULL; | |
202 | } | |
66e481b0 DB |
203 | |
204 | struct object_list *object_list_insert(struct object *item, | |
205 | struct object_list **list_p) | |
206 | { | |
207 | struct object_list *new_list = xmalloc(sizeof(struct object_list)); | |
208 | new_list->item = item; | |
209 | new_list->next = *list_p; | |
210 | *list_p = new_list; | |
211 | return new_list; | |
212 | } | |
213 | ||
680bab3d DB |
214 | void object_list_append(struct object *item, |
215 | struct object_list **list_p) | |
216 | { | |
217 | while (*list_p) { | |
218 | list_p = &((*list_p)->next); | |
219 | } | |
220 | *list_p = xmalloc(sizeof(struct object_list)); | |
221 | (*list_p)->next = NULL; | |
222 | (*list_p)->item = item; | |
223 | } | |
224 | ||
66e481b0 DB |
225 | unsigned object_list_length(struct object_list *list) |
226 | { | |
227 | unsigned ret = 0; | |
228 | while (list) { | |
229 | list = list->next; | |
230 | ret++; | |
231 | } | |
232 | return ret; | |
233 | } | |
234 | ||
235 | int object_list_contains(struct object_list *list, struct object *obj) | |
236 | { | |
237 | while (list) { | |
238 | if (list->item == obj) | |
239 | return 1; | |
240 | list = list->next; | |
241 | } | |
242 | return 0; | |
243 | } | |
1f1e895f LT |
244 | |
245 | void add_object_array(struct object *obj, const char *name, struct object_array *array) | |
e5709a4a MK |
246 | { |
247 | add_object_array_with_mode(obj, name, array, S_IFINVALID); | |
248 | } | |
249 | ||
250 | void add_object_array_with_mode(struct object *obj, const char *name, struct object_array *array, unsigned mode) | |
1f1e895f LT |
251 | { |
252 | unsigned nr = array->nr; | |
253 | unsigned alloc = array->alloc; | |
254 | struct object_array_entry *objects = array->objects; | |
255 | ||
256 | if (nr >= alloc) { | |
257 | alloc = (alloc + 32) * 2; | |
258 | objects = xrealloc(objects, alloc * sizeof(*objects)); | |
259 | array->alloc = alloc; | |
260 | array->objects = objects; | |
261 | } | |
262 | objects[nr].item = obj; | |
263 | objects[nr].name = name; | |
e5709a4a | 264 | objects[nr].mode = mode; |
1f1e895f LT |
265 | array->nr = ++nr; |
266 | } |