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
162c996b
Commit
162c996b
authored
Jan 25, 2022
by
Edward Thomson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
oid: add git_oid_fmt_substr
Tidy up `nfmt` / `pathfmt`.
parent
4d7ec76c
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
61 additions
and
25 deletions
+61
-25
src/libgit2/oid.c
+7
-25
src/libgit2/oid.h
+29
-0
tests/libgit2/core/oid.c
+25
-0
No files found.
src/libgit2/oid.c
View file @
162c996b
...
@@ -22,8 +22,6 @@ const git_oid git_oid__empty_tree_sha1 =
...
@@ -22,8 +22,6 @@ const git_oid git_oid__empty_tree_sha1 =
{
0x4b
,
0x82
,
0x5d
,
0xc6
,
0x42
,
0xcb
,
0x6e
,
0xb9
,
0xa0
,
0x60
,
{
0x4b
,
0x82
,
0x5d
,
0xc6
,
0x42
,
0xcb
,
0x6e
,
0xb9
,
0xa0
,
0x60
,
0xe5
,
0x4b
,
0xf8
,
0xd6
,
0x92
,
0x88
,
0xfb
,
0xee
,
0x49
,
0x04
}};
0xe5
,
0x4b
,
0xf8
,
0xd6
,
0x92
,
0x88
,
0xfb
,
0xee
,
0x49
,
0x04
}};
static
char
to_hex
[]
=
"0123456789abcdef"
;
static
int
oid_error_invalid
(
const
char
*
msg
)
static
int
oid_error_invalid
(
const
char
*
msg
)
{
{
git_error_set
(
GIT_ERROR_INVALID
,
"unable to parse OID - %s"
,
msg
);
git_error_set
(
GIT_ERROR_INVALID
,
"unable to parse OID - %s"
,
msg
);
...
@@ -75,16 +73,9 @@ int git_oid_fromstr(git_oid *out, const char *str, git_oid_t type)
...
@@ -75,16 +73,9 @@ int git_oid_fromstr(git_oid *out, const char *str, git_oid_t type)
return
git_oid_fromstrn
(
out
,
str
,
git_oid_hexsize
(
type
),
type
);
return
git_oid_fromstrn
(
out
,
str
,
git_oid_hexsize
(
type
),
type
);
}
}
GIT_INLINE
(
char
)
*
fmt_one
(
char
*
str
,
unsigned
int
val
)
{
*
str
++
=
to_hex
[
val
>>
4
];
*
str
++
=
to_hex
[
val
&
0xf
];
return
str
;
}
int
git_oid_nfmt
(
char
*
str
,
size_t
n
,
const
git_oid
*
oid
)
int
git_oid_nfmt
(
char
*
str
,
size_t
n
,
const
git_oid
*
oid
)
{
{
size_t
hex_size
,
i
,
max_i
;
size_t
hex_size
;
if
(
!
oid
)
{
if
(
!
oid
)
{
memset
(
str
,
0
,
n
);
memset
(
str
,
0
,
n
);
...
@@ -99,14 +90,7 @@ int git_oid_nfmt(char *str, size_t n, const git_oid *oid)
...
@@ -99,14 +90,7 @@ int git_oid_nfmt(char *str, size_t n, const git_oid *oid)
n
=
hex_size
;
n
=
hex_size
;
}
}
max_i
=
n
/
2
;
git_oid_fmt_substr
(
str
,
oid
,
0
,
n
);
for
(
i
=
0
;
i
<
max_i
;
i
++
)
str
=
fmt_one
(
str
,
oid
->
id
[
i
]);
if
(
n
&
1
)
*
str
++
=
to_hex
[
oid
->
id
[
i
]
>>
4
];
return
0
;
return
0
;
}
}
...
@@ -117,16 +101,14 @@ int git_oid_fmt(char *str, const git_oid *oid)
...
@@ -117,16 +101,14 @@ int git_oid_fmt(char *str, const git_oid *oid)
int
git_oid_pathfmt
(
char
*
str
,
const
git_oid
*
oid
)
int
git_oid_pathfmt
(
char
*
str
,
const
git_oid
*
oid
)
{
{
size_t
size
,
i
;
size_t
hex_size
;
if
(
!
(
size
=
git_oid_
size
(
oid
->
type
)))
if
(
!
(
hex_size
=
git_oid_hex
size
(
oid
->
type
)))
return
oid_error_invalid
(
"unknown type"
);
return
oid_error_invalid
(
"unknown type"
);
str
=
fmt_one
(
str
,
oid
->
id
[
0
]);
git_oid_fmt_substr
(
str
,
oid
,
0
,
2
);
*
str
++
=
'/'
;
str
[
2
]
=
'/'
;
for
(
i
=
1
;
i
<
size
;
i
++
)
git_oid_fmt_substr
(
&
str
[
3
],
oid
,
2
,
(
hex_size
-
2
));
str
=
fmt_one
(
str
,
oid
->
id
[
i
]);
return
0
;
return
0
;
}
}
...
...
src/libgit2/oid.h
View file @
162c996b
...
@@ -64,6 +64,35 @@ GIT_INLINE(git_hash_algorithm_t) git_oid_algorithm(git_oid_t type)
...
@@ -64,6 +64,35 @@ GIT_INLINE(git_hash_algorithm_t) git_oid_algorithm(git_oid_t type)
*/
*/
char
*
git_oid_allocfmt
(
const
git_oid
*
id
);
char
*
git_oid_allocfmt
(
const
git_oid
*
id
);
/**
* Format the requested nibbles of an object id.
*
* @param str the string to write into
* @param oid the oid structure to format
* @param start the starting number of nibbles
* @param count the number of nibbles to format
*/
GIT_INLINE
(
void
)
git_oid_fmt_substr
(
char
*
str
,
const
git_oid
*
oid
,
size_t
start
,
size_t
count
)
{
static
char
hex
[]
=
"0123456789abcdef"
;
size_t
i
,
end
=
start
+
count
,
min
=
start
/
2
,
max
=
end
/
2
;
if
(
start
&
1
)
*
str
++
=
hex
[
oid
->
id
[
min
++
]
&
0x0f
];
for
(
i
=
min
;
i
<
max
;
i
++
)
{
*
str
++
=
hex
[
oid
->
id
[
i
]
>>
4
];
*
str
++
=
hex
[
oid
->
id
[
i
]
&
0x0f
];
}
if
(
end
&
1
)
*
str
++
=
hex
[
oid
->
id
[
i
]
>>
4
];
}
GIT_INLINE
(
int
)
git_oid_raw_ncmp
(
GIT_INLINE
(
int
)
git_oid_raw_ncmp
(
const
unsigned
char
*
sha1
,
const
unsigned
char
*
sha1
,
const
unsigned
char
*
sha2
,
const
unsigned
char
*
sha2
,
...
...
tests/libgit2/core/oid.c
View file @
162c996b
...
@@ -151,3 +151,28 @@ void test_core_oid__is_hexstr(void)
...
@@ -151,3 +151,28 @@ void test_core_oid__is_hexstr(void)
cl_assert
(
!
git_oid__is_hexstr
(
"zeadbeefdeadbeefdeadbeefdeadbeefdeadbeef"
,
GIT_OID_SHA1
));
cl_assert
(
!
git_oid__is_hexstr
(
"zeadbeefdeadbeefdeadbeefdeadbeefdeadbeef"
,
GIT_OID_SHA1
));
cl_assert
(
!
git_oid__is_hexstr
(
"deadbeefdeadbeefdeadbeefdeadbeefdeadbeef1"
,
GIT_OID_SHA1
));
cl_assert
(
!
git_oid__is_hexstr
(
"deadbeefdeadbeefdeadbeefdeadbeefdeadbeef1"
,
GIT_OID_SHA1
));
}
}
void
test_core_oid__fmt_substr_sha1
(
void
)
{
char
buf
[
GIT_OID_MAX_HEXSIZE
];
memset
(
buf
,
0
,
GIT_OID_MAX_HEXSIZE
);
git_oid_fmt_substr
(
buf
,
&
id_sha1
,
0
,
40
);
cl_assert_equal_s
(
buf
,
str_oid_sha1
);
memset
(
buf
,
0
,
GIT_OID_MAX_HEXSIZE
);
git_oid_fmt_substr
(
buf
,
&
id_sha1
,
0
,
18
);
cl_assert_equal_s
(
buf
,
str_oid_sha1_p
);
memset
(
buf
,
0
,
GIT_OID_MAX_HEXSIZE
);
git_oid_fmt_substr
(
buf
,
&
id_sha1
,
0
,
5
);
cl_assert_equal_s
(
buf
,
"ae90f"
);
memset
(
buf
,
0
,
GIT_OID_MAX_HEXSIZE
);
git_oid_fmt_substr
(
buf
,
&
id_sha1
,
5
,
5
);
cl_assert_equal_s
(
buf
,
"12eea"
);
memset
(
buf
,
0
,
GIT_OID_MAX_HEXSIZE
);
git_oid_fmt_substr
(
buf
,
&
id_sha1
,
5
,
6
);
cl_assert_equal_s
(
buf
,
"12eea6"
);
}
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