git

Форк
0
/
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

20
unsigned int get_max_object_index(void)
21
{
22
	return the_repository->parsed_objects->obj_hash_size;
23
}
24

25
struct object *get_indexed_object(unsigned int idx)
26
{
27
	return the_repository->parsed_objects->obj_hash[idx];
28
}
29

30
static const char *object_type_strings[] = {
31
	NULL,		/* 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

38
const char *type_name(unsigned int type)
39
{
40
	if (type >= ARRAY_SIZE(object_type_strings))
41
		return NULL;
42
	return object_type_strings[type];
43
}
44

45
int type_from_string_gently(const char *str, ssize_t len, int gentle)
46
{
47
	int i;
48

49
	if (len < 0)
50
		len = strlen(str);
51

52
	for (i = 1; i < ARRAY_SIZE(object_type_strings); i++)
53
		if (!xstrncmpz(object_type_strings[i], str, len))
54
			return i;
55

56
	if (gentle)
57
		return -1;
58

59
	die(_("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
 */
67
static unsigned int hash_obj(const struct object_id *oid, unsigned int n)
68
{
69
	return 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
 */
77
static void insert_obj_hash(struct object *obj, struct object **hash, unsigned int size)
78
{
79
	unsigned int j = hash_obj(&obj->oid, size);
80

81
	while (hash[j]) {
82
		j++;
83
		if (j >= size)
84
			j = 0;
85
	}
86
	hash[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
 */
93
struct object *lookup_object(struct repository *r, const struct object_id *oid)
94
{
95
	unsigned int i, first;
96
	struct object *obj;
97

98
	if (!r->parsed_objects->obj_hash)
99
		return NULL;
100

101
	first = i = hash_obj(oid, r->parsed_objects->obj_hash_size);
102
	while ((obj = r->parsed_objects->obj_hash[i]) != NULL) {
103
		if (oideq(oid, &obj->oid))
104
			break;
105
		i++;
106
		if (i == r->parsed_objects->obj_hash_size)
107
			i = 0;
108
	}
109
	if (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
		 */
115
		SWAP(r->parsed_objects->obj_hash[i],
116
		     r->parsed_objects->obj_hash[first]);
117
	}
118
	return 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
 */
126
static void grow_object_hash(struct repository *r)
127
{
128
	int i;
129
	/*
130
	 * Note that this size must always be power-of-2 to match hash_obj
131
	 * above.
132
	 */
133
	int new_hash_size = r->parsed_objects->obj_hash_size < 32 ? 32 : 2 * r->parsed_objects->obj_hash_size;
134
	struct object **new_hash;
135

136
	CALLOC_ARRAY(new_hash, new_hash_size);
137
	for (i = 0; i < r->parsed_objects->obj_hash_size; i++) {
138
		struct object *obj = r->parsed_objects->obj_hash[i];
139

140
		if (!obj)
141
			continue;
142
		insert_obj_hash(obj, new_hash, new_hash_size);
143
	}
144
	free(r->parsed_objects->obj_hash);
145
	r->parsed_objects->obj_hash = new_hash;
146
	r->parsed_objects->obj_hash_size = new_hash_size;
147
}
148

149
void *create_object(struct repository *r, const struct object_id *oid, void *o)
150
{
151
	struct object *obj = o;
152

153
	obj->parsed = 0;
154
	obj->flags = 0;
155
	oidcpy(&obj->oid, oid);
156

157
	if (r->parsed_objects->obj_hash_size - 1 <= r->parsed_objects->nr_objs * 2)
158
		grow_object_hash(r);
159

160
	insert_obj_hash(obj, r->parsed_objects->obj_hash,
161
			r->parsed_objects->obj_hash_size);
162
	r->parsed_objects->nr_objs++;
163
	return obj;
164
}
165

166
void *object_as_type(struct object *obj, enum object_type type, int quiet)
167
{
168
	if (obj->type == type)
169
		return obj;
170
	else if (obj->type == OBJ_NONE) {
171
		if (type == OBJ_COMMIT)
172
			init_commit_node((struct commit *) obj);
173
		else
174
			obj->type = type;
175
		return obj;
176
	}
177
	else {
178
		if (!quiet)
179
			error(_("object %s is a %s, not a %s"),
180
			      oid_to_hex(&obj->oid),
181
			      type_name(obj->type), type_name(type));
182
		return NULL;
183
	}
184
}
185

186
struct object *lookup_unknown_object(struct repository *r, const struct object_id *oid)
187
{
188
	struct object *obj = lookup_object(r, oid);
189
	if (!obj)
190
		obj = create_object(r, oid, alloc_object_node(r));
191
	return obj;
192
}
193

194
struct object *lookup_object_by_type(struct repository *r,
195
			    const struct object_id *oid,
196
			    enum object_type type)
197
{
198
	switch (type) {
199
	case OBJ_COMMIT:
200
		return (struct object *)lookup_commit(r, oid);
201
	case OBJ_TREE:
202
		return (struct object *)lookup_tree(r, oid);
203
	case OBJ_TAG:
204
		return (struct object *)lookup_tag(r, oid);
205
	case OBJ_BLOB:
206
		return (struct object *)lookup_blob(r, oid);
207
	default:
208
		BUG("unknown object type %d", type);
209
	}
210
}
211

212
enum peel_status peel_object(struct repository *r,
213
			     const struct object_id *name,
214
			     struct object_id *oid)
215
{
216
	struct object *o = lookup_unknown_object(r, name);
217

218
	if (o->type == OBJ_NONE) {
219
		int type = oid_object_info(r, name, NULL);
220
		if (type < 0 || !object_as_type(o, type, 0))
221
			return PEEL_INVALID;
222
	}
223

224
	if (o->type != OBJ_TAG)
225
		return PEEL_NON_TAG;
226

227
	o = deref_tag_noverify(r, o);
228
	if (!o)
229
		return PEEL_INVALID;
230

231
	oidcpy(oid, &o->oid);
232
	return PEEL_PEELED;
233
}
234

235
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)
236
{
237
	struct object *obj;
238
	*eaten_p = 0;
239

240
	obj = NULL;
241
	if (type == OBJ_BLOB) {
242
		struct blob *blob = lookup_blob(r, oid);
243
		if (blob) {
244
			parse_blob_buffer(blob);
245
			obj = &blob->object;
246
		}
247
	} else if (type == OBJ_TREE) {
248
		struct tree *tree = lookup_tree(r, oid);
249
		if (tree) {
250
			obj = &tree->object;
251
			if (!tree->buffer)
252
				tree->object.parsed = 0;
253
			if (!tree->object.parsed) {
254
				if (parse_tree_buffer(tree, buffer, size))
255
					return NULL;
256
				*eaten_p = 1;
257
			}
258
		}
259
	} else if (type == OBJ_COMMIT) {
260
		struct commit *commit = lookup_commit(r, oid);
261
		if (commit) {
262
			if (parse_commit_buffer(r, commit, buffer, size, 1))
263
				return NULL;
264
			if (save_commit_buffer &&
265
			    !get_cached_commit_buffer(r, commit, NULL)) {
266
				set_commit_buffer(r, commit, buffer, size);
267
				*eaten_p = 1;
268
			}
269
			obj = &commit->object;
270
		}
271
	} else if (type == OBJ_TAG) {
272
		struct tag *tag = lookup_tag(r, oid);
273
		if (tag) {
274
			if (parse_tag_buffer(r, tag, buffer, size))
275
			       return NULL;
276
			obj = &tag->object;
277
		}
278
	} else {
279
		warning(_("object %s has unknown type id %d"), oid_to_hex(oid), type);
280
		obj = NULL;
281
	}
282
	return obj;
283
}
284

285
struct object *parse_object_or_die(const struct object_id *oid,
286
				   const char *name)
287
{
288
	struct object *o = parse_object(the_repository, oid);
289
	if (o)
290
		return o;
291

292
	die(_("unable to parse object: %s"), name ? name : oid_to_hex(oid));
293
}
294

295
struct object *parse_object_with_flags(struct repository *r,
296
				       const struct object_id *oid,
297
				       enum parse_object_flags flags)
298
{
299
	int skip_hash = !!(flags & PARSE_OBJECT_SKIP_HASH_CHECK);
300
	int discard_tree = !!(flags & PARSE_OBJECT_DISCARD_TREE);
301
	unsigned long size;
302
	enum object_type type;
303
	int eaten;
304
	const struct object_id *repl = lookup_replace_object(r, oid);
305
	void *buffer;
306
	struct object *obj;
307

308
	obj = lookup_object(r, oid);
309
	if (obj && obj->parsed)
310
		return obj;
311

312
	if (skip_hash) {
313
		struct commit *commit = lookup_commit_in_graph(r, repl);
314
		if (commit)
315
			return &commit->object;
316
	}
317

318
	if ((!obj || obj->type == OBJ_BLOB) &&
319
	    oid_object_info(r, oid, NULL) == OBJ_BLOB) {
320
		if (!skip_hash && stream_object_signature(r, repl) < 0) {
321
			error(_("hash mismatch %s"), oid_to_hex(oid));
322
			return NULL;
323
		}
324
		parse_blob_buffer(lookup_blob(r, oid));
325
		return 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
	 */
333
	if (skip_hash && discard_tree &&
334
	    (!obj || obj->type == OBJ_TREE) &&
335
	    oid_object_info(r, oid, NULL) == OBJ_TREE) {
336
		return &lookup_tree(r, oid)->object;
337
	}
338

339
	buffer = repo_read_object_file(r, oid, &type, &size);
340
	if (buffer) {
341
		if (!skip_hash &&
342
		    check_object_signature(r, repl, buffer, size, type) < 0) {
343
			free(buffer);
344
			error(_("hash mismatch %s"), oid_to_hex(repl));
345
			return NULL;
346
		}
347

348
		obj = parse_object_buffer(r, oid, type, size,
349
					  buffer, &eaten);
350
		if (!eaten)
351
			free(buffer);
352
		if (discard_tree && type == OBJ_TREE)
353
			free_tree_buffer((struct tree *)obj);
354
		return obj;
355
	}
356
	return NULL;
357
}
358

359
struct object *parse_object(struct repository *r, const struct object_id *oid)
360
{
361
	return parse_object_with_flags(r, oid, 0);
362
}
363

364
struct object_list *object_list_insert(struct object *item,
365
				       struct object_list **list_p)
366
{
367
	struct object_list *new_list = xmalloc(sizeof(struct object_list));
368
	new_list->item = item;
369
	new_list->next = *list_p;
370
	*list_p = new_list;
371
	return new_list;
372
}
373

374
int object_list_contains(struct object_list *list, struct object *obj)
375
{
376
	while (list) {
377
		if (list->item == obj)
378
			return 1;
379
		list = list->next;
380
	}
381
	return 0;
382
}
383

384
void object_list_free(struct object_list **list)
385
{
386
	while (*list) {
387
		struct object_list *p = *list;
388
		*list = p->next;
389
		free(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
 */
397
static char object_array_slopbuf[1];
398

399
void object_array_init(struct object_array *array)
400
{
401
	struct object_array blank = OBJECT_ARRAY_INIT;
402
	memcpy(array, &blank, sizeof(*array));
403
}
404

405
void add_object_array_with_path(struct object *obj, const char *name,
406
				struct object_array *array,
407
				unsigned mode, const char *path)
408
{
409
	unsigned nr = array->nr;
410
	unsigned alloc = array->alloc;
411
	struct object_array_entry *objects = array->objects;
412
	struct object_array_entry *entry;
413

414
	if (nr >= alloc) {
415
		alloc = (alloc + 32) * 2;
416
		REALLOC_ARRAY(objects, alloc);
417
		array->alloc = alloc;
418
		array->objects = objects;
419
	}
420
	entry = &objects[nr];
421
	entry->item = obj;
422
	if (!name)
423
		entry->name = NULL;
424
	else if (!*name)
425
		/* Use our own empty string instead of allocating one: */
426
		entry->name = object_array_slopbuf;
427
	else
428
		entry->name = xstrdup(name);
429
	entry->mode = mode;
430
	if (path)
431
		entry->path = xstrdup(path);
432
	else
433
		entry->path = NULL;
434
	array->nr = ++nr;
435
}
436

437
void add_object_array(struct object *obj, const char *name, struct object_array *array)
438
{
439
	add_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
 */
446
static void object_array_release_entry(struct object_array_entry *ent)
447
{
448
	if (ent->name != object_array_slopbuf)
449
		free(ent->name);
450
	free(ent->path);
451
}
452

453
struct object *object_array_pop(struct object_array *array)
454
{
455
	struct object *ret;
456

457
	if (!array->nr)
458
		return NULL;
459

460
	ret = array->objects[array->nr - 1].item;
461
	object_array_release_entry(&array->objects[array->nr - 1]);
462
	array->nr--;
463
	return ret;
464
}
465

466
void object_array_filter(struct object_array *array,
467
			 object_array_each_func_t want, void *cb_data)
468
{
469
	unsigned nr = array->nr, src, dst;
470
	struct object_array_entry *objects = array->objects;
471

472
	for (src = dst = 0; src < nr; src++) {
473
		if (want(&objects[src], cb_data)) {
474
			if (src != dst)
475
				objects[dst] = objects[src];
476
			dst++;
477
		} else {
478
			object_array_release_entry(&objects[src]);
479
		}
480
	}
481
	array->nr = dst;
482
}
483

484
void object_array_clear(struct object_array *array)
485
{
486
	int i;
487
	for (i = 0; i < array->nr; i++)
488
		object_array_release_entry(&array->objects[i]);
489
	FREE_AND_NULL(array->objects);
490
	array->nr = array->alloc = 0;
491
}
492

493
/*
494
 * Return true if array already contains an entry.
495
 */
496
static int contains_object(struct object_array *array,
497
			   const struct object *item, const char *name)
498
{
499
	unsigned nr = array->nr, i;
500
	struct object_array_entry *object = array->objects;
501

502
	for (i = 0; i < nr; i++, object++)
503
		if (item == object->item && !strcmp(object->name, name))
504
			return 1;
505
	return 0;
506
}
507

508
void object_array_remove_duplicates(struct object_array *array)
509
{
510
	unsigned nr = array->nr, src;
511
	struct object_array_entry *objects = array->objects;
512

513
	array->nr = 0;
514
	for (src = 0; src < nr; src++) {
515
		if (!contains_object(array, objects[src].item,
516
				     objects[src].name)) {
517
			if (src != array->nr)
518
				objects[array->nr] = objects[src];
519
			array->nr++;
520
		} else {
521
			object_array_release_entry(&objects[src]);
522
		}
523
	}
524
}
525

526
void clear_object_flags(unsigned flags)
527
{
528
	int i;
529

530
	for (i=0; i < the_repository->parsed_objects->obj_hash_size; i++) {
531
		struct object *obj = the_repository->parsed_objects->obj_hash[i];
532
		if (obj)
533
			obj->flags &= ~flags;
534
	}
535
}
536

537
void repo_clear_commit_marks(struct repository *r, unsigned int flags)
538
{
539
	int i;
540

541
	for (i = 0; i < r->parsed_objects->obj_hash_size; i++) {
542
		struct object *obj = r->parsed_objects->obj_hash[i];
543
		if (obj && obj->type == OBJ_COMMIT)
544
			obj->flags &= ~flags;
545
	}
546
}
547

548
struct parsed_object_pool *parsed_object_pool_new(void)
549
{
550
	struct parsed_object_pool *o = xmalloc(sizeof(*o));
551
	memset(o, 0, sizeof(*o));
552

553
	o->blob_state = allocate_alloc_state();
554
	o->tree_state = allocate_alloc_state();
555
	o->commit_state = allocate_alloc_state();
556
	o->tag_state = allocate_alloc_state();
557
	o->object_state = allocate_alloc_state();
558

559
	o->is_shallow = -1;
560
	CALLOC_ARRAY(o->shallow_stat, 1);
561

562
	o->buffer_slab = allocate_commit_buffer_slab();
563

564
	return o;
565
}
566

567
struct raw_object_store *raw_object_store_new(void)
568
{
569
	struct raw_object_store *o = xmalloc(sizeof(*o));
570

571
	memset(o, 0, sizeof(*o));
572
	INIT_LIST_HEAD(&o->packed_git_mru);
573
	hashmap_init(&o->pack_map, pack_map_entry_cmp, NULL, 0);
574
	pthread_mutex_init(&o->replace_mutex, NULL);
575
	return o;
576
}
577

578
void free_object_directory(struct object_directory *odb)
579
{
580
	free(odb->path);
581
	odb_clear_loose_cache(odb);
582
	loose_object_map_clear(&odb->loose_map);
583
	free(odb);
584
}
585

586
static void free_object_directories(struct raw_object_store *o)
587
{
588
	while (o->odb) {
589
		struct object_directory *next;
590

591
		next = o->odb->next;
592
		free_object_directory(o->odb);
593
		o->odb = next;
594
	}
595
	kh_destroy_odb_path_map(o->odb_by_path);
596
	o->odb_by_path = NULL;
597
}
598

599
void raw_object_store_clear(struct raw_object_store *o)
600
{
601
	FREE_AND_NULL(o->alternate_db);
602

603
	oidmap_free(o->replace_map, 1);
604
	FREE_AND_NULL(o->replace_map);
605
	pthread_mutex_destroy(&o->replace_mutex);
606

607
	free_commit_graph(o->commit_graph);
608
	o->commit_graph = NULL;
609
	o->commit_graph_attempted = 0;
610

611
	free_object_directories(o);
612
	o->odb_tail = NULL;
613
	o->loaded_alternates = 0;
614

615
	INIT_LIST_HEAD(&o->packed_git_mru);
616
	close_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
	 */
622
	for (struct packed_git *p = o->packed_git, *next; p; p = next) {
623
		next = p->next;
624
		free(p);
625
	}
626
	o->packed_git = NULL;
627

628
	hashmap_clear(&o->pack_map);
629
}
630

631
void 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
	 */
640
	unsigned i;
641

642
	for (i = 0; i < o->obj_hash_size; i++) {
643
		struct object *obj = o->obj_hash[i];
644

645
		if (!obj)
646
			continue;
647

648
		if (obj->type == OBJ_TREE)
649
			free_tree_buffer((struct tree*)obj);
650
		else if (obj->type == OBJ_COMMIT)
651
			release_commit_memory(o, (struct commit*)obj);
652
		else if (obj->type == OBJ_TAG)
653
			release_tag_memory((struct tag*)obj);
654
	}
655

656
	FREE_AND_NULL(o->obj_hash);
657
	o->obj_hash_size = 0;
658

659
	free_commit_buffer_slab(o->buffer_slab);
660
	o->buffer_slab = NULL;
661

662
	clear_alloc_state(o->blob_state);
663
	clear_alloc_state(o->tree_state);
664
	clear_alloc_state(o->commit_state);
665
	clear_alloc_state(o->tag_state);
666
	clear_alloc_state(o->object_state);
667
	stat_validity_clear(o->shallow_stat);
668
	FREE_AND_NULL(o->blob_state);
669
	FREE_AND_NULL(o->tree_state);
670
	FREE_AND_NULL(o->commit_state);
671
	FREE_AND_NULL(o->tag_state);
672
	FREE_AND_NULL(o->object_state);
673
	FREE_AND_NULL(o->shallow_stat);
674
}
675

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.