1 /*
2 * Copyright (C) the libgit2 contributors. All rights reserved.
3 *
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
6 */
7
8 #include "object.h"
9
10 #include "git2/object.h"
11
12 #include "repository.h"
13
14 #include "commit.h"
15 #include "tree.h"
16 #include "blob.h"
17 #include "oid.h"
18 #include "tag.h"
19
20 bool git_object__strict_input_validation = true;
21
22 typedef struct {
23 const char *str; /* type name string */
24 size_t size; /* size in bytes of the object structure */
25
26 int (*parse)(void *self, git_odb_object *obj);
27 void (*free)(void *self);
28 } git_object_def;
29
30 static git_object_def git_objects_table[] = {
31 /* 0 = GIT_OBJ__EXT1 */
32 { "", 0, NULL, NULL },
33
34 /* 1 = GIT_OBJ_COMMIT */
35 { "commit", sizeof(git_commit), git_commit__parse, git_commit__free },
36
37 /* 2 = GIT_OBJ_TREE */
38 { "tree", sizeof(git_tree), git_tree__parse, git_tree__free },
39
40 /* 3 = GIT_OBJ_BLOB */
41 { "blob", sizeof(git_blob), git_blob__parse, git_blob__free },
42
43 /* 4 = GIT_OBJ_TAG */
44 { "tag", sizeof(git_tag), git_tag__parse, git_tag__free },
45
46 /* 5 = GIT_OBJ__EXT2 */
47 { "", 0, NULL, NULL },
48 /* 6 = GIT_OBJ_OFS_DELTA */
49 { "OFS_DELTA", 0, NULL, NULL },
50 /* 7 = GIT_OBJ_REF_DELTA */
51 { "REF_DELTA", 0, NULL, NULL },
52 };
53
git_object__from_odb_object(git_object ** object_out,git_repository * repo,git_odb_object * odb_obj,git_otype type)54 int git_object__from_odb_object(
55 git_object **object_out,
56 git_repository *repo,
57 git_odb_object *odb_obj,
58 git_otype type)
59 {
60 int error;
61 size_t object_size;
62 git_object_def *def;
63 git_object *object = NULL;
64
65 assert(object_out);
66 *object_out = NULL;
67
68 /* Validate type match */
69 if (type != GIT_OBJ_ANY && type != odb_obj->cached.type) {
70 giterr_set(GITERR_INVALID,
71 "the requested type does not match the type in the ODB");
72 return GIT_ENOTFOUND;
73 }
74
75 if ((object_size = git_object__size(odb_obj->cached.type)) == 0) {
76 giterr_set(GITERR_INVALID, "the requested type is invalid");
77 return GIT_ENOTFOUND;
78 }
79
80 /* Allocate and initialize base object */
81 object = git__calloc(1, object_size);
82 GITERR_CHECK_ALLOC(object);
83
84 git_oid_cpy(&object->cached.oid, &odb_obj->cached.oid);
85 object->cached.type = odb_obj->cached.type;
86 object->cached.size = odb_obj->cached.size;
87 object->repo = repo;
88
89 /* Parse raw object data */
90 def = &git_objects_table[odb_obj->cached.type];
91 assert(def->free && def->parse);
92
93 if ((error = def->parse(object, odb_obj)) < 0)
94 def->free(object);
95 else
96 *object_out = git_cache_store_parsed(&repo->objects, object);
97
98 return error;
99 }
100
git_object__free(void * obj)101 void git_object__free(void *obj)
102 {
103 git_otype type = ((git_object *)obj)->cached.type;
104
105 if (type < 0 || ((size_t)type) >= ARRAY_SIZE(git_objects_table) ||
106 !git_objects_table[type].free)
107 git__free(obj);
108 else
109 git_objects_table[type].free(obj);
110 }
111
git_object_lookup_prefix(git_object ** object_out,git_repository * repo,const git_oid * id,size_t len,git_otype type)112 int git_object_lookup_prefix(
113 git_object **object_out,
114 git_repository *repo,
115 const git_oid *id,
116 size_t len,
117 git_otype type)
118 {
119 git_object *object = NULL;
120 git_odb *odb = NULL;
121 git_odb_object *odb_obj = NULL;
122 int error = 0;
123
124 assert(repo && object_out && id);
125
126 if (len < GIT_OID_MINPREFIXLEN) {
127 giterr_set(GITERR_OBJECT, "ambiguous lookup - OID prefix is too short");
128 return GIT_EAMBIGUOUS;
129 }
130
131 error = git_repository_odb__weakptr(&odb, repo);
132 if (error < 0)
133 return error;
134
135 if (len > GIT_OID_HEXSZ)
136 len = GIT_OID_HEXSZ;
137
138 if (len == GIT_OID_HEXSZ) {
139 git_cached_obj *cached = NULL;
140
141 /* We want to match the full id : we can first look up in the cache,
142 * since there is no need to check for non ambiguousity
143 */
144 cached = git_cache_get_any(&repo->objects, id);
145 if (cached != NULL) {
146 if (cached->flags == GIT_CACHE_STORE_PARSED) {
147 object = (git_object *)cached;
148
149 if (type != GIT_OBJ_ANY && type != object->cached.type) {
150 git_object_free(object);
151 giterr_set(GITERR_INVALID,
152 "the requested type does not match the type in ODB");
153 return GIT_ENOTFOUND;
154 }
155
156 *object_out = object;
157 return 0;
158 } else if (cached->flags == GIT_CACHE_STORE_RAW) {
159 odb_obj = (git_odb_object *)cached;
160 } else {
161 assert(!"Wrong caching type in the global object cache");
162 }
163 } else {
164 /* Object was not found in the cache, let's explore the backends.
165 * We could just use git_odb_read_unique_short_oid,
166 * it is the same cost for packed and loose object backends,
167 * but it may be much more costly for sqlite and hiredis.
168 */
169 error = git_odb_read(&odb_obj, odb, id);
170 }
171 } else {
172 git_oid short_oid = {{ 0 }};
173
174 git_oid__cpy_prefix(&short_oid, id, len);
175
176 /* If len < GIT_OID_HEXSZ (a strict short oid was given), we have
177 * 2 options :
178 * - We always search in the cache first. If we find that short oid is
179 * ambiguous, we can stop. But in all the other cases, we must then
180 * explore all the backends (to find an object if there was match,
181 * or to check that oid is not ambiguous if we have found 1 match in
182 * the cache)
183 * - We never explore the cache, go right to exploring the backends
184 * We chose the latter : we explore directly the backends.
185 */
186 error = git_odb_read_prefix(&odb_obj, odb, &short_oid, len);
187 }
188
189 if (error < 0)
190 return error;
191
192 error = git_object__from_odb_object(object_out, repo, odb_obj, type);
193
194 git_odb_object_free(odb_obj);
195
196 return error;
197 }
198
git_object_lookup(git_object ** object_out,git_repository * repo,const git_oid * id,git_otype type)199 int git_object_lookup(git_object **object_out, git_repository *repo, const git_oid *id, git_otype type) {
200 return git_object_lookup_prefix(object_out, repo, id, GIT_OID_HEXSZ, type);
201 }
202
git_object_free(git_object * object)203 void git_object_free(git_object *object)
204 {
205 if (object == NULL)
206 return;
207
208 git_cached_obj_decref(object);
209 }
210
git_object_id(const git_object * obj)211 const git_oid *git_object_id(const git_object *obj)
212 {
213 assert(obj);
214 return &obj->cached.oid;
215 }
216
git_object_type(const git_object * obj)217 git_otype git_object_type(const git_object *obj)
218 {
219 assert(obj);
220 return obj->cached.type;
221 }
222
git_object_owner(const git_object * obj)223 git_repository *git_object_owner(const git_object *obj)
224 {
225 assert(obj);
226 return obj->repo;
227 }
228
git_object_type2string(git_otype type)229 const char *git_object_type2string(git_otype type)
230 {
231 if (type < 0 || ((size_t) type) >= ARRAY_SIZE(git_objects_table))
232 return "";
233
234 return git_objects_table[type].str;
235 }
236
git_object_string2type(const char * str)237 git_otype git_object_string2type(const char *str)
238 {
239 if (!str)
240 return GIT_OBJ_BAD;
241
242 return git_object_stringn2type(str, strlen(str));
243 }
244
git_object_stringn2type(const char * str,size_t len)245 git_otype git_object_stringn2type(const char *str, size_t len)
246 {
247 size_t i;
248
249 if (!str || !len || !*str)
250 return GIT_OBJ_BAD;
251
252 for (i = 0; i < ARRAY_SIZE(git_objects_table); i++)
253 if (*git_objects_table[i].str &&
254 !git__prefixncmp(str, len, git_objects_table[i].str))
255 return (git_otype)i;
256
257 return GIT_OBJ_BAD;
258 }
259
git_object_typeisloose(git_otype type)260 int git_object_typeisloose(git_otype type)
261 {
262 if (type < 0 || ((size_t) type) >= ARRAY_SIZE(git_objects_table))
263 return 0;
264
265 return (git_objects_table[type].size > 0) ? 1 : 0;
266 }
267
git_object__size(git_otype type)268 size_t git_object__size(git_otype type)
269 {
270 if (type < 0 || ((size_t) type) >= ARRAY_SIZE(git_objects_table))
271 return 0;
272
273 return git_objects_table[type].size;
274 }
275
dereference_object(git_object ** dereferenced,git_object * obj)276 static int dereference_object(git_object **dereferenced, git_object *obj)
277 {
278 git_otype type = git_object_type(obj);
279
280 switch (type) {
281 case GIT_OBJ_COMMIT:
282 return git_commit_tree((git_tree **)dereferenced, (git_commit*)obj);
283
284 case GIT_OBJ_TAG:
285 return git_tag_target(dereferenced, (git_tag*)obj);
286
287 case GIT_OBJ_BLOB:
288 case GIT_OBJ_TREE:
289 return GIT_EPEEL;
290
291 default:
292 return GIT_EINVALIDSPEC;
293 }
294 }
295
peel_error(int error,const git_oid * oid,git_otype type)296 static int peel_error(int error, const git_oid *oid, git_otype type)
297 {
298 const char *type_name;
299 char hex_oid[GIT_OID_HEXSZ + 1];
300
301 type_name = git_object_type2string(type);
302
303 git_oid_fmt(hex_oid, oid);
304 hex_oid[GIT_OID_HEXSZ] = '\0';
305
306 giterr_set(GITERR_OBJECT, "the git_object of id '%s' can not be "
307 "successfully peeled into a %s (git_otype=%i).", hex_oid, type_name, type);
308
309 return error;
310 }
311
check_type_combination(git_otype type,git_otype target)312 static int check_type_combination(git_otype type, git_otype target)
313 {
314 if (type == target)
315 return 0;
316
317 switch (type) {
318 case GIT_OBJ_BLOB:
319 case GIT_OBJ_TREE:
320 /* a blob or tree can never be peeled to anything but themselves */
321 return GIT_EINVALIDSPEC;
322 break;
323 case GIT_OBJ_COMMIT:
324 /* a commit can only be peeled to a tree */
325 if (target != GIT_OBJ_TREE && target != GIT_OBJ_ANY)
326 return GIT_EINVALIDSPEC;
327 break;
328 case GIT_OBJ_TAG:
329 /* a tag may point to anything, so we let anything through */
330 break;
331 default:
332 return GIT_EINVALIDSPEC;
333 }
334
335 return 0;
336 }
337
git_object_peel(git_object ** peeled,const git_object * object,git_otype target_type)338 int git_object_peel(
339 git_object **peeled,
340 const git_object *object,
341 git_otype target_type)
342 {
343 git_object *source, *deref = NULL;
344 int error;
345
346 assert(object && peeled);
347
348 assert(target_type == GIT_OBJ_TAG ||
349 target_type == GIT_OBJ_COMMIT ||
350 target_type == GIT_OBJ_TREE ||
351 target_type == GIT_OBJ_BLOB ||
352 target_type == GIT_OBJ_ANY);
353
354 if ((error = check_type_combination(git_object_type(object), target_type)) < 0)
355 return peel_error(error, git_object_id(object), target_type);
356
357 if (git_object_type(object) == target_type)
358 return git_object_dup(peeled, (git_object *)object);
359
360 source = (git_object *)object;
361
362 while (!(error = dereference_object(&deref, source))) {
363
364 if (source != object)
365 git_object_free(source);
366
367 if (git_object_type(deref) == target_type) {
368 *peeled = deref;
369 return 0;
370 }
371
372 if (target_type == GIT_OBJ_ANY &&
373 git_object_type(deref) != git_object_type(object))
374 {
375 *peeled = deref;
376 return 0;
377 }
378
379 source = deref;
380 deref = NULL;
381 }
382
383 if (source != object)
384 git_object_free(source);
385
386 git_object_free(deref);
387
388 if (error)
389 error = peel_error(error, git_object_id(object), target_type);
390
391 return error;
392 }
393
git_object_dup(git_object ** dest,git_object * source)394 int git_object_dup(git_object **dest, git_object *source)
395 {
396 git_cached_obj_incref(source);
397 *dest = source;
398 return 0;
399 }
400
git_object_lookup_bypath(git_object ** out,const git_object * treeish,const char * path,git_otype type)401 int git_object_lookup_bypath(
402 git_object **out,
403 const git_object *treeish,
404 const char *path,
405 git_otype type)
406 {
407 int error = -1;
408 git_tree *tree = NULL;
409 git_tree_entry *entry = NULL;
410
411 assert(out && treeish && path);
412
413 if ((error = git_object_peel((git_object**)&tree, treeish, GIT_OBJ_TREE)) < 0 ||
414 (error = git_tree_entry_bypath(&entry, tree, path)) < 0)
415 {
416 goto cleanup;
417 }
418
419 if (type != GIT_OBJ_ANY && git_tree_entry_type(entry) != type)
420 {
421 giterr_set(GITERR_OBJECT,
422 "object at path '%s' is not of the asked-for type %d",
423 path, type);
424 error = GIT_EINVALIDSPEC;
425 goto cleanup;
426 }
427
428 error = git_tree_entry_to_object(out, git_object_owner(treeish), entry);
429
430 cleanup:
431 git_tree_entry_free(entry);
432 git_tree_free(tree);
433 return error;
434 }
435
git_object_short_id(git_buf * out,const git_object * obj)436 int git_object_short_id(git_buf *out, const git_object *obj)
437 {
438 git_repository *repo;
439 int len = GIT_ABBREV_DEFAULT, error;
440 git_oid id = {{0}};
441 git_odb *odb;
442
443 assert(out && obj);
444
445 git_buf_sanitize(out);
446 repo = git_object_owner(obj);
447
448 if ((error = git_repository__cvar(&len, repo, GIT_CVAR_ABBREV)) < 0)
449 return error;
450
451 if ((error = git_repository_odb(&odb, repo)) < 0)
452 return error;
453
454 while (len < GIT_OID_HEXSZ) {
455 /* set up short oid */
456 memcpy(&id.id, &obj->cached.oid.id, (len + 1) / 2);
457 if (len & 1)
458 id.id[len / 2] &= 0xf0;
459
460 error = git_odb_exists_prefix(NULL, odb, &id, len);
461 if (error != GIT_EAMBIGUOUS)
462 break;
463
464 giterr_clear();
465 len++;
466 }
467
468 if (!error && !(error = git_buf_grow(out, len + 1))) {
469 git_oid_tostr(out->ptr, len + 1, &id);
470 out->size = len;
471 }
472
473 git_odb_free(odb);
474
475 return error;
476 }
477
git_object__is_valid(git_repository * repo,const git_oid * id,git_otype expected_type)478 bool git_object__is_valid(
479 git_repository *repo, const git_oid *id, git_otype expected_type)
480 {
481 git_odb *odb;
482 git_otype actual_type;
483 size_t len;
484 int error;
485
486 if (!git_object__strict_input_validation)
487 return true;
488
489 if ((error = git_repository_odb__weakptr(&odb, repo)) < 0 ||
490 (error = git_odb_read_header(&len, &actual_type, odb, id)) < 0)
491 return false;
492
493 if (expected_type != GIT_OBJ_ANY && expected_type != actual_type) {
494 giterr_set(GITERR_INVALID,
495 "the requested type does not match the type in the ODB");
496 return false;
497 }
498
499 return true;
500 }
501
502