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
a3337f10
Commit
a3337f10
authored
Dec 17, 2012
by
nulltoken
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
blob: introduce git_blob_is_binary()
parent
bdb94c21
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
37 additions
and
0 deletions
+37
-0
include/git2/blob.h
+13
-0
src/blob.c
+12
-0
tests-clar/diff/blob.c
+12
-0
No files found.
include/git2/blob.h
View file @
a3337f10
...
...
@@ -183,6 +183,19 @@ GIT_EXTERN(int) git_blob_create_fromchunks(
*/
GIT_EXTERN
(
int
)
git_blob_create_frombuffer
(
git_oid
*
oid
,
git_repository
*
repo
,
const
void
*
buffer
,
size_t
len
);
/**
* Determine if the blob content is most certainly binary or not.
*
* The heuristic used to guess if a file is binary is taken from core git:
* Searching for NUL bytes and looking for a reasonable ratio of printable
* to non-printable characters among the first 4000 bytes.
*
* @param blob The blob which content should be analyzed
* @return 1 if the content of the blob is detected
* as binary; 0 otherwise.
*/
GIT_EXTERN
(
int
)
git_blob_is_binary
(
git_blob
*
blob
);
/** @} */
GIT_END_DECL
#endif
src/blob.c
View file @
a3337f10
...
...
@@ -296,3 +296,15 @@ cleanup:
git__free
(
content
);
return
error
;
}
int
git_blob_is_binary
(
git_blob
*
blob
)
{
git_buf
content
;
assert
(
blob
);
content
.
ptr
=
blob
->
odb_object
->
raw
.
data
;
content
.
size
=
min
(
blob
->
odb_object
->
raw
.
len
,
4000
);
return
git_buf_text_is_binary
(
&
content
);
}
tests-clar/diff/blob.c
View file @
a3337f10
...
...
@@ -335,3 +335,15 @@ void test_diff_blob__checks_options_version_too_high(void)
err
=
giterr_last
();
cl_assert_equal_i
(
GITERR_INVALID
,
err
->
klass
);
}
void
test_diff_blob__can_correctly_detect_a_binary_blob_as_binary
(
void
)
{
/* alien.png */
cl_assert_equal_i
(
true
,
git_blob_is_binary
(
alien
));
}
void
test_diff_blob__can_correctly_detect_a_textual_blob_as_non_binary
(
void
)
{
/* tests/resources/attr/root_test4.txt */
cl_assert_equal_i
(
false
,
git_blob_is_binary
(
d
));
}
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