object.c 15 KB
Newer Older
1
/*
Edward Thomson committed
2
 * Copyright (C) the libgit2 contributors. All rights reserved.
3
 *
Vicent Marti committed
4 5
 * This file is part of libgit2, distributed under the GNU GPL v2 with
 * a Linking Exception. For full terms see the included COPYING file.
6
 */
7 8 9

#include "object.h"

10 11 12 13
#include "git2/object.h"

#include "repository.h"

14
#include "buf.h"
15
#include "commit.h"
16
#include "hash.h"
17 18
#include "tree.h"
#include "blob.h"
19
#include "oid.h"
20 21
#include "tag.h"

22
bool git_object__strict_input_validation = true;
23

24
size_t git_object__size(git_object_t type);
25

26
typedef struct {
Vicent Marti committed
27
	const char	*str;	/* type name string */
28
	size_t		size;	/* size in bytes of the object structure */
29

30
	int  (*parse)(void *self, git_odb_object *obj);
31
	int  (*parse_raw)(void *self, const char *data, size_t size);
32 33 34 35
	void (*free)(void *self);
} git_object_def;

static git_object_def git_objects_table[] = {
36
	/* 0 = GIT_OBJECT__EXT1 */
37
	{ "", 0, NULL, NULL, NULL },
38

39
	/* 1 = GIT_OBJECT_COMMIT */
40
	{ "commit", sizeof(git_commit), git_commit__parse, git_commit__parse_raw, git_commit__free },
41

42
	/* 2 = GIT_OBJECT_TREE */
43
	{ "tree", sizeof(git_tree), git_tree__parse, git_tree__parse_raw, git_tree__free },
44

45
	/* 3 = GIT_OBJECT_BLOB */
46
	{ "blob", sizeof(git_blob), git_blob__parse, git_blob__parse_raw, git_blob__free },
47

48
	/* 4 = GIT_OBJECT_TAG */
49
	{ "tag", sizeof(git_tag), git_tag__parse, git_tag__parse_raw, git_tag__free },
50

51
	/* 5 = GIT_OBJECT__EXT2 */
52
	{ "", 0, NULL, NULL, NULL },
53
	/* 6 = GIT_OBJECT_OFS_DELTA */
54
	{ "OFS_DELTA", 0, NULL, NULL, NULL },
55
	/* 7 = GIT_OBJECT_REF_DELTA */
56
	{ "REF_DELTA", 0, NULL, NULL, NULL },
57 58
};

59 60 61 62
int git_object__from_raw(
	git_object **object_out,
	const char *data,
	size_t size,
63
	git_object_t type)
64 65 66 67 68 69
{
	git_object_def *def;
	git_object *object;
	size_t object_size;
	int error;

70
	GIT_ASSERT_ARG(object_out);
71 72 73
	*object_out = NULL;

	/* Validate type match */
74
	if (type != GIT_OBJECT_BLOB && type != GIT_OBJECT_TREE && type != GIT_OBJECT_COMMIT && type != GIT_OBJECT_TAG) {
75
		git_error_set(GIT_ERROR_INVALID, "the requested type is invalid");
76 77 78
		return GIT_ENOTFOUND;
	}

79
	if ((object_size = git_object__size(type)) == 0) {
80
		git_error_set(GIT_ERROR_INVALID, "the requested type is invalid");
81 82 83 84 85
		return GIT_ENOTFOUND;
	}

	/* Allocate and initialize base object */
	object = git__calloc(1, object_size);
86
	GIT_ERROR_CHECK_ALLOC(object);
87 88
	object->cached.flags = GIT_CACHE_STORE_PARSED;
	object->cached.type = type;
89
	if ((error = git_odb__hash(&object->cached.oid, data, size, type, GIT_OID_SHA1)) < 0)
90
		return error;
91 92 93

	/* Parse raw object data */
	def = &git_objects_table[type];
94
	GIT_ASSERT(def->free && def->parse_raw);
95

96
	if ((error = def->parse_raw(object, data, size)) < 0) {
97
		def->free(object);
98 99
		return error;
	}
100 101 102 103 104 105 106

	git_cached_obj_incref(object);
	*object_out = object;

	return 0;
}

107 108 109 110
int git_object__from_odb_object(
	git_object **object_out,
	git_repository *repo,
	git_odb_object *odb_obj,
111
	git_object_t type)
