MSVC: Fix build by adding missing termios.h dummy
[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;
49         memcpy(&hash, obj->sha1, sizeof(unsigned int));
50         return hash % n;
51 }
52
53 static void insert_obj_hash(struct object *obj, struct object **hash, unsigned int size)
54 {
55         unsigned int j = hash_obj(obj, size);
56
57         while (hash[j]) {
58                 j++;
59                 if (j >= size)
60                         j = 0;
61         }
62         hash[j] = obj;
63 }
64
65 static unsigned int hashtable_index(const unsigned char *sha1)
66 {
67         unsigned int i;
68         memcpy(&i, sha1, sizeof(unsigned int));
69         return i % obj_hash_size;
70 }
71
72 struct object *lookup_object(const unsigned char *sha1)
73 {
74         unsigned int i;
75         struct object *obj;
76
77         if (!obj_hash)
78                 return NULL;
79
80         i = hashtable_index(sha1);
81         while ((obj = obj_hash[i]) != NULL) {
82                 if (!hashcmp(sha1, obj->sha1))
83                         break;
84                 i++;
85                 if (i == obj_hash_size)
86                         i = 0;
87         }
88         return obj;
89 }
90
91 static void grow_object_hash(void)
92 {
93         int i;
94         int new_hash_size = obj_hash_size < 32 ? 32 : 2 * obj_hash_size;
95         struct object **new_hash;
96
97         new_hash = xcalloc(new_hash_size, sizeof(struct object *));
98         for (i = 0; i < obj_hash_size; i++) {
99                 struct object *obj = obj_hash[i];
100                 if (!obj)
101                         continue;
102                 insert_obj_hash(obj, new_hash, new_hash_size);
103         }
104         free(obj_hash);
105         obj_hash = new_hash;
106         obj_hash_size = new_hash_size;
107 }
108
109 void *create_object(const unsigned char *sha1, int type, void *o)
110 {
111         struct object *obj = o;
112
113         obj->parsed = 0;
114         obj->used = 0;
115         obj->type = type;
116         obj->flags = 0;
117         hashcpy(obj->sha1, sha1);
118
119         if (obj_hash_size - 1 <= nr_objs * 2)
120                 grow_object_hash();
121
122         insert_obj_hash(obj, obj_hash, obj_hash_size);
123         nr_objs++;
124         return obj;
125 }
126
127 struct object *lookup_unknown_object(const unsigned char *sha1)
128 {
129         struct object *obj = lookup_object(sha1);
130         if (!obj)
131                 obj = create_object(sha1, OBJ_NONE, alloc_object_node());
132         return obj;
133 }
134
135 struct object *parse_object_buffer(const unsigned char *sha1, enum object_type type, unsigned long size, void *buffer, int *eaten_p)
136 {
137         struct object *obj;
138         int eaten = 0;
139
140         obj = NULL;
141         if (type == OBJ_BLOB) {
142                 struct blob *blob = lookup_blob(sha1);
143                 if (blob) {
144                         if (parse_blob_buffer(blob, buffer, size))
145                                 return NULL;
146                         obj = &blob->object;
147                 }
148         } else if (type == OBJ_TREE) {
149                 struct tree *tree = lookup_tree(sha1);
150                 if (tree) {
151                         obj = &tree->object;
152                         if (!tree->object.parsed) {
153                                 if (parse_tree_buffer(tree, buffer, size))
154                                         return NULL;
155                                 eaten = 1;
156                         }
157                 }
158         } else if (type == OBJ_COMMIT) {
159                 struct commit *commit = lookup_commit(sha1);
160                 if (commit) {
161                         if (parse_commit_buffer(commit, buffer, size))
162                                 return NULL;
163                         if (!commit->buffer) {
164                                 commit->buffer = buffer;
165                                 eaten = 1;
166                         }
167                         obj = &commit->object;
168                 }
169         } else if (type == OBJ_TAG) {
170                 struct tag *tag = lookup_tag(sha1);
171                 if (tag) {
172                         if (parse_tag_buffer(tag, buffer, size))
173                                return NULL;
174                         obj = &tag->object;
175                 }
176         } else {
177                 warning("object %s has unknown type id %d\n", sha1_to_hex(sha1), type);
178                 obj = NULL;
179         }
180         if (obj && obj->type == OBJ_NONE)
181                 obj->type = type;
182         *eaten_p = eaten;
183         return obj;
184 }
185
186 struct object *parse_object(const unsigned char *sha1)
187 {
188         unsigned long size;
189         enum object_type type;
190         int eaten;
191         const unsigned char *repl;
192         void *buffer = read_sha1_file_repl(sha1, &type, &size, &repl);
193
194         if (buffer) {
195                 struct object *obj;
196                 if (check_sha1_signature(repl, buffer, size, typename(type)) < 0) {
197                         free(buffer);
198                         error("sha1 mismatch %s\n", sha1_to_hex(repl));
199                         return NULL;
200                 }
201
202                 obj = parse_object_buffer(repl, type, size, buffer, &eaten);
203                 if (!eaten)
204                         free(buffer);
205                 return obj;
206         }
207         return NULL;
208 }
209
210 struct object_list *object_list_insert(struct object *item,
211                                        struct object_list **list_p)
212 {
213         struct object_list *new_list = xmalloc(sizeof(struct object_list));
214         new_list->item = item;
215         new_list->next = *list_p;
216         *list_p = new_list;
217         return new_list;
218 }
219
220 void object_list_append(struct object *item,
221                         struct object_list **list_p)
222 {
223         while (*list_p) {
224                 list_p = &((*list_p)->next);
225         }
226         *list_p = xmalloc(sizeof(struct object_list));
227         (*list_p)->next = NULL;
228         (*list_p)->item = item;
229 }
230
231 unsigned object_list_length(struct object_list *list)
232 {
233         unsigned ret = 0;
234         while (list) {
235                 list = list->next;
236                 ret++;
237         }
238         return ret;
239 }
240
241 int object_list_contains(struct object_list *list, struct object *obj)
242 {
243         while (list) {
244                 if (list->item == obj)
245                         return 1;
246                 list = list->next;
247         }
248         return 0;
249 }
250
251 void add_object_array(struct object *obj, const char *name, struct object_array *array)
252 {
253         add_object_array_with_mode(obj, name, array, S_IFINVALID);
254 }
255
256 void add_object_array_with_mode(struct object *obj, const char *name, struct object_array *array, unsigned mode)
257 {
258         unsigned nr = array->nr;
259         unsigned alloc = array->alloc;
260         struct object_array_entry *objects = array->objects;
261
262         if (nr >= alloc) {
263                 alloc = (alloc + 32) * 2;
264                 objects = xrealloc(objects, alloc * sizeof(*objects));
265                 array->alloc = alloc;
266                 array->objects = objects;
267         }
268         objects[nr].item = obj;
269         objects[nr].name = name;
270         objects[nr].mode = mode;
271         array->nr = ++nr;
272 }
273
274 void object_array_remove_duplicates(struct object_array *array)
275 {
276         int ref, src, dst;
277         struct object_array_entry *objects = array->objects;
278
279         for (ref = 0; ref < array->nr - 1; ref++) {
280                 for (src = ref + 1, dst = src;
281                      src < array->nr;
282                      src++) {
283                         if (!strcmp(objects[ref].name, objects[src].name))
284                                 continue;
285                         if (src != dst)
286                                 objects[dst] = objects[src];
287                         dst++;
288                 }
289                 array->nr = dst;
290         }
291 }