object.c 8.48 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 10 11 12 13 14 15 16 17 18 19 20
 */
#include <stdarg.h>

#include "git2/object.h"

#include "common.h"
#include "repository.h"

#include "commit.h"
#include "tree.h"
#include "blob.h"
#include "tag.h"

static const int OBJECT_BASE_SIZE = 4096;

21
typedef struct {
Vicent Marti committed
22
	const char	*str;	/* type name string */
23
	size_t		size;	/* size in bytes of the object structure */
24

25
	int  (*parse)(void *self, git_odb_object *obj);
26 27 28 29
	void (*free)(void *self);
} git_object_def;

static git_object_def git_objects_table[] = {
30
	/* 0 = GIT_OBJ__EXT1 */
31
	{ "", 0, NULL, NULL },
32 33

	/* 1 = GIT_OBJ_COMMIT */
34
	{ "commit", sizeof(git_commit), git_commit__parse, git_commit__free },
35 36

	/* 2 = GIT_OBJ_TREE */
37
	{ "tree", sizeof(git_tree), git_tree__parse, git_tree__free },
38 39

	/* 3 = GIT_OBJ_BLOB */
40
	{ "blob", sizeof(git_blob), git_blob__parse, git_blob__free },
41 42

	/* 4 = GIT_OBJ_TAG */
43
	{ "tag", sizeof(git_tag), git_tag__parse, git_tag__free },
44 45

	/* 5 = GIT_OBJ__EXT2 */
46
	{ "", 0, NULL, NULL },
47
	/* 6 = GIT_OBJ_OFS_DELTA */
48
	{ "OFS_DELTA", 0, NULL, NULL },
49
	/* 7 = GIT_OBJ_REF_DELTA */
50
	{ "REF_DELTA", 0, NULL, NULL },
51 52
};

53 54 55 56 57 58 59
int git_object__from_odb_object(
	git_object **object_out,
	git_repository *repo,
	git_odb_object *odb_obj,
	git_otype type)
{
	int error;
60 61
	size_t object_size;
	git_object_def *def;
62 63
	git_object *object = NULL;

64 65 66 67
	assert(object_out);
	*object_out = NULL;

	/* Validate type match */
Vicent Marti committed
68 69 70
	if (type != GIT_OBJ_ANY && type != odb_obj->cached.type) {
		giterr_set(GITERR_INVALID,
			"The requested type does not match the type in the ODB");
71 72 73
		return GIT_ENOTFOUND;
	}

74 75 76 77 78 79 80 81
	if ((object_size = git_object__size(odb_obj->cached.type)) == 0) {
		giterr_set(GITERR_INVALID, "The requested type is invalid");
		return GIT_ENOTFOUND;
	}

	/* Allocate and initialize base object */
	object = git__calloc(1, object_size);
	GITERR_CHECK_ALLOC(object);
82 83

	git_oid_cpy(&object->cached.oid, &odb_obj->cached.oid);
Vicent Marti committed
84
	object->cached.type = odb_obj->cached.type;
85
	object->cached.size = odb_obj->cached.size;
86 87
	object->repo = repo;

88 89
	/* Parse raw object data */
	def = &git_objects_table[odb_obj->cached.type];
90
	assert(def->free && def->parse);
91

92
	if ((error = def->parse(object, odb_obj)) < 0)
93
		def->free(object);
94 95
	else
		*object_out = git_cache_store_parsed(&repo->objects, object);
96

97
	return error;
98 99
}

100 101 102 103 104 105 106 107 108 109 110
void git_object__free(void *obj)
{
	git_otype type = ((git_object *)obj)->cached.type;

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

111 112 113 114
int git_object_lookup_prefix(
	git_object **object_out,
	git_repository *repo,
	const git_oid *id,
115
	size_t len,
116
	git_otype type)
117 118
{
	git_object *object = NULL;
119
	git_odb *odb = NULL;
120
	git_odb_object *odb_obj = NULL;
121
	int error = 0;
122 123 124

	assert(repo && object_out && id);

125 126
	if (len < GIT_OID_MINPREFIXLEN) {
		giterr_set(GITERR_OBJECT, "Ambiguous lookup - OID prefix is too short");
127
		return GIT_EAMBIGUOUS;
128
	}
Vicent Marti committed
129

130
	error = git_repository_odb__weakptr(&odb, repo);
131
	if (error < 0)
132 133
		return error;

Vicent Marti committed
134
	if (len > GIT_OID_HEXSZ)
135
		len = GIT_OID_HEXSZ;
136

Vicent Marti committed
137
	if (len == GIT_OID_HEXSZ) {
138 139
		git_cached_obj *cached = NULL;

140 141 142
		/* 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
		 */
143 144 145 146 147
		cached = git_cache_get_any(&repo->objects, id);
		if (cached != NULL) {
			if (cached->flags == GIT_CACHE_STORE_PARSED) {
				object = (git_object *)cached;

Vicent Marti committed
148
				if (type != GIT_OBJ_ANY && type != object->cached.type) {
149 150 151 152 153 154 155 156 157 158 159 160
					git_object_free(object);
					giterr_set(GITERR_INVALID,
						"The requested type does not match the type in ODB");
					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");
161
			}
162 163 164 165 166 167 168
		} 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);
169 170 171 172 173 174 175 176 177 178 179 180 181
		}
	} else {
		git_oid short_oid;

		/* We copy the first len*4 bits from id and fill the remaining with 0s */
		memcpy(short_oid.id, id->id, (len + 1) / 2);
		if (len % 2)
			short_oid.id[len / 2] &= 0xF0;
		memset(short_oid.id + (len + 1) / 2, 0, (GIT_OID_HEXSZ - len) / 2);

		/* If len < GIT_OID_HEXSZ (a strict short oid was given), we have
		 * 2 options :
		 * - We always search in the cache first. If we find that short oid is
Vicent Marti committed
182 183 184 185
		 *	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)
186 187 188
		 * - We never explore the cache, go right to exploring the backends
		 * We chose the latter : we explore directly the backends.
		 */
189
		error = git_odb_read_prefix(&odb_obj, odb, &short_oid, len);
190 191
	}

192
	if (error < 0)
Russell Belfer committed
193
		return error;
194

195
	error = git_object__from_odb_object(object_out, repo, odb_obj, type);
196

197
	git_odb_object_free(odb_obj);
Vicent Marti committed
198

199
	return error;
200 201
}

