feat: add :Gdiff command for unified diff against git revision
Compares current buffer against any git revision (default HEAD), opens result in vsplit with full diffs.nvim syntax highlighting.
This commit is contained in:
parent
2ce76e7683
commit
bf2c91f79f
5 changed files with 271 additions and 0 deletions
37
spec/commands_spec.lua
Normal file
37
spec/commands_spec.lua
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
require('spec.helpers')
|
||||
|
||||
describe('commands', function()
|
||||
describe('setup', function()
|
||||
it('registers Gdiff command', function()
|
||||
require('diffs.commands').setup()
|
||||
local commands = vim.api.nvim_get_commands({})
|
||||
assert.is_not_nil(commands.Gdiff)
|
||||
end)
|
||||
end)
|
||||
|
||||
describe('unified diff generation', function()
|
||||
local old_lines = { 'local M = {}', 'return M' }
|
||||
local new_lines = { 'local M = {}', 'local x = 1', 'return M' }
|
||||
|
||||
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 = vim.diff(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 = vim.diff(content, content, {
|
||||
result_type = 'unified',
|
||||
ctxlen = 3,
|
||||
})
|
||||
assert.are.equal('', diff_output)
|
||||
end)
|
||||
end)
|
||||
end)
|
||||
68
spec/git_spec.lua
Normal file
68
spec/git_spec.lua
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
require('spec.helpers')
|
||||
local git = require('diffs.git')
|
||||
|
||||
describe('git', function()
|
||||
describe('get_git_dir', function()
|
||||
it('returns git dir for current repo', function()
|
||||
local cwd = vim.fn.getcwd()
|
||||
local git_dir = git.get_git_dir(cwd .. '/lua/diffs/init.lua')
|
||||
assert.is_not_nil(git_dir)
|
||||
assert.is_true(vim.fn.isdirectory(git_dir) == 1)
|
||||
end)
|
||||
|
||||
it('returns nil for non-git directory', function()
|
||||
local git_dir = git.get_git_dir('/tmp')
|
||||
assert.is_nil(git_dir)
|
||||
end)
|
||||
end)
|
||||
|
||||
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)
|
||||
Loading…
Add table
Add a link
Reference in a new issue