object.c 12.9 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 14
#include "git2/object.h"

#include "repository.h"

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

21
bool git_object__strict_input_validation = true;
22

23
extern int git_odb_hash(git_oid *out, const void *data, size_t len, git_object_t type);
24

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

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

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

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

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

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

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

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

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

	assert(object_out);
	*object_out = NULL;

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

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

	/* Allocate and initialize base object */
	object = git__calloc(1, object_size);
85
	GIT_ERROR_CHECK_ALLOC(object);
86 87 88 89 90 91 92 93
	object->cached.flags = GIT_CACHE_STORE_PARSED;
	object->cached.type = type;
	git_odb_hash(&object->cached.oid, data, size, type);

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

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

	git_cached_obj_incref(object);
	*object_out = object;

	return 0;
}

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

116 117 118 119
	assert(object_out);
	*object_out = NULL;

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

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

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

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

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

144
	if ((error = def->parse(object, odb_obj)) < 0)
145
		def->free(object);
146 147
	else
		*object_out = git_cache_store_parsed(&repo->objects, object);
148

149
	return error;
150 151
}

152 153
void git_object__free(void *obj)
{
154
	git_object_t type = ((git_object *)obj)->cached.type;
155 156 157 158 159 160 161 162

	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);
}

163 164 165 166
int git_object_lookup_prefix(
	git_object **object_out,
	git_repository *repo,
	const git_oid *id,
167
	size_t len,
168
	git_object_t type)
169 170
{
	git_object *object = NULL;
171
	git_odb *odb = NULL;
172
	git_odb_object *odb_obj = NULL;
173
	int error = 0;
174 175 176

	assert(repo && object_out && id);

177
	if (len < GIT_OID_MINPREFIXLEN) {
178
		git_error_set(GIT_ERROR_OBJECT, "ambiguous lookup - OID prefix is too short");
179
		return GIT_EAMBIGUOUS;
180
	}
Vicent Marti committed
181

182
	error = git_repository_odb__weakptr(&odb, repo);
183
	if (error < 0)
184 185
		return error;

186 187
	if (len > GIT_OID_HEXSZ)
		len = GIT_OID_HEXSZ;
188

189
	if (len == GIT_OID_HEXSZ) {
190 191
		git_cached_obj *cached = NULL;

192 193 194
		/* 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
		 */
195 196 197 198 199
		cached = git_cache_get_any(&repo->objects, id);
		if (cached != NULL) {
			if (cached->flags == GIT_CACHE_STORE_PARSED) {
				object = (git_object *)cached;

200
				if (type != GIT_OBJECT_ANY && type != object->cached.type) {
201
					git_object_free(object);
202
					git_error_set(GIT_ERROR_INVALID,
203
						"the requested type does not match the type in ODB");
204 205 206 207 208 209 210 211 212
					return GIT_ENOTFOUND;
				}

				*object_out = object;
				return 0;
			} else if (cached->flags == GIT_CACHE_STORE_RAW) {
				odb_obj = (git_odb_object *)cached;
			} else {
				assert(!"Wrong caching type in the global object cache");
213
			}
214 215 216 217 218 219 220
		} 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);
221 222
		}
	} else {
223
		git_oid short_oid = {{ 0 }};
224

225
		git_oid__cpy_prefix(&short_oid, id, len);
226

227
		/* If len < GIT_OID_HEXSZ (a strict short oid was given), we have
228 229
		 * 2 options :
		 * - We always search in the cache first. If we find that short oid is
Vicent Marti committed
230 231 232 233
		 *	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)
234 235 236
		 * - We never explore the cache, go right to exploring the backends
		 * We chose the latter : we explore directly the backends.
		 */
237
		error = git_odb_read_prefix(&odb_obj, odb, &short_oid, len);
238 239
	}

240
	if (error < 0)
Russell Belfer committed
241
		return error;
242

243
	error = git_object__from_odb_object(object_out, repo, odb_obj, type);
244

245
	git_odb_object_free(odb_obj);
Vicent Marti committed
246

247
	return error;
248 249
}

250
int git_object_lookup(git_object **object_out, git_repository *repo, const git_oid *id, git_object_t type) {
Vicent Marti committed
251
	return git_object_lookup_prefix(object_out, repo, id, GIT_OID_HEXSZ, type);
252 253
}

