Skip to content

Commit

Permalink
fix(dos): correct check for dos files
Browse files Browse the repository at this point in the history
When scanning for CRLF do not check EOF.

Fixes #927, #835
  • Loading branch information
lewis6991 committed Dec 12, 2023
1 parent 6e05045 commit aeab36f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 7 deletions.
7 changes: 5 additions & 2 deletions lua/gitsigns/diffthis.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,17 @@ local bufread = async.void(function(bufnr, dbufnr, base)
async.scheduler_if_buf_valid(bufnr)
end

vim.bo[dbufnr].fileformat = vim.bo[bufnr].fileformat
vim.bo[dbufnr].filetype = vim.bo[bufnr].filetype
vim.bo[dbufnr].bufhidden = 'wipe'

local modifiable = vim.bo[dbufnr].modifiable
vim.bo[dbufnr].modifiable = true

util.set_lines(dbufnr, 0, -1, text)

vim.bo[dbufnr].modifiable = modifiable
vim.bo[dbufnr].modified = false
vim.bo[dbufnr].filetype = vim.bo[bufnr].filetype
vim.bo[dbufnr].bufhidden = 'wipe'
end)

--- @param bufnr integer
Expand Down
21 changes: 16 additions & 5 deletions lua/gitsigns/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -191,16 +191,27 @@ function M.get_relative_time(timestamp)
end
end

--- @param xs string[]
--- @return boolean
local function is_dos(xs)
-- Do not check CR at EOF
for i = 1, #xs - 1 do
if xs[i]:sub(-1) ~= '\r' then
return false
end
end
return true
end

--- Strip '\r' from the EOL of each line only if all lines end with '\r'
--- @param xs0 string[]
--- @return string[]
function M.strip_cr(xs0)
for i = 1, #xs0 do
if xs0[i]:sub(-1) ~= '\r' then
-- don't strip, return early
return xs0
end
if not is_dos(xs0) then
-- don't strip, return early
return xs0
end

-- all lines end with '\r', need to strip
local xs = vim.deepcopy(xs0)
for i = 1, #xs do
Expand Down

0 comments on commit aeab36f

Please sign in to comment.