Add TopicDiff, TopicEdit

This commit is contained in:
Alex Yatskov 2023-09-25 12:23:19 -07:00
parent 7cb2603262
commit 684d932298

View File

@ -1,3 +1,27 @@
local function invoke(args)
output = vim.fn.system(table.concat(args, ' '))
return string.gsub(output, '%s+$', '')
end
local function get_branch_parent()
local author = invoke({'git', 'config', 'user.name'})
local hash = invoke{
'git',
'log',
'-n1',
'--author="^((?!' .. author .. ').)*$"',
'--perl-regexp',
'--pretty=format:"%H"'
}
if #hash == 0 then
print('All commits are by current author')
else
return hash
end
end
-- UnAlign -- UnAlign
vim.api.nvim_create_user_command( vim.api.nvim_create_user_command(
'UnAlign', 'UnAlign',
@ -7,33 +31,34 @@ vim.api.nvim_create_user_command(
{range = '%'} {range = '%'}
) )
-- GitStreak -- TopicEdit
vim.api.nvim_create_user_command( vim.api.nvim_create_user_command(
'GitStreak', 'TopicEdit',
function(ctx) function(ctx)
local function call(args) local hash = get_branch_parent()
output = vim.fn.system(table.concat(args, ' ')) if hash then
return string.gsub(output, '%s+$', '') local names = invoke({'git', 'diff', '--name-only', hash})
end
local root_dir = call({'git', 'rev-parse', '--show-toplevel'})
local author = call({'git', 'config', 'user.name'})
local hash = call{'git', 'log', '-n1', '--author="^((?!' .. author .. ').)*$"', '--perl-regexp', '--pretty=format:"%H"'}
if #hash == 0 then
print('All commits are by current author')
return
end
local names = call({'git', 'diff', '--name-only', hash})
if #names == 0 then if #names == 0 then
print('No files changed since previous author') print('No files changed since previous author')
return else
end local root_dir = invoke({'git', 'rev-parse', '--show-toplevel'})
for name in string.gmatch(names, '[^\r\n]+') do for name in string.gmatch(names, '[^\r\n]+') do
vim.cmd(string.format('e %s/%s', root_dir, name)) vim.cmd(string.format('e %s/%s', root_dir, name))
end end
end
end
end,
{}
)
-- TopicDiff
vim.api.nvim_create_user_command(
'TopicDiff',
function(ctx)
local hash = get_branch_parent()
if hash then
invoke({'git', 'difftool', '-d', hash})
end
end, end,
{} {}
) )