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
0d10e79d
Commit
0d10e79d
authored
Dec 17, 2012
by
Vicent Martí
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #1149 from nulltoken/topic/blob_isbinary
Introduce git_blob_is_binary()
parents
f7953509
a3337f10
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
40 additions
and
4 deletions
+40
-4
include/git2/blob.h
+13
-0
src/blob.c
+12
-0
src/indexer.c
+3
-3
src/win32/posix_w32.c
+0
-1
tests-clar/diff/blob.c
+12
-0
No files found.
include/git2/blob.h
View file @
0d10e79d
...
@@ -183,6 +183,19 @@ GIT_EXTERN(int) git_blob_create_fromchunks(
...
@@ -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
);
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
GIT_END_DECL
#endif
#endif
src/blob.c
View file @
0d10e79d
...
@@ -296,3 +296,15 @@ cleanup:
...
@@ -296,3 +296,15 @@ cleanup:
git__free
(
content
);
git__free
(
content
);
return
error
;
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
);
}
src/indexer.c
View file @
0d10e79d
...
@@ -201,7 +201,7 @@ static void hash_header(git_hash_ctx *ctx, git_off_t len, git_otype type)
...
@@ -201,7 +201,7 @@ static void hash_header(git_hash_ctx *ctx, git_off_t len, git_otype type)
char
buffer
[
64
];
char
buffer
[
64
];
size_t
hdrlen
;
size_t
hdrlen
;
hdrlen
=
git_odb__format_object_header
(
buffer
,
sizeof
(
buffer
),
len
,
type
);
hdrlen
=
git_odb__format_object_header
(
buffer
,
sizeof
(
buffer
),
(
size_t
)
len
,
type
);
git_hash_update
(
ctx
,
buffer
,
hdrlen
);
git_hash_update
(
ctx
,
buffer
,
hdrlen
);
}
}
...
@@ -269,11 +269,11 @@ static int crc_object(uint32_t *crc_out, git_mwindow_file *mwf, git_off_t start,
...
@@ -269,11 +269,11 @@ static int crc_object(uint32_t *crc_out, git_mwindow_file *mwf, git_off_t start,
crc
=
crc32
(
0L
,
Z_NULL
,
0
);
crc
=
crc32
(
0L
,
Z_NULL
,
0
);
while
(
size
)
{
while
(
size
)
{
ptr
=
git_mwindow_open
(
mwf
,
&
w
,
start
,
size
,
&
left
);
ptr
=
git_mwindow_open
(
mwf
,
&
w
,
start
,
(
size_t
)
size
,
&
left
);
if
(
ptr
==
NULL
)
if
(
ptr
==
NULL
)
return
-
1
;
return
-
1
;
len
=
min
(
left
,
size
);
len
=
min
(
left
,
(
size_t
)
size
);
crc
=
crc32
(
crc
,
ptr
,
len
);
crc
=
crc32
(
crc
,
ptr
,
len
);
size
-=
len
;
size
-=
len
;
start
+=
len
;
start
+=
len
;
...
...
src/win32/posix_w32.c
View file @
0d10e79d
...
@@ -59,7 +59,6 @@ static int do_lstat(
...
@@ -59,7 +59,6 @@ static int do_lstat(
{
{
WIN32_FILE_ATTRIBUTE_DATA
fdata
;
WIN32_FILE_ATTRIBUTE_DATA
fdata
;
wchar_t
fbuf
[
GIT_WIN_PATH
],
lastch
;
wchar_t
fbuf
[
GIT_WIN_PATH
],
lastch
;
DWORD
last_error
;
int
flen
;
int
flen
;
flen
=
git__utf8_to_16
(
fbuf
,
GIT_WIN_PATH
,
file_name
);
flen
=
git__utf8_to_16
(
fbuf
,
GIT_WIN_PATH
,
file_name
);
...
...
tests-clar/diff/blob.c
View file @
0d10e79d
...
@@ -335,3 +335,15 @@ void test_diff_blob__checks_options_version_too_high(void)
...
@@ -335,3 +335,15 @@ void test_diff_blob__checks_options_version_too_high(void)
err
=
giterr_last
();
err
=
giterr_last
();
cl_assert_equal_i
(
GITERR_INVALID
,
err
->
klass
);
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