Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
libgit2/src/object.c at development · dncc/libgit2 · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
See all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
dncc
libgit2
Repository navigation
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
More
items
Files
Expand file tree
development
Breadcrumbs
libgit2
/
src
/
object.c
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
284 lines (219 loc) · 6.56 KB
development
Breadcrumbs
libgit2
/
src
/
object.c
Copy path
Top
File metadata and controls
Code
Blame
284 lines (219 loc) · 6.56 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*
* Copyright (C) 2009-2011 the libgit2 contributors
*
* This file is part of libgit2, distributed under the GNU GPL v2 with
* a Linking Exception. For full terms see the included COPYING file.
*/
#include
<stdarg.h>
#include
"git2/object.h"
#include
"common.h"
#include
"repository.h"
#include
"commit.h"
#include
"tree.h"
#include
"blob.h"
#include
"tag.h"
static
const
int
OBJECT_BASE_SIZE
=
4096
;
static
struct
{
const
char
*
str
;
/* type name string */
int
loose
;
/* valid loose object type flag */
size_t
size
;
/* size in bytes of the object structure */
}
git_objects_table
[]
=
{
/* 0 = GIT_OBJ__EXT1 */
{
""
,
0
,
0
},
/* 1 = GIT_OBJ_COMMIT */
{
"commit"
,
1
,
sizeof
(
struct
git_commit
)},
/* 2 = GIT_OBJ_TREE */
{
"tree"
,
1
,
sizeof
(
struct
git_tree
) },
/* 3 = GIT_OBJ_BLOB */
{
"blob"
,
1
,
sizeof
(
struct
git_blob
) },
/* 4 = GIT_OBJ_TAG */
{
"tag"
,
1
,
sizeof
(
struct
git_tag
) },
/* 5 = GIT_OBJ__EXT2 */
{
""
,
0
,
0
},
/* 6 = GIT_OBJ_OFS_DELTA */
{
"OFS_DELTA"
,
0
,
0
},
/* 7 = GIT_OBJ_REF_DELTA */
{
"REF_DELTA"
,
0
,
0
}
};
static
int
create_object
(
git_object
*
*
object_out
,
git_otype
type
)
{
git_object
*
object
=
NULL
;
assert
(
object_out
);
*
object_out
=
NULL
;
switch
(
type
) {
case
GIT_OBJ_COMMIT
:
case
GIT_OBJ_TAG
:
case
GIT_OBJ_BLOB
:
case
GIT_OBJ_TREE
:
object
=
git__malloc
(
git_object__size
(
type
));
if
(
object
==
NULL
)
return
GIT_ENOMEM
;
memset
(
object
,
0x0
,
git_object__size
(
type
));
break
;
default
:
return
git__throw
(
GIT_EINVALIDTYPE
,
"The given type is invalid"
);
}
object
->
type
=
type
;
*
object_out
=
object
;
return
GIT_SUCCESS
;
}
int
git_object_lookup_prefix
(
git_object
*
*
object_out
,
git_repository
*
repo
,
const
git_oid
*
id
,
unsigned
int
len
,
git_otype
type
)
{
git_object
*
object
=
NULL
;
git_odb_object
*
odb_obj
;
int
error
=
GIT_SUCCESS
;
assert
(
repo
&&
object_out
&&
id
);
if
(
len
<
GIT_OID_MINPREFIXLEN
)
return
git__throw
(
GIT_EAMBIGUOUSOIDPREFIX
,
"Failed to lookup object. Prefix length is lower than %d."
,
GIT_OID_MINPREFIXLEN
);
if
(
len
>
GIT_OID_HEXSZ
)
len
=
GIT_OID_HEXSZ
;
if
(
len
==
GIT_OID_HEXSZ
) {
/* We want to match the full id : we can first look up in the cache,
* since there is no need to check for non ambiguousity
*/
object
=
git_cache_get
(
&
repo
->
objects
,
id
);
if
(
object
!=
NULL
) {
if
(
type
!=
GIT_OBJ_ANY
&&
type
!=
object
->
type
)
{
git_object_close
(
object
);
return
git__throw
(
GIT_EINVALIDTYPE
,
"Failed to lookup object. The given type does not match the type on the ODB"
);
}
*
object_out
=
object
;
return
GIT_SUCCESS
;
}
/* Object was not found in the cache, let's explore the backends.
* We could just use git_odb_read_unique_short_oid,
* it is the same cost for packed and loose object backends,
* but it may be much more costly for sqlite and hiredis.
*/
error
=
git_odb_read
(
&
odb_obj
,
repo
->
db
,
id
);
}
else
{
git_oid
short_oid
;
/* We copy the first len*4 bits from id and fill the remaining with 0s */
memcpy
(
short_oid
.
id
,
id
->
id
, (
len
+
1
) /
2
);
if
(
len
%
2
)
short_oid
.
id
[
len
/
2
] &=
0xF0
;
memset
(
short_oid
.
id
+
(
len
+
1
) /
2
,
0
, (
GIT_OID_HEXSZ
-
len
) /
2
);
/* If len < GIT_OID_HEXSZ (a strict short oid was given), we have
* 2 options :
* - We always search in the cache first. If we find that short oid is
* ambiguous, we can stop. But in all the other cases, we must then
* explore all the backends (to find an object if there was match,
* or to check that oid is not ambiguous if we have found 1 match in
* the cache)
* - We never explore the cache, go right to exploring the backends
* We chose the latter : we explore directly the backends.
*/
error
=
git_odb_read_prefix
(
&
odb_obj
,
repo
->
db
,
&
short_oid
,
len
);
}
if
(
error
<
GIT_SUCCESS
)
return
git__rethrow
(
error
,
"Failed to lookup object"
);
if
(
type
!=
GIT_OBJ_ANY
&&
type
!=
odb_obj
->
raw
.
type
) {
git_odb_object_close
(
odb_obj
);
return
git__throw
(
GIT_EINVALIDTYPE
,
"Failed to lookup object. The given type does not match the type on the ODB"
);
}
type
=
odb_obj
->
raw
.
type
;
if
((
error
=
create_object
(
&
object
,
type
))
<
GIT_SUCCESS
)
return
git__rethrow
(
error
,
"Failed to lookup object"
);
/* Initialize parent object */
git_oid_cpy
(
&
object
->
cached
.
oid
,
&
odb_obj
->
cached
.
oid
);
object
->
repo
=
repo
;
switch
(
type
) {
case
GIT_OBJ_COMMIT
:
error
=
git_commit__parse
((
git_commit
*
)
object
,
odb_obj
);
break
;
case
GIT_OBJ_TREE
:
error
=
git_tree__parse
((
git_tree
*
)
object
,
odb_obj
);
break
;
case
GIT_OBJ_TAG
:
error
=
git_tag__parse
((
git_tag
*
)
object
,
odb_obj
);
break
;
case
GIT_OBJ_BLOB
:
error
=
git_blob__parse
((
git_blob
*
)
object
,
odb_obj
);
break
;
default
:
break
;
}
git_odb_object_close
(
odb_obj
);
if
(
error
<
GIT_SUCCESS
) {
git_object__free
(
object
);
return
git__rethrow
(
error
,
"Failed to lookup object"
);
}
*
object_out
=
git_cache_try_store
(
&
repo
->
objects
,
object
);
return
GIT_SUCCESS
;
}
int
git_object_lookup
(
git_object
*
*
object_out
,
git_repository
*
repo
,
const
git_oid
*
id
,
git_otype
type
) {
return
git_object_lookup_prefix
(
object_out
,
repo
,
id
,
GIT_OID_HEXSZ
,
type
);
}
void
git_object__free
(
void
*
_obj
)
{
git_object
*
object
=
(
git_object
*
)
_obj
;
assert
(
object
);
switch
(
object
->
type
) {
case
GIT_OBJ_COMMIT
:
git_commit__free
((
git_commit
*
)
object
);
break
;
case
GIT_OBJ_TREE
:
git_tree__free
((
git_tree
*
)
object
);
break
;
case
GIT_OBJ_TAG
:
git_tag__free
((
git_tag
*
)
object
);
break
;
case
GIT_OBJ_BLOB
:
git_blob__free
((
git_blob
*
)
object
);
break
;
default
:
git__free
(
object
);
break
;
}
}
void
git_object_close
(
git_object
*
object
)
{
if
(
object
==
NULL
)
return
;
git_cached_obj_decref
((
git_cached_obj
*
)
object
,
git_object__free
);
}
const
git_oid
*
git_object_id
(
const
git_object
*
obj
)
{
assert
(
obj
);
return
&
obj
->
cached
.
oid
;
}
git_otype
git_object_type
(
const
git_object
*
obj
)
{
assert
(
obj
);
return
obj
->
type
;
}
git_repository
*
git_object_owner
(
const
git_object
*
obj
)
{
assert
(
obj
);
return
obj
->
repo
;
}
const
char
*
git_object_type2string
(
git_otype
type
)
{
if
(
type
<
0
||
((
size_t
)
type
) >=
ARRAY_SIZE
(
git_objects_table
))
return
""
;
return
git_objects_table
[
type
].
str
;
}
git_otype
git_object_string2type
(
const
char
*
str
)
{
size_t
i
;
if
(!
str
||
!
*
str
)
return
GIT_OBJ_BAD
;
for
(
i
=
0
;
i
<
ARRAY_SIZE
(
git_objects_table
);
i
++
)
if
(!
strcmp
(
str
,
git_objects_table
[
i
].
str
))
return
(
git_otype
)
i
;
return
GIT_OBJ_BAD
;
}
int
git_object_typeisloose
(
git_otype
type
)
{
if
(
type
<
0
||
((
size_t
)
type
) >=
ARRAY_SIZE
(
git_objects_table
))
return
0
;
return
git_objects_table
[
type
].
loose
;
}
size_t
git_object__size
(
git_otype
type
)
{
if
(
type
<
0
||
((
size_t
)
type
) >=
ARRAY_SIZE
(
git_objects_table
))
return
0
;
return
git_objects_table
[
type
].
size
;
}
You can’t perform that action at this time.