Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
libgit2/examples/cat-file.c at development · barkinet/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 }}
barkinet
libgit2
Repository navigation
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
More
items
Files
Expand file tree
development
Breadcrumbs
libgit2
/
examples
/
cat-file.c
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
229 lines (190 loc) · 4.99 KB
development
Breadcrumbs
libgit2
/
examples
/
cat-file.c
Copy path
Top
File metadata and controls
Code
Blame
229 lines (190 loc) · 4.99 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
#include
<stdio.h>
#include
<git2.h>
#include
<stdlib.h>
#include
<string.h>
static
git_repository
*
g_repo
;
static
void
check
(
int
error
,
const
char
*
message
)
{
if
(
error
) {
fprintf
(
stderr
,
"%s (%d)\n"
,
message
,
error
);
exit
(
1
);
}
}
static
void
usage
(
const
char
*
message
,
const
char
*
arg
)
{
if
(
message
&&
arg
)
fprintf
(
stderr
,
"%s: %s\n"
,
message
,
arg
);
else
if
(
message
)
fprintf
(
stderr
,
"%s\n"
,
message
);
fprintf
(
stderr
,
"usage: cat-file (-t | -s | -e | -p) [<options>] <object>\n"
);
exit
(
1
);
}
static
int
check_str_param
(
const
char
*
arg
,
const
char
*
pattern
,
const
char
*
*
val
)
{
size_t
len
=
strlen
(
pattern
);
if
(
strncmp
(
arg
,
pattern
,
len
))
return
0
;
*
val
=
(
const
char
*
)(
arg
+
len
);
return
1
;
}
static
void
print_signature
(
const
char
*
header
,
const
git_signature
*
sig
)
{
char
sign
;
int
offset
,
hours
,
minutes
;
if
(!
sig
)
return
;
offset
=
sig
->
when
.
offset
;
if
(
offset
<
0
) {
sign
=
'-'
;
offset
=
-
offset
;
}
else
{
sign
=
'+'
;
}
hours
=
offset
/
60
;
minutes
=
offset
%
60
;
printf
(
"%s %s <%s> %ld %c%02d%02d\n"
,
header
,
sig
->
name
,
sig
->
email
, (
long
)
sig
->
when
.
time
,
sign
,
hours
,
minutes
);
}
static
void
show_blob
(
const
git_blob
*
blob
)
{
/* ? Does this need crlf filtering? */
fwrite
(
git_blob_rawcontent
(
blob
),
git_blob_rawsize
(
blob
),
1
,
stdout
);
}
static
void
show_tree
(
const
git_tree
*
tree
)
{
size_t
i
,
max_i
=
(
int
)
git_tree_entrycount
(
tree
);
char
oidstr
[
GIT_OID_HEXSZ
+
1
];
const
git_tree_entry
*
te
;
for
(
i
=
0
;
i
<
max_i
;
++
i
) {
te
=
git_tree_entry_byindex
(
tree
,
i
);
git_oid_tostr
(
oidstr
,
sizeof
(
oidstr
),
git_tree_entry_id
(
te
));
printf
(
"%06o %s %s\t%s\n"
,
git_tree_entry_filemode
(
te
),
git_object_type2string
(
git_tree_entry_type
(
te
)),
oidstr
,
git_tree_entry_name
(
te
));
}
}
static
void
show_commit
(
const
git_commit
*
commit
)
{
unsigned
int
i
,
max_i
;
char
oidstr
[
GIT_OID_HEXSZ
+
1
];
git_oid_tostr
(
oidstr
,
sizeof
(
oidstr
),
git_commit_tree_id
(
commit
));
printf
(
"tree %s\n"
,
oidstr
);
max_i
=
(
unsigned
int
)
git_commit_parentcount
(
commit
);
for
(
i
=
0
;
i
<
max_i
;
++
i
) {
git_oid_tostr
(
oidstr
,
sizeof
(
oidstr
),
git_commit_parent_id
(
commit
,
i
));
printf
(
"parent %s\n"
,
oidstr
);
}
print_signature
(
"author"
,
git_commit_author
(
commit
));
print_signature
(
"committer"
,
git_commit_committer
(
commit
));
if
(
git_commit_message
(
commit
))
printf
(
"\n%s\n"
,
git_commit_message
(
commit
));
}
static
void
show_tag
(
const
git_tag
*
tag
)
{
char
oidstr
[
GIT_OID_HEXSZ
+
1
];
git_oid_tostr
(
oidstr
,
sizeof
(
oidstr
),
git_tag_target_id
(
tag
));;
printf
(
"object %s\n"
,
oidstr
);
printf
(
"type %s\n"
,
git_object_type2string
(
git_tag_target_type
(
tag
)));
printf
(
"tag %s\n"
,
git_tag_name
(
tag
));
print_signature
(
"tagger"
,
git_tag_tagger
(
tag
));
if
(
git_tag_message
(
tag
))
printf
(
"\n%s\n"
,
git_tag_message
(
tag
));
}
enum
{
SHOW_TYPE
=
1
,
SHOW_SIZE
=
2
,
SHOW_NONE
=
3
,
SHOW_PRETTY
=
4
};
int
main
(
int
argc
,
char
*
argv
[])
{
const
char
*
dir
=
"."
,
*
rev
=
NULL
;
int
i
,
action
=
0
,
verbose
=
0
;
git_object
*
obj
=
NULL
;
char
oidstr
[
GIT_OID_HEXSZ
+
1
];
git_threads_init
();
for
(
i
=
1
;
i
<
argc
;
++
i
) {
char
*
a
=
argv
[
i
];
if
(
a
[
0
]
!=
'-'
) {
if
(
rev
!=
NULL
)
usage
(
"Only one rev should be provided"
,
NULL
);
else
rev
=
a
;
}
else
if
(!
strcmp
(
a
,
"-t"
))
action
=
SHOW_TYPE
;
else
if
(!
strcmp
(
a
,
"-s"
))
action
=
SHOW_SIZE
;
else
if
(!
strcmp
(
a
,
"-e"
))
action
=
SHOW_NONE
;
else
if
(!
strcmp
(
a
,
"-p"
))
action
=
SHOW_PRETTY
;
else
if
(!
strcmp
(
a
,
"-q"
))
verbose
=
0
;
else
if
(!
strcmp
(
a
,
"-v"
))
verbose
=
1
;
else
if
(!
strcmp
(
a
,
"--help"
)
||
!
strcmp
(
a
,
"-h"
))
usage
(
NULL
,
NULL
);
else
if
(!
check_str_param
(
a
,
"--git-dir="
,
&
dir
))
usage
(
"Unknown option"
,
a
);
}
if
(!
action
||
!
rev
)
usage
(
NULL
,
NULL
);
check
(
git_repository_open_ext
(
&
g_repo
,
dir
,
0
,
NULL
),
"Could not open repository"
);
if
(
git_revparse_single
(
&
obj
,
g_repo
,
rev
)
<
0
) {
fprintf
(
stderr
,
"Could not resolve '%s'\n"
,
rev
);
exit
(
1
);
}
if
(
verbose
) {
char
oidstr
[
GIT_OID_HEXSZ
+
1
];
git_oid_tostr
(
oidstr
,
sizeof
(
oidstr
),
git_object_id
(
obj
));
printf
(
"%s %s\n--\n"
,
git_object_type2string
(
git_object_type
(
obj
)),
oidstr
);
}
switch
(
action
) {
case
SHOW_TYPE
:
printf
(
"%s\n"
,
git_object_type2string
(
git_object_type
(
obj
)));
break
;
case
SHOW_SIZE
: {
git_odb
*
odb
;
git_odb_object
*
odbobj
;
check
(
git_repository_odb
(
&
odb
,
g_repo
),
"Could not open ODB"
);
check
(
git_odb_read
(
&
odbobj
,
odb
,
git_object_id
(
obj
)),
"Could not find obj"
);
printf
(
"%ld\n"
, (
long
)
git_odb_object_size
(
odbobj
));
git_odb_object_free
(
odbobj
);
git_odb_free
(
odb
);
}
break
;
case
SHOW_NONE
:
/* just want return result */
break
;
case
SHOW_PRETTY
:
switch
(
git_object_type
(
obj
)) {
case
GIT_OBJ_BLOB
:
show_blob
((
const
git_blob
*
)
obj
);
break
;
case
GIT_OBJ_COMMIT
:
show_commit
((
const
git_commit
*
)
obj
);
break
;
case
GIT_OBJ_TREE
:
show_tree
((
const
git_tree
*
)
obj
);
break
;
case
GIT_OBJ_TAG
:
show_tag
((
const
git_tag
*
)
obj
);
break
;
default
:
printf
(
"unknown %s\n"
,
oidstr
);
break
;
}
break
;
}
git_object_free
(
obj
);
git_repository_free
(
g_repo
);
git_threads_shutdown
();
return
0
;
}
You can’t perform that action at this time.