Skip to content

Commit

Permalink
fix: attach to fugitive and gitsigns buffers
Browse files Browse the repository at this point in the history
Resolved #593
  • Loading branch information
lewis6991 committed Apr 5, 2024
1 parent 320b232 commit 81369ed
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 21 deletions.
3 changes: 1 addition & 2 deletions lua/gitsigns/actions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1122,11 +1122,10 @@ M.diffthis = function(base, opts)
base = tostring(base)
end
opts = opts or {}
local diffthis = require('gitsigns.diffthis')
if not opts.vertical then
opts.vertical = config.diff_opts.vertical
end
diffthis.diffthis(base, opts)
require('gitsigns.diffthis').diffthis(base, opts)
end

C.diffthis = function(args, params)
Expand Down
40 changes: 21 additions & 19 deletions lua/gitsigns/attach.lua
Original file line number Diff line number Diff line change
Expand Up @@ -62,35 +62,35 @@ local function parse_gitsigns_uri(name)
end

--- @param bufnr integer
--- @return string, string?
--- @return string buffer
--- @return string? commit
--- @return boolean? force_attach
local function get_buf_path(bufnr)
local file = uv.fs_realpath(api.nvim_buf_get_name(bufnr))
or api.nvim_buf_call(bufnr, function()
return vim.fn.expand('%:p')
end)

if not vim.wo.diff then
if vim.startswith(file, 'fugitive://') then
local path, commit = parse_fugitive_uri(file)
dprintf("Fugitive buffer for file '%s' from path '%s'", path, file)
if path then
local realpath = uv.fs_realpath(path)
if realpath then
return realpath, commit
end
end
end

if vim.startswith(file, 'gitsigns://') then
local path, commit = parse_gitsigns_uri(file)
dprintf("Gitsigns buffer for file '%s' from path '%s'", path, file)
if vim.startswith(file, 'fugitive://') then
local path, commit = parse_fugitive_uri(file)
dprintf("Fugitive buffer for file '%s' from path '%s'", path, file)
if path then
local realpath = uv.fs_realpath(path)
if realpath then
return realpath, commit
return realpath, commit, true
end
end
end

if vim.startswith(file, 'gitsigns://') then
local path, commit = parse_gitsigns_uri(file)
dprintf("Gitsigns buffer for file '%s' from path '%s' on commit '%s'", path, file, commit)
local realpath = uv.fs_realpath(path)
if realpath then
return realpath, commit, true
end
end

return file
end

Expand Down Expand Up @@ -271,12 +271,14 @@ local attach_throttled = throttle_by_id(function(cbuf, ctx, aucmd)
return
end

if vim.bo[cbuf].buftype ~= '' then
local force_attach
file, commit, force_attach = get_buf_path(cbuf)

if vim.bo[cbuf].buftype ~= '' and not force_attach then
dprint('Non-normal buffer')
return
end

file, commit = get_buf_path(cbuf)
local file_dir = util.dirname(file)

if not file_dir or not util.path_exists(file_dir) then
Expand Down
1 change: 1 addition & 0 deletions lua/gitsigns/diffthis.lua
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ local function bufread(bufnr, dbufnr, base)

vim.bo[dbufnr].modifiable = modifiable
vim.bo[dbufnr].modified = false
require('gitsigns.attach').attach(dbufnr, nil, 'BufReadCmd')
end

--- @param bufnr integer
Expand Down

0 comments on commit 81369ed

Please sign in to comment.