Raw
1 #define DISABLE_SIGN_COMPARE_WARNINGS
2
3 #include "git-compat-util.h"
4 #include "gettext.h"
5 #include "hex.h"
6 #include "object.h"
7 #include "replace-object.h"
8 #include "object-file.h"
9 #include "odb/streaming.h"
10 #include "blob.h"
11 #include "statinfo.h"
12 #include "tree.h"
13 #include "commit.h"
14 #include "tag.h"
15 #include "alloc.h"
16 #include "commit-graph.h"
17
18 unsigned int get_max_object_index(const struct repository *repo)
19 {
20 return repo->parsed_objects->obj_hash_size;
21 }
22
23 struct object *get_indexed_object(const struct repository *repo,
24 unsigned int idx)
25 {
26 return repo->parsed_objects->obj_hash[idx];
27 }
28
29 static const char *object_type_strings[] = {
30 NULL, /* OBJ_NONE = 0 */
31 "commit", /* OBJ_COMMIT = 1 */
32 "tree", /* OBJ_TREE = 2 */
33 "blob", /* OBJ_BLOB = 3 */
34 "tag", /* OBJ_TAG = 4 */
35 };
36
37 const char *type_name(unsigned int type)
38 {
39 if (type >= ARRAY_SIZE(object_type_strings))
40 return NULL;
41 return object_type_strings[type];
42 }
43
44 int type_from_string_gently(const char *str, ssize_t len, int gentle)
45 {
46 int i;
47
48 if (len < 0)
49 len = strlen(str);
50
51 for (i = 1; i < ARRAY_SIZE(object_type_strings); i++)
52 if (!xstrncmpz(object_type_strings[i], str, len))
53 return i;
54
55 if (gentle)
56 return -1;
57
58 die(_("invalid object type \"%s\""), str);
59 }
60
61 /*
62 * Return a numerical hash value between 0 and n-1 for the object with
63 * the specified sha1. n must be a power of 2. Please note that the
64 * return value is *not* consistent across computer architectures.
65 */
66 static unsigned int hash_obj(const struct object_id *oid, unsigned int n)
67 {
68 return oidhash(oid) & (n - 1);
69 }
70
71 /*
72 * Insert obj into the hash table hash, which has length size (which
73 * must be a power of 2). On collisions, simply overflow to the next
74 * empty bucket.
75 */
76 static void insert_obj_hash(struct object *obj, struct object **hash, unsigned int size)
77 {
78 unsigned int j = hash_obj(&obj->oid, size);
79
80 while (hash[j]) {
81 j++;
82 if (j >= size)
83 j = 0;
84 }
85 hash[j] = obj;
86 }
87
88 /*
89 * Look up the record for the given sha1 in the hash map stored in
90 * obj_hash. Return NULL if it was not found.
91 */
92 struct object *lookup_object(struct repository *r, const struct object_id *oid)
93 {
94 unsigned int i, first;
95 struct object *obj;
96
97 if (!r->parsed_objects->obj_hash)
98 return NULL;
99
100 first = i = hash_obj(oid, r->parsed_objects->obj_hash_size);
101 while ((obj = r->parsed_objects->obj_hash[i]) != NULL) {
102 if (oideq(oid, &obj->oid))
103 break;
104 i++;
105 if (i == r->parsed_objects->obj_hash_size)
106 i = 0;
107 }
108 if (obj && i != first) {
109 /*
110 * Move object to where we started to look for it so
111 * that we do not need to walk the hash table the next
112 * time we look for it.
113 */
114 SWAP(r->parsed_objects->obj_hash[i],
115 r->parsed_objects->obj_hash[first]);
116 }
117 return obj;
118 }
119
120 /*
121 * Increase the size of the hash map stored in obj_hash to the next
122 * power of 2 (but at least 32). Copy the existing values to the new
123 * hash map.
124 */
125 static void grow_object_hash(struct repository *r)
126 {
127 int i;
128 /*
129 * Note that this size must always be power-of-2 to match hash_obj
130 * above.
131 */
132 int new_hash_size = r->parsed_objects->obj_hash_size < 32 ? 32 : 2 * r->parsed_objects->obj_hash_size;
133 struct object **new_hash;
134
135 CALLOC_ARRAY(new_hash, new_hash_size);
136 for (i = 0; i < r->parsed_objects->obj_hash_size; i++) {
137 struct object *obj = r->parsed_objects->obj_hash[i];
138
139 if (!obj)
140 continue;
141 insert_obj_hash(obj, new_hash, new_hash_size);
142 }
143 free(r->parsed_objects->obj_hash);
144 r->parsed_objects->obj_hash = new_hash;
145 r->parsed_objects->obj_hash_size = new_hash_size;
146 }
147
148 void *create_object(struct repository *r, const struct object_id *oid, void *o)
149 {
150 struct object *obj = o;
151
152 obj->parsed = 0;
153 obj->flags = 0;
154 oidcpy(&obj->oid, oid);
155
156 if (r->parsed_objects->obj_hash_size - 1 <= r->parsed_objects->nr_objs * 2)
157 grow_object_hash(r);
158
159 insert_obj_hash(obj, r->parsed_objects->obj_hash,
160 r->parsed_objects->obj_hash_size);
161 r->parsed_objects->nr_objs++;
162 return obj;
163 }
164
165 void *object_as_type(struct object *obj, enum object_type type, int quiet)
166 {
167 if (obj->type == type)
168 return obj;
169 else if (obj->type == OBJ_NONE) {
170 if (type == OBJ_COMMIT)
171 init_commit_node((struct commit *) obj);
172 else
173 obj->type = type;
174 return obj;
175 }
176 else {
177 if (!quiet)
178 error(_("object %s is a %s, not a %s"),
179 oid_to_hex(&obj->oid),
180 type_name(obj->type), type_name(type));
181 return NULL;
182 }
183 }
184
185 struct object *lookup_unknown_object(struct repository *r, const struct object_id *oid)
186 {
187 struct object *obj = lookup_object(r, oid);
188 if (!obj)
189 obj = create_object(r, oid, alloc_object_node(r));
190 return obj;
191 }
192
193 struct object *lookup_object_by_type(struct repository *r,
194 const struct object_id *oid,
195 enum object_type type)
196 {
197 switch (type) {
198 case OBJ_COMMIT:
199 return (struct object *)lookup_commit(r, oid);
200 case OBJ_TREE:
201 return (struct object *)lookup_tree(r, oid);
202 case OBJ_TAG:
203 return (struct object *)lookup_tag(r, oid);
204 case OBJ_BLOB:
205 return (struct object *)lookup_blob(r, oid);
206 default:
207 BUG("unknown object type %d", type);
208 }
209 }
210
211 enum peel_status peel_object_ext(struct repository *r,
212 const struct object_id *name,
213 struct object_id *oid,
214 unsigned flags,
215 enum object_type *typep)
216 {
217 struct object *o = lookup_unknown_object(r, name);
218
219 if (o->type == OBJ_NONE) {
220 int type = odb_read_object_info(r->objects, name, NULL);
221 if (type < 0 || !object_as_type(o, type, 0))
222 return PEEL_INVALID;
223 }
224
225 if (o->type != OBJ_TAG) {
226 *typep = o->type;
227 return PEEL_NON_TAG;
228 }
229
230 while (o && o->type == OBJ_TAG) {
231 o = parse_object(r, &o->oid);
232 if (o && o->type == OBJ_TAG && ((struct tag *)o)->tagged) {
233 o = ((struct tag *)o)->tagged;
234
235 if (flags & PEEL_OBJECT_VERIFY_TAGGED_OBJECT_TYPE) {
236 int type = odb_read_object_info(r->objects, &o->oid, NULL);
237 if (type < 0 || !object_as_type(o, type, 0))
238 return PEEL_INVALID;
239 }
240 } else {
241 o = NULL;
242 }
243 }
244 if (!o)
245 return PEEL_INVALID;
246
247 oidcpy(oid, &o->oid);
248 *typep = o->type;
249 return PEEL_PEELED;
250 }
251
252 enum peel_status peel_object(struct repository *r,
253 const struct object_id *name,
254 struct object_id *oid,
255 unsigned flags)
256 {
257 enum object_type dummy;
258 return peel_object_ext(r, name, oid, flags, &dummy);
259 }
260
261 struct object *parse_object_buffer(struct repository *r, const struct object_id *oid, enum object_type type, unsigned long size, void *buffer, int *eaten_p)
262 {
263 struct object *obj;
264 *eaten_p = 0;
265
266 obj = NULL;
267 if (type == OBJ_BLOB) {
268 struct blob *blob = lookup_blob(r, oid);
269 if (blob) {
270 parse_blob_buffer(blob);
271 obj = &blob->object;
272 }
273 } else if (type == OBJ_TREE) {
274 struct tree *tree = lookup_tree(r, oid);
275 if (tree) {
276 obj = &tree->object;
277 if (!tree->buffer)
278 tree->object.parsed = 0;
279 if (!tree->object.parsed) {
280 if (parse_tree_buffer(tree, buffer, size))
281 return NULL;
282 *eaten_p = 1;
283 }
284 }
285 } else if (type == OBJ_COMMIT) {
286 struct commit *commit = lookup_commit(r, oid);
287 if (commit) {
288 if (parse_commit_buffer(r, commit, buffer, size, 1))
289 return NULL;
290 if (save_commit_buffer &&
291 !get_cached_commit_buffer(r, commit, NULL)) {
292 set_commit_buffer(r, commit, buffer, size);
293 *eaten_p = 1;
294 }
295 obj = &commit->object;
296 }
297 } else if (type == OBJ_TAG) {
298 struct tag *tag = lookup_tag(r, oid);
299 if (tag) {
300 if (parse_tag_buffer(r, tag, buffer, size))
301 return NULL;
302 obj = &tag->object;
303 }
304 } else {
305 warning(_("object %s has unknown type id %d"), oid_to_hex(oid), type);
306 obj = NULL;
307 }
308 return obj;
309 }
310
311 struct object *parse_object_or_die(struct repository *repo,
312 const struct object_id *oid,
313 const char *name)
314 {
315 struct object *o = parse_object(repo, oid);
316 if (o)
317 return o;
318
319 die(_("unable to parse object: %s"), name ? name : oid_to_hex(oid));
320 }
321
322 struct object *parse_object_with_flags(struct repository *r,
323 const struct object_id *oid,
324 enum parse_object_flags flags)
325 {
326 int skip_hash = !!(flags & PARSE_OBJECT_SKIP_HASH_CHECK);
327 int discard_tree = !!(flags & PARSE_OBJECT_DISCARD_TREE);
328 size_t size;
329 enum object_type type;
330 int eaten;
331 const struct object_id *repl = lookup_replace_object(r, oid);
332 void *buffer;
333 struct object *obj;
334
335 obj = lookup_object(r, oid);
336 if (obj && obj->parsed)
337 return obj;
338
339 if (skip_hash) {
340 struct commit *commit = lookup_commit_in_graph(r, repl);
341 if (commit)
342 return &commit->object;
343 }
344
345 if ((!obj || obj->type == OBJ_NONE || obj->type == OBJ_BLOB) &&
346 odb_read_object_info(r->objects, oid, NULL) == OBJ_BLOB) {
347 if (!skip_hash) {
348 struct odb_read_stream *stream = odb_read_stream_open(r->objects, oid, NULL);
349
350 if (!stream) {
351 error(_("unable to open object stream for %s"), oid_to_hex(oid));
352 return NULL;
353 }
354
355 if (stream_object_signature(r, stream, repl) < 0) {
356 error(_("hash mismatch %s"), oid_to_hex(oid));
357 odb_read_stream_close(stream);
358 return NULL;
359 }
360
361 odb_read_stream_close(stream);
362 }
363 parse_blob_buffer(lookup_blob(r, oid));
364 return lookup_object(r, oid);
365 }
366
367 /*
368 * If the caller does not care about the tree buffer and does not
369 * care about checking the hash, we can simply verify that we
370 * have the on-disk object with the correct type.
371 */
372 if (skip_hash && discard_tree &&
373 (!obj || obj->type == OBJ_NONE || obj->type == OBJ_TREE) &&
374 odb_read_object_info(r->objects, oid, NULL) == OBJ_TREE) {
375 return &lookup_tree(r, oid)->object;
376 }
377
378 buffer = odb_read_object(r->objects, oid, &type, &size);
379 if (buffer) {
380 if (!skip_hash &&
381 check_object_signature(r, repl, buffer, size, type) < 0) {
382 free(buffer);
383 error(_("hash mismatch %s"), oid_to_hex(repl));
384 return NULL;
385 }
386
387 obj = parse_object_buffer(r, oid, type, size,
388 buffer, &eaten);
389 if (!eaten)
390 free(buffer);
391 if (discard_tree && type == OBJ_TREE)
392 free_tree_buffer((struct tree *)obj);
393 return obj;
394 }
395 return NULL;
396 }
397
398 struct object *parse_object(struct repository *r, const struct object_id *oid)
399 {
400 return parse_object_with_flags(r, oid, 0);
401 }
402
403 struct object_list *object_list_insert(struct object *item,
404 struct object_list **list_p)
405 {
406 struct object_list *new_list = xmalloc(sizeof(struct object_list));
407 new_list->item = item;
408 new_list->next = *list_p;
409 *list_p = new_list;
410 return new_list;
411 }
412
413 int object_list_contains(struct object_list *list, struct object *obj)
414 {
415 while (list) {
416 if (list->item == obj)
417 return 1;
418 list = list->next;
419 }
420 return 0;
421 }
422
423 void object_list_free(struct object_list **list)
424 {
425 while (*list) {
426 struct object_list *p = *list;
427 *list = p->next;
428 free(p);
429 }
430 }
431
432 /*
433 * A zero-length string to which object_array_entry::name can be
434 * initialized without requiring a malloc/free.
435 */
436 static char object_array_slopbuf[1];
437
438 void object_array_init(struct object_array *array)
439 {
440 struct object_array blank = OBJECT_ARRAY_INIT;
441 memcpy(array, &blank, sizeof(*array));
442 }
443
444 void add_object_array_with_path(struct object *obj, const char *name,
445 struct object_array *array,
446 unsigned mode, const char *path)
447 {
448 unsigned nr = array->nr;
449 unsigned alloc = array->alloc;
450 struct object_array_entry *objects = array->objects;
451 struct object_array_entry *entry;
452
453 if (nr >= alloc) {
454 alloc = (alloc + 32) * 2;
455 REALLOC_ARRAY(objects, alloc);
456 array->alloc = alloc;
457 array->objects = objects;
458 }
459 entry = &objects[nr];
460 entry->item = obj;
461 if (!name)
462 entry->name = NULL;
463 else if (!*name)
464 /* Use our own empty string instead of allocating one: */
465 entry->name = object_array_slopbuf;
466 else
467 entry->name = xstrdup(name);
468 entry->mode = mode;
469 if (path)
470 entry->path = xstrdup(path);
471 else
472 entry->path = NULL;
473 array->nr = ++nr;
474 }
475
476 void add_object_array(struct object *obj, const char *name, struct object_array *array)
477 {
478 add_object_array_with_path(obj, name, array, S_IFINVALID, NULL);
479 }
480
481 /*
482 * Free all memory associated with an entry; the result is
483 * in an unspecified state and should not be examined.
484 */
485 static void object_array_release_entry(struct object_array_entry *ent)
486 {
487 if (ent->name != object_array_slopbuf)
488 free(ent->name);
489 free(ent->path);
490 }
491
492 struct object *object_array_pop(struct object_array *array)
493 {
494 struct object *ret;
495
496 if (!array->nr)
497 return NULL;
498
499 ret = array->objects[array->nr - 1].item;
500 object_array_release_entry(&array->objects[array->nr - 1]);
501 array->nr--;
502 return ret;
503 }
504
505 void object_array_filter(struct object_array *array,
506 object_array_each_func_t want, void *cb_data)
507 {
508 unsigned nr = array->nr, src, dst;
509 struct object_array_entry *objects = array->objects;
510
511 for (src = dst = 0; src < nr; src++) {
512 if (want(&objects[src], cb_data)) {
513 if (src != dst)
514 objects[dst] = objects[src];
515 dst++;
516 } else {
517 object_array_release_entry(&objects[src]);
518 }
519 }
520 array->nr = dst;
521 }
522
523 void object_array_clear(struct object_array *array)
524 {
525 int i;
526 for (i = 0; i < array->nr; i++)
527 object_array_release_entry(&array->objects[i]);
528 FREE_AND_NULL(array->objects);
529 array->nr = array->alloc = 0;
530 }
531
532 void clear_object_flags(struct repository *repo, unsigned flags)
533 {
534 int i;
535
536 for (i = 0; i < repo->parsed_objects->obj_hash_size; i++) {
537 struct object *obj = repo->parsed_objects->obj_hash[i];
538 if (obj)
539 obj->flags &= ~flags;
540 }
541 }
542
543 void repo_clear_commit_marks(struct repository *r, unsigned int flags)
544 {
545 int i;
546
547 for (i = 0; i < r->parsed_objects->obj_hash_size; i++) {
548 struct object *obj = r->parsed_objects->obj_hash[i];
549 if (obj && obj->type == OBJ_COMMIT)
550 obj->flags &= ~flags;
551 }
552 }
553
554 struct parsed_object_pool *parsed_object_pool_new(struct repository *repo)
555 {
556 struct parsed_object_pool *o = xmalloc(sizeof(*o));
557 memset(o, 0, sizeof(*o));
558
559 o->repo = repo;
560 o->blob_state = alloc_state_alloc();
561 o->tree_state = alloc_state_alloc();
562 o->commit_state = alloc_state_alloc();
563 o->tag_state = alloc_state_alloc();
564 o->object_state = alloc_state_alloc();
565 o->is_shallow = -1;
566 CALLOC_ARRAY(o->shallow_stat, 1);
567
568 o->buffer_slab = allocate_commit_buffer_slab();
569
570 return o;
571 }
572
573 void parsed_object_pool_reset_commit_grafts(struct parsed_object_pool *o)
574 {
575 for (int i = 0; i < o->grafts_nr; i++) {
576 unparse_commit(o->repo, &o->grafts[i]->oid);
577 free(o->grafts[i]);
578 }
579 o->grafts_nr = 0;
580 o->commit_graft_prepared = 0;
581 }
582
583 void parsed_object_pool_clear(struct parsed_object_pool *o)
584 {
585 /*
586 * As objects are allocated in slabs (see alloc.c), we do
587 * not need to free each object, but each slab instead.
588 *
589 * Before doing so, we need to free any additional memory
590 * the objects may hold.
591 */
592 unsigned i;
593
594 for (i = 0; i < o->obj_hash_size; i++) {
595 struct object *obj = o->obj_hash[i];
596
597 if (!obj)
598 continue;
599
600 if (obj->type == OBJ_TREE)
601 free_tree_buffer((struct tree*)obj);
602 else if (obj->type == OBJ_COMMIT)
603 release_commit_memory(o, (struct commit*)obj);
604 else if (obj->type == OBJ_TAG)
605 release_tag_memory((struct tag*)obj);
606 }
607
608 FREE_AND_NULL(o->obj_hash);
609 o->obj_hash_size = 0;
610
611 free_commit_buffer_slab(o->buffer_slab);
612 o->buffer_slab = NULL;
613
614 parsed_object_pool_reset_commit_grafts(o);
615 alloc_state_free_and_null(&o->blob_state);
616 alloc_state_free_and_null(&o->tree_state);
617 alloc_state_free_and_null(&o->commit_state);
618 alloc_state_free_and_null(&o->tag_state);
619 alloc_state_free_and_null(&o->object_state);
620 stat_validity_clear(o->shallow_stat);
621 FREE_AND_NULL(o->shallow_stat);
622 }