112 113
{
	int error;
114 115
	size_t object_size;
	git_object_def *def;
116 117
	git_object *object = NULL;

118
	GIT_ASSERT_ARG(object_out);
119 120 121
	*object_out = NULL;

	/* Validate type match */
122
	if (type != GIT_OBJECT_ANY && type != odb_obj->cached.type) {
123
		git_error_set(GIT_ERROR_INVALID,
124
			"the requested type does not match the type in the ODB");
125 126 127
		return GIT_ENOTFOUND;
	}

128
	if ((object_size = git_object__size(odb_obj->cached.type)) == 0) {
129
		git_error_set(GIT_ERROR_INVALID, "the requested type is invalid");
130 131 132 133 134
		return GIT_ENOTFOUND;
	}

	/* Allocate and initialize base object */
	object = git__calloc(1, object_size);
135
	GIT_ERROR_CHECK_ALLOC(object);
136 137

	git_oid_cpy(&object->cached.oid, &odb_obj->cached.oid);
Vicent Marti committed
138
	object->cached.type = odb_obj->cached.type;
139
	object->cached.size = odb_obj->cached.size;
140 141
	object->repo = repo;

142 143
	/* Parse raw object data */
	def = &git_objects_table[odb_obj->cached.type];
144
	GIT_ASSERT(def->free && def->parse);
145

146 147 148 149 150
	if ((error = def->parse(object, odb_obj)) < 0) {
		/*
		 * parse returns EINVALID on invalid data; downgrade
		 * that to a normal -1 error code.
		 */
151
		def->free(object);
152 153
		return -1;
	}
154

155 156
	*object_out = git_cache_store_parsed(&repo->objects, object);
	return 0;
157 158
}

159 160
void git_object__free(void *obj)
{
161
	git_object_t type = ((git_object *)obj)->cached.type;
162 163 164 165 166 167 168 169

	if (type < 0 || ((size_t)type) >= ARRAY_SIZE(git_objects_table) ||
		!git_objects_table[type].free)
		git__free(obj);
	else
		git_objects_table[type].free(obj);
}

170 171 172 173
int git_object_lookup_prefix(
	git_object **object_out,
	git_repository *repo,
	const git_oid *id,
174
	size_t len,
175
	git_object_t type)
176 177
{
	git_object *object = NULL;
178
	git_odb *odb = NULL;
179
	git_odb_object *odb_obj = NULL;
180
	int error = 0;
181

182 183 184
	GIT_ASSERT_ARG(repo);
	GIT_ASSERT_ARG(object_out);
	GIT_ASSERT_ARG(id);
185

186
	if (len < GIT_OID_MINPREFIXLEN) {
187
		git_error_set(GIT_ERROR_OBJECT, "ambiguous lookup - OID prefix is too short");
188
		return GIT_EAMBIGUOUS;
189
	}
Vicent Marti committed
190

191
	error = git_repository_odb__weakptr(&odb, repo);
192
	if (error < 0)
193 194
		return error;

195 196
	if (len > GIT_OID_SHA1_HEXSIZE)
		len = GIT_OID_SHA1_HEXSIZE;
197

198
	if (len == GIT_OID_SHA1_HEXSIZE) {
199 200
		git_cached_obj *cached = NULL;

201 202 203
		/* We want to match the full id : we can first look up in the cache,
		 * since there is no need to check for non ambiguousity
		 */
204 205 206 207 208
		cached = git_cache_get_any(&repo->objects, id);
		if (cached != NULL) {
			if (cached->flags == GIT_CACHE_STORE_PARSED) {
				object = (git_object *)cached;

209
				if (type != GIT_OBJECT_ANY && type != object->cached.type) {
210
					git_object_free(object);
211
					git_error_set(GIT_ERROR_INVALID,
212
						"the requested type does not match the type in the ODB");
213 214 215 216 217 218 219 220
					return GIT_ENOTFOUND;
				}

				*object_out = object;
				return 0;
			} else if (cached->flags == GIT_CACHE_STORE_RAW) {
				odb_obj = (git_odb_object *)cached;
			} else {
221
				GIT_ASSERT(!"Wrong caching type in the global object cache");
222
			}
223 224 225 226 227 228 229
		} else {
			/* Object was not found in the cache, let's explore the backends.
			 * We could just use git_odb_read_unique_short_oid,
			 * it is the same cost for packed and loose object backends,
			 * but it may be much more costly for sqlite and hiredis.
			 */
			error = git_odb_read(&odb_obj, odb, id);
230 231
		}
	} else {
232
		git_oid short_oid = GIT_OID_SHA1_ZERO;
233

234
		git_oid__cpy_prefix(&short_oid, id, len);
235

236
		/* If len < GIT_OID_SHA1_HEXSIZE (a strict short oid was given), we have
237 238
		 * 2 options :
		 * - We always search in the cache first. If we find that short oid is
Vicent Marti committed
239 240 241 242
		 *	ambiguous, we can stop. But in all the other cases, we must then
		 *	explore all the backends (to find an object if there was match,
		 *	or to check that oid is not ambiguous if we have found 1 match in
		 *	the cache)
243 244 245
		 * - We never explore the cache, go right to exploring the backends
		 * We chose the latter : we explore directly the backends.
		 */
246
		error = git_odb_read_prefix(&odb_obj, odb, &short_oid, len);
247 248
	}

249
	if (error < 0)
Russell Belfer committed
250
		return error;
251

252
	GIT_ASSERT(odb_obj);
253
	error = git_object__from_odb_object(object_out, repo, odb_obj, type);
254

255
	git_odb_object_free(odb_obj);
Vicent Marti committed
256

257
	return error;
258 259
}

