Skip to content

Commit

Permalink
fix(util): close file after reading
Browse files Browse the repository at this point in the history
  • Loading branch information
lewis6991 committed May 1, 2024
1 parent 790355f commit f65d1d8
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 14 deletions.
2 changes: 1 addition & 1 deletion lua/gitsigns/actions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1254,7 +1254,7 @@ local function buildqflist(target)
if stat and stat.type == 'file' then
local a = r:get_show_text(':0:' .. f)
async.scheduler()
local hunks = run_diff(a, util.file_lines(f_abs, { raw = true }))
local hunks = run_diff(a, util.file_lines(f_abs))
hunks_to_qflist(f_abs, hunks, qflist)
end
end
Expand Down
2 changes: 1 addition & 1 deletion lua/gitsigns/git.lua
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ end
--- @return string[] stdout, string? stderr
function Obj:get_show_text(revision)
if revision == 'FILE' then
return util.file_lines(self.file, { raw = true })
return util.file_lines(self.file)
end

if not self.relpath then
Expand Down
16 changes: 4 additions & 12 deletions lua/gitsigns/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,12 @@ function M.dirname(file)
end

--- @param path string
--- @param opts {raw: boolean}?
--- @return string[]
function M.file_lines(path, opts)
opts = opts or {}
local file = assert(io.open(path))
function M.file_lines(path)
local file = assert(io.open(path, 'rb'))
local contents = file:read('*a')
local lines = vim.split(contents, '\n', { plain = true })
if not opts.raw then
-- If contents ends with a newline, then remove the final empty string after the split
if lines[#lines] == '' then
lines[#lines] = nil
end
end
return lines
file:close()
return vim.split(contents, '\n')
end

M.path_sep = package.config:sub(1, 1)
Expand Down

0 comments on commit f65d1d8

Please sign in to comment.