Update git utils

This commit is contained in:
Alex Yatskov 2024-04-19 19:33:16 -07:00
parent c73f688f2c
commit 4048b8f59b

View File

@ -12,9 +12,16 @@ local function invoke(args)
return string.gsub(output, '%s+$', '') return string.gsub(output, '%s+$', '')
end end
local function git_branch_parent(branch) local function git_parent_branch(parent_branch)
if branch ~= '' then for _, branch in ipairs({'master', 'main'}) do
return branch local output = invoke({'git', 'rev-parse', '--verify', branch})
if not string.find(output, 'fatal') then
parent_branch = branch
end
end
if parent_branch ~= '' then
return parent_branch
end end
local author = invoke({'git', 'config', 'user.name'}) local author = invoke({'git', 'config', 'user.name'})
@ -59,7 +66,7 @@ vim.api.nvim_create_user_command(
vim.api.nvim_create_user_command( vim.api.nvim_create_user_command(
'GitTopicEdit', 'GitTopicEdit',
function(ctx) function(ctx)
local hash = git_branch_parent(ctx.args) local hash = git_parent_branch(ctx.args)
if hash then if hash then
git_edit_paths(invoke({'git', 'diff', '--name-only', hash})) git_edit_paths(invoke({'git', 'diff', '--name-only', hash}))
end end
@ -71,7 +78,7 @@ vim.api.nvim_create_user_command(
vim.api.nvim_create_user_command( vim.api.nvim_create_user_command(
'GitTopicDiff', 'GitTopicDiff',
function(ctx) function(ctx)
local hash = git_branch_parent(ctx.args) local hash = git_parent_branch(ctx.args)
if hash then if hash then
invoke({'git', 'difftool', '-d', hash}) invoke({'git', 'difftool', '-d', hash})
end end