FIXME: hotpatch for compatibility with latest hg
[git] / object.c
1 #include "cache.h"
2 #include "object.h"
3 #include "blob.h"
4 #include "tree.h"
5 #include "commit.h"
6 #include "tag.h"
7
8 static const char *object_type_strings[] = {
9         NULL,           /* OBJ_NONE = 0 */
10         "commit",       /* OBJ_COMMIT = 1 */
11         "tree",         /* OBJ_TREE = 2 */
12         "blob",         /* OBJ_BLOB = 3 */
13         "tag",          /* OBJ_TAG = 4 */
14 };
15
16 const char *typename(unsigned int type)
17 {
18         if (type >= ARRAY_SIZE(object_type_strings))
19                 return NULL;
20         return object_type_strings[type];
21 }
22
23 int type_from_string(const char *str)
24 {
25         int i;
26
27         for (i = 1; i < ARRAY_SIZE(object_type_strings); i++)
28                 if (!strcmp(str, object_type_strings[i]))
29                         return i;
30         die("invalid object type \"%s\"", str);
31 }
32
33 static struct object **obj_hash;
34 static int nr_objs, obj_hash_size;
35
36 unsigned int get_max_object_index(void)
37 {
38         return obj_hash_size;
39 }
40
41 struct object *get_indexed_object(unsigned int idx)
42 {
43         return obj_hash[idx];
44 }
45
46
47 /* Choose from 2, 3, 4 or 5 */
48 #define CUCKOO_FACTOR 4
49
50 #define H(hv,ix) ((hv[ix]) & (obj_hash_size-1))
51
52 struct object *lookup_object(const unsigned char *sha1)
53 {
54         struct object *obj;
55         const unsigned int *hashval;
56
57         if (!obj_hash)
58                 return NULL;
59
60         hashval = (const unsigned int *)sha1;
61         if ((obj = obj_hash[H(hashval, 0)]) && !hashcmp(sha1, obj->sha1))
62                 return obj;
63         if ((obj = obj_hash[H(hashval, 1)]) && !hashcmp(sha1, obj->sha1))
64                 return obj;
65 #if CUCKOO_FACTOR >= 3
66         if ((obj = obj_hash[H(hashval, 2)]) && !hashcmp(sha1, obj->sha1))
67                 return obj;
68 #endif
69 #if CUCKOO_FACTOR >= 4
70         if ((obj = obj_hash[H(hashval, 3)]) && !hashcmp(sha1, obj->sha1))
71                 return obj;
72 #endif
73 #if CUCKOO_FACTOR >= 5
74         if ((obj = obj_hash[H(hashval, 4)]) && !hashcmp(sha1, obj->sha1))
75                 return obj;
76 #endif
77         return NULL;
78 }
79
80 static void grow_object_hash(void); /* forward */
81
82 /*
83  * A naive single-table cuckoo hashing implementation.
84  * Return NULL when "obj" is successfully inserted. Otherwise
85  * return a pointer to the object to be inserted (which may
86  * be different from the original obj). The caller is expected
87  * to grow the hash table and re-insert the returned object.
88  */
89 static struct object *insert_obj_hash(struct object *obj)
90 {
91         int loop;
92
93         for (loop = obj_hash_size - obj_hash_size / 8; 0 <= loop; loop--) {
94                 struct object *tmp_obj;
95                 unsigned int ix, i0;
96                 const unsigned int *hashval;
97
98                 hashval = (const unsigned int *)(obj->sha1);
99                 i0 = ix = H(hashval, 0);
100                 tmp_obj = obj_hash[i0];
101                 if (!tmp_obj) {
102                         obj_hash[ix] = obj;
103                         return NULL;
104                 }
105                 ix = H(hashval, 1);
106                 if (!obj_hash[ix]) {
107                         obj_hash[ix] = obj;
108                         return NULL;
109                 }
110 #if CUCKOO_FACTOR >= 3
111                 ix = H(hashval, 2);
112                 if (!obj_hash[ix]) {
113                         obj_hash[ix] = obj;
114                         return NULL;
115                 }
116 #endif
117 #if CUCKOO_FACTOR >= 4
118                 ix = H(hashval, 3);
119                 if (!obj_hash[ix]) {
120                         obj_hash[ix] = obj;
121                         return NULL;
122                 }
123 #endif
124 #if CUCKOO_FACTOR >= 5
125                 ix = H(hashval, 4);
126                 if (!obj_hash[ix]) {
127                         obj_hash[ix] = obj;
128                         return NULL;
129                 }
130 #endif
131                 obj_hash[i0] = obj;
132                 obj = tmp_obj;
133         }
134         return obj;
135 }
136
137 static int next_size(int sz)
138 {
139         return (sz < 32 ? 32 :
140                 (sz < 1024 * 1024 ? 8 : 2) * sz);
141 }
142
143 static void grow_object_hash(void)
144 {
145         struct object **current_hash;
146         int current_size;
147
148         current_hash = obj_hash;
149         current_size = obj_hash_size;
150         while (1) {
151                 int i;
152                 obj_hash_size = next_size(obj_hash_size);
153                 obj_hash = xcalloc(obj_hash_size, sizeof(struct object *));
154
155                 for (i = 0; i < current_size; i++) {
156                         if (!current_hash[i])
157                                 continue;
158                         if (insert_obj_hash(current_hash[i]))
159                                 break;
160                 }
161                 if (i < current_size) {
162                         /* too small - grow and retry */
163                         free(obj_hash);
164                         continue;
165                 }
166                 free(current_hash);
167                 return;
168         }
169 }
170
171 void *create_object(const unsigned char *sha1, int type, void *o)
172 {
173         struct object *obj = o;
174         struct object *to_insert;
175
176         obj->parsed = 0;
177         obj->used = 0;
178         obj->type = type;
179         obj->flags = 0;
180         hashcpy(obj->sha1, sha1);
181
182         if (!obj_hash_size)
183                 grow_object_hash();
184
185         to_insert = obj;
186         while (1) {
187                 to_insert = insert_obj_hash(to_insert);
188                 if (!to_insert)
189                         break;
190                 grow_object_hash();
191         }
192         nr_objs++;
193         return obj;
194 }
195
196 struct object *lookup_unknown_object(const unsigned char *sha1)
197 {
198         struct object *obj = lookup_object(sha1);
199         if (!obj)
200                 obj = create_object(sha1, OBJ_NONE, alloc_object_node());
201         return obj;
202 }
203
204 struct object *parse_object_buffer(const unsigned char *sha1, enum object_type type, unsigned long size, void *buffer, int *eaten_p)
205 {
206         struct object *obj;
207         int eaten = 0;
208
209         obj = NULL;
210         if (type == OBJ_BLOB) {
211                 struct blob *blob = lookup_blob(sha1);
212                 if (blob) {
213                         if (parse_blob_buffer(blob, buffer, size))
214                                 return NULL;
215                         obj = &blob->object;
216                 }
217         } else if (type == OBJ_TREE) {
218                 struct tree *tree = lookup_tree(sha1);
219                 if (tree) {
220                         obj = &tree->object;
221                         if (!tree->object.parsed) {
222                                 if (parse_tree_buffer(tree, buffer, size))
223                                         return NULL;
224                                 eaten = 1;
225                         }
226                 }
227         } else if (type == OBJ_COMMIT) {
228                 struct commit *commit = lookup_commit(sha1);
229                 if (commit) {
230                         if (parse_commit_buffer(commit, buffer, size))
231                                 return NULL;
232                         if (!commit->buffer) {
233                                 commit->buffer = buffer;
234                                 eaten = 1;
235                         }
236                         obj = &commit->object;
237                 }
238         } else if (type == OBJ_TAG) {
239                 struct tag *tag = lookup_tag(sha1);
240                 if (tag) {
241                         if (parse_tag_buffer(tag, buffer, size))
242                                return NULL;
243                         obj = &tag->object;
244                 }
245         } else {
246                 warning("object %s has unknown type id %d\n", sha1_to_hex(sha1), type);
247                 obj = NULL;
248         }
249         if (obj && obj->type == OBJ_NONE)
250                 obj->type = type;
251         *eaten_p = eaten;
252         return obj;
253 }
254
255 struct object *parse_object(const unsigned char *sha1)
256 {
257         unsigned long size;
258         enum object_type type;
259         int eaten;
260         const unsigned char *repl = lookup_replace_object(sha1);
261         void *buffer = read_sha1_file(sha1, &type, &size);
262
263         if (buffer) {
264                 struct object *obj;
265                 if (check_sha1_signature(repl, buffer, size, typename(type)) < 0) {
266                         free(buffer);
267                         error("sha1 mismatch %s\n", sha1_to_hex(repl));
268                         return NULL;
269                 }
270
271                 obj = parse_object_buffer(sha1, type, size, buffer, &eaten);
272                 if (!eaten)
273                         free(buffer);
274                 return obj;
275         }
276         return NULL;
277 }
278
279 struct object_list *object_list_insert(struct object *item,
280                                        struct object_list **list_p)
281 {
282         struct object_list *new_list = xmalloc(sizeof(struct object_list));
283         new_list->item = item;
284         new_list->next = *list_p;
285         *list_p = new_list;
286         return new_list;
287 }
288
289 int object_list_contains(struct object_list *list, struct object *obj)
290 {
291         while (list) {
292                 if (list->item == obj)
293                         return 1;
294                 list = list->next;
295         }
296         return 0;
297 }
298
299 void add_object_array(struct object *obj, const char *name, struct object_array *array)
300 {
301         add_object_array_with_mode(obj, name, array, S_IFINVALID);
302 }
303
304 void add_object_array_with_mode(struct object *obj, const char *name, struct object_array *array, unsigned mode)
305 {
306         unsigned nr = array->nr;
307         unsigned alloc = array->alloc;
308         struct object_array_entry *objects = array->objects;
309
310         if (nr >= alloc) {
311                 alloc = (alloc + 32) * 2;
312                 objects = xrealloc(objects, alloc * sizeof(*objects));
313                 array->alloc = alloc;
314                 array->objects = objects;
315         }
316         objects[nr].item = obj;
317         objects[nr].name = name;
318         objects[nr].mode = mode;
319         array->nr = ++nr;
320 }
321
322 void object_array_remove_duplicates(struct object_array *array)
323 {
324         unsigned int ref, src, dst;
325         struct object_array_entry *objects = array->objects;
326
327         for (ref = 0; ref + 1 < array->nr; ref++) {
328                 for (src = ref + 1, dst = src;
329                      src < array->nr;
330                      src++) {
331                         if (!strcmp(objects[ref].name, objects[src].name))
332                                 continue;
333                         if (src != dst)
334                                 objects[dst] = objects[src];
335                         dst++;
336                 }
337                 array->nr = dst;
338         }
339 }