Skip to content

Commit

Permalink
fix: be more graceful with binary files
Browse files Browse the repository at this point in the history
Diffs will still be wrong as we can't detect if the file is binary or
not without using .gitattributes

Fixes: #549
  • Loading branch information
lewis6991 committed Apr 27, 2022
1 parent d6ac4ad commit 5f1f0c9
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 27 deletions.
25 changes: 15 additions & 10 deletions lua/gitsigns/git.lua

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

8 changes: 5 additions & 3 deletions lua/gitsigns/popup.lua

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

27 changes: 16 additions & 11 deletions teal/gitsigns/git.tl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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

Expand Down
8 changes: 5 additions & 3 deletions teal/gitsigns/popup.tl
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ 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
local len = vim.fn.strdisplaywidth(l)
if len > width then
width = len
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
Expand Down
3 changes: 3 additions & 0 deletions types/types.d.tl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -336,6 +338,7 @@ global record vim

record v
vim_did_enter: integer
t_string: integer
end


Expand Down

0 comments on commit 5f1f0c9

Please sign in to comment.