260
int git_object_lookup(git_object **object_out, git_repository *repo, const git_oid *id, git_object_t type) {
261
	return git_object_lookup_prefix(object_out, repo, id, GIT_OID_SHA1_HEXSIZE, type);
262 263
}

264
void git_object_free(git_object *object)
265 266 267 268
{
	if (object == NULL)
		return;

269
	git_cached_obj_decref(object);
270 271
}

272
const git_oid *git_object_id(const git_object *obj)
273
{
274
	GIT_ASSERT_ARG_WITH_RETVAL(obj, NULL);
Vicent Marti committed
275
	return &obj->cached.oid;
276 277
}

278
git_object_t git_object_type(const git_object *obj)
279
{
280
	GIT_ASSERT_ARG_WITH_RETVAL(obj, GIT_OBJECT_INVALID);
Vicent Marti committed
281
	return obj->cached.type;
282 283
}

284
git_repository *git_object_owner(const git_object *obj)
285
{
286
	GIT_ASSERT_ARG_WITH_RETVAL(obj, NULL);
287 288 289
	return obj->repo;
}

290
const char *git_object_type2string(git_object_t type)
291 292 293 294 295 296 297
{
	if (type < 0 || ((size_t) type) >= ARRAY_SIZE(git_objects_table))
		return "";

	return git_objects_table[type].str;
}

298
git_object_t git_object_string2type(const char *str)
299
{
300
	if (!str)
301
		return GIT_OBJECT_INVALID;
302 303 304 305

	return git_object_stringn2type(str, strlen(str));
}

306
git_object_t git_object_stringn2type(const char *str, size_t len)
307
{
308 309
	size_t i;

310
	if (!str || !len || !*str)
311
		return GIT_OBJECT_INVALID;
312 313

	for (i = 0; i < ARRAY_SIZE(git_objects_table); i++)
314 315
		if (*git_objects_table[i].str &&
			!git__prefixncmp(str, len, git_objects_table[i].str))
316
			return (git_object_t)i;
317

318
	return GIT_OBJECT_INVALID;
319 320
}

321
int git_object_typeisloose(git_object_t type)
322 323 324 325
{
	if (type < 0 || ((size_t) type) >= ARRAY_SIZE(git_objects_table))
		return 0;

326
	return (git_objects_table[type].size > 0) ? 1 : 0;
327 328
}

329
size_t git_object__size(git_object_t type)
330 331 332 333 334 335 336
{
	if (type < 0 || ((size_t) type) >= ARRAY_SIZE(git_objects_table))
		return 0;

	return git_objects_table[type].size;
}

337 338
static int dereference_object(git_object **dereferenced, git_object *obj)
{
339
	git_object_t type = git_object_type(obj);
340 341

	switch (type) {
342
	case GIT_OBJECT_COMMIT:
343 344
		return git_commit_tree((git_tree **)dereferenced, (git_commit*)obj);

345
	case GIT_OBJECT_TAG:
346
		return git_tag_target(dereferenced, (git_tag*)obj);
347

348 349
	case GIT_OBJECT_BLOB:
	case GIT_OBJECT_TREE:
350
		return GIT_EPEEL;
351 352

	default:
353
		return GIT_EINVALIDSPEC;
354 355 356
	}
}

357
static int peel_error(int error, const git_oid *oid, git_object_t type)
358 359
{
	const char *type_name;
360
	char hex_oid[GIT_OID_SHA1_HEXSIZE + 1];
361 362 363 364

	type_name = git_object_type2string(type);

	git_oid_fmt(hex_oid, oid);
365
	hex_oid[GIT_OID_SHA1_HEXSIZE] = '\0';
366

367
	git_error_set(GIT_ERROR_OBJECT, "the git_object of id '%s' can not be "
368
		"successfully peeled into a %s (git_object_t=%i).", hex_oid, type_name, type);
369 370 371 372

	return error;
}

