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