fix: be more graceful with binary files

Diffs will still be wrong as we can't detect if the file is binary or
not without using .gitattributes

Fixes: #549
This commit is contained in:
Lewis Russell 2022-04-27 11:32:12 +01:00 committed by Lewis Russell
parent d6ac4ad416
commit 5f1f0c9afd
5 changed files with 44 additions and 27 deletions

25
lua/gitsigns/git.lua generated
View File

@ -286,8 +286,20 @@ Repo.files_changed = function(self)
end
Repo.get_show_text = function(self, object)
return self:command({ 'show', object }, { supress_stderr = true })
Repo.get_show_text = function(self, object, encoding)
local stdout, stderr = self:command({ 'show', object }, { supress_stderr = true })
if encoding ~= 'utf-8' then
scheduler()
for i, l in ipairs(stdout) do
if vim.fn.type(l) == vim.v.t_string then
stdout[i] = vim.fn.iconv(l, encoding, 'utf-8')
end
end
end
return stdout, stderr
end
Repo.update_abbrev_head = function(self)
@ -384,7 +396,7 @@ Obj.get_show_text = function(self, revision)
return {}
end
local stdout, stderr = self.repo:get_show_text(revision .. ':' .. self.relpath)
local stdout, stderr = self.repo:get_show_text(revision .. ':' .. self.relpath, self.encoding)
if not self.i_crlf and self.w_crlf then
@ -393,13 +405,6 @@ Obj.get_show_text = function(self, revision)
end
end
if self.encoding ~= 'utf-8' then
scheduler()
for i, l in ipairs(stdout) do
stdout[i] = vim.fn.iconv(l, self.encoding, 'utf-8')
end
end
return stdout, stderr
end

View File

@ -19,11 +19,13 @@ local function bufnr_calc_width(bufnr, lines)
return api.nvim_buf_call(bufnr, function()
local width = 0
for _, l in ipairs(lines) do
if vim.fn.type(l) == vim.v.t_string then
local len = vim.fn.strdisplaywidth(l)
if len > width then
width = len
end
end
end
return width + 1
end)
end

View File

@ -69,7 +69,7 @@ local record M
command : function(Repo, {string}, GJobSpec): {string}, string
files_changed : function(Repo): {string}
get_show_text : function(Repo, string): {string}, string
get_show_text : function(Repo, string, string): {string}, string
update_abbrev_head : function(Repo)
new : function(string): Repo
end
@ -286,8 +286,20 @@ Repo.files_changed = function(self: Repo): {string}
end
--- Get version of file in the index, return array lines
Repo.get_show_text = function(self: Repo, object: string): {string}, string
return self:command({'show', object}, {supress_stderr = true})
Repo.get_show_text = function(self: Repo, object: string, encoding: string): {string}, string
local stdout, stderr = self:command({'show', object}, {supress_stderr = true})
if encoding ~= 'utf-8' then
scheduler()
for i, l in ipairs(stdout) do
-- TODO(lewis6991): How should we handle blob types?
if vim.fn.type(l) == vim.v.t_string then
stdout[i] = vim.fn.iconv(l, encoding, 'utf-8')
end
end
end
return stdout, stderr
end
Repo.update_abbrev_head = function(self: Repo)
@ -384,7 +396,7 @@ Obj.get_show_text = function(self: Obj, revision: string): {string}, string
return {}
end
local stdout, stderr = self.repo:get_show_text(revision..':'..self.relpath)
local stdout, stderr = self.repo:get_show_text(revision..':'..self.relpath, self.encoding)
if not self.i_crlf and self.w_crlf then
-- Add cr
@ -393,13 +405,6 @@ Obj.get_show_text = function(self: Obj, revision: string): {string}, string
end
end
if self.encoding ~= 'utf-8' then
scheduler()
for i, l in ipairs(stdout) do
stdout[i] = vim.fn.iconv(l, self.encoding, 'utf-8')
end
end
return stdout, stderr
end

View File

@ -19,11 +19,13 @@ local function bufnr_calc_width(bufnr: integer, lines: {string}): integer
return api.nvim_buf_call(bufnr, function(): integer
local width = 0
for _, l in ipairs(lines) do
if vim.fn.type(l) == vim.v.t_string then
local len = vim.fn.strdisplaywidth(l)
if len > width then
width = len
end
end
end
return width + 1 -- Add 1 for some miinor padding
end)
end

View File

@ -280,7 +280,9 @@ global record vim
sign_undefine: function(string): number
strdisplaywidth: function(string, integer): integer
stridx: function(haystack: string, needle: string, start: integer): integer
string: function(any): string
tempname: function(): string
type: function(any): integer
end
cmd: function(string): any
@ -336,6 +338,7 @@ global record vim
record v
vim_did_enter: integer
t_string: integer
end