202
int git_object_lookup(git_object **object_out, git_repository *repo, const git_oid *id, git_otype type) {
Vicent Marti committed
203
	return git_object_lookup_prefix(object_out, repo, id, GIT_OID_HEXSZ, type);
204 205
}

206
void git_object_free(git_object *object)
207 208 209 210
{
	if (object == NULL)
		return;

211
	git_cached_obj_decref(object);
212 213
}

214
const git_oid *git_object_id(const git_object *obj)
215 216
{
	assert(obj);
Vicent Marti committed
217
	return &obj->cached.oid;
218 219
}

220
git_otype git_object_type(const git_object *obj)
221 222
{
	assert(obj);
Vicent Marti committed
223
	return obj->cached.type;
224 225
}

226
git_repository *git_object_owner(const git_object *obj)
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
{
	assert(obj);
	return obj->repo;
}

const char *git_object_type2string(git_otype type)
{
	if (type < 0 || ((size_t) type) >= ARRAY_SIZE(git_objects_table))
		return "";

	return git_objects_table[type].str;
}

git_otype git_object_string2type(const char *str)
{
	size_t i;

	if (!str || !*str)
		return GIT_OBJ_BAD;

	for (i = 0; i < ARRAY_SIZE(git_objects_table); i++)
		if (!strcmp(str, git_objects_table[i].str))
			return (git_otype)i;

	return GIT_OBJ_BAD;
}

int git_object_typeisloose(git_otype type)
{
	if (type < 0 || ((size_t) type) >= ARRAY_SIZE(git_objects_table))
		return 0;

259
	return (git_objects_table[type].size > 0) ? 1 : 0;
260 261 262 263 264 265 266 267 268 269
}

size_t git_object__size(git_otype type)
{
	if (type < 0 || ((size_t) type) >= ARRAY_SIZE(git_objects_table))
		return 0;

	return git_objects_table[type].size;
}

270 271 272 273 274 275 276 277 278 279
static int dereference_object(git_object **dereferenced, git_object *obj)
{
	git_otype type = git_object_type(obj);

	switch (type) {
	case GIT_OBJ_COMMIT:
		return git_commit_tree((git_tree **)dereferenced, (git_commit*)obj);

	case GIT_OBJ_TAG:
		return git_tag_target(dereferenced, (git_tag*)obj);
280 281

	case GIT_OBJ_BLOB:
282
		return GIT_ENOTFOUND;
283 284

	case GIT_OBJ_TREE:
285
		return GIT_EAMBIGUOUS;
286 287

	default:
288
		return GIT_EINVALIDSPEC;
289 290 291
	}
}

292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
static int peel_error(int error, const git_oid *oid, git_otype type)
{
	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';

	giterr_set(GITERR_OBJECT, "The git_object of id '%s' can not be "
		"successfully peeled into a %s (git_otype=%i).", hex_oid, type_name, type);

	return error;
}

308
int git_object_peel(
309
	git_object **peeled,
Vicent Marti committed
310
	const git_object *object,
311
	git_otype target_type)
312 313
{
	git_object *source, *deref = NULL;
314 315
	int error;

316
	assert(object && peeled);
317 318

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

321 322 323 324 325
	assert(target_type == GIT_OBJ_TAG ||
		target_type == GIT_OBJ_COMMIT ||
		target_type == GIT_OBJ_TREE ||
		target_type == GIT_OBJ_BLOB ||
		target_type == GIT_OBJ_ANY);
326

Vicent Marti committed
327
	source = (git_object *)object;
328

329
	while (!(error = dereference_object(&deref, source))) {
330 331 332 333 334 335 336 337 338

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

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

339 340 341 342 343 344 345
		if (target_type == GIT_OBJ_ANY &&
			git_object_type(deref) != git_object_type(object))
		{
			*peeled = deref;
			return 0;
		}

346 347 348 349 350 351
		source = deref;
		deref = NULL;
	}

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

353
	git_object_free(deref);
354 355 356 357 358

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

	return error;
359
}
360

361 362 363 364 365 366
int git_object_dup(git_object **dest, git_object *source)
{
	git_cached_obj_incref(source);
	*dest = source;
	return 0;
}