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"
17 static const int OBJECT_BASE_SIZE
= 4096;
20 const char *str
; /* type name string */
21 size_t size
; /* size in bytes of the object structure */
23 int (*parse
)(void *self
, git_odb_object
*obj
);
24 void (*free
)(void *self
);
27 static git_object_def git_objects_table
[] = {
28 /* 0 = GIT_OBJ__EXT1 */
29 { "", 0, NULL
, NULL
},
31 /* 1 = GIT_OBJ_COMMIT */
32 { "commit", sizeof(git_commit
), git_commit__parse
, git_commit__free
},
34 /* 2 = GIT_OBJ_TREE */
35 { "tree", sizeof(git_tree
), git_tree__parse
, git_tree__free
},
37 /* 3 = GIT_OBJ_BLOB */
38 { "blob", sizeof(git_blob
), git_blob__parse
, git_blob__free
},
41 { "tag", sizeof(git_tag
), git_tag__parse
, git_tag__free
},
43 /* 5 = GIT_OBJ__EXT2 */
44 { "", 0, NULL
, NULL
},
45 /* 6 = GIT_OBJ_OFS_DELTA */
46 { "OFS_DELTA", 0, NULL
, NULL
},
47 /* 7 = GIT_OBJ_REF_DELTA */
48 { "REF_DELTA", 0, NULL
, NULL
},
51 int git_object__from_odb_object(
52 git_object
**object_out
,
54 git_odb_object
*odb_obj
,
60 git_object
*object
= NULL
;
65 /* Validate type match */
66 if (type
!= GIT_OBJ_ANY
&& type
!= odb_obj
->cached
.type
) {
67 giterr_set(GITERR_INVALID
,
68 "The requested type does not match the type in the ODB");
72 if ((object_size
= git_object__size(odb_obj
->cached
.type
)) == 0) {
73 giterr_set(GITERR_INVALID
, "The requested type is invalid");
77 /* Allocate and initialize base object */
78 object
= git__calloc(1, object_size
);
79 GITERR_CHECK_ALLOC(object
);
81 git_oid_cpy(&object
->cached
.oid
, &odb_obj
->cached
.oid
);
82 object
->cached
.type
= odb_obj
->cached
.type
;
83 object
->cached
.size
= odb_obj
->cached
.size
;
86 /* Parse raw object data */
87 def
= &git_objects_table
[odb_obj
->cached
.type
];
88 assert(def
->free
&& def
->parse
);
90 if ((error
= def
->parse(object
, odb_obj
)) < 0)
93 *object_out
= git_cache_store_parsed(&repo
->objects
, object
);
98 void git_object__free(void *obj
)
100 git_otype type
= ((git_object
*)obj
)->cached
.type
;
102 if (type
< 0 || ((size_t)type
) >= ARRAY_SIZE(git_objects_table
) ||
103 !git_objects_table
[type
].free
)
106 git_objects_table
[type
].free(obj
);
109 int git_object_lookup_prefix(
110 git_object
**object_out
,
111 git_repository
*repo
,
116 git_object
*object
= NULL
;
118 git_odb_object
*odb_obj
= NULL
;
121 assert(repo
&& object_out
&& id
);
123 if (len
< GIT_OID_MINPREFIXLEN
) {
124 giterr_set(GITERR_OBJECT
, "Ambiguous lookup - OID prefix is too short");
125 return GIT_EAMBIGUOUS
;
128 error
= git_repository_odb__weakptr(&odb
, repo
);
132 if (len
> GIT_OID_HEXSZ
)
135 if (len
== GIT_OID_HEXSZ
) {
136 git_cached_obj
*cached
= NULL
;
138 /* We want to match the full id : we can first look up in the cache,
139 * since there is no need to check for non ambiguousity
141 cached
= git_cache_get_any(&repo
->objects
, id
);
142 if (cached
!= NULL
) {
143 if (cached
->flags
== GIT_CACHE_STORE_PARSED
) {
144 object
= (git_object
*)cached
;
146 if (type
!= GIT_OBJ_ANY
&& type
!= object
->cached
.type
) {
147 git_object_free(object
);
148 giterr_set(GITERR_INVALID
,
149 "The requested type does not match the type in ODB");
150 return GIT_ENOTFOUND
;
153 *object_out
= object
;
155 } else if (cached
->flags
== GIT_CACHE_STORE_RAW
) {
156 odb_obj
= (git_odb_object
*)cached
;
158 assert(!"Wrong caching type in the global object cache");
161 /* Object was not found in the cache, let's explore the backends.
162 * We could just use git_odb_read_unique_short_oid,
163 * it is the same cost for packed and loose object backends,
164 * but it may be much more costly for sqlite and hiredis.
166 error
= git_odb_read(&odb_obj
, odb
, id
);
171 /* We copy the first len*4 bits from id and fill the remaining with 0s */
172 memcpy(short_oid
.id
, id
->id
, (len
+ 1) / 2);
174 short_oid
.id
[len
/ 2] &= 0xF0;
175 memset(short_oid
.id
+ (len
+ 1) / 2, 0, (GIT_OID_HEXSZ
- len
) / 2);
177 /* If len < GIT_OID_HEXSZ (a strict short oid was given), we have
179 * - We always search in the cache first. If we find that short oid is
180 * ambiguous, we can stop. But in all the other cases, we must then
181 * explore all the backends (to find an object if there was match,
182 * or to check that oid is not ambiguous if we have found 1 match in
184 * - We never explore the cache, go right to exploring the backends
185 * We chose the latter : we explore directly the backends.
187 error
= git_odb_read_prefix(&odb_obj
, odb
, &short_oid
, len
);
193 error
= git_object__from_odb_object(object_out
, repo
, odb_obj
, type
);
195 git_odb_object_free(odb_obj
);
200 int git_object_lookup(git_object
**object_out
, git_repository
*repo
, const git_oid
*id
, git_otype type
) {
201 return git_object_lookup_prefix(object_out
, repo
, id
, GIT_OID_HEXSZ
, type
);
204 void git_object_free(git_object
*object
)
209 git_cached_obj_decref(object
);
212 const git_oid
*git_object_id(const git_object
*obj
)
215 return &obj
->cached
.oid
;
218 git_otype
git_object_type(const git_object
*obj
)
221 return obj
->cached
.type
;
224 git_repository
*git_object_owner(const git_object
*obj
)
230 const char *git_object_type2string(git_otype type
)
232 if (type
< 0 || ((size_t) type
) >= ARRAY_SIZE(git_objects_table
))
235 return git_objects_table
[type
].str
;
238 git_otype
git_object_string2type(const char *str
)
245 for (i
= 0; i
< ARRAY_SIZE(git_objects_table
); i
++)
246 if (!strcmp(str
, git_objects_table
[i
].str
))
252 int git_object_typeisloose(git_otype type
)
254 if (type
< 0 || ((size_t) type
) >= ARRAY_SIZE(git_objects_table
))
257 return (git_objects_table
[type
].size
> 0) ? 1 : 0;
260 size_t git_object__size(git_otype type
)
262 if (type
< 0 || ((size_t) type
) >= ARRAY_SIZE(git_objects_table
))
265 return git_objects_table
[type
].size
;
268 static int dereference_object(git_object
**dereferenced
, git_object
*obj
)
270 git_otype type
= git_object_type(obj
);
274 return git_commit_tree((git_tree
**)dereferenced
, (git_commit
*)obj
);
277 return git_tag_target(dereferenced
, (git_tag
*)obj
);
280 return GIT_ENOTFOUND
;
283 return GIT_EAMBIGUOUS
;
286 return GIT_EINVALIDSPEC
;
290 static int peel_error(int error
, const git_oid
*oid
, git_otype type
)
292 const char *type_name
;
293 char hex_oid
[GIT_OID_HEXSZ
+ 1];
295 type_name
= git_object_type2string(type
);
297 git_oid_fmt(hex_oid
, oid
);
298 hex_oid
[GIT_OID_HEXSZ
] = '\0';
300 giterr_set(GITERR_OBJECT
, "The git_object of id '%s' can not be "
301 "successfully peeled into a %s (git_otype=%i).", hex_oid
, type_name
, type
);
308 const git_object
*object
,
309 git_otype target_type
)
311 git_object
*source
, *deref
= NULL
;
314 assert(object
&& peeled
);
316 if (git_object_type(object
) == target_type
)
317 return git_object_dup(peeled
, (git_object
*)object
);
319 assert(target_type
== GIT_OBJ_TAG
||
320 target_type
== GIT_OBJ_COMMIT
||
321 target_type
== GIT_OBJ_TREE
||
322 target_type
== GIT_OBJ_BLOB
||
323 target_type
== GIT_OBJ_ANY
);
325 source
= (git_object
*)object
;
327 while (!(error
= dereference_object(&deref
, source
))) {
329 if (source
!= object
)
330 git_object_free(source
);
332 if (git_object_type(deref
) == target_type
) {
337 if (target_type
== GIT_OBJ_ANY
&&
338 git_object_type(deref
) != git_object_type(object
))
348 if (source
!= object
)
349 git_object_free(source
);
351 git_object_free(deref
);
354 error
= peel_error(error
, git_object_id(object
), target_type
);
359 int git_object_dup(git_object
**dest
, git_object
*source
)
361 git_cached_obj_incref(source
);
366 int git_object_lookup_bypath(
368 const git_object
*treeish
,
373 git_tree
*tree
= NULL
;
374 git_tree_entry
*entry
= NULL
;
376 assert(out
&& treeish
&& path
);
378 if ((error
= git_object_peel((git_object
**)&tree
, treeish
, GIT_OBJ_TREE
)) < 0 ||
379 (error
= git_tree_entry_bypath(&entry
, tree
, path
)) < 0)
384 if (type
!= GIT_OBJ_ANY
&& git_tree_entry_type(entry
) != type
)
386 giterr_set(GITERR_OBJECT
,
387 "object at path '%s' is not of the asked-for type %d",
389 error
= GIT_EINVALIDSPEC
;
393 error
= git_tree_entry_to_object(out
, git_object_owner(treeish
), entry
);
396 git_tree_entry_free(entry
);
401 int git_object_short_id(git_buf
*out
, const git_object
*obj
)
403 git_repository
*repo
;
404 int len
= GIT_ABBREV_DEFAULT
, error
;
410 git_buf_sanitize(out
);
411 repo
= git_object_owner(obj
);
413 if ((error
= git_repository__cvar(&len
, repo
, GIT_CVAR_ABBREV
)) < 0)
416 if ((error
= git_repository_odb(&odb
, repo
)) < 0)
419 while (len
< GIT_OID_HEXSZ
) {
420 /* set up short oid */
421 memcpy(&id
.id
, &obj
->cached
.oid
.id
, (len
+ 1) / 2);
423 id
.id
[len
/ 2] &= 0xf0;
425 error
= git_odb_exists_prefix(NULL
, odb
, &id
, len
);
426 if (error
!= GIT_EAMBIGUOUS
)
433 if (!error
&& !(error
= git_buf_grow(out
, len
+ 1))) {
434 git_oid_tostr(out
->ptr
, len
+ 1, &id
);