254
void git_object_free(git_object *object)
255 256 257 258
{
	if (object == NULL)
		return;

259
	git_cached_obj_decref(object);
260 261
}

262
const git_oid *git_object_id(const git_object *obj)
263 264
{
	assert(obj);
Vicent Marti committed
265
	return &obj->cached.oid;
266 267
}

268
git_object_t git_object_type(const git_object *obj)
269 270
{
	assert(obj);
Vicent Marti committed
271
	return obj->cached.type;
272 273
}

274
git_repository *git_object_owner(const git_object *obj)
275 276 277 278 279
{
	assert(obj);
	return obj->repo;
}

280
const char *git_object_type2string(git_object_t type)
281 282 283 284 285 286 287
{
	if (type < 0 || ((size_t) type) >= ARRAY_SIZE(git_objects_table))
		return "";

	return git_objects_table[type].str;
}

288
git_object_t git_object_string2type(const char *str)
289
{
290
	if (!str)
291
		return GIT_OBJECT_INVALID;
292 293 294 295

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

296
git_object_t git_object_stringn2type(const char *str, size_t len)
297
{
298 299
	size_t i;

300
	if (!str || !len || !*str)
301
		return GIT_OBJECT_INVALID;
302 303

	for (i = 0; i < ARRAY_SIZE(git_objects_table); i++)
304 305
		if (*git_objects_table[i].str &&
			!git__prefixncmp(str, len, git_objects_table[i].str))
306
			return (git_object_t)i;
307

308
	return GIT_OBJECT_INVALID;
309 310
}

311
int git_object_typeisloose(git_object_t type)
312 313 314 315
{
	if (type < 0 || ((size_t) type) >= ARRAY_SIZE(git_objects_table))
		return 0;

316
	return (git_objects_table[type].size > 0) ? 1 : 0;
317 318
}

319
size_t git_object__size(git_object_t type)
320 321 322 323 324 325 326
{
	if (type < 0 || ((size_t) type) >= ARRAY_SIZE(git_objects_table))
		return 0;

	return git_objects_table[type].size;
}

327 328
static int dereference_object(git_object **dereferenced, git_object *obj)
{
329
	git_object_t type = git_object_type(obj);
330 331

	switch (type) {
332
	case GIT_OBJECT_COMMIT:
333 334
		return git_commit_tree((git_tree **)dereferenced, (git_commit*)obj);

335
	case GIT_OBJECT_TAG:
336
		return git_tag_target(dereferenced, (git_tag*)obj);
337

338 339
	case GIT_OBJECT_BLOB:
	case GIT_OBJECT_TREE:
340
		return GIT_EPEEL;
341 342

	default:
343
		return GIT_EINVALIDSPEC;
344 345 346
	}
}

347
static int peel_error(int error, const git_oid *oid, git_object_t type)
348 349 350 351 352 353 354 355 356
{
	const char *type_name;
	char hex_oid[GIT_OID_HEXSZ + 1];

	type_name = git_object_type2string(type);

	git_oid_fmt(hex_oid, oid);
	hex_oid[GIT_OID_HEXSZ] = '\0';

357
	git_error_set(GIT_ERROR_OBJECT, "the git_object of id '%s' can not be "
358
		"successfully peeled into a %s (git_object_t=%i).", hex_oid, type_name, type);
359 360 361 362

	return error;
}

363
static int check_type_combination(git_object_t type, git_object_t target)
364 365 366 367 368
{
	if (type == target)
		return 0;

	switch (type) {
369 370
	case GIT_OBJECT_BLOB:
	case GIT_OBJECT_TREE:
371 372 373
		/* a blob or tree can never be peeled to anything but themselves */
		return GIT_EINVALIDSPEC;
		break;
374
	case GIT_OBJECT_COMMIT:
375
		/* a commit can only be peeled to a tree */
376
		if (target != GIT_OBJECT_TREE && target != GIT_OBJECT_ANY)
377 378
			return GIT_EINVALIDSPEC;
		break;
379
	case GIT_OBJECT_TAG:
380 381 382 383 384 385 386 387 388
		/* a tag may point to anything, so we let anything through */
		break;
	default:
		return GIT_EINVALIDSPEC;
	}

	return 0;
}

389
int git_object_peel(
390
	git_object **peeled,
Vicent Marti committed
391
	const git_object *object,
392
	git_object_t target_type)
393 394
{
	git_object *source, *deref = NULL;
395 396
	int error;

397
	assert(object && peeled);
398

399 400 401 402 403
	assert(target_type == GIT_OBJECT_TAG ||
		target_type == GIT_OBJECT_COMMIT ||
		target_type == GIT_OBJECT_TREE ||
		target_type == GIT_OBJECT_BLOB ||
		target_type == GIT_OBJECT_ANY);
404

405 406 407 408 409 410
	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
411
	source = (git_object *)object;
412

413
	while (!(error = dereference_object(&deref, source))) {
414 415 416 417 418 419 420 421 422

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

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

423
		if (target_type == GIT_OBJECT_ANY &&
424 425 426 427 428 429
			git_object_type(deref) != git_object_type(object))
		{
			*peeled = deref;
			return 0;
		}

430 431 432 433 434 435
		source = deref;
		deref = NULL;
	}

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

437
	git_object_free(deref);
438 439 440 441 442

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

	return error;
443
}
444

445 446 447 448 449 450
int git_object_dup(git_object **dest, git_object *source)
{
	git_cached_obj_incref(source);
	*dest = source;
	return 0;
}
Ben Straub committed
451 452 453 454 455

int git_object_lookup_bypath(
		git_object **out,
		const git_object *treeish,
		const char *path,
456
		git_object_t type)
Ben Straub committed
457 458 459 460 461 462 463
{
	int error = -1;
	git_tree *tree = NULL;
	git_tree_entry *entry = NULL;

	assert(out && treeish && path);

464
	if ((error = git_object_peel((git_object**)&tree, treeish, GIT_OBJECT_TREE)) < 0 ||
465
		 (error = git_tree_entry_bypath(&entry, tree, path)) < 0)
Ben Straub committed
466 467 468 469
	{
		goto cleanup;
	}

470
	if (type != GIT_OBJECT_ANY && git_tree_entry_type(entry) != type)
471
	{
472
		git_error_set(GIT_ERROR_OBJECT,
Ben Straub committed
473 474 475
				"object at path '%s' is not of the asked-for type %d",
				path, type);
		error = GIT_EINVALIDSPEC;
476
		goto cleanup;
Ben Straub committed
477 478
	}

479 480
	error = git_tree_entry_to_object(out, git_object_owner(treeish), entry);

Ben Straub committed
481 482 483 484 485
cleanup:
	git_tree_entry_free(entry);
	git_tree_free(tree);
	return error;
}
486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514

int git_object_short_id(git_buf *out, const git_object *obj)
{
	git_repository *repo;
	int len = GIT_ABBREV_DEFAULT, error;
	git_oid id = {{0}};
	git_odb *odb;

	assert(out && obj);

	git_buf_sanitize(out);
	repo = git_object_owner(obj);

	if ((error = git_repository__cvar(&len, repo, GIT_CVAR_ABBREV)) < 0)
		return error;

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

	while (len < GIT_OID_HEXSZ) {
		/* set up short oid */
		memcpy(&id.id, &obj->cached.oid.id, (len + 1) / 2);
		if (len & 1)
			id.id[len / 2] &= 0xf0;

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

515
		git_error_clear();
516 517 518 519 520 521 522 523 524 525 526 527 528
		len++;
	}

	if (!error && !(error = git_buf_grow(out, len + 1))) {
		git_oid_tostr(out->ptr, len + 1, &id);
		out->size = len;
	}

	git_odb_free(odb);

	return error;
}

529
bool git_object__is_valid(
530
	git_repository *repo, const git_oid *id, git_object_t expected_type)
531 532
{
	git_odb *odb;
533
	git_object_t actual_type;
534 535 536 537 538 539 540 541 542 543
	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;

544
	if (expected_type != GIT_OBJECT_ANY && expected_type != actual_type) {
545
		git_error_set(GIT_ERROR_INVALID,
546 547 548 549 550 551 552
			"the requested type does not match the type in the ODB");
		return false;
	}

	return true;
}