Skip to content

Conversation

igorlfs
Copy link

@igorlfs igorlfs commented Oct 9, 2025

Problem: documentation mentions that bufnr can be 0 for get_hunks, but 0 is truthy, so the current logic does not work

Solution: properly check if the current buf should be used (same logic as vim._resolve_bufnr)

I also took the liberty of updating the type annotations to be more precise.


There are two other instances where the same logic is being used, but for them, 0 is not documented as being the current buf (so I haven't updated them):

--- Detach Gitsigns from the buffer {bufnr}. If {bufnr} is not
--- provided then the current buffer is used.
---
--- @param bufnr integer Buffer number
--- @param keep_signs? boolean
function M.detach(bufnr, keep_signs)
-- When this is called interactively (with no arguments) we want to remove all
-- the signs, however if called via a detach event (due to nvim_buf_attach)
-- then we don't want to clear the signs in case the buffer is just being
-- updated due to the file externally changing. When this happens a detach and
-- attach event happen in sequence and so we keep the old signs to stop the
-- sign column width moving about between updates.
bufnr = bufnr or api.nvim_get_current_buf()
dprint('Detached')
local bcache = cache[bufnr]
if not bcache then
dprint('Cache was nil')
return
end

--- Attach Gitsigns to the buffer.
---
--- Attributes: ~
--- {async}
---
--- @param bufnr integer Buffer number
--- @param ctx Gitsigns.GitContext|nil
--- Git context data that may optionally be used to attach to any
--- buffer that represents a real git object.
--- • {file}: (string)
--- Path to the file represented by the buffer, relative to the
--- top-level.
--- • {toplevel}: (string?)
--- Path to the top-level of the parent git repository.
--- • {gitdir}: (string?)
--- Path to the git directory of the parent git repository
--- (typically the ".git/" directory).
--- • {commit}: (string?)
--- The git revision that the file belongs to.
--- • {base}: (string?)
--- The git revision that the file should be compared to.
--- @param _trigger? string
M.attach = async.create(3, function(bufnr, ctx, _trigger)
require('gitsigns.attach').attach(bufnr or api.nvim_get_current_buf(), ctx, _trigger)
end)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant