fix(git): keep index URI buffer in sync on save and reload
This commit is contained in:
+14
-14
@@ -108,9 +108,6 @@ local function attach_index_writer(buf, r, path)
|
||||
state.index_mode = mode
|
||||
end
|
||||
end
|
||||
-- Use the 3-arg form (mode sha path) instead of the comma
|
||||
-- form (mode,sha,path), which doesn't survive paths
|
||||
-- containing a comma.
|
||||
if
|
||||
not util.exec({
|
||||
"git",
|
||||
@@ -123,6 +120,9 @@ local function attach_index_writer(buf, r, path)
|
||||
then
|
||||
return
|
||||
end
|
||||
if state then
|
||||
state.sha = r:rev_parse(":" .. path, true)
|
||||
end
|
||||
vim.bo[buf].modified = false
|
||||
end,
|
||||
})
|
||||
@@ -146,7 +146,7 @@ end
|
||||
function M.buf_for(r, rev, content)
|
||||
local buf = vim.fn.bufadd(M.format_uri(rev))
|
||||
repo.bind(buf, r)
|
||||
if content then
|
||||
if content and not vim.api.nvim_buf_is_loaded(buf) then
|
||||
local state = r:state(buf) --[[@as -nil]]
|
||||
state.pending_content = content
|
||||
end
|
||||
@@ -155,7 +155,9 @@ function M.buf_for(r, rev, content)
|
||||
end
|
||||
|
||||
---@param buf integer
|
||||
function M.read_uri(buf)
|
||||
---@param opts { force?: boolean }?
|
||||
function M.read_uri(buf, opts)
|
||||
opts = opts or {}
|
||||
local name = vim.api.nvim_buf_get_name(buf)
|
||||
local rev = M.parse_uri(name)
|
||||
if not rev then
|
||||
@@ -177,13 +179,15 @@ function M.read_uri(buf)
|
||||
---@type string?
|
||||
local stdout = state.pending_content
|
||||
state.pending_content = nil
|
||||
-- On a refresh tick (no caller-provided content), skip the re-read
|
||||
-- when the rev still resolves to the same sha. Avoids re-firing
|
||||
-- BufReadPost (and the LSP/treesitter re-attach storm) on every
|
||||
-- fs-event for buffers whose content can't have changed.
|
||||
if stdout == nil then
|
||||
local rev_sha = r:rev_parse(rev_str, true)
|
||||
if not rev_sha or rev_sha == state.sha then
|
||||
if not rev_sha then
|
||||
return
|
||||
end
|
||||
if
|
||||
not opts.force
|
||||
and (rev_sha == state.sha or vim.bo[buf].modified)
|
||||
then
|
||||
return
|
||||
end
|
||||
stdout = util.exec(
|
||||
@@ -231,10 +235,6 @@ function M.read_uri(buf)
|
||||
end
|
||||
vim.bo[buf].modified = false
|
||||
|
||||
-- Match on the inner path directly. `vim.filetype.add` patterns
|
||||
-- don't work because Vim normalises `git://` filenames (cwd-prefix
|
||||
-- + `://` -> `:/`) before matching, breaking any pattern keyed on
|
||||
-- the raw scheme.
|
||||
if rev.path then
|
||||
local ft = vim.filetype.match({ filename = rev.path, buf = buf })
|
||||
if ft then
|
||||
|
||||
Reference in New Issue
Block a user