Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
G
git2
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
lvzhengyang
git2
Commits
4083e46f
Commit
4083e46f
authored
Dec 03, 2022
by
Edward Thomson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
oid: provide type lookups by enum value or name
parent
f02bcf72
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
60 additions
and
0 deletions
+60
-0
src/libgit2/oid.h
+41
-0
tests/libgit2/core/oid.c
+19
-0
No files found.
src/libgit2/oid.h
View file @
4083e46f
...
...
@@ -66,6 +66,47 @@ GIT_INLINE(size_t) git_oid_hexsize(git_oid_t type)
return
0
;
}
GIT_INLINE
(
const
char
*
)
git_oid_type_name
(
git_oid_t
type
)
{
switch
(
type
)
{
case
GIT_OID_SHA1
:
return
"sha1"
;
#ifdef GIT_EXPERIMENTAL_SHA256
case
GIT_OID_SHA256
:
return
"sha256"
;
#endif
}
return
"unknown"
;
}
GIT_INLINE
(
git_oid_t
)
git_oid_type_fromstr
(
const
char
*
name
)
{
if
(
strcmp
(
name
,
"sha1"
)
==
0
)
return
GIT_OID_SHA1
;
#ifdef GIT_EXPERIMENTAL_SHA256
if
(
strcmp
(
name
,
"sha256"
)
==
0
)
return
GIT_OID_SHA256
;
#endif
return
0
;
}
GIT_INLINE
(
git_oid_t
)
git_oid_type_fromstrn
(
const
char
*
name
,
size_t
len
)
{
if
(
len
==
CONST_STRLEN
(
"sha1"
)
&&
strncmp
(
name
,
"sha1"
,
len
)
==
0
)
return
GIT_OID_SHA1
;
#ifdef GIT_EXPERIMENTAL_SHA256
if
(
len
==
CONST_STRLEN
(
"sha256"
)
&&
strncmp
(
name
,
"sha256"
,
len
)
==
0
)
return
GIT_OID_SHA256
;
#endif
return
0
;
}
GIT_INLINE
(
git_hash_algorithm_t
)
git_oid_algorithm
(
git_oid_t
type
)
{
switch
(
type
)
{
...
...
tests/libgit2/core/oid.c
View file @
4083e46f
...
...
@@ -192,3 +192,22 @@ void test_core_oid__fmt_substr_sha1(void)
git_oid_fmt_substr
(
buf
,
&
id_sha1
,
5
,
6
);
cl_assert_equal_s
(
buf
,
"12eea6"
);
}
void
test_core_oid__type_lookup
(
void
)
{
cl_assert_equal_i
(
GIT_OID_SHA1
,
git_oid_type_fromstr
(
"sha1"
));
cl_assert_equal_i
(
GIT_OID_SHA1
,
git_oid_type_fromstrn
(
"sha1..."
,
4
));
cl_assert_equal_s
(
"sha1"
,
git_oid_type_name
(
GIT_OID_SHA1
));
#ifdef GIT_EXPERIMENTAL_SHA256
cl_assert_equal_i
(
GIT_OID_SHA256
,
git_oid_type_fromstr
(
"sha256"
));
cl_assert_equal_i
(
GIT_OID_SHA256
,
git_oid_type_fromstrn
(
"sha256..."
,
6
));
cl_assert_equal_s
(
"sha256"
,
git_oid_type_name
(
GIT_OID_SHA256
));
#endif
cl_assert_equal_i
(
0
,
git_oid_type_fromstr
(
"sha42"
));
cl_assert_equal_i
(
0
,
git_oid_type_fromstrn
(
"sha1"
,
3
));
cl_assert_equal_i
(
0
,
git_oid_type_fromstrn
(
"sha1..."
,
5
));
cl_assert_equal_s
(
"unknown"
,
git_oid_type_name
(
0
));
cl_assert_equal_s
(
"unknown"
,
git_oid_type_name
(
42
));
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment