]> git.ipfire.org Git - thirdparty/git.git/blame - object.c
Merge branch 'maint-2.1' into maint
[thirdparty/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
fe8e3b71 36int type_from_string_gently(const char *str, ssize_t len, int gentle)
df843662
NP
37{
38 int i;
39
fe8e3b71
JS
40 if (len < 0)
41 len = strlen(str);
42
df843662 43 for (i = 1; i < ARRAY_SIZE(object_type_strings); i++)
fe8e3b71 44 if (!strncmp(str, object_type_strings[i], len))
df843662 45 return i;
fe8e3b71
JS
46
47 if (gentle)
48 return -1;
49
df843662
NP
50 die("invalid object type \"%s\"", str);
51}
52
33bef7ea
MH
53/*
54 * Return a numerical hash value between 0 and n-1 for the object with
55 * the specified sha1. n must be a power of 2. Please note that the
56 * return value is *not* consistent across computer architectures.
57 */
9f36c9b7 58static unsigned int hash_obj(const unsigned char *sha1, unsigned int n)
0556a11a 59{
039dc71a 60 return sha1hash(sha1) & (n - 1);
0556a11a
LT
61}
62
33bef7ea
MH
63/*
64 * Insert obj into the hash table hash, which has length size (which
65 * must be a power of 2). On collisions, simply overflow to the next
66 * empty bucket.
67 */
0556a11a
LT
68static void insert_obj_hash(struct object *obj, struct object **hash, unsigned int size)
69{
9f36c9b7 70 unsigned int j = hash_obj(obj->sha1, size);
0556a11a
LT
71
72 while (hash[j]) {
73 j++;
74 if (j >= size)
75 j = 0;
76 }
77 hash[j] = obj;
78}
79
33bef7ea
MH
80/*
81 * Look up the record for the given sha1 in the hash map stored in
82 * obj_hash. Return NULL if it was not found.
83 */
0556a11a 84struct object *lookup_object(const unsigned char *sha1)
175785e5 85{
9a414486 86 unsigned int i, first;
0556a11a 87 struct object *obj;
175785e5 88
0556a11a
LT
89 if (!obj_hash)
90 return NULL;
175785e5 91
9f36c9b7 92 first = i = hash_obj(sha1, obj_hash_size);
0556a11a 93 while ((obj = obj_hash[i]) != NULL) {
a89fccd2 94 if (!hashcmp(sha1, obj->sha1))
0556a11a 95 break;
070879ca 96 i++;
0556a11a 97 if (i == obj_hash_size)
070879ca
JS
98 i = 0;
99 }
9a414486
JK
100 if (obj && i != first) {
101 /*
102 * Move object to where we started to look for it so
103 * that we do not need to walk the hash table the next
104 * time we look for it.
105 */
106 struct object *tmp = obj_hash[i];
107 obj_hash[i] = obj_hash[first];
108 obj_hash[first] = tmp;
109 }
0556a11a 110 return obj;
175785e5
DB
111}
112
33bef7ea
MH
113/*
114 * Increase the size of the hash map stored in obj_hash to the next
115 * power of 2 (but at least 32). Copy the existing values to the new
116 * hash map.
117 */
0556a11a 118static void grow_object_hash(void)
175785e5 119{
0556a11a 120 int i;
9f36c9b7
NP
121 /*
122 * Note that this size must always be power-of-2 to match hash_obj
123 * above.
124 */
0556a11a
LT
125 int new_hash_size = obj_hash_size < 32 ? 32 : 2 * obj_hash_size;
126 struct object **new_hash;
127
b3c952f8 128 new_hash = xcalloc(new_hash_size, sizeof(struct object *));
0556a11a
LT
129 for (i = 0; i < obj_hash_size; i++) {
130 struct object *obj = obj_hash[i];
131 if (!obj)
132 continue;
133 insert_obj_hash(obj, new_hash, new_hash_size);
134 }
135 free(obj_hash);
136 obj_hash = new_hash;
137 obj_hash_size = new_hash_size;
175785e5
DB
138}
139
d36f51c1 140void *create_object(const unsigned char *sha1, void *o)
175785e5 141{
100c5f3b
LT
142 struct object *obj = o;
143
175785e5 144 obj->parsed = 0;
175785e5 145 obj->used = 0;
0556a11a 146 obj->flags = 0;
e702496e 147 hashcpy(obj->sha1, sha1);
175785e5 148
0556a11a
LT
149 if (obj_hash_size - 1 <= nr_objs * 2)
150 grow_object_hash();
175785e5 151
0556a11a 152 insert_obj_hash(obj, obj_hash, obj_hash_size);
175785e5 153 nr_objs++;
100c5f3b 154 return obj;
175785e5
DB
155}
156
8ff226a9
JK
157void *object_as_type(struct object *obj, enum object_type type, int quiet)
158{
159 if (obj->type == type)
160 return obj;
161 else if (obj->type == OBJ_NONE) {
d66bebcb
JK
162 if (type == OBJ_COMMIT)
163 ((struct commit *)obj)->index = alloc_commit_index();
8ff226a9
JK
164 obj->type = type;
165 return obj;
166 }
167 else {
168 if (!quiet)
169 error("object %s is a %s, not a %s",
170 sha1_to_hex(obj->sha1),
171 typename(obj->type), typename(type));
172 return NULL;
173 }
174}
175
66e481b0
DB
176struct object *lookup_unknown_object(const unsigned char *sha1)
177{
178 struct object *obj = lookup_object(sha1);
100c5f3b 179 if (!obj)
d36f51c1 180 obj = create_object(sha1, alloc_object_node());
66e481b0
DB
181 return obj;
182}
183
21666f1a 184struct object *parse_object_buffer(const unsigned char *sha1, enum object_type type, unsigned long size, void *buffer, int *eaten_p)
9f613ddd
JH
185{
186 struct object *obj;
8e92e8f2 187 *eaten_p = 0;
9f613ddd 188
cc216827 189 obj = NULL;
21666f1a 190 if (type == OBJ_BLOB) {
9f613ddd 191 struct blob *blob = lookup_blob(sha1);
cc216827 192 if (blob) {
d0b8c9e5
MK
193 if (parse_blob_buffer(blob, buffer, size))
194 return NULL;
cc216827
JM
195 obj = &blob->object;
196 }
21666f1a 197 } else if (type == OBJ_TREE) {
9f613ddd 198 struct tree *tree = lookup_tree(sha1);
cc216827
JM
199 if (tree) {
200 obj = &tree->object;
68be2fea
JH
201 if (!tree->buffer)
202 tree->object.parsed = 0;
cc216827 203 if (!tree->object.parsed) {
d0b8c9e5
MK
204 if (parse_tree_buffer(tree, buffer, size))
205 return NULL;
8e92e8f2 206 *eaten_p = 1;
cc216827 207 }
9f613ddd 208 }
21666f1a 209 } else if (type == OBJ_COMMIT) {
9f613ddd 210 struct commit *commit = lookup_commit(sha1);
cc216827 211 if (commit) {
d0b8c9e5
MK
212 if (parse_commit_buffer(commit, buffer, size))
213 return NULL;
8597ea3a
JK
214 if (!get_cached_commit_buffer(commit, NULL)) {
215 set_commit_buffer(commit, buffer, size);
8e92e8f2 216 *eaten_p = 1;
cc216827
JM
217 }
218 obj = &commit->object;
9f613ddd 219 }
21666f1a 220 } else if (type == OBJ_TAG) {
9f613ddd 221 struct tag *tag = lookup_tag(sha1);
cc216827 222 if (tag) {
d0b8c9e5
MK
223 if (parse_tag_buffer(tag, buffer, size))
224 return NULL;
cc216827
JM
225 obj = &tag->object;
226 }
9f613ddd 227 } else {
82247e9b 228 warning("object %s has unknown type id %d", sha1_to_hex(sha1), type);
9f613ddd
JH
229 obj = NULL;
230 }
9f613ddd
JH
231 return obj;
232}
233
75a95490
JK
234struct object *parse_object_or_die(const unsigned char *sha1,
235 const char *name)
236{
237 struct object *o = parse_object(sha1);
238 if (o)
239 return o;
240
241 die(_("unable to parse object: %s"), name ? name : sha1_to_hex(sha1));
242}
243
5d6ccf5c 244struct object *parse_object(const unsigned char *sha1)
e9eefa67 245{
c4584ae3 246 unsigned long size;
21666f1a 247 enum object_type type;
9f613ddd 248 int eaten;
4bbf5a26 249 const unsigned char *repl = lookup_replace_object(sha1);
ccdc6037
JK
250 void *buffer;
251 struct object *obj;
252
253 obj = lookup_object(sha1);
254 if (obj && obj->parsed)
255 return obj;
9f613ddd 256
090ea126
NTND
257 if ((obj && obj->type == OBJ_BLOB) ||
258 (!obj && has_sha1_file(sha1) &&
259 sha1_object_info(sha1, NULL) == OBJ_BLOB)) {
260 if (check_sha1_signature(repl, NULL, 0, NULL) < 0) {
82247e9b 261 error("sha1 mismatch %s", sha1_to_hex(repl));
090ea126
NTND
262 return NULL;
263 }
264 parse_blob_buffer(lookup_blob(sha1), NULL, 0);
265 return lookup_object(sha1);
266 }
267
ccdc6037 268 buffer = read_sha1_file(sha1, &type, &size);
c4584ae3 269 if (buffer) {
0e87c367 270 if (check_sha1_signature(repl, buffer, size, typename(type)) < 0) {
0b1f1130 271 free(buffer);
82247e9b 272 error("sha1 mismatch %s", sha1_to_hex(repl));
acdeec62
LT
273 return NULL;
274 }
9f613ddd 275
2e3400c0 276 obj = parse_object_buffer(sha1, type, size, buffer, &eaten);
9f613ddd
JH
277 if (!eaten)
278 free(buffer);
bd2c39f5 279 return obj;
e9eefa67
DB
280 }
281 return NULL;
282}
66e481b0
DB
283
284struct object_list *object_list_insert(struct object *item,
285 struct object_list **list_p)
286{
287 struct object_list *new_list = xmalloc(sizeof(struct object_list));
55b4e9e4
JH
288 new_list->item = item;
289 new_list->next = *list_p;
290 *list_p = new_list;
291 return new_list;
66e481b0
DB
292}
293
66e481b0
DB
294int object_list_contains(struct object_list *list, struct object *obj)
295{
296 while (list) {
297 if (list->item == obj)
298 return 1;
299 list = list->next;
300 }
301 return 0;
302}
1f1e895f 303
31faeb20
MH
304/*
305 * A zero-length string to which object_array_entry::name can be
306 * initialized without requiring a malloc/free.
307 */
308static char object_array_slopbuf[1];
309
9e0c3c4f
JK
310void add_object_array_with_path(struct object *obj, const char *name,
311 struct object_array *array,
312 unsigned mode, const char *path)
1f1e895f
LT
313{
314 unsigned nr = array->nr;
315 unsigned alloc = array->alloc;
316 struct object_array_entry *objects = array->objects;
31faeb20 317 struct object_array_entry *entry;
1f1e895f
LT
318
319 if (nr >= alloc) {
320 alloc = (alloc + 32) * 2;
2756ca43 321 REALLOC_ARRAY(objects, alloc);
1f1e895f
LT
322 array->alloc = alloc;
323 array->objects = objects;
324 }
31faeb20
MH
325 entry = &objects[nr];
326 entry->item = obj;
327 if (!name)
328 entry->name = NULL;
329 else if (!*name)
330 /* Use our own empty string instead of allocating one: */
331 entry->name = object_array_slopbuf;
332 else
333 entry->name = xstrdup(name);
334 entry->mode = mode;
9e0c3c4f
JK
335 if (path)
336 entry->path = xstrdup(path);
337 else
338 entry->path = NULL;
1f1e895f
LT
339 array->nr = ++nr;
340}
b2a6d1c6 341
afa15f3c
MG
342void add_object_array(struct object *obj, const char *name, struct object_array *array)
343{
189a1222 344 add_object_array_with_path(obj, name, array, S_IFINVALID, NULL);
afa15f3c
MG
345}
346
68f49235
JK
347/*
348 * Free all memory associated with an entry; the result is
349 * in an unspecified state and should not be examined.
350 */
351static void object_array_release_entry(struct object_array_entry *ent)
352{
353 if (ent->name != object_array_slopbuf)
354 free(ent->name);
9e0c3c4f 355 free(ent->path);
68f49235
JK
356}
357
aeb4a51e
MH
358void object_array_filter(struct object_array *array,
359 object_array_each_func_t want, void *cb_data)
b2a6d1c6 360{
aeb4a51e 361 unsigned nr = array->nr, src, dst;
b2a6d1c6
JH
362 struct object_array_entry *objects = array->objects;
363
aeb4a51e
MH
364 for (src = dst = 0; src < nr; src++) {
365 if (want(&objects[src], cb_data)) {
b2a6d1c6
JH
366 if (src != dst)
367 objects[dst] = objects[src];
368 dst++;
31faeb20 369 } else {
68f49235 370 object_array_release_entry(&objects[src]);
aeb4a51e
MH
371 }
372 }
373 array->nr = dst;
374}
375
46be8231
JK
376void object_array_clear(struct object_array *array)
377{
378 int i;
379 for (i = 0; i < array->nr; i++)
380 object_array_release_entry(&array->objects[i]);
381 free(array->objects);
382 array->objects = NULL;
383 array->nr = array->alloc = 0;
384}
385
1506510c
MH
386/*
387 * Return true iff array already contains an entry with name.
388 */
389static int contains_name(struct object_array *array, const char *name)
390{
391 unsigned nr = array->nr, i;
392 struct object_array_entry *object = array->objects;
393
394 for (i = 0; i < nr; i++, object++)
395 if (!strcmp(object->name, name))
396 return 1;
397 return 0;
398}
399
b2a6d1c6
JH
400void object_array_remove_duplicates(struct object_array *array)
401{
1506510c 402 unsigned nr = array->nr, src;
b2a6d1c6
JH
403 struct object_array_entry *objects = array->objects;
404
1506510c
MH
405 array->nr = 0;
406 for (src = 0; src < nr; src++) {
407 if (!contains_name(array, objects[src].name)) {
408 if (src != array->nr)
409 objects[array->nr] = objects[src];
410 array->nr++;
31faeb20 411 } else {
68f49235 412 object_array_release_entry(&objects[src]);
b2a6d1c6 413 }
b2a6d1c6
JH
414 }
415}
bcc0a3ea
HV
416
417void clear_object_flags(unsigned flags)
418{
419 int i;
420
421 for (i=0; i < obj_hash_size; i++) {
422 struct object *obj = obj_hash[i];
423 if (obj)
424 obj->flags &= ~flags;
425 }
426}