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