line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
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
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
#include "object.h" |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
#include "git2/object.h" |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
#include "repository.h" |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
#include "commit.h" |
15
|
|
|
|
|
|
|
#include "hash.h" |
16
|
|
|
|
|
|
|
#include "tree.h" |
17
|
|
|
|
|
|
|
#include "blob.h" |
18
|
|
|
|
|
|
|
#include "oid.h" |
19
|
|
|
|
|
|
|
#include "tag.h" |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
bool git_object__strict_input_validation = true; |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
extern int git_odb_hash(git_oid *out, const void *data, size_t len, git_object_t type); |
24
|
|
|
|
|
|
|
size_t git_object__size(git_object_t type); |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
typedef struct { |
27
|
|
|
|
|
|
|
const char *str; /* type name string */ |
28
|
|
|
|
|
|
|
size_t size; /* size in bytes of the object structure */ |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
int (*parse)(void *self, git_odb_object *obj); |
31
|
|
|
|
|
|
|
int (*parse_raw)(void *self, const char *data, size_t size); |
32
|
|
|
|
|
|
|
void (*free)(void *self); |
33
|
|
|
|
|
|
|
} git_object_def; |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
static git_object_def git_objects_table[] = { |
36
|
|
|
|
|
|
|
/* 0 = GIT_OBJECT__EXT1 */ |
37
|
|
|
|
|
|
|
{ "", 0, NULL, NULL, NULL }, |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
/* 1 = GIT_OBJECT_COMMIT */ |
40
|
|
|
|
|
|
|
{ "commit", sizeof(git_commit), git_commit__parse, git_commit__parse_raw, git_commit__free }, |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
/* 2 = GIT_OBJECT_TREE */ |
43
|
|
|
|
|
|
|
{ "tree", sizeof(git_tree), git_tree__parse, git_tree__parse_raw, git_tree__free }, |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
/* 3 = GIT_OBJECT_BLOB */ |
46
|
|
|
|
|
|
|
{ "blob", sizeof(git_blob), git_blob__parse, git_blob__parse_raw, git_blob__free }, |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
/* 4 = GIT_OBJECT_TAG */ |
49
|
|
|
|
|
|
|
{ "tag", sizeof(git_tag), git_tag__parse, git_tag__parse_raw, git_tag__free }, |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
/* 5 = GIT_OBJECT__EXT2 */ |
52
|
|
|
|
|
|
|
{ "", 0, NULL, NULL, NULL }, |
53
|
|
|
|
|
|
|
/* 6 = GIT_OBJECT_OFS_DELTA */ |
54
|
|
|
|
|
|
|
{ "OFS_DELTA", 0, NULL, NULL, NULL }, |
55
|
|
|
|
|
|
|
/* 7 = GIT_OBJECT_REF_DELTA */ |
56
|
|
|
|
|
|
|
{ "REF_DELTA", 0, NULL, NULL, NULL }, |
57
|
|
|
|
|
|
|
}; |
58
|
|
|
|
|
|
|
|
59
|
0
|
|
|
|
|
|
int git_object__from_raw( |
60
|
|
|
|
|
|
|
git_object **object_out, |
61
|
|
|
|
|
|
|
const char *data, |
62
|
|
|
|
|
|
|
size_t size, |
63
|
|
|
|
|
|
|
git_object_t type) |
64
|
|
|
|
|
|
|
{ |
65
|
|
|
|
|
|
|
git_object_def *def; |
66
|
|
|
|
|
|
|
git_object *object; |
67
|
|
|
|
|
|
|
size_t object_size; |
68
|
|
|
|
|
|
|
int error; |
69
|
|
|
|
|
|
|
|
70
|
0
|
0
|
|
|
|
|
assert(object_out); |
71
|
0
|
|
|
|
|
|
*object_out = NULL; |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
/* Validate type match */ |
74
|
0
|
0
|
|
|
|
|
if (type != GIT_OBJECT_BLOB && type != GIT_OBJECT_TREE && type != GIT_OBJECT_COMMIT && type != GIT_OBJECT_TAG) { |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
75
|
0
|
|
|
|
|
|
git_error_set(GIT_ERROR_INVALID, "the requested type is invalid"); |
76
|
0
|
|
|
|
|
|
return GIT_ENOTFOUND; |
77
|
|
|
|
|
|
|
} |
78
|
|
|
|
|
|
|
|
79
|
0
|
0
|
|
|
|
|
if ((object_size = git_object__size(type)) == 0) { |
80
|
0
|
|
|
|
|
|
git_error_set(GIT_ERROR_INVALID, "the requested type is invalid"); |
81
|
0
|
|
|
|
|
|
return GIT_ENOTFOUND; |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
/* Allocate and initialize base object */ |
85
|
0
|
|
|
|
|
|
object = git__calloc(1, object_size); |
86
|
0
|
0
|
|
|
|
|
GIT_ERROR_CHECK_ALLOC(object); |
87
|
0
|
|
|
|
|
|
object->cached.flags = GIT_CACHE_STORE_PARSED; |
88
|
0
|
|
|
|
|
|
object->cached.type = type; |
89
|
0
|
|
|
|
|
|
git_odb_hash(&object->cached.oid, data, size, type); |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
/* Parse raw object data */ |
92
|
0
|
|
|
|
|
|
def = &git_objects_table[type]; |
93
|
0
|
0
|
|
|
|
|
assert(def->free && def->parse_raw); |
|
|
0
|
|
|
|
|
|
94
|
|
|
|
|
|
|
|
95
|
0
|
0
|
|
|
|
|
if ((error = def->parse_raw(object, data, size)) < 0) { |
96
|
0
|
|
|
|
|
|
def->free(object); |
97
|
0
|
|
|
|
|
|
return error; |
98
|
|
|
|
|
|
|
} |
99
|
|
|
|
|
|
|
|
100
|
0
|
|
|
|
|
|
git_cached_obj_incref(object); |
101
|
0
|
|
|
|
|
|
*object_out = object; |
102
|
|
|
|
|
|
|
|
103
|
0
|
|
|
|
|
|
return 0; |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
|
106
|
367
|
|
|
|
|
|
int git_object__from_odb_object( |
107
|
|
|
|
|
|
|
git_object **object_out, |
108
|
|
|
|
|
|
|
git_repository *repo, |
109
|
|
|
|
|
|
|
git_odb_object *odb_obj, |
110
|
|
|
|
|
|
|
git_object_t type) |
111
|
|
|
|
|
|
|
{ |
112
|
|
|
|
|
|
|
int error; |
113
|
|
|
|
|
|
|
size_t object_size; |
114
|
|
|
|
|
|
|
git_object_def *def; |
115
|
367
|
|
|
|
|
|
git_object *object = NULL; |
116
|
|
|
|
|
|
|
|
117
|
367
|
50
|
|
|
|
|
assert(object_out); |
118
|
367
|
|
|
|
|
|
*object_out = NULL; |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
/* Validate type match */ |
121
|
367
|
100
|
|
|
|
|
if (type != GIT_OBJECT_ANY && type != odb_obj->cached.type) { |
|
|
50
|
|
|
|
|
|
122
|
0
|
|
|
|
|
|
git_error_set(GIT_ERROR_INVALID, |
123
|
|
|
|
|
|
|
"the requested type does not match the type in the ODB"); |
124
|
0
|
|
|
|
|
|
return GIT_ENOTFOUND; |
125
|
|
|
|
|
|
|
} |
126
|
|
|
|
|
|
|
|
127
|
367
|
50
|
|
|
|
|
if ((object_size = git_object__size(odb_obj->cached.type)) == 0) { |
128
|
0
|
|
|
|
|
|
git_error_set(GIT_ERROR_INVALID, "the requested type is invalid"); |
129
|
0
|
|
|
|
|
|
return GIT_ENOTFOUND; |
130
|
|
|
|
|
|
|
} |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
/* Allocate and initialize base object */ |
133
|
367
|
|
|
|
|
|
object = git__calloc(1, object_size); |
134
|
367
|
50
|
|
|
|
|
GIT_ERROR_CHECK_ALLOC(object); |
135
|
|
|
|
|
|
|
|
136
|
367
|
|
|
|
|
|
git_oid_cpy(&object->cached.oid, &odb_obj->cached.oid); |
137
|
367
|
|
|
|
|
|
object->cached.type = odb_obj->cached.type; |
138
|
367
|
|
|
|
|
|
object->cached.size = odb_obj->cached.size; |
139
|
367
|
|
|
|
|
|
object->repo = repo; |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
/* Parse raw object data */ |
142
|
367
|
|
|
|
|
|
def = &git_objects_table[odb_obj->cached.type]; |
143
|
367
|
50
|
|
|
|
|
assert(def->free && def->parse); |
|
|
50
|
|
|
|
|
|
144
|
|
|
|
|
|
|
|
145
|
367
|
50
|
|
|
|
|
if ((error = def->parse(object, odb_obj)) < 0) |
146
|
0
|
|
|
|
|
|
def->free(object); |
147
|
|
|
|
|
|
|
else |
148
|
367
|
|
|
|
|
|
*object_out = git_cache_store_parsed(&repo->objects, object); |
149
|
|
|
|
|
|
|
|
150
|
367
|
|
|
|
|
|
return error; |
151
|
|
|
|
|
|
|
} |
152
|
|
|
|
|
|
|
|
153
|
360
|
|
|
|
|
|
void git_object__free(void *obj) |
154
|
|
|
|
|
|
|
{ |
155
|
360
|
|
|
|
|
|
git_object_t type = ((git_object *)obj)->cached.type; |
156
|
|
|
|
|
|
|
|
157
|
360
|
50
|
|
|
|
|
if (type < 0 || ((size_t)type) >= ARRAY_SIZE(git_objects_table) || |
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
158
|
360
|
|
|
|
|
|
!git_objects_table[type].free) |
159
|
0
|
|
|
|
|
|
git__free(obj); |
160
|
|
|
|
|
|
|
else |
161
|
360
|
|
|
|
|
|
git_objects_table[type].free(obj); |
162
|
360
|
|
|
|
|
|
} |
163
|
|
|
|
|
|
|
|
164
|
1680
|
|
|
|
|
|
int git_object_lookup_prefix( |
165
|
|
|
|
|
|
|
git_object **object_out, |
166
|
|
|
|
|
|
|
git_repository *repo, |
167
|
|
|
|
|
|
|
const git_oid *id, |
168
|
|
|
|
|
|
|
size_t len, |
169
|
|
|
|
|
|
|
git_object_t type) |
170
|
|
|
|
|
|
|
{ |
171
|
1680
|
|
|
|
|
|
git_object *object = NULL; |
172
|
1680
|
|
|
|
|
|
git_odb *odb = NULL; |
173
|
1680
|
|
|
|
|
|
git_odb_object *odb_obj = NULL; |
174
|
1680
|
|
|
|
|
|
int error = 0; |
175
|
|
|
|
|
|
|
|
176
|
1680
|
50
|
|
|
|
|
assert(repo && object_out && id); |
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
177
|
|
|
|
|
|
|
|
178
|
1680
|
50
|
|
|
|
|
if (len < GIT_OID_MINPREFIXLEN) { |
179
|
0
|
|
|
|
|
|
git_error_set(GIT_ERROR_OBJECT, "ambiguous lookup - OID prefix is too short"); |
180
|
0
|
|
|
|
|
|
return GIT_EAMBIGUOUS; |
181
|
|
|
|
|
|
|
} |
182
|
|
|
|
|
|
|
|
183
|
1680
|
|
|
|
|
|
error = git_repository_odb__weakptr(&odb, repo); |
184
|
1680
|
50
|
|
|
|
|
if (error < 0) |
185
|
0
|
|
|
|
|
|
return error; |
186
|
|
|
|
|
|
|
|
187
|
1680
|
50
|
|
|
|
|
if (len > GIT_OID_HEXSZ) |
188
|
0
|
|
|
|
|
|
len = GIT_OID_HEXSZ; |
189
|
|
|
|
|
|
|
|
190
|
1680
|
100
|
|
|
|
|
if (len == GIT_OID_HEXSZ) { |
191
|
1666
|
|
|
|
|
|
git_cached_obj *cached = NULL; |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
/* We want to match the full id : we can first look up in the cache, |
194
|
|
|
|
|
|
|
* since there is no need to check for non ambiguousity |
195
|
|
|
|
|
|
|
*/ |
196
|
1666
|
|
|
|
|
|
cached = git_cache_get_any(&repo->objects, id); |
197
|
1666
|
100
|
|
|
|
|
if (cached != NULL) { |
198
|
1309
|
100
|
|
|
|
|
if (cached->flags == GIT_CACHE_STORE_PARSED) { |
199
|
1304
|
|
|
|
|
|
object = (git_object *)cached; |
200
|
|
|
|
|
|
|
|
201
|
1304
|
100
|
|
|
|
|
if (type != GIT_OBJECT_ANY && type != object->cached.type) { |
|
|
50
|
|
|
|
|
|
202
|
0
|
|
|
|
|
|
git_object_free(object); |
203
|
0
|
|
|
|
|
|
git_error_set(GIT_ERROR_INVALID, |
204
|
|
|
|
|
|
|
"the requested type does not match the type in the ODB"); |
205
|
0
|
|
|
|
|
|
return GIT_ENOTFOUND; |
206
|
|
|
|
|
|
|
} |
207
|
|
|
|
|
|
|
|
208
|
1304
|
|
|
|
|
|
*object_out = object; |
209
|
1304
|
|
|
|
|
|
return 0; |
210
|
5
|
50
|
|
|
|
|
} else if (cached->flags == GIT_CACHE_STORE_RAW) { |
211
|
5
|
|
|
|
|
|
odb_obj = (git_odb_object *)cached; |
212
|
|
|
|
|
|
|
} else { |
213
|
0
|
|
|
|
|
|
assert(!"Wrong caching type in the global object cache"); |
214
|
|
|
|
|
|
|
} |
215
|
|
|
|
|
|
|
} else { |
216
|
|
|
|
|
|
|
/* Object was not found in the cache, let's explore the backends. |
217
|
|
|
|
|
|
|
* We could just use git_odb_read_unique_short_oid, |
218
|
|
|
|
|
|
|
* it is the same cost for packed and loose object backends, |
219
|
|
|
|
|
|
|
* but it may be much more costly for sqlite and hiredis. |
220
|
|
|
|
|
|
|
*/ |
221
|
362
|
|
|
|
|
|
error = git_odb_read(&odb_obj, odb, id); |
222
|
|
|
|
|
|
|
} |
223
|
|
|
|
|
|
|
} else { |
224
|
14
|
|
|
|
|
|
git_oid short_oid = {{ 0 }}; |
225
|
|
|
|
|
|
|
|
226
|
14
|
|
|
|
|
|
git_oid__cpy_prefix(&short_oid, id, len); |
227
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
/* If len < GIT_OID_HEXSZ (a strict short oid was given), we have |
229
|
|
|
|
|
|
|
* 2 options : |
230
|
|
|
|
|
|
|
* - We always search in the cache first. If we find that short oid is |
231
|
|
|
|
|
|
|
* ambiguous, we can stop. But in all the other cases, we must then |
232
|
|
|
|
|
|
|
* explore all the backends (to find an object if there was match, |
233
|
|
|
|
|
|
|
* or to check that oid is not ambiguous if we have found 1 match in |
234
|
|
|
|
|
|
|
* the cache) |
235
|
|
|
|
|
|
|
* - We never explore the cache, go right to exploring the backends |
236
|
|
|
|
|
|
|
* We chose the latter : we explore directly the backends. |
237
|
|
|
|
|
|
|
*/ |
238
|
14
|
|
|
|
|
|
error = git_odb_read_prefix(&odb_obj, odb, &short_oid, len); |
239
|
|
|
|
|
|
|
} |
240
|
|
|
|
|
|
|
|
241
|
376
|
100
|
|
|
|
|
if (error < 0) |
242
|
9
|
|
|
|
|
|
return error; |
243
|
|
|
|
|
|
|
|
244
|
367
|
|
|
|
|
|
error = git_object__from_odb_object(object_out, repo, odb_obj, type); |
245
|
|
|
|
|
|
|
|
246
|
367
|
|
|
|
|
|
git_odb_object_free(odb_obj); |
247
|
|
|
|
|
|
|
|
248
|
1680
|
|
|
|
|
|
return error; |
249
|
|
|
|
|
|
|
} |
250
|
|
|
|
|
|
|
|
251
|
1649
|
|
|
|
|
|
int git_object_lookup(git_object **object_out, git_repository *repo, const git_oid *id, git_object_t type) { |
252
|
1649
|
|
|
|
|
|
return git_object_lookup_prefix(object_out, repo, id, GIT_OID_HEXSZ, type); |
253
|
|
|
|
|
|
|
} |
254
|
|
|
|
|
|
|
|
255
|
7383
|
|
|
|
|
|
void git_object_free(git_object *object) |
256
|
|
|
|
|
|
|
{ |
257
|
7383
|
100
|
|
|
|
|
if (object == NULL) |
258
|
4690
|
|
|
|
|
|
return; |
259
|
|
|
|
|
|
|
|
260
|
2693
|
|
|
|
|
|
git_cached_obj_decref(object); |
261
|
|
|
|
|
|
|
} |
262
|
|
|
|
|
|
|
|
263
|
938
|
|
|
|
|
|
const git_oid *git_object_id(const git_object *obj) |
264
|
|
|
|
|
|
|
{ |
265
|
938
|
50
|
|
|
|
|
assert(obj); |
266
|
938
|
|
|
|
|
|
return &obj->cached.oid; |
267
|
|
|
|
|
|
|
} |
268
|
|
|
|
|
|
|
|
269
|
1048
|
|
|
|
|
|
git_object_t git_object_type(const git_object *obj) |
270
|
|
|
|
|
|
|
{ |
271
|
1048
|
50
|
|
|
|
|
assert(obj); |
272
|
1048
|
|
|
|
|
|
return obj->cached.type; |
273
|
|
|
|
|
|
|
} |
274
|
|
|
|
|
|
|
|
275
|
586
|
|
|
|
|
|
git_repository *git_object_owner(const git_object *obj) |
276
|
|
|
|
|
|
|
{ |
277
|
586
|
50
|
|
|
|
|
assert(obj); |
278
|
586
|
|
|
|
|
|
return obj->repo; |
279
|
|
|
|
|
|
|
} |
280
|
|
|
|
|
|
|
|
281
|
1351
|
|
|
|
|
|
const char *git_object_type2string(git_object_t type) |
282
|
|
|
|
|
|
|
{ |
283
|
1351
|
50
|
|
|
|
|
if (type < 0 || ((size_t) type) >= ARRAY_SIZE(git_objects_table)) |
|
|
50
|
|
|
|
|
|
284
|
0
|
|
|
|
|
|
return ""; |
285
|
|
|
|
|
|
|
|
286
|
1351
|
|
|
|
|
|
return git_objects_table[type].str; |
287
|
|
|
|
|
|
|
} |
288
|
|
|
|
|
|
|
|
289
|
0
|
|
|
|
|
|
git_object_t git_object_string2type(const char *str) |
290
|
|
|
|
|
|
|
{ |
291
|
0
|
0
|
|
|
|
|
if (!str) |
292
|
0
|
|
|
|
|
|
return GIT_OBJECT_INVALID; |
293
|
|
|
|
|
|
|
|
294
|
0
|
|
|
|
|
|
return git_object_stringn2type(str, strlen(str)); |
295
|
|
|
|
|
|
|
} |
296
|
|
|
|
|
|
|
|
297
|
1128
|
|
|
|
|
|
git_object_t git_object_stringn2type(const char *str, size_t len) |
298
|
|
|
|
|
|
|
{ |
299
|
|
|
|
|
|
|
size_t i; |
300
|
|
|
|
|
|
|
|
301
|
1128
|
50
|
|
|
|
|
if (!str || !len || !*str) |
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
302
|
0
|
|
|
|
|
|
return GIT_OBJECT_INVALID; |
303
|
|
|
|
|
|
|
|
304
|
3492
|
50
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE(git_objects_table); i++) |
305
|
5856
|
|
|
|
|
|
if (*git_objects_table[i].str && |
306
|
2364
|
|
|
|
|
|
!git__prefixncmp(str, len, git_objects_table[i].str)) |
307
|
1128
|
|
|
|
|
|
return (git_object_t)i; |
308
|
|
|
|
|
|
|
|
309
|
0
|
|
|
|
|
|
return GIT_OBJECT_INVALID; |
310
|
|
|
|
|
|
|
} |
311
|
|
|
|
|
|
|
|
312
|
2195
|
|
|
|
|
|
int git_object_typeisloose(git_object_t type) |
313
|
|
|
|
|
|
|
{ |
314
|
2195
|
50
|
|
|
|
|
if (type < 0 || ((size_t) type) >= ARRAY_SIZE(git_objects_table)) |
|
|
50
|
|
|
|
|
|
315
|
0
|
|
|
|
|
|
return 0; |
316
|
|
|
|
|
|
|
|
317
|
2195
|
|
|
|
|
|
return (git_objects_table[type].size > 0) ? 1 : 0; |
318
|
|
|
|
|
|
|
} |
319
|
|
|
|
|
|
|
|
320
|
367
|
|
|
|
|
|
size_t git_object__size(git_object_t type) |
321
|
|
|
|
|
|
|
{ |
322
|
367
|
50
|
|
|
|
|
if (type < 0 || ((size_t) type) >= ARRAY_SIZE(git_objects_table)) |
|
|
50
|
|
|
|
|
|
323
|
0
|
|
|
|
|
|
return 0; |
324
|
|
|
|
|
|
|
|
325
|
367
|
|
|
|
|
|
return git_objects_table[type].size; |
326
|
|
|
|
|
|
|
} |
327
|
|
|
|
|
|
|
|
328
|
170
|
|
|
|
|
|
static int dereference_object(git_object **dereferenced, git_object *obj) |
329
|
|
|
|
|
|
|
{ |
330
|
170
|
|
|
|
|
|
git_object_t type = git_object_type(obj); |
331
|
|
|
|
|
|
|
|
332
|
170
|
|
|
|
|
|
switch (type) { |
333
|
|
|
|
|
|
|
case GIT_OBJECT_COMMIT: |
334
|
170
|
|
|
|
|
|
return git_commit_tree((git_tree **)dereferenced, (git_commit*)obj); |
335
|
|
|
|
|
|
|
|
336
|
|
|
|
|
|
|
case GIT_OBJECT_TAG: |
337
|
0
|
|
|
|
|
|
return git_tag_target(dereferenced, (git_tag*)obj); |
338
|
|
|
|
|
|
|
|
339
|
|
|
|
|
|
|
case GIT_OBJECT_BLOB: |
340
|
|
|
|
|
|
|
case GIT_OBJECT_TREE: |
341
|
0
|
|
|
|
|
|
return GIT_EPEEL; |
342
|
|
|
|
|
|
|
|
343
|
|
|
|
|
|
|
default: |
344
|
0
|
|
|
|
|
|
return GIT_EINVALIDSPEC; |
345
|
|
|
|
|
|
|
} |
346
|
|
|
|
|
|
|
} |
347
|
|
|
|
|
|
|
|
348
|
0
|
|
|
|
|
|
static int peel_error(int error, const git_oid *oid, git_object_t type) |
349
|
|
|
|
|
|
|
{ |
350
|
|
|
|
|
|
|
const char *type_name; |
351
|
|
|
|
|
|
|
char hex_oid[GIT_OID_HEXSZ + 1]; |
352
|
|
|
|
|
|
|
|
353
|
0
|
|
|
|
|
|
type_name = git_object_type2string(type); |
354
|
|
|
|
|
|
|
|
355
|
0
|
|
|
|
|
|
git_oid_fmt(hex_oid, oid); |
356
|
0
|
|
|
|
|
|
hex_oid[GIT_OID_HEXSZ] = '\0'; |
357
|
|
|
|
|
|
|
|
358
|
0
|
|
|
|
|
|
git_error_set(GIT_ERROR_OBJECT, "the git_object of id '%s' can not be " |
359
|
|
|
|
|
|
|
"successfully peeled into a %s (git_object_t=%i).", hex_oid, type_name, type); |
360
|
|
|
|
|
|
|
|
361
|
0
|
|
|
|
|
|
return error; |
362
|
|
|
|
|
|
|
} |
363
|
|
|
|
|
|
|
|
364
|
278
|
|
|
|
|
|
static int check_type_combination(git_object_t type, git_object_t target) |
365
|
|
|
|
|
|
|
{ |
366
|
278
|
100
|
|
|
|
|
if (type == target) |
367
|
108
|
|
|
|
|
|
return 0; |
368
|
|
|
|
|
|
|
|
369
|
170
|
|
|
|
|
|
switch (type) { |
370
|
|
|
|
|
|
|
case GIT_OBJECT_BLOB: |
371
|
|
|
|
|
|
|
case GIT_OBJECT_TREE: |
372
|
|
|
|
|
|
|
/* a blob or tree can never be peeled to anything but themselves */ |
373
|
0
|
|
|
|
|
|
return GIT_EINVALIDSPEC; |
374
|
|
|
|
|
|
|
break; |
375
|
|
|
|
|
|
|
case GIT_OBJECT_COMMIT: |
376
|
|
|
|
|
|
|
/* a commit can only be peeled to a tree */ |
377
|
170
|
50
|
|
|
|
|
if (target != GIT_OBJECT_TREE && target != GIT_OBJECT_ANY) |
|
|
0
|
|
|
|
|
|
378
|
0
|
|
|
|
|
|
return GIT_EINVALIDSPEC; |
379
|
170
|
|
|
|
|
|
break; |
380
|
|
|
|
|
|
|
case GIT_OBJECT_TAG: |
381
|
|
|
|
|
|
|
/* a tag may point to anything, so we let anything through */ |
382
|
0
|
|
|
|
|
|
break; |
383
|
|
|
|
|
|
|
default: |
384
|
0
|
|
|
|
|
|
return GIT_EINVALIDSPEC; |
385
|
|
|
|
|
|
|
} |
386
|
|
|
|
|
|
|
|
387
|
170
|
|
|
|
|
|
return 0; |
388
|
|
|
|
|
|
|
} |
389
|
|
|
|
|
|
|
|
390
|
278
|
|
|
|
|
|
int git_object_peel( |
391
|
|
|
|
|
|
|
git_object **peeled, |
392
|
|
|
|
|
|
|
const git_object *object, |
393
|
|
|
|
|
|
|
git_object_t target_type) |
394
|
|
|
|
|
|
|
{ |
395
|
278
|
|
|
|
|
|
git_object *source, *deref = NULL; |
396
|
|
|
|
|
|
|
int error; |
397
|
|
|
|
|
|
|
|
398
|
278
|
50
|
|
|
|
|
assert(object && peeled); |
|
|
50
|
|
|
|
|
|
399
|
|
|
|
|
|
|
|
400
|
278
|
50
|
|
|
|
|
assert(target_type == GIT_OBJECT_TAG || |
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
401
|
|
|
|
|
|
|
target_type == GIT_OBJECT_COMMIT || |
402
|
|
|
|
|
|
|
target_type == GIT_OBJECT_TREE || |
403
|
|
|
|
|
|
|
target_type == GIT_OBJECT_BLOB || |
404
|
|
|
|
|
|
|
target_type == GIT_OBJECT_ANY); |
405
|
|
|
|
|
|
|
|
406
|
278
|
50
|
|
|
|
|
if ((error = check_type_combination(git_object_type(object), target_type)) < 0) |
407
|
0
|
|
|
|
|
|
return peel_error(error, git_object_id(object), target_type); |
408
|
|
|
|
|
|
|
|
409
|
278
|
100
|
|
|
|
|
if (git_object_type(object) == target_type) |
410
|
108
|
|
|
|
|
|
return git_object_dup(peeled, (git_object *)object); |
411
|
|
|
|
|
|
|
|
412
|
170
|
|
|
|
|
|
source = (git_object *)object; |
413
|
|
|
|
|
|
|
|
414
|
170
|
50
|
|
|
|
|
while (!(error = dereference_object(&deref, source))) { |
415
|
|
|
|
|
|
|
|
416
|
170
|
50
|
|
|
|
|
if (source != object) |
417
|
0
|
|
|
|
|
|
git_object_free(source); |
418
|
|
|
|
|
|
|
|
419
|
170
|
50
|
|
|
|
|
if (git_object_type(deref) == target_type) { |
420
|
170
|
|
|
|
|
|
*peeled = deref; |
421
|
170
|
|
|
|
|
|
return 0; |
422
|
|
|
|
|
|
|
} |
423
|
|
|
|
|
|
|
|
424
|
0
|
|
|
|
|
|
if (target_type == GIT_OBJECT_ANY && |
425
|
0
|
|
|
|
|
|
git_object_type(deref) != git_object_type(object)) |
426
|
|
|
|
|
|
|
{ |
427
|
0
|
|
|
|
|
|
*peeled = deref; |
428
|
0
|
|
|
|
|
|
return 0; |
429
|
|
|
|
|
|
|
} |
430
|
|
|
|
|
|
|
|
431
|
0
|
|
|
|
|
|
source = deref; |
432
|
0
|
|
|
|
|
|
deref = NULL; |
433
|
|
|
|
|
|
|
} |
434
|
|
|
|
|
|
|
|
435
|
0
|
0
|
|
|
|
|
if (source != object) |
436
|
0
|
|
|
|
|
|
git_object_free(source); |
437
|
|
|
|
|
|
|
|
438
|
0
|
|
|
|
|
|
git_object_free(deref); |
439
|
|
|
|
|
|
|
|
440
|
0
|
0
|
|
|
|
|
if (error) |
441
|
0
|
|
|
|
|
|
error = peel_error(error, git_object_id(object), target_type); |
442
|
|
|
|
|
|
|
|
443
|
278
|
|
|
|
|
|
return error; |
444
|
|
|
|
|
|
|
} |
445
|
|
|
|
|
|
|
|
446
|
1037
|
|
|
|
|
|
int git_object_dup(git_object **dest, git_object *source) |
447
|
|
|
|
|
|
|
{ |
448
|
1037
|
|
|
|
|
|
git_cached_obj_incref(source); |
449
|
1037
|
|
|
|
|
|
*dest = source; |
450
|
1037
|
|
|
|
|
|
return 0; |
451
|
|
|
|
|
|
|
} |
452
|
|
|
|
|
|
|
|
453
|
5
|
|
|
|
|
|
int git_object_lookup_bypath( |
454
|
|
|
|
|
|
|
git_object **out, |
455
|
|
|
|
|
|
|
const git_object *treeish, |
456
|
|
|
|
|
|
|
const char *path, |
457
|
|
|
|
|
|
|
git_object_t type) |
458
|
|
|
|
|
|
|
{ |
459
|
5
|
|
|
|
|
|
int error = -1; |
460
|
5
|
|
|
|
|
|
git_tree *tree = NULL; |
461
|
5
|
|
|
|
|
|
git_tree_entry *entry = NULL; |
462
|
|
|
|
|
|
|
|
463
|
5
|
50
|
|
|
|
|
assert(out && treeish && path); |
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
464
|
|
|
|
|
|
|
|
465
|
5
|
50
|
|
|
|
|
if ((error = git_object_peel((git_object**)&tree, treeish, GIT_OBJECT_TREE)) < 0 || |
|
|
100
|
|
|
|
|
|
466
|
5
|
|
|
|
|
|
(error = git_tree_entry_bypath(&entry, tree, path)) < 0) |
467
|
|
|
|
|
|
|
{ |
468
|
|
|
|
|
|
|
goto cleanup; |
469
|
|
|
|
|
|
|
} |
470
|
|
|
|
|
|
|
|
471
|
4
|
50
|
|
|
|
|
if (type != GIT_OBJECT_ANY && git_tree_entry_type(entry) != type) |
|
|
50
|
|
|
|
|
|
472
|
|
|
|
|
|
|
{ |
473
|
0
|
|
|
|
|
|
git_error_set(GIT_ERROR_OBJECT, |
474
|
|
|
|
|
|
|
"object at path '%s' is not of the asked-for type %d", |
475
|
|
|
|
|
|
|
path, type); |
476
|
0
|
|
|
|
|
|
error = GIT_EINVALIDSPEC; |
477
|
0
|
|
|
|
|
|
goto cleanup; |
478
|
|
|
|
|
|
|
} |
479
|
|
|
|
|
|
|
|
480
|
4
|
|
|
|
|
|
error = git_tree_entry_to_object(out, git_object_owner(treeish), entry); |
481
|
|
|
|
|
|
|
|
482
|
|
|
|
|
|
|
cleanup: |
483
|
5
|
|
|
|
|
|
git_tree_entry_free(entry); |
484
|
5
|
|
|
|
|
|
git_tree_free(tree); |
485
|
5
|
|
|
|
|
|
return error; |
486
|
|
|
|
|
|
|
} |
487
|
|
|
|
|
|
|
|
488
|
0
|
|
|
|
|
|
int git_object_short_id(git_buf *out, const git_object *obj) |
489
|
|
|
|
|
|
|
{ |
490
|
|
|
|
|
|
|
git_repository *repo; |
491
|
0
|
|
|
|
|
|
int len = GIT_ABBREV_DEFAULT, error; |
492
|
0
|
|
|
|
|
|
git_oid id = {{0}}; |
493
|
|
|
|
|
|
|
git_odb *odb; |
494
|
|
|
|
|
|
|
|
495
|
0
|
0
|
|
|
|
|
assert(out && obj); |
|
|
0
|
|
|
|
|
|
496
|
|
|
|
|
|
|
|
497
|
0
|
|
|
|
|
|
git_buf_sanitize(out); |
498
|
0
|
|
|
|
|
|
repo = git_object_owner(obj); |
499
|
|
|
|
|
|
|
|
500
|
0
|
0
|
|
|
|
|
if ((error = git_repository__configmap_lookup(&len, repo, GIT_CONFIGMAP_ABBREV)) < 0) |
501
|
0
|
|
|
|
|
|
return error; |
502
|
|
|
|
|
|
|
|
503
|
0
|
0
|
|
|
|
|
if ((error = git_repository_odb(&odb, repo)) < 0) |
504
|
0
|
|
|
|
|
|
return error; |
505
|
|
|
|
|
|
|
|
506
|
0
|
0
|
|
|
|
|
while (len < GIT_OID_HEXSZ) { |
507
|
|
|
|
|
|
|
/* set up short oid */ |
508
|
0
|
|
|
|
|
|
memcpy(&id.id, &obj->cached.oid.id, (len + 1) / 2); |
509
|
0
|
0
|
|
|
|
|
if (len & 1) |
510
|
0
|
|
|
|
|
|
id.id[len / 2] &= 0xf0; |
511
|
|
|
|
|
|
|
|
512
|
0
|
|
|
|
|
|
error = git_odb_exists_prefix(NULL, odb, &id, len); |
513
|
0
|
0
|
|
|
|
|
if (error != GIT_EAMBIGUOUS) |
514
|
0
|
|
|
|
|
|
break; |
515
|
|
|
|
|
|
|
|
516
|
0
|
|
|
|
|
|
git_error_clear(); |
517
|
0
|
|
|
|
|
|
len++; |
518
|
|
|
|
|
|
|
} |
519
|
|
|
|
|
|
|
|
520
|
0
|
0
|
|
|
|
|
if (!error && !(error = git_buf_grow(out, len + 1))) { |
|
|
0
|
|
|
|
|
|
521
|
0
|
|
|
|
|
|
git_oid_tostr(out->ptr, len + 1, &id); |
522
|
0
|
|
|
|
|
|
out->size = len; |
523
|
|
|
|
|
|
|
} |
524
|
|
|
|
|
|
|
|
525
|
0
|
|
|
|
|
|
git_odb_free(odb); |
526
|
|
|
|
|
|
|
|
527
|
0
|
|
|
|
|
|
return error; |
528
|
|
|
|
|
|
|
} |
529
|
|
|
|
|
|
|
|
530
|
327
|
|
|
|
|
|
bool git_object__is_valid( |
531
|
|
|
|
|
|
|
git_repository *repo, const git_oid *id, git_object_t expected_type) |
532
|
|
|
|
|
|
|
{ |
533
|
|
|
|
|
|
|
git_odb *odb; |
534
|
|
|
|
|
|
|
git_object_t actual_type; |
535
|
|
|
|
|
|
|
size_t len; |
536
|
|
|
|
|
|
|
int error; |
537
|
|
|
|
|
|
|
|
538
|
327
|
50
|
|
|
|
|
if (!git_object__strict_input_validation) |
539
|
0
|
|
|
|
|
|
return true; |
540
|
|
|
|
|
|
|
|
541
|
327
|
50
|
|
|
|
|
if ((error = git_repository_odb__weakptr(&odb, repo)) < 0 || |
|
|
50
|
|
|
|
|
|
542
|
327
|
|
|
|
|
|
(error = git_odb_read_header(&len, &actual_type, odb, id)) < 0) |
543
|
0
|
|
|
|
|
|
return false; |
544
|
|
|
|
|
|
|
|
545
|
327
|
100
|
|
|
|
|
if (expected_type != GIT_OBJECT_ANY && expected_type != actual_type) { |
|
|
50
|
|
|
|
|
|
546
|
0
|
|
|
|
|
|
git_error_set(GIT_ERROR_INVALID, |
547
|
|
|
|
|
|
|
"the requested type does not match the type in the ODB"); |
548
|
0
|
|
|
|
|
|
return false; |
549
|
|
|
|
|
|
|
} |
550
|
|
|
|
|
|
|
|
551
|
327
|
|
|
|
|
|
return true; |
552
|
|
|
|
|
|
|
} |