feat: add :Gdiff, :Gvdiff, :Ghdiff commands for unified diff view

Compares current buffer against any git revision (default HEAD), opens result
with full diffs.nvim syntax highlighting. Follows fugitive convention:
:Gdiff/:Gvdiff open vertical split, :Ghdiff opens horizontal split.
This commit is contained in:
Barrett Ruth 2026-02-04 18:14:18 -05:00
parent 2ce76e7683
commit 045a9044b5
10 changed files with 404 additions and 7 deletions

40
spec/commands_spec.lua Normal file
View file

@ -0,0 +1,40 @@
require('spec.helpers')
describe('commands', function()
describe('setup', function()
it('registers Gdiff, Gvdiff, and Ghdiff commands', function()
require('diffs.commands').setup()
local commands = vim.api.nvim_get_commands({})
assert.is_not_nil(commands.Gdiff)
assert.is_not_nil(commands.Gvdiff)
assert.is_not_nil(commands.Ghdiff)
end)
end)
describe('unified diff generation', function()
local old_lines = { 'local M = {}', 'return M' }
local new_lines = { 'local M = {}', 'local x = 1', 'return M' }
local diff_fn = vim.text and vim.text.diff or vim.diff
it('generates valid unified diff', function()
local old_content = table.concat(old_lines, '\n')
local new_content = table.concat(new_lines, '\n')
local diff_output = diff_fn(old_content, new_content, {
result_type = 'unified',
ctxlen = 3,
})
assert.is_not_nil(diff_output)
assert.is_true(diff_output:find('@@ ') ~= nil)
assert.is_true(diff_output:find('+local x = 1') ~= nil)
end)
it('returns empty for identical content', function()
local content = table.concat(old_lines, '\n')
local diff_output = diff_fn(content, content, {
result_type = 'unified',
ctxlen = 3,
})
assert.are.equal('', diff_output)
end)
end)
end)

54
spec/git_spec.lua Normal file
View file

@ -0,0 +1,54 @@
require('spec.helpers')
local git = require('diffs.git')
describe('git', function()
describe('get_repo_root', function()
it('returns repo root for current repo', function()
local cwd = vim.fn.getcwd()
local root = git.get_repo_root(cwd .. '/lua/diffs/init.lua')
assert.is_not_nil(root)
assert.are.equal(cwd, root)
end)
it('returns nil for non-git directory', function()
local root = git.get_repo_root('/tmp')
assert.is_nil(root)
end)
end)
describe('get_file_content', function()
it('returns file content at HEAD', function()
local cwd = vim.fn.getcwd()
local content, err = git.get_file_content('HEAD', cwd .. '/lua/diffs/init.lua')
assert.is_nil(err)
assert.is_not_nil(content)
assert.is_true(#content > 0)
end)
it('returns error for non-existent file', function()
local cwd = vim.fn.getcwd()
local content, err = git.get_file_content('HEAD', cwd .. '/does_not_exist.lua')
assert.is_nil(content)
assert.is_not_nil(err)
end)
it('returns error for non-git directory', function()
local content, err = git.get_file_content('HEAD', '/tmp/some_file.txt')
assert.is_nil(content)
assert.is_not_nil(err)
end)
end)
describe('get_relative_path', function()
it('returns relative path within repo', function()
local cwd = vim.fn.getcwd()
local rel = git.get_relative_path(cwd .. '/lua/diffs/init.lua')
assert.are.equal('lua/diffs/init.lua', rel)
end)
it('returns nil for non-git directory', function()
local rel = git.get_relative_path('/tmp/some_file.txt')
assert.is_nil(rel)
end)
end)
end)

View file

@ -885,6 +885,111 @@ describe('highlight', function()
end)
end)
describe('extmark priority', function()
local ns
before_each(function()
ns = vim.api.nvim_create_namespace('diffs_test_priority')
end)
local function create_buffer(lines)
local bufnr = vim.api.nvim_create_buf(false, true)
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, lines)
return bufnr
end
local function delete_buffer(bufnr)
if vim.api.nvim_buf_is_valid(bufnr) then
vim.api.nvim_buf_delete(bufnr, { force = true })
end
end
local function get_extmarks(bufnr)
return vim.api.nvim_buf_get_extmarks(bufnr, ns, 0, -1, { details = true })
end
local function default_opts()
return {
hide_prefix = false,
highlights = {
background = false,
gutter = false,
treesitter = { enabled = true, max_lines = 500 },
vim = { enabled = false, max_lines = 200 },
},
}
end
it('uses priority 200 for code languages', function()
local bufnr = create_buffer({
'@@ -1,1 +1,2 @@',
' local x = 1',
'+local y = 2',
})
local hunk = {
filename = 'test.lua',
lang = 'lua',
start_line = 1,
lines = { ' local x = 1', '+local y = 2' },
}
highlight.highlight_hunk(bufnr, ns, hunk, default_opts())
local extmarks = get_extmarks(bufnr)
local has_priority_200 = false
for _, mark in ipairs(extmarks) do
if mark[4] and mark[4].hl_group and mark[4].hl_group:match('^@.*%.lua$') then
if mark[4].priority == 200 then
has_priority_200 = true
break
end
end
end
assert.is_true(has_priority_200)
delete_buffer(bufnr)
end)
it('uses treesitter priority for diff language', function()
local bufnr = create_buffer({
'diff --git a/test.lua b/test.lua',
'--- a/test.lua',
'+++ b/test.lua',
'@@ -1,1 +1,2 @@',
' local x = 1',
'+local y = 2',
})
local hunk = {
filename = 'test.lua',
lang = 'lua',
start_line = 5,
lines = { ' local x = 1', '+local y = 2' },
header_start_line = 1,
header_lines = {
'diff --git a/test.lua b/test.lua',
'--- a/test.lua',
'+++ b/test.lua',
},
}
highlight.highlight_hunk(bufnr, ns, hunk, default_opts())
local extmarks = get_extmarks(bufnr)
local diff_extmark_priorities = {}
for _, mark in ipairs(extmarks) do
if mark[4] and mark[4].hl_group and mark[4].hl_group:match('^@.*%.diff$') then
table.insert(diff_extmark_priorities, mark[4].priority)
end
end
assert.is_true(#diff_extmark_priorities > 0)
for _, priority in ipairs(diff_extmark_priorities) do
assert.is_true(priority < 200)
end
delete_buffer(bufnr)
end)
end)
describe('coalesce_syntax_spans', function()
it('coalesces adjacent chars with same hl group', function()
local function query_fn(_line, _col)