]> git.proxmox.com Git - libgit2.git/blob - src/object.c
Merge pull request #2730 from libgit2/cmn/local-push
[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 case GIT_OBJ_TREE:
281 return GIT_EPEEL;
282
283 default:
284 return GIT_EINVALIDSPEC;
285 }
286 }
287
288 static int peel_error(int error, const git_oid *oid, git_otype type)
289 {
290 const char *type_name;
291 char hex_oid[GIT_OID_HEXSZ + 1];
292
293 type_name = git_object_type2string(type);
294
295 git_oid_fmt(hex_oid, oid);
296 hex_oid[GIT_OID_HEXSZ] = '\0';
297
298 giterr_set(GITERR_OBJECT, "The git_object of id '%s' can not be "
299 "successfully peeled into a %s (git_otype=%i).", hex_oid, type_name, type);
300
301 return error;
302 }
303
304 static int check_type_combination(git_otype type, git_otype target)
305 {
306 if (type == target)
307 return 0;
308
309 switch (type) {
310 case GIT_OBJ_BLOB:
311 case GIT_OBJ_TREE:
312 /* a blob or tree can never be peeled to anything but themselves */
313 return GIT_EINVALIDSPEC;
314 break;
315 case GIT_OBJ_COMMIT:
316 /* a commit can only be peeled to a tree */
317 if (target != GIT_OBJ_TREE && target != GIT_OBJ_ANY)
318 return GIT_EINVALIDSPEC;
319 break;
320 case GIT_OBJ_TAG:
321 /* a tag may point to anything, so we let anything through */
322 break;
323 default:
324 return GIT_EINVALIDSPEC;
325 }
326
327 return 0;
328 }
329
330 int git_object_peel(
331 git_object **peeled,
332 const git_object *object,
333 git_otype target_type)
334 {
335 git_object *source, *deref = NULL;
336 int error;
337
338 assert(object && peeled);
339
340 assert(target_type == GIT_OBJ_TAG ||
341 target_type == GIT_OBJ_COMMIT ||
342 target_type == GIT_OBJ_TREE ||
343 target_type == GIT_OBJ_BLOB ||
344 target_type == GIT_OBJ_ANY);
345
346 if ((error = check_type_combination(git_object_type(object), target_type)) < 0)
347 return peel_error(error, git_object_id(object), target_type);
348
349 if (git_object_type(object) == target_type)
350 return git_object_dup(peeled, (git_object *)object);
351
352 source = (git_object *)object;
353
354 while (!(error = dereference_object(&deref, source))) {
355
356 if (source != object)
357 git_object_free(source);
358
359 if (git_object_type(deref) == target_type) {
360 *peeled = deref;
361 return 0;
362 }
363
364 if (target_type == GIT_OBJ_ANY &&
365 git_object_type(deref) != git_object_type(object))
366 {
367 *peeled = deref;
368 return 0;
369 }
370
371 source = deref;
372 deref = NULL;
373 }
374
375 if (source != object)
376 git_object_free(source);
377
378 git_object_free(deref);
379
380 if (error)
381 error = peel_error(error, git_object_id(object), target_type);
382
383 return error;
384 }
385
386 int git_object_dup(git_object **dest, git_object *source)
387 {
388 git_cached_obj_incref(source);
389 *dest = source;
390 return 0;
391 }
392
393 int git_object_lookup_bypath(
394 git_object **out,
395 const git_object *treeish,
396 const char *path,
397 git_otype type)
398 {
399 int error = -1;
400 git_tree *tree = NULL;
401 git_tree_entry *entry = NULL;
402
403 assert(out && treeish && path);
404
405 if ((error = git_object_peel((git_object**)&tree, treeish, GIT_OBJ_TREE)) < 0 ||
406 (error = git_tree_entry_bypath(&entry, tree, path)) < 0)
407 {
408 goto cleanup;
409 }
410
411 if (type != GIT_OBJ_ANY && git_tree_entry_type(entry) != type)
412 {
413 giterr_set(GITERR_OBJECT,
414 "object at path '%s' is not of the asked-for type %d",
415 path, type);
416 error = GIT_EINVALIDSPEC;
417 goto cleanup;
418 }
419
420 error = git_tree_entry_to_object(out, git_object_owner(treeish), entry);
421
422 cleanup:
423 git_tree_entry_free(entry);
424 git_tree_free(tree);
425 return error;
426 }
427
428 int git_object_short_id(git_buf *out, const git_object *obj)
429 {
430 git_repository *repo;
431 int len = GIT_ABBREV_DEFAULT, error;
432 git_oid id = {{0}};
433 git_odb *odb;
434
435 assert(out && obj);
436
437 git_buf_sanitize(out);
438 repo = git_object_owner(obj);
439
440 if ((error = git_repository__cvar(&len, repo, GIT_CVAR_ABBREV)) < 0)
441 return error;
442
443 if ((error = git_repository_odb(&odb, repo)) < 0)
444 return error;
445
446 while (len < GIT_OID_HEXSZ) {
447 /* set up short oid */
448 memcpy(&id.id, &obj->cached.oid.id, (len + 1) / 2);
449 if (len & 1)
450 id.id[len / 2] &= 0xf0;
451
452 error = git_odb_exists_prefix(NULL, odb, &id, len);
453 if (error != GIT_EAMBIGUOUS)
454 break;
455
456 giterr_clear();
457 len++;
458 }
459
460 if (!error && !(error = git_buf_grow(out, len + 1))) {
461 git_oid_tostr(out->ptr, len + 1, &id);
462 out->size = len;
463 }
464
465 git_odb_free(odb);
466
467 return error;
468 }
469