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