diff --git a/lua/git/diff.lua b/lua/git/diff.lua index d54585f..b3caeab 100644 --- a/lua/git/diff.lua +++ b/lua/git/diff.lua @@ -24,14 +24,13 @@ end ---@param right integer ---@param vertical boolean function M.open(left, right, vertical) - local left_name = vim.api.nvim_buf_get_name(left) vim.cmd.normal({ "m'", bang = true }) vim.api.nvim_set_current_buf(right) - vim.cmd.diffsplit({ - args = { left_name }, - mods = { split = "aboveleft", vertical = vertical, keepjumps = true }, - magic = { file = false }, + vim.cmd.diffthis() + vim.api.nvim_open_win(left, true, { + split = vertical and "left" or "above", }) + vim.cmd.diffthis() end ---@param left_win integer diff --git a/lua/git/init.lua b/lua/git/init.lua index eed4b28..d75c2e8 100644 --- a/lua/git/init.lua +++ b/lua/git/init.lua @@ -71,7 +71,7 @@ function M.init() pattern = "git://*", group = group, callback = function(args) - require("git.object").read_uri(args.buf) + require("git.object").read_uri(args.buf, { force = true }) end, }) diff --git a/lua/git/object.lua b/lua/git/object.lua index 735390d..1f9bcf8 100644 --- a/lua/git/object.lua +++ b/lua/git/object.lua @@ -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