Skip to content

Commit

Permalink
fix(blame): ensure blame object is valid when all lines are requested
Browse files Browse the repository at this point in the history
Fixes #1156
  • Loading branch information
lewis6991 committed Jan 17, 2025
1 parent 0797734 commit 817bd84
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lua/gitsigns/blame.lua
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ local function menu(name, items)
end

--- @async
M.blame = function()
function M.blame()
local __FUNC__ = 'blame'
local bufnr = api.nvim_get_current_buf()
local win = api.nvim_get_current_win()
Expand Down
25 changes: 24 additions & 1 deletion lua/gitsigns/cache.lua
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,29 @@ function CacheEntry:run_blame(lnum, opts)
return blame, lnum0 == nil
end

--- @private
--- @param lnum? integer
--- @return boolean
function CacheEntry:blame_valid(lnum)
local blame = self.blame
if not blame then
return false
end

if lnum then
return blame[lnum] ~= nil
end

-- Need to check we have blame info for all lines
for i = 1, vim.api.nvim_buf_line_count(self.bufnr) do
if not blame[i] then
return false
end
end

return true
end

--- If lnum is nil then run blame for the entire buffer.
--- @async
--- @param lnum? integer
Expand All @@ -109,7 +132,7 @@ end
function CacheEntry:get_blame(lnum, opts)
local blame = self.blame

if not blame or (lnum and not blame[lnum]) then
if not blame or not self:blame_valid(lnum) then
self:wait_for_hunks()
blame = blame or {}
local Hunks = require('gitsigns.hunks')
Expand Down

0 comments on commit 817bd84

Please sign in to comment.