fix: check if file is text file before reading it

This commit is contained in:
2024-12-12 22:13:04 +01:00
parent bd49a8903f
commit b2ecb15d7f

View File

@@ -31,6 +31,20 @@ local function should_ignore_file(file, ignore_patterns)
return false return false
end end
local function is_text_file(file)
local fd = uv.fs_open(file, "r", 438)
if not fd then
return false
end
local chunk = uv.fs_read(fd, 1024, 0) or ""
uv.fs_close(fd)
-- Check for null bytes as a heuristic for binary files
if chunk:find("\0") then
return false
end
return true
end
local function scandir(dir, ignore_patterns, files) local function scandir(dir, ignore_patterns, files)
files = files or {} files = files or {}
local fd = uv.fs_opendir(dir, nil, 50) local fd = uv.fs_opendir(dir, nil, 50)
@@ -41,7 +55,7 @@ local function scandir(dir, ignore_patterns, files)
for _, ent in ipairs(ents) do for _, ent in ipairs(ents) do
local fullpath = dir .. "/" .. ent.name local fullpath = dir .. "/" .. ent.name
if not should_ignore_file(fullpath, ignore_patterns) then if not should_ignore_file(fullpath, ignore_patterns) then
if ent.type == "file" then if ent.type == "file" and is_text_file(fullpath) then
table.insert(files, fullpath) table.insert(files, fullpath)
elseif ent.type == "directory" and ent.name ~= ".git" then elseif ent.type == "directory" and ent.name ~= ".git" then
scandir(fullpath, ignore_patterns, files) scandir(fullpath, ignore_patterns, files)