]> git.proxmox.com Git - libgit2.git/blame - src/object.c
Update upstream source from tag 'upstream/1.3.0+dfsg.1'
[libgit2.git] / src / object.c
CommitLineData
e52ed7a5 1/*
359fc2d2 2 * Copyright (C) the libgit2 contributors. All rights reserved.
e52ed7a5 3 *
bb742ede
VM
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.
e52ed7a5 6 */
eae0bfdc
PP
7
8#include "object.h"
9
e52ed7a5
VM
10#include "git2/object.h"
11
e52ed7a5
VM
12#include "repository.h"
13
14#include "commit.h"
ac3d33df 15#include "hash.h"
e52ed7a5
VM
16#include "tree.h"
17#include "blob.h"
61d7328d 18#include "oid.h"
e52ed7a5
VM
19#include "tag.h"
20
f2dddf52 21bool git_object__strict_input_validation = true;
22a19f5b 22
ac3d33df 23extern int git_odb_hash(git_oid *out, const void *data, size_t len, git_object_t type);
22a2d3d5 24size_t git_object__size(git_object_t type);
ac3d33df 25
78606263 26typedef struct {
87d9869f 27 const char *str; /* type name string */
e52ed7a5 28 size_t size; /* size in bytes of the object structure */
78606263 29
3f27127d 30 int (*parse)(void *self, git_odb_object *obj);
ac3d33df 31 int (*parse_raw)(void *self, const char *data, size_t size);
78606263
RB
32 void (*free)(void *self);
33} git_object_def;
34
35static git_object_def git_objects_table[] = {
ac3d33df
JK
36 /* 0 = GIT_OBJECT__EXT1 */
37 { "", 0, NULL, NULL, NULL },
e52ed7a5 38
ac3d33df
JK
39 /* 1 = GIT_OBJECT_COMMIT */
40 { "commit", sizeof(git_commit), git_commit__parse, git_commit__parse_raw, git_commit__free },
e52ed7a5 41
ac3d33df
JK
42 /* 2 = GIT_OBJECT_TREE */
43 { "tree", sizeof(git_tree), git_tree__parse, git_tree__parse_raw, git_tree__free },
e52ed7a5 44
ac3d33df
JK
45 /* 3 = GIT_OBJECT_BLOB */
46 { "blob", sizeof(git_blob), git_blob__parse, git_blob__parse_raw, git_blob__free },
e52ed7a5 47
ac3d33df
JK
48 /* 4 = GIT_OBJECT_TAG */
49 { "tag", sizeof(git_tag), git_tag__parse, git_tag__parse_raw, git_tag__free },
e52ed7a5 50
ac3d33df
JK
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 },
e52ed7a5
VM
57};
58
ac3d33df
JK
59int git_object__from_raw(
60 git_object **object_out,
61 const char *data,
62 size_t size,
63 git_object_t type)
64{
65 git_object_def *def;
66 git_object *object;
67 size_t object_size;
68 int error;
69
c25aa7cd 70 GIT_ASSERT_ARG(object_out);
ac3d33df
JK
71 *object_out = NULL;
72
73 /* 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 return GIT_ENOTFOUND;
77 }
78
79 if ((object_size = git_object__size(type)) == 0) {
80 git_error_set(GIT_ERROR_INVALID, "the requested type is invalid");
81 return GIT_ENOTFOUND;
82 }
83
84 /* Allocate and initialize base object */
85 object = git__calloc(1, object_size);
86 GIT_ERROR_CHECK_ALLOC(object);
87 object->cached.flags = GIT_CACHE_STORE_PARSED;
88 object->cached.type = type;
22a2d3d5
UG
89 if ((error = git_odb_hash(&object->cached.oid, data, size, type)) < 0)
90 return error;
ac3d33df
JK
91
92 /* Parse raw object data */
93 def = &git_objects_table[type];
c25aa7cd 94 GIT_ASSERT(def->free && def->parse_raw);
ac3d33df
JK
95
96 if ((error = def->parse_raw(object, data, size)) < 0) {
97 def->free(object);
98 return error;
99 }
100
101 git_cached_obj_incref(object);
102 *object_out = object;
103
104 return 0;
105}
106
c6ac28fd
RB
107int git_object__from_odb_object(
108 git_object **object_out,
109 git_repository *repo,
110 git_odb_object *odb_obj,
ac3d33df 111 git_object_t type)
c6ac28fd
RB
112{
113 int error;
78606263
RB
114 size_t object_size;
115 git_object_def *def;
c6ac28fd
RB
116 git_object *object = NULL;
117
c25aa7cd 118 GIT_ASSERT_ARG(object_out);
78606263
RB
119 *object_out = NULL;
120
121 /* Validate type match */
ac3d33df
JK
122 if (type != GIT_OBJECT_ANY && type != odb_obj->cached.type) {
123 git_error_set(GIT_ERROR_INVALID,
909d5494 124 "the requested type does not match the type in the ODB");
c6ac28fd
RB
125 return GIT_ENOTFOUND;
126 }
127
78606263 128 if ((object_size = git_object__size(odb_obj->cached.type)) == 0) {
ac3d33df 129 git_error_set(GIT_ERROR_INVALID, "the requested type is invalid");
78606263
RB
130 return GIT_ENOTFOUND;
131 }
132
133 /* Allocate and initialize base object */
134 object = git__calloc(1, object_size);
ac3d33df 135 GIT_ERROR_CHECK_ALLOC(object);
c6ac28fd 136
c6ac28fd 137 git_oid_cpy(&object->cached.oid, &odb_obj->cached.oid);
cf7850a4 138 object->cached.type = odb_obj->cached.type;
78606263 139 object->cached.size = odb_obj->cached.size;
c6ac28fd
RB
140 object->repo = repo;
141
78606263
RB
142 /* Parse raw object data */
143 def = &git_objects_table[odb_obj->cached.type];
c25aa7cd 144 GIT_ASSERT(def->free && def->parse);
c6ac28fd 145
3f27127d 146 if ((error = def->parse(object, odb_obj)) < 0)
78606263 147 def->free(object);
3f27127d
RB
148 else
149 *object_out = git_cache_store_parsed(&repo->objects, object);
c6ac28fd 150
3f27127d 151 return error;
c6ac28fd
RB
152}
153
78606263
RB
154void git_object__free(void *obj)
155{
ac3d33df 156 git_object_t type = ((git_object *)obj)->cached.type;
78606263
RB
157
158 if (type < 0 || ((size_t)type) >= ARRAY_SIZE(git_objects_table) ||
159 !git_objects_table[type].free)
160 git__free(obj);
161 else
162 git_objects_table[type].free(obj);
163}
164
9462c471
VM
165int git_object_lookup_prefix(
166 git_object **object_out,
167 git_repository *repo,
168 const git_oid *id,
b8457baa 169 size_t len,
ac3d33df 170 git_object_t type)
5de079b8
VM
171{
172 git_object *object = NULL;
9462c471 173 git_odb *odb = NULL;
e583334c 174 git_odb_object *odb_obj = NULL;
e172cf08 175 int error = 0;
5de079b8 176
c25aa7cd
PP
177 GIT_ASSERT_ARG(repo);
178 GIT_ASSERT_ARG(object_out);
179 GIT_ASSERT_ARG(id);
5de079b8 180
8915a140 181 if (len < GIT_OID_MINPREFIXLEN) {
ac3d33df 182 git_error_set(GIT_ERROR_OBJECT, "ambiguous lookup - OID prefix is too short");
904b67e6 183 return GIT_EAMBIGUOUS;
8915a140 184 }
d0323a5f 185
9462c471 186 error = git_repository_odb__weakptr(&odb, repo);
e172cf08 187 if (error < 0)
9462c471
VM
188 return error;
189
3d9ef2dc
VM
190 if (len > GIT_OID_HEXSZ)
191 len = GIT_OID_HEXSZ;
bd1aa741 192
3d9ef2dc 193 if (len == GIT_OID_HEXSZ) {
5df18424
VM
194 git_cached_obj *cached = NULL;
195
dd453c4d
MP
196 /* We want to match the full id : we can first look up in the cache,
197 * since there is no need to check for non ambiguousity
198 */
5df18424
VM
199 cached = git_cache_get_any(&repo->objects, id);
200 if (cached != NULL) {
201 if (cached->flags == GIT_CACHE_STORE_PARSED) {
202 object = (git_object *)cached;
203
ac3d33df 204 if (type != GIT_OBJECT_ANY && type != object->cached.type) {
5df18424 205 git_object_free(object);
ac3d33df 206 git_error_set(GIT_ERROR_INVALID,
22a2d3d5 207 "the requested type does not match the type in the ODB");
5df18424
VM
208 return GIT_ENOTFOUND;
209 }
210
211 *object_out = object;
212 return 0;
213 } else if (cached->flags == GIT_CACHE_STORE_RAW) {
214 odb_obj = (git_odb_object *)cached;
215 } else {
c25aa7cd 216 GIT_ASSERT(!"Wrong caching type in the global object cache");
7d3ec3ca 217 }
5df18424
VM
218 } else {
219 /* Object was not found in the cache, let's explore the backends.
220 * We could just use git_odb_read_unique_short_oid,
221 * it is the same cost for packed and loose object backends,
222 * but it may be much more costly for sqlite and hiredis.
223 */
224 error = git_odb_read(&odb_obj, odb, id);
dd453c4d 225 }
dd453c4d 226 } else {
61d7328d 227 git_oid short_oid = {{ 0 }};
dd453c4d 228
61d7328d 229 git_oid__cpy_prefix(&short_oid, id, len);
dd453c4d 230
3d9ef2dc 231 /* If len < GIT_OID_HEXSZ (a strict short oid was given), we have
dd453c4d
MP
232 * 2 options :
233 * - We always search in the cache first. If we find that short oid is
87d9869f
VM
234 * ambiguous, we can stop. But in all the other cases, we must then
235 * explore all the backends (to find an object if there was match,
236 * or to check that oid is not ambiguous if we have found 1 match in
237 * the cache)
dd453c4d
MP
238 * - We never explore the cache, go right to exploring the backends
239 * We chose the latter : we explore directly the backends.
240 */
9462c471 241 error = git_odb_read_prefix(&odb_obj, odb, &short_oid, len);
5de079b8
VM
242 }
243
3aa351ea 244 if (error < 0)
282283ac 245 return error;
5de079b8 246
c6ac28fd 247 error = git_object__from_odb_object(object_out, repo, odb_obj, type);
5de079b8 248
45e79e37 249 git_odb_object_free(odb_obj);
72a3fe42 250
c6ac28fd 251 return error;
5de079b8
VM
252}
253
ac3d33df 254int git_object_lookup(git_object **object_out, git_repository *repo, const git_oid *id, git_object_t type) {
d0323a5f 255 return git_object_lookup_prefix(object_out, repo, id, GIT_OID_HEXSZ, type);
dd453c4d
MP
256}
257
45e79e37 258void git_object_free(git_object *object)
48c27f86
VM
259{
260 if (object == NULL)
261 return;
262
5df18424 263 git_cached_obj_decref(object);
48c27f86
VM
264}
265
17cdf252 266const git_oid *git_object_id(const git_object *obj)
e52ed7a5 267{
c25aa7cd 268 GIT_ASSERT_ARG_WITH_RETVAL(obj, NULL);
72a3fe42 269 return &obj->cached.oid;
e52ed7a5
VM
270}
271
ac3d33df 272git_object_t git_object_type(const git_object *obj)
e52ed7a5 273{
c25aa7cd 274 GIT_ASSERT_ARG_WITH_RETVAL(obj, GIT_OBJECT_INVALID);
cf7850a4 275 return obj->cached.type;
e52ed7a5
VM
276}
277
17cdf252 278git_repository *git_object_owner(const git_object *obj)
e52ed7a5 279{
c25aa7cd 280 GIT_ASSERT_ARG_WITH_RETVAL(obj, NULL);
e52ed7a5
VM
281 return obj->repo;
282}
283
ac3d33df 284const char *git_object_type2string(git_object_t type)
e52ed7a5
VM
285{
286 if (type < 0 || ((size_t) type) >= ARRAY_SIZE(git_objects_table))
287 return "";
288
289 return git_objects_table[type].str;
290}
291
ac3d33df 292git_object_t git_object_string2type(const char *str)
eae0bfdc
PP
293{
294 if (!str)
ac3d33df 295 return GIT_OBJECT_INVALID;
eae0bfdc
PP
296
297 return git_object_stringn2type(str, strlen(str));
298}
299
ac3d33df 300git_object_t git_object_stringn2type(const char *str, size_t len)
e52ed7a5
VM
301{
302 size_t i;
303
eae0bfdc 304 if (!str || !len || !*str)
ac3d33df 305 return GIT_OBJECT_INVALID;
e52ed7a5
VM
306
307 for (i = 0; i < ARRAY_SIZE(git_objects_table); i++)
eae0bfdc
PP
308 if (*git_objects_table[i].str &&
309 !git__prefixncmp(str, len, git_objects_table[i].str))
ac3d33df 310 return (git_object_t)i;
e52ed7a5 311
ac3d33df 312 return GIT_OBJECT_INVALID;
e52ed7a5
VM
313}
314
ac3d33df 315int git_object_typeisloose(git_object_t type)
e52ed7a5
VM
316{
317 if (type < 0 || ((size_t) type) >= ARRAY_SIZE(git_objects_table))
318 return 0;
319
78606263 320 return (git_objects_table[type].size > 0) ? 1 : 0;
e52ed7a5
VM
321}
322
ac3d33df 323size_t git_object__size(git_object_t type)
e52ed7a5
VM
324{
325 if (type < 0 || ((size_t) type) >= ARRAY_SIZE(git_objects_table))
326 return 0;
327
328 return git_objects_table[type].size;
329}
330
db9be945 331static int dereference_object(git_object **dereferenced, git_object *obj)
332{
ac3d33df 333 git_object_t type = git_object_type(obj);
db9be945 334
335 switch (type) {
ac3d33df 336 case GIT_OBJECT_COMMIT:
db9be945 337 return git_commit_tree((git_tree **)dereferenced, (git_commit*)obj);
db9be945 338
ac3d33df 339 case GIT_OBJECT_TAG:
db9be945 340 return git_tag_target(dereferenced, (git_tag*)obj);
d8057a5b 341
ac3d33df
JK
342 case GIT_OBJECT_BLOB:
343 case GIT_OBJECT_TREE:
753e17b0 344 return GIT_EPEEL;
db9be945 345
346 default:
bc05f30c 347 return GIT_EINVALIDSPEC;
db9be945 348 }
349}
350
ac3d33df 351static int peel_error(int error, const git_oid *oid, git_object_t type)
bc05f30c 352{
353 const char *type_name;
354 char hex_oid[GIT_OID_HEXSZ + 1];
355
356 type_name = git_object_type2string(type);
357
358 git_oid_fmt(hex_oid, oid);
359 hex_oid[GIT_OID_HEXSZ] = '\0';
360
ac3d33df
JK
361 git_error_set(GIT_ERROR_OBJECT, "the git_object of id '%s' can not be "
362 "successfully peeled into a %s (git_object_t=%i).", hex_oid, type_name, type);
bc05f30c 363
364 return error;
365}
366
ac3d33df 367static int check_type_combination(git_object_t type, git_object_t target)
753e17b0
CMN
368{
369 if (type == target)
370 return 0;
371
372 switch (type) {
ac3d33df
JK
373 case GIT_OBJECT_BLOB:
374 case GIT_OBJECT_TREE:
753e17b0
CMN
375 /* a blob or tree can never be peeled to anything but themselves */
376 return GIT_EINVALIDSPEC;
377 break;
ac3d33df 378 case GIT_OBJECT_COMMIT:
753e17b0 379 /* a commit can only be peeled to a tree */
ac3d33df 380 if (target != GIT_OBJECT_TREE && target != GIT_OBJECT_ANY)
753e17b0
CMN
381 return GIT_EINVALIDSPEC;
382 break;
ac3d33df 383 case GIT_OBJECT_TAG:
753e17b0
CMN
384 /* a tag may point to anything, so we let anything through */
385 break;
386 default:
387 return GIT_EINVALIDSPEC;
388 }
389
390 return 0;
391}
392
db9be945 393int git_object_peel(
d8057a5b 394 git_object **peeled,
cfbe4be3 395 const git_object *object,
ac3d33df 396 git_object_t target_type)
db9be945 397{
398 git_object *source, *deref = NULL;
bc05f30c 399 int error;
400
c25aa7cd
PP
401 GIT_ASSERT_ARG(object);
402 GIT_ASSERT_ARG(peeled);
db9be945 403
c25aa7cd 404 GIT_ASSERT_ARG(target_type == GIT_OBJECT_TAG ||
ac3d33df
JK
405 target_type == GIT_OBJECT_COMMIT ||
406 target_type == GIT_OBJECT_TREE ||
407 target_type == GIT_OBJECT_BLOB ||
408 target_type == GIT_OBJECT_ANY);
8915a140 409
753e17b0
CMN
410 if ((error = check_type_combination(git_object_type(object), target_type)) < 0)
411 return peel_error(error, git_object_id(object), target_type);
412
413 if (git_object_type(object) == target_type)
414 return git_object_dup(peeled, (git_object *)object);
415
cfbe4be3 416 source = (git_object *)object;
db9be945 417
bc05f30c 418 while (!(error = dereference_object(&deref, source))) {
db9be945 419
420 if (source != object)
421 git_object_free(source);
422
423 if (git_object_type(deref) == target_type) {
424 *peeled = deref;
425 return 0;
426 }
427
ac3d33df 428 if (target_type == GIT_OBJECT_ANY &&
d8057a5b
RB
429 git_object_type(deref) != git_object_type(object))
430 {
431 *peeled = deref;
432 return 0;
433 }
434
db9be945 435 source = deref;
436 deref = NULL;
437 }
438
db9be945 439 if (source != object)
440 git_object_free(source);
d8057a5b 441
db9be945 442 git_object_free(deref);
bc05f30c 443
444 if (error)
445 error = peel_error(error, git_object_id(object), target_type);
446
447 return error;
db9be945 448}
613d5eb9 449
575a54db
VM
450int git_object_dup(git_object **dest, git_object *source)
451{
452 git_cached_obj_incref(source);
453 *dest = source;
454 return 0;
455}
ceab4e26
BS
456
457int git_object_lookup_bypath(
458 git_object **out,
459 const git_object *treeish,
460 const char *path,
ac3d33df 461 git_object_t type)
ceab4e26
BS
462{
463 int error = -1;
464 git_tree *tree = NULL;
465 git_tree_entry *entry = NULL;
ceab4e26 466
c25aa7cd
PP
467 GIT_ASSERT_ARG(out);
468 GIT_ASSERT_ARG(treeish);
469 GIT_ASSERT_ARG(path);
ceab4e26 470
ac3d33df 471 if ((error = git_object_peel((git_object**)&tree, treeish, GIT_OBJECT_TREE)) < 0 ||
ba02079f 472 (error = git_tree_entry_bypath(&entry, tree, path)) < 0)
ceab4e26
BS
473 {
474 goto cleanup;
475 }
476
ac3d33df 477 if (type != GIT_OBJECT_ANY && git_tree_entry_type(entry) != type)
ba02079f 478 {
ac3d33df 479 git_error_set(GIT_ERROR_OBJECT,
ceab4e26
BS
480 "object at path '%s' is not of the asked-for type %d",
481 path, type);
482 error = GIT_EINVALIDSPEC;
ba02079f 483 goto cleanup;
ceab4e26
BS
484 }
485
ba02079f
BS
486 error = git_tree_entry_to_object(out, git_object_owner(treeish), entry);
487
ceab4e26
BS
488cleanup:
489 git_tree_entry_free(entry);
490 git_tree_free(tree);
491 return error;
492}
13f7ecd7
RB
493
494int git_object_short_id(git_buf *out, const git_object *obj)
495{
496 git_repository *repo;
497 int len = GIT_ABBREV_DEFAULT, error;
498 git_oid id = {{0}};
499 git_odb *odb;
500
c25aa7cd
PP
501 GIT_ASSERT_ARG(out);
502 GIT_ASSERT_ARG(obj);
503
504 if ((error = git_buf_sanitize(out)) < 0)
505 return error;
13f7ecd7 506
13f7ecd7
RB
507 repo = git_object_owner(obj);
508
22a2d3d5 509 if ((error = git_repository__configmap_lookup(&len, repo, GIT_CONFIGMAP_ABBREV)) < 0)
13f7ecd7
RB
510 return error;
511
512 if ((error = git_repository_odb(&odb, repo)) < 0)
513 return error;
514
515 while (len < GIT_OID_HEXSZ) {
516 /* set up short oid */
517 memcpy(&id.id, &obj->cached.oid.id, (len + 1) / 2);
518 if (len & 1)
519 id.id[len / 2] &= 0xf0;
520
521 error = git_odb_exists_prefix(NULL, odb, &id, len);
522 if (error != GIT_EAMBIGUOUS)
523 break;
524
ac3d33df 525 git_error_clear();
13f7ecd7
RB
526 len++;
527 }
528
529 if (!error && !(error = git_buf_grow(out, len + 1))) {
530 git_oid_tostr(out->ptr, len + 1, &id);
531 out->size = len;
532 }
533
534 git_odb_free(odb);
535
536 return error;
537}
538
3ef01e77 539bool git_object__is_valid(
ac3d33df 540 git_repository *repo, const git_oid *id, git_object_t expected_type)
3ef01e77
ET
541{
542 git_odb *odb;
ac3d33df 543 git_object_t actual_type;
3ef01e77
ET
544 size_t len;
545 int error;
546
547 if (!git_object__strict_input_validation)
548 return true;
549
550 if ((error = git_repository_odb__weakptr(&odb, repo)) < 0 ||
551 (error = git_odb_read_header(&len, &actual_type, odb, id)) < 0)
552 return false;
553
ac3d33df
JK
554 if (expected_type != GIT_OBJECT_ANY && expected_type != actual_type) {
555 git_error_set(GIT_ERROR_INVALID,
3ef01e77
ET
556 "the requested type does not match the type in the ODB");
557 return false;
558 }
559
560 return true;
561}