From 4048b8f59b6e145331ac033dad761d566c5483f9 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Fri, 19 Apr 2024 19:33:16 -0700 Subject: [PATCH] Update git utils --- lua/config/util.lua | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/lua/config/util.lua b/lua/config/util.lua index 2a2c8ca..f74efab 100644 --- a/lua/config/util.lua +++ b/lua/config/util.lua @@ -12,9 +12,16 @@ local function invoke(args) return string.gsub(output, '%s+$', '') end -local function git_branch_parent(branch) - if branch ~= '' then - return branch +local function git_parent_branch(parent_branch) + for _, branch in ipairs({'master', 'main'}) do + 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 local author = invoke({'git', 'config', 'user.name'}) @@ -59,7 +66,7 @@ vim.api.nvim_create_user_command( vim.api.nvim_create_user_command( 'GitTopicEdit', function(ctx) - local hash = git_branch_parent(ctx.args) + local hash = git_parent_branch(ctx.args) if hash then git_edit_paths(invoke({'git', 'diff', '--name-only', hash})) end @@ -71,7 +78,7 @@ vim.api.nvim_create_user_command( vim.api.nvim_create_user_command( 'GitTopicDiff', function(ctx) - local hash = git_branch_parent(ctx.args) + local hash = git_parent_branch(ctx.args) if hash then invoke({'git', 'difftool', '-d', hash}) end