373
static int check_type_combination(git_object_t type, git_object_t target)
374 375 376 377 378
{
	if (type == target)
		return 0;

	switch (type) {
379 380
	case GIT_OBJECT_BLOB:
	case GIT_OBJECT_TREE:
381 382 383
		/* a blob or tree can never be peeled to anything but themselves */
		return GIT_EINVALIDSPEC;
		break;
384
	case GIT_OBJECT_COMMIT:
385
		/* a commit can only be peeled to a tree */
386
		if (target != GIT_OBJECT_TREE && target != GIT_OBJECT_ANY)
387 388
			return GIT_EINVALIDSPEC;
		break;
389
	case GIT_OBJECT_TAG:
390 391 392 393 394 395 396 397 398
		/* a tag may point to anything, so we let anything through */
		break;
	default:
		return GIT_EINVALIDSPEC;
	}

	return 0;
}

399
int git_object_peel(
400
	git_object **peeled,
Vicent Marti committed
401
	const git_object *object,
402
	git_object_t target_type)
403 404
{
	git_object *source, *deref = NULL;
405 406
	int error;

407 408
	GIT_ASSERT_ARG(object);
	GIT_ASSERT_ARG(peeled);
409

410
	GIT_ASSERT_ARG(target_type == GIT_OBJECT_TAG ||
411 412 413 414
		target_type == GIT_OBJECT_COMMIT ||
		target_type == GIT_OBJECT_TREE ||
		target_type == GIT_OBJECT_BLOB ||
		target_type == GIT_OBJECT_ANY);
415

416 417 418 419 420 421
	if ((error = check_type_combination(git_object_type(object), target_type)) < 0)
		return peel_error(error, git_object_id(object), target_type);

	if (git_object_type(object) == target_type)
		return git_object_dup(peeled, (git_object *)object);

Vicent Marti committed
422
	source = (git_object *)object;
423

424
	while (!(error = dereference_object(&deref, source))) {
425 426 427 428 429 430 431 432 433

		if (source != object)
			git_object_free(source);

		if (git_object_type(deref) == target_type) {
			*peeled = deref;
			return 0;
		}

434
		if (target_type == GIT_OBJECT_ANY &&
435 436 437 438 439 440
			git_object_type(deref) != git_object_type(object))
		{
			*peeled = deref;
			return 0;
		}

441 442 443 444 445 446
		source = deref;
		deref = NULL;
	}

	if (source != object)
		git_object_free(source);
447

448
	git_object_free(deref);
449 450 451 452 453

	if (error)
		error = peel_error(error, git_object_id(object), target_type);

	return error;
454
}
455

456 457 458 459 460 461
int git_object_dup(git_object **dest, git_object *source)
{
	git_cached_obj_incref(source);
	*dest = source;
	return 0;
}
Ben Straub committed
462 463 464 465 466

int git_object_lookup_bypath(
		git_object **out,
		const git_object *treeish,
		const char *path,
467
		git_object_t type)
Ben Straub committed
468 469 470 471 472
{
	int error = -1;
	git_tree *tree = NULL;
	git_tree_entry *entry = NULL;

473 474 475
	GIT_ASSERT_ARG(out);
	GIT_ASSERT_ARG(treeish);
	GIT_ASSERT_ARG(path);
Ben Straub committed
476

477
	if ((error = git_object_peel((git_object**)&tree, treeish, GIT_OBJECT_TREE)) < 0 ||
478
		 (error = git_tree_entry_bypath(&entry, tree, path)) < 0)
Ben Straub committed
479 480 481 482
	{
		goto cleanup;
	}

483
	if (type != GIT_OBJECT_ANY && git_tree_entry_type(entry) != type)
484
	{
485
		git_error_set(GIT_ERROR_OBJECT,
Ben Straub committed
486 487 488
				"object at path '%s' is not of the asked-for type %d",
				path, type);
		error = GIT_EINVALIDSPEC;
489
		goto cleanup;
Ben Straub committed
490 491
	}

492 493
	error = git_tree_entry_to_object(out, git_object_owner(treeish), entry);

Ben Straub committed
494 495 496 497 498
cleanup:
	git_tree_entry_free(entry);
	git_tree_free(tree);
	return error;
}
499

