Merge branch 'jg/mergetool'
[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 struct object **obj_hash;
9 static int nr_objs, obj_hash_size;
10
11 unsigned int get_max_object_index(void)
12 {
13         return obj_hash_size;
14 }
15
16 struct object *get_indexed_object(unsigned int idx)
17 {
18         return obj_hash[idx];
19 }
20
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 */
27 };
28
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
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
64 static int hashtable_index(const unsigned char *sha1)
65 {
66         unsigned int i;
67         memcpy(&i, sha1, sizeof(unsigned int));
68         return (int)(i % obj_hash_size);
69 }
70
71 struct object *lookup_object(const unsigned char *sha1)
72 {
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) {
81                 if (!hashcmp(sha1, obj->sha1))
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
96         new_hash = xcalloc(new_hash_size, sizeof(struct object *));
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;
106 }
107
108 void *create_object(const unsigned char *sha1, int type, void *o)
109 {
110         struct object *obj = o;
111
112         obj->parsed = 0;
113         obj->used = 0;
114         obj->type = type;
115         obj->flags = 0;
116         hashcpy(obj->sha1, sha1);
117
118         if (obj_hash_size - 1 <= nr_objs * 2)
119                 grow_object_hash();
120
121         insert_obj_hash(obj, obj_hash, obj_hash_size);
122         nr_objs++;
123         return obj;
124 }
125
126 struct object *lookup_unknown_object(const unsigned char *sha1)
127 {
128         struct object *obj = lookup_object(sha1);
129         if (!obj)
130                 obj = create_object(sha1, OBJ_NONE, alloc_object_node());
131         return obj;
132 }
133
134 struct object *parse_object_buffer(const unsigned char *sha1, enum object_type type, unsigned long size, void *buffer, int *eaten_p)
135 {
136         struct object *obj;
137         int eaten = 0;
138
139         obj = NULL;
140         if (type == OBJ_BLOB) {
141                 struct blob *blob = lookup_blob(sha1);
142                 if (blob) {
143                         if (parse_blob_buffer(blob, buffer, size))
144                                 return NULL;
145                         obj = &blob->object;
146                 }
147         } else if (type == OBJ_TREE) {
148                 struct tree *tree = lookup_tree(sha1);
149                 if (tree) {
150                         obj = &tree->object;
151                         if (!tree->object.parsed) {
152                                 if (parse_tree_buffer(tree, buffer, size))
153                                         return NULL;
154                                 eaten = 1;
155                         }
156                 }
157         } else if (type == OBJ_COMMIT) {
158                 struct commit *commit = lookup_commit(sha1);
159                 if (commit) {
160                         if (parse_commit_buffer(commit, buffer, size))
161                                 return NULL;
162                         if (!commit->buffer) {
163                                 commit->buffer = buffer;
164                                 eaten = 1;
165                         }
166                         obj = &commit->object;
167                 }
168         } else if (type == OBJ_TAG) {
169                 struct tag *tag = lookup_tag(sha1);
170                 if (tag) {
171                         if (parse_tag_buffer(tag, buffer, size))
172                                return NULL;
173                         obj = &tag->object;
174                 }
175         } else {
176                 warning("object %s has unknown type id %d\n", sha1_to_hex(sha1), type);
177                 obj = NULL;
178         }
179         if (obj && obj->type == OBJ_NONE)
180                 obj->type = type;
181         *eaten_p = eaten;
182         return obj;
183 }
184
185 struct object *parse_object(const unsigned char *sha1)
186 {
187         unsigned long size;
188         enum object_type type;
189         int eaten;
190         void *buffer = read_sha1_file(sha1, &type, &size);
191
192         if (buffer) {
193                 struct object *obj;
194                 if (check_sha1_signature(sha1, buffer, size, typename(type)) < 0) {
195                         free(buffer);
196                         error("sha1 mismatch %s\n", sha1_to_hex(sha1));
197                         return NULL;
198                 }
199
200                 obj = parse_object_buffer(sha1, type, size, buffer, &eaten);
201                 if (!eaten)
202                         free(buffer);
203                 return obj;
204         }
205         return NULL;
206 }
207
208 struct object_list *object_list_insert(struct object *item,
209                                        struct object_list **list_p)
210 {
211         struct object_list *new_list = xmalloc(sizeof(struct object_list));
212         new_list->item = item;
213         new_list->next = *list_p;
214         *list_p = new_list;
215         return new_list;
216 }
217
218 void object_list_append(struct object *item,
219                         struct object_list **list_p)
220 {
221         while (*list_p) {
222                 list_p = &((*list_p)->next);
223         }
224         *list_p = xmalloc(sizeof(struct object_list));
225         (*list_p)->next = NULL;
226         (*list_p)->item = item;
227 }
228
229 unsigned object_list_length(struct object_list *list)
230 {
231         unsigned ret = 0;
232         while (list) {
233                 list = list->next;
234                 ret++;
235         }
236         return ret;
237 }
238
239 int object_list_contains(struct object_list *list, struct object *obj)
240 {
241         while (list) {
242                 if (list->item == obj)
243                         return 1;
244                 list = list->next;
245         }
246         return 0;
247 }
248
249 void add_object_array(struct object *obj, const char *name, struct object_array *array)
250 {
251         add_object_array_with_mode(obj, name, array, S_IFINVALID);
252 }
253
254 void add_object_array_with_mode(struct object *obj, const char *name, struct object_array *array, unsigned mode)
255 {
256         unsigned nr = array->nr;
257         unsigned alloc = array->alloc;
258         struct object_array_entry *objects = array->objects;
259
260         if (nr >= alloc) {
261                 alloc = (alloc + 32) * 2;
262                 objects = xrealloc(objects, alloc * sizeof(*objects));
263                 array->alloc = alloc;
264                 array->objects = objects;
265         }
266         objects[nr].item = obj;
267         objects[nr].name = name;
268         objects[nr].mode = mode;
269         array->nr = ++nr;
270 }
271
272 void object_array_remove_duplicates(struct object_array *array)
273 {
274         int ref, src, dst;
275         struct object_array_entry *objects = array->objects;
276
277         for (ref = 0; ref < array->nr - 1; ref++) {
278                 for (src = ref + 1, dst = src;
279                      src < array->nr;
280                      src++) {
281                         if (!strcmp(objects[ref].name, objects[src].name))
282                                 continue;
283                         if (src != dst)
284                                 objects[dst] = objects[src];
285                         dst++;
286                 }
287                 array->nr = dst;
288         }
289 }