diff --git a/.config/nvim/ftplugin/fugitive.lua b/.config/nvim/ftplugin/fugitive.lua index bda056f..fd76c64 100644 --- a/.config/nvim/ftplugin/fugitive.lua +++ b/.config/nvim/ftplugin/fugitive.lua @@ -31,7 +31,7 @@ local function first_commit() vim.notify('Committing: ' .. head) vim.cmd('silent! Git commit --quiet -m ' .. head) vim.cmd('silent! Git push -u origin ' .. head) - vim.cmd 'silent! !cpr' + vim.cmd 'silent! Cpr' end local function enter_wip() @@ -97,7 +97,7 @@ vim.schedule(function() silent = true, desc = 'Pull request', callback = function() - vim.cmd 'silent! !cpr' + vim.cmd 'silent! Cpr' end, }) diff --git a/.config/nvim/lua/plugins/git.lua b/.config/nvim/lua/plugins/git.lua index 90907d3..1373523 100644 --- a/.config/nvim/lua/plugins/git.lua +++ b/.config/nvim/lua/plugins/git.lua @@ -68,11 +68,44 @@ local function create_new_branch(branch_opts) end) end +local function create_pull_request() + local function get_git_remote(callback) + vim.system({ 'git', 'remote', '-v' }, { text = true }, function(obj) + callback(obj.stdout:match '(.-)%s+%(fetch%)') + end) + end + + local function get_branch_name(callback) + vim.system({ 'git', 'branch', '--show-current' }, { text = true }, function(obj) + callback(obj.stdout:gsub('%s+', '')) + end) + end + + get_git_remote(function(git_remote) + local git_remote_name = 'origin' + local git_remote_url = git_remote:match(git_remote_name .. '%s+(%S+)') + local prefix = git_remote_url:match '^%w+' + prefix = prefix == 'git' and 'git@' or 'https://' + local git_name, project, repo, _ = git_remote_url:match(('^' .. prefix .. '(%w+).com[:/](.+)/(.+)%.git')) + local pr_link = git_name == 'gitlab' and '-/merge_requests/new?merge_request[source_branch]=' or 'pull/new/' + + get_branch_name(function(branch_name) + vim.print('git_name: ' .. git_name .. ' project: ' .. project .. ' repo: ' .. repo .. ' pr_link: ' .. pr_link .. ' branch_name: ' .. branch_name) + local url = string.format('https://%s.com/%s/%s/%s%s', git_name, project, repo, pr_link, branch_name) + vim.print('Opening ' .. url) + vim.ui.open(url) + end) + end) +end + local actions = function() return { ['Change branch (F4)'] = function() vim.fn.feedkeys(vim.keycode '') end, + ['Create Pull Request'] = function() + create_pull_request() + end, ['Checkout new branch (:Gcb {new_branch})'] = function() create_new_branch { args = '' } end, @@ -304,6 +337,11 @@ local fugitive_config = function() actions_pretty_print('Changed directory to Git root' .. cwd) end) + ------------------------- + -- Create Pull Request -- + ------------------------- + vim.api.nvim_create_user_command('Cpr', create_pull_request, {}) + ---------------------- -- Git actions menu -- ----------------------