500
static int git_object__short_id(git_str *out, const git_object *obj)
501 502 503
{
	git_repository *repo;
	int len = GIT_ABBREV_DEFAULT, error;
504
	git_oid id = GIT_OID_SHA1_ZERO;
505 506
	git_odb *odb;

507 508
	GIT_ASSERT_ARG(out);
	GIT_ASSERT_ARG(obj);
509 510 511

	repo = git_object_owner(obj);

512
	if ((error = git_repository__configmap_lookup(&len, repo, GIT_CONFIGMAP_ABBREV)) < 0)
513 514 515 516 517
		return error;

	if ((error = git_repository_odb(&odb, repo)) < 0)
		return error;

518
	while (len < GIT_OID_SHA1_HEXSIZE) {
519 520 521 522
		/* set up short oid */
		memcpy(&id.id, &obj->cached.oid.id, (len + 1) / 2);
		if (len & 1)
			id.id[len / 2] &= 0xf0;
523 524

#ifdef GIT_EXPERIMENTAL_SHA256
Edward Thomson committed
525
		id.type = GIT_OID_SHA1;
526
#endif
527 528 529 530 531

		error = git_odb_exists_prefix(NULL, odb, &id, len);
		if (error != GIT_EAMBIGUOUS)
			break;

532
		git_error_clear();
533 534 535
		len++;
	}

536
	if (!error && !(error = git_str_grow(out, len + 1))) {
537 538 539 540 541 542 543 544 545
		git_oid_tostr(out->ptr, len + 1, &id);
		out->size = len;
	}

	git_odb_free(odb);

	return error;
}

546 547 548 549 550
int git_object_short_id(git_buf *out, const git_object *obj)
{
	GIT_BUF_WRAP_PRIVATE(out, git_object__short_id, obj);
}

551
bool git_object__is_valid(
552
	git_repository *repo, const git_oid *id, git_object_t expected_type)
553 554
{
	git_odb *odb;
555
	git_object_t actual_type;
556 557 558 559 560 561 562 563 564 565
	size_t len;
	int error;

	if (!git_object__strict_input_validation)
		return true;

	if ((error = git_repository_odb__weakptr(&odb, repo)) < 0 ||
		(error = git_odb_read_header(&len, &actual_type, odb, id)) < 0)
		return false;

566
	if (expected_type != GIT_OBJECT_ANY && expected_type != actual_type) {
567
		git_error_set(GIT_ERROR_INVALID,
568 569 570 571 572 573
			"the requested type does not match the type in the ODB");
		return false;
	}

	return true;
}
574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605

int git_object_rawcontent_is_valid(
	int *valid,
	const char *buf,
	size_t len,
	git_object_t type)
{
	git_object *obj = NULL;
	int error;

	GIT_ASSERT_ARG(valid);
	GIT_ASSERT_ARG(buf);

	/* Blobs are always valid; don't bother parsing. */
	if (type == GIT_OBJECT_BLOB) {
		*valid = 1;
		return 0;
	}

	error = git_object__from_raw(&obj, buf, len, type);
	git_object_free(obj);

	if (error == 0) {
		*valid = 1;
		return 0;
	} else if (error == GIT_EINVALID) {
		*valid = 0;
		return 0;
	}

	return error;
}
606 607 608 609 610

int git_object__parse_oid_header(
	git_oid *oid,
	const char **buffer_out,
	const char *buffer_end,
Edward Thomson committed
611 612
	const char *header,
	git_oid_t oid_type)
613 614 615 616 617 618 619 620 621 622 623 624 625 626 627
{
	const size_t sha_len = GIT_OID_SHA1_HEXSIZE;
	const size_t header_len = strlen(header);

	const char *buffer = *buffer_out;

	if (buffer + (header_len + sha_len + 1) > buffer_end)
		return -1;

	if (memcmp(buffer, header, header_len) != 0)
		return -1;

	if (buffer[header_len + sha_len] != '\n')
		return -1;

628
	if (git_oid__fromstr(oid, buffer + header_len, oid_type) < 0)
629 630 631 632 633 634
		return -1;

	*buffer_out = buffer + (header_len + sha_len + 1);

	return 0;
}
635 636 637 638 639 640

int git_object__write_oid_header(
	git_str *buf,
	const char *header,
	const git_oid *oid)
{
641
	size_t hex_size = git_oid_hexsize(git_oid_type(oid));
Edward Thomson committed
642 643 644 645 646 647
	char hex_oid[GIT_OID_MAX_HEXSIZE];

	if (!hex_size) {
		git_error_set(GIT_ERROR_INVALID, "unknown type");
		return -1;
	}
648 649 650

	git_oid_fmt(hex_oid, oid);
	git_str_puts(buf, header);
Edward Thomson committed
651
	git_str_put(buf, hex_oid, hex_size);
652 653 654 655
	git_str_putc(buf, '\n');

	return git_str_oom(buf) ? -1 : 0;
}