]>
git.proxmox.com Git - libgit2.git/blob - src/object.c
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.
9 #include "git2/object.h"
12 #include "repository.h"
19 static const int OBJECT_BASE_SIZE
= 4096;
22 const char *str
; /* type name string */
23 int loose
; /* valid loose object type flag */
24 size_t size
; /* size in bytes of the object structure */
25 } git_objects_table
[] = {
26 /* 0 = GIT_OBJ__EXT1 */
29 /* 1 = GIT_OBJ_COMMIT */
30 { "commit", 1, sizeof(struct git_commit
)},
32 /* 2 = GIT_OBJ_TREE */
33 { "tree", 1, sizeof(struct git_tree
) },
35 /* 3 = GIT_OBJ_BLOB */
36 { "blob", 1, sizeof(struct git_blob
) },
39 { "tag", 1, sizeof(struct git_tag
) },
41 /* 5 = GIT_OBJ__EXT2 */
44 /* 6 = GIT_OBJ_OFS_DELTA */
45 { "OFS_DELTA", 0, 0 },
47 /* 7 = GIT_OBJ_REF_DELTA */
51 static int create_object(git_object
**object_out
, git_otype type
)
53 git_object
*object
= NULL
;
64 object
= git__malloc(git_object__size(type
));
65 GITERR_CHECK_ALLOC(object
);
66 memset(object
, 0x0, git_object__size(type
));
70 giterr_set(GITERR_INVALID
, "The given type is invalid");
78 int git_object__from_odb_object(
79 git_object
**object_out
,
81 git_odb_object
*odb_obj
,
85 git_object
*object
= NULL
;
87 if (type
!= GIT_OBJ_ANY
&& type
!= odb_obj
->cached
.type
) {
88 giterr_set(GITERR_INVALID
,
89 "The requested type does not match the type in the ODB");
93 if ((error
= create_object(&object
, odb_obj
->cached
.type
)) < 0)
96 /* Initialize parent object */
97 git_oid_cpy(&object
->cached
.oid
, &odb_obj
->cached
.oid
);
98 object
->cached
.size
= odb_obj
->cached
.size
;
99 object
->cached
.type
= odb_obj
->cached
.type
;
102 switch (object
->cached
.type
) {
104 error
= git_commit__parse((git_commit
*)object
, odb_obj
);
108 error
= git_tree__parse((git_tree
*)object
, odb_obj
);
112 error
= git_tag__parse((git_tag
*)object
, odb_obj
);
116 error
= git_blob__parse((git_blob
*)object
, odb_obj
);
124 git_object__free(object
);
128 *object_out
= git_cache_store_parsed(&repo
->objects
, object
);
132 int git_object_lookup_prefix(
133 git_object
**object_out
,
134 git_repository
*repo
,
139 git_object
*object
= NULL
;
141 git_odb_object
*odb_obj
;
144 assert(repo
&& object_out
&& id
);
146 if (len
< GIT_OID_MINPREFIXLEN
)
147 return GIT_EAMBIGUOUS
;
149 error
= git_repository_odb__weakptr(&odb
, repo
);
153 if (len
> GIT_OID_HEXSZ
)
156 if (len
== GIT_OID_HEXSZ
) {
157 git_cached_obj
*cached
= NULL
;
159 /* We want to match the full id : we can first look up in the cache,
160 * since there is no need to check for non ambiguousity
162 cached
= git_cache_get_any(&repo
->objects
, id
);
163 if (cached
!= NULL
) {
164 if (cached
->flags
== GIT_CACHE_STORE_PARSED
) {
165 object
= (git_object
*)cached
;
167 if (type
!= GIT_OBJ_ANY
&& type
!= object
->cached
.type
) {
168 git_object_free(object
);
169 giterr_set(GITERR_INVALID
,
170 "The requested type does not match the type in ODB");
171 return GIT_ENOTFOUND
;
174 *object_out
= object
;
176 } else if (cached
->flags
== GIT_CACHE_STORE_RAW
) {
177 odb_obj
= (git_odb_object
*)cached
;
179 assert(!"Wrong caching type in the global object cache");
182 /* Object was not found in the cache, let's explore the backends.
183 * We could just use git_odb_read_unique_short_oid,
184 * it is the same cost for packed and loose object backends,
185 * but it may be much more costly for sqlite and hiredis.
187 error
= git_odb_read(&odb_obj
, odb
, id
);
192 /* We copy the first len*4 bits from id and fill the remaining with 0s */
193 memcpy(short_oid
.id
, id
->id
, (len
+ 1) / 2);
195 short_oid
.id
[len
/ 2] &= 0xF0;
196 memset(short_oid
.id
+ (len
+ 1) / 2, 0, (GIT_OID_HEXSZ
- len
) / 2);
198 /* If len < GIT_OID_HEXSZ (a strict short oid was given), we have
200 * - We always search in the cache first. If we find that short oid is
201 * ambiguous, we can stop. But in all the other cases, we must then
202 * explore all the backends (to find an object if there was match,
203 * or to check that oid is not ambiguous if we have found 1 match in
205 * - We never explore the cache, go right to exploring the backends
206 * We chose the latter : we explore directly the backends.
208 error
= git_odb_read_prefix(&odb_obj
, odb
, &short_oid
, len
);
214 error
= git_object__from_odb_object(object_out
, repo
, odb_obj
, type
);
216 git_odb_object_free(odb_obj
);
221 int git_object_lookup(git_object
**object_out
, git_repository
*repo
, const git_oid
*id
, git_otype type
) {
222 return git_object_lookup_prefix(object_out
, repo
, id
, GIT_OID_HEXSZ
, type
);
225 void git_object__free(void *_obj
)
227 git_object
*object
= (git_object
*)_obj
;
231 switch (object
->cached
.type
) {
233 git_commit__free((git_commit
*)object
);
237 git_tree__free((git_tree
*)object
);
241 git_tag__free((git_tag
*)object
);
245 git_blob__free((git_blob
*)object
);
254 void git_object_free(git_object
*object
)
259 git_cached_obj_decref(object
);
262 const git_oid
*git_object_id(const git_object
*obj
)
265 return &obj
->cached
.oid
;
268 git_otype
git_object_type(const git_object
*obj
)
271 return obj
->cached
.type
;
274 git_repository
*git_object_owner(const git_object
*obj
)
280 const char *git_object_type2string(git_otype type
)
282 if (type
< 0 || ((size_t) type
) >= ARRAY_SIZE(git_objects_table
))
285 return git_objects_table
[type
].str
;
288 git_otype
git_object_string2type(const char *str
)
295 for (i
= 0; i
< ARRAY_SIZE(git_objects_table
); i
++)
296 if (!strcmp(str
, git_objects_table
[i
].str
))
302 int git_object_typeisloose(git_otype type
)
304 if (type
< 0 || ((size_t) type
) >= ARRAY_SIZE(git_objects_table
))
307 return git_objects_table
[type
].loose
;
310 size_t git_object__size(git_otype type
)
312 if (type
< 0 || ((size_t) type
) >= ARRAY_SIZE(git_objects_table
))
315 return git_objects_table
[type
].size
;
318 static int dereference_object(git_object
**dereferenced
, git_object
*obj
)
320 git_otype type
= git_object_type(obj
);
324 return git_commit_tree((git_tree
**)dereferenced
, (git_commit
*)obj
);
327 return git_tag_target(dereferenced
, (git_tag
*)obj
);
330 return GIT_ENOTFOUND
;
333 return GIT_EAMBIGUOUS
;
336 return GIT_EINVALIDSPEC
;
340 static int peel_error(int error
, const git_oid
*oid
, git_otype type
)
342 const char *type_name
;
343 char hex_oid
[GIT_OID_HEXSZ
+ 1];
345 type_name
= git_object_type2string(type
);
347 git_oid_fmt(hex_oid
, oid
);
348 hex_oid
[GIT_OID_HEXSZ
] = '\0';
350 giterr_set(GITERR_OBJECT
, "The git_object of id '%s' can not be "
351 "successfully peeled into a %s (git_otype=%i).", hex_oid
, type_name
, type
);
358 const git_object
*object
,
359 git_otype target_type
)
361 git_object
*source
, *deref
= NULL
;
364 if (target_type
!= GIT_OBJ_TAG
&&
365 target_type
!= GIT_OBJ_COMMIT
&&
366 target_type
!= GIT_OBJ_TREE
&&
367 target_type
!= GIT_OBJ_BLOB
&&
368 target_type
!= GIT_OBJ_ANY
)
369 return GIT_EINVALIDSPEC
;
371 assert(object
&& peeled
);
373 if (git_object_type(object
) == target_type
)
374 return git_object_dup(peeled
, (git_object
*)object
);
376 source
= (git_object
*)object
;
378 while (!(error
= dereference_object(&deref
, source
))) {
380 if (source
!= object
)
381 git_object_free(source
);
383 if (git_object_type(deref
) == target_type
) {
388 if (target_type
== GIT_OBJ_ANY
&&
389 git_object_type(deref
) != git_object_type(object
))
399 if (source
!= object
)
400 git_object_free(source
);
402 git_object_free(deref
);
405 error
= peel_error(error
, git_object_id(object
), target_type
);
410 int git_object_dup(git_object
**dest
, git_object
*source
)
412 git_cached_obj_incref(source
);