Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: don't run iconv on utf-16 #725

Merged
merged 1 commit into from
Jan 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 40 additions & 1 deletion lua/gitsigns/git.lua

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 40 additions & 1 deletion teal/gitsigns/git.tl
Original file line number Diff line number Diff line change
Expand Up @@ -357,11 +357,50 @@ function Repo:files_changed(): {string}
return ret
end

local function make_bom(...: integer): string
local r = {}
for i, a in ipairs{...} do
r[i] = string.char(a)
end
return table.concat(r)
end

local BOM_TABLE: {string:string} = {
['utf-8'] = make_bom(0xef, 0xbb, 0xbf),
['utf-16le'] = make_bom(0xff, 0xfe),
['utf-16'] = make_bom(0xfe, 0xff),
['utf-16be'] = make_bom(0xfe, 0xff),
['utf-32le'] = make_bom(0xff, 0xfe, 0x00, 0x00),
['utf-32'] = make_bom(0xff, 0xfe, 0x00, 0x00),
['utf-32be'] = make_bom(0x00, 0x00, 0xfe, 0xff),
['utf-7'] = make_bom(0x2b, 0x2f, 0x76),
['utf-1'] = make_bom(0xf7, 0x54, 0x4c),
}

local function strip_bom(x: string, encoding: string): string
local bom = BOM_TABLE[encoding]
if bom and vim.startswith(x, bom) then
return x:sub(bom:len() + 1)
end
return x
end

local function iconv_supported(encoding: string): boolean
-- TODO(lewis6991): needs https://github.com/neovim/neovim/pull/21924
if vim.startswith(encoding, 'utf-16') then
return false
elseif vim.startswith(encoding, 'utf-32') then
return false
end
return true
end

--- Get version of file in the index, return array lines
function Repo:get_show_text(object: string, encoding: string): {string}, string
local stdout, stderr = self:command({'show', object}, {suppress_stderr = true})

if encoding and encoding ~= 'utf-8' then
if encoding and encoding ~= 'utf-8' and iconv_supported(encoding) then
stdout[1] = strip_bom(stdout[1], encoding)
if vim.iconv then
for i, l in ipairs(stdout) do
stdout[i] = vim.iconv(l, encoding, 'utf-8')
Expand Down