2 * Copyright (C) the libgit2 contributors. All rights reserved.
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.
7 #include "git2/object.h"
10 #include "repository.h"
18 bool git_object__strict_input_validation
= true;
21 const char *str
; /* type name string */
22 size_t size
; /* size in bytes of the object structure */
24 int (*parse
)(void *self
, git_odb_object
*obj
);
25 void (*free
)(void *self
);
28 static git_object_def git_objects_table
[] = {
29 /* 0 = GIT_OBJ__EXT1 */
30 { "", 0, NULL
, NULL
},
32 /* 1 = GIT_OBJ_COMMIT */
33 { "commit", sizeof(git_commit
), git_commit__parse
, git_commit__free
},
35 /* 2 = GIT_OBJ_TREE */
36 { "tree", sizeof(git_tree
), git_tree__parse
, git_tree__free
},
38 /* 3 = GIT_OBJ_BLOB */
39 { "blob", sizeof(git_blob
), git_blob__parse
, git_blob__free
},
42 { "tag", sizeof(git_tag
), git_tag__parse
, git_tag__free
},
44 /* 5 = GIT_OBJ__EXT2 */
45 { "", 0, NULL
, NULL
},
46 /* 6 = GIT_OBJ_OFS_DELTA */
47 { "OFS_DELTA", 0, NULL
, NULL
},
48 /* 7 = GIT_OBJ_REF_DELTA */
49 { "REF_DELTA", 0, NULL
, NULL
},
52 int git_object__from_odb_object(
53 git_object
**object_out
,
55 git_odb_object
*odb_obj
,
61 git_object
*object
= NULL
;
66 /* Validate type match */
67 if (type
!= GIT_OBJ_ANY
&& type
!= odb_obj
->cached
.type
) {
68 giterr_set(GITERR_INVALID
,
69 "the requested type does not match the type in the ODB");
73 if ((object_size
= git_object__size(odb_obj
->cached
.type
)) == 0) {
74 giterr_set(GITERR_INVALID
, "the requested type is invalid");
78 /* Allocate and initialize base object */
79 object
= git__calloc(1, object_size
);
80 GITERR_CHECK_ALLOC(object
);
82 git_oid_cpy(&object
->cached
.oid
, &odb_obj
->cached
.oid
);
83 object
->cached
.type
= odb_obj
->cached
.type
;
84 object
->cached
.size
= odb_obj
->cached
.size
;
87 /* Parse raw object data */
88 def
= &git_objects_table
[odb_obj
->cached
.type
];
89 assert(def
->free
&& def
->parse
);
91 if ((error
= def
->parse(object
, odb_obj
)) < 0)
94 *object_out
= git_cache_store_parsed(&repo
->objects
, object
);
99 void git_object__free(void *obj
)
101 git_otype type
= ((git_object
*)obj
)->cached
.type
;
103 if (type
< 0 || ((size_t)type
) >= ARRAY_SIZE(git_objects_table
) ||
104 !git_objects_table
[type
].free
)
107 git_objects_table
[type
].free(obj
);
110 int git_object_lookup_prefix(
111 git_object
**object_out
,
112 git_repository
*repo
,
117 git_object
*object
= NULL
;
119 git_odb_object
*odb_obj
= NULL
;
122 assert(repo
&& object_out
&& id
);
124 if (len
< GIT_OID_MINPREFIXLEN
) {
125 giterr_set(GITERR_OBJECT
, "ambiguous lookup - OID prefix is too short");
126 return GIT_EAMBIGUOUS
;
129 error
= git_repository_odb__weakptr(&odb
, repo
);
133 if (len
> GIT_OID_HEXSZ
)
136 if (len
== GIT_OID_HEXSZ
) {
137 git_cached_obj
*cached
= NULL
;
139 /* We want to match the full id : we can first look up in the cache,
140 * since there is no need to check for non ambiguousity
142 cached
= git_cache_get_any(&repo
->objects
, id
);
143 if (cached
!= NULL
) {
144 if (cached
->flags
== GIT_CACHE_STORE_PARSED
) {
145 object
= (git_object
*)cached
;
147 if (type
!= GIT_OBJ_ANY
&& type
!= object
->cached
.type
) {
148 git_object_free(object
);
149 giterr_set(GITERR_INVALID
,
150 "the requested type does not match the type in ODB");
151 return GIT_ENOTFOUND
;
154 *object_out
= object
;
156 } else if (cached
->flags
== GIT_CACHE_STORE_RAW
) {
157 odb_obj
= (git_odb_object
*)cached
;
159 assert(!"Wrong caching type in the global object cache");
162 /* Object was not found in the cache, let's explore the backends.
163 * We could just use git_odb_read_unique_short_oid,
164 * it is the same cost for packed and loose object backends,
165 * but it may be much more costly for sqlite and hiredis.
167 error
= git_odb_read(&odb_obj
, odb
, id
);
170 git_oid short_oid
= {{ 0 }};
172 git_oid__cpy_prefix(&short_oid
, id
, len
);
174 /* If len < GIT_OID_HEXSZ (a strict short oid was given), we have
176 * - We always search in the cache first. If we find that short oid is
177 * ambiguous, we can stop. But in all the other cases, we must then
178 * explore all the backends (to find an object if there was match,
179 * or to check that oid is not ambiguous if we have found 1 match in
181 * - We never explore the cache, go right to exploring the backends
182 * We chose the latter : we explore directly the backends.
184 error
= git_odb_read_prefix(&odb_obj
, odb
, &short_oid
, len
);
190 error
= git_object__from_odb_object(object_out
, repo
, odb_obj
, type
);
192 git_odb_object_free(odb_obj
);
197 int git_object_lookup(git_object
**object_out
, git_repository
*repo
, const git_oid
*id
, git_otype type
) {
198 return git_object_lookup_prefix(object_out
, repo
, id
, GIT_OID_HEXSZ
, type
);
201 void git_object_free(git_object
*object
)
206 git_cached_obj_decref(object
);
209 const git_oid
*git_object_id(const git_object
*obj
)
212 return &obj
->cached
.oid
;
215 git_otype
git_object_type(const git_object
*obj
)
218 return obj
->cached
.type
;
221 git_repository
*git_object_owner(const git_object
*obj
)
227 const char *git_object_type2string(git_otype type
)
229 if (type
< 0 || ((size_t) type
) >= ARRAY_SIZE(git_objects_table
))
232 return git_objects_table
[type
].str
;
235 git_otype
git_object_string2type(const char *str
)
242 for (i
= 0; i
< ARRAY_SIZE(git_objects_table
); i
++)
243 if (!strcmp(str
, git_objects_table
[i
].str
))
249 int git_object_typeisloose(git_otype type
)
251 if (type
< 0 || ((size_t) type
) >= ARRAY_SIZE(git_objects_table
))
254 return (git_objects_table
[type
].size
> 0) ? 1 : 0;
257 size_t git_object__size(git_otype type
)
259 if (type
< 0 || ((size_t) type
) >= ARRAY_SIZE(git_objects_table
))
262 return git_objects_table
[type
].size
;
265 static int dereference_object(git_object
**dereferenced
, git_object
*obj
)
267 git_otype type
= git_object_type(obj
);
271 return git_commit_tree((git_tree
**)dereferenced
, (git_commit
*)obj
);
274 return git_tag_target(dereferenced
, (git_tag
*)obj
);
281 return GIT_EINVALIDSPEC
;
285 static int peel_error(int error
, const git_oid
*oid
, git_otype type
)
287 const char *type_name
;
288 char hex_oid
[GIT_OID_HEXSZ
+ 1];
290 type_name
= git_object_type2string(type
);
292 git_oid_fmt(hex_oid
, oid
);
293 hex_oid
[GIT_OID_HEXSZ
] = '\0';
295 giterr_set(GITERR_OBJECT
, "the git_object of id '%s' can not be "
296 "successfully peeled into a %s (git_otype=%i).", hex_oid
, type_name
, type
);
301 static int check_type_combination(git_otype type
, git_otype target
)
309 /* a blob or tree can never be peeled to anything but themselves */
310 return GIT_EINVALIDSPEC
;
313 /* a commit can only be peeled to a tree */
314 if (target
!= GIT_OBJ_TREE
&& target
!= GIT_OBJ_ANY
)
315 return GIT_EINVALIDSPEC
;
318 /* a tag may point to anything, so we let anything through */
321 return GIT_EINVALIDSPEC
;
329 const git_object
*object
,
330 git_otype target_type
)
332 git_object
*source
, *deref
= NULL
;
335 assert(object
&& peeled
);
337 assert(target_type
== GIT_OBJ_TAG
||
338 target_type
== GIT_OBJ_COMMIT
||
339 target_type
== GIT_OBJ_TREE
||
340 target_type
== GIT_OBJ_BLOB
||
341 target_type
== GIT_OBJ_ANY
);
343 if ((error
= check_type_combination(git_object_type(object
), target_type
)) < 0)
344 return peel_error(error
, git_object_id(object
), target_type
);
346 if (git_object_type(object
) == target_type
)
347 return git_object_dup(peeled
, (git_object
*)object
);
349 source
= (git_object
*)object
;
351 while (!(error
= dereference_object(&deref
, source
))) {
353 if (source
!= object
)
354 git_object_free(source
);
356 if (git_object_type(deref
) == target_type
) {
361 if (target_type
== GIT_OBJ_ANY
&&
362 git_object_type(deref
) != git_object_type(object
))
372 if (source
!= object
)
373 git_object_free(source
);
375 git_object_free(deref
);
378 error
= peel_error(error
, git_object_id(object
), target_type
);
383 int git_object_dup(git_object
**dest
, git_object
*source
)
385 git_cached_obj_incref(source
);
390 int git_object_lookup_bypath(
392 const git_object
*treeish
,
397 git_tree
*tree
= NULL
;
398 git_tree_entry
*entry
= NULL
;
400 assert(out
&& treeish
&& path
);
402 if ((error
= git_object_peel((git_object
**)&tree
, treeish
, GIT_OBJ_TREE
)) < 0 ||
403 (error
= git_tree_entry_bypath(&entry
, tree
, path
)) < 0)
408 if (type
!= GIT_OBJ_ANY
&& git_tree_entry_type(entry
) != type
)
410 giterr_set(GITERR_OBJECT
,
411 "object at path '%s' is not of the asked-for type %d",
413 error
= GIT_EINVALIDSPEC
;
417 error
= git_tree_entry_to_object(out
, git_object_owner(treeish
), entry
);
420 git_tree_entry_free(entry
);
425 int git_object_short_id(git_buf
*out
, const git_object
*obj
)
427 git_repository
*repo
;
428 int len
= GIT_ABBREV_DEFAULT
, error
;
434 git_buf_sanitize(out
);
435 repo
= git_object_owner(obj
);
437 if ((error
= git_repository__cvar(&len
, repo
, GIT_CVAR_ABBREV
)) < 0)
440 if ((error
= git_repository_odb(&odb
, repo
)) < 0)
443 while (len
< GIT_OID_HEXSZ
) {
444 /* set up short oid */
445 memcpy(&id
.id
, &obj
->cached
.oid
.id
, (len
+ 1) / 2);
447 id
.id
[len
/ 2] &= 0xf0;
449 error
= git_odb_exists_prefix(NULL
, odb
, &id
, len
);
450 if (error
!= GIT_EAMBIGUOUS
)
457 if (!error
&& !(error
= git_buf_grow(out
, len
+ 1))) {
458 git_oid_tostr(out
->ptr
, len
+ 1, &id
);
467 bool git_object__is_valid(
468 git_repository
*repo
, const git_oid
*id
, git_otype expected_type
)
471 git_otype actual_type
;
475 if (!git_object__strict_input_validation
)
478 if ((error
= git_repository_odb__weakptr(&odb
, repo
)) < 0 ||
479 (error
= git_odb_read_header(&len
, &actual_type
, odb
, id
)) < 0)
482 if (expected_type
!= GIT_OBJ_ANY
&& expected_type
!= actual_type
) {
483 giterr_set(GITERR_INVALID
,
484 "the requested type does not match the type in the ODB");