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