2021-03-05 10:08:05 +00:00
|
|
|
-------------------------------------------------------------------------------
|
|
|
|
-- - Fugitive.vim extensions library -
|
|
|
|
-------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
-- Generates url for creating PR for current branch
|
|
|
|
-- Tested only with github.com
|
|
|
|
-- Works regardless of ssh or https for origin config
|
|
|
|
-- Hardcoded to use 'origin' remote
|
|
|
|
local function get_pr_url(...)
|
|
|
|
local origin_url = vim.fn['fugitive#RemoteUrl']('origin')
|
|
|
|
origin_url = string.gsub(origin_url, '.git$', '')
|
|
|
|
origin_url = string.gsub(origin_url, ':', '/')
|
|
|
|
origin_url = string.gsub(origin_url, 'git@', 'https://')
|
|
|
|
|
|
|
|
-- Remove prefix if it is available, for some of common git services
|
|
|
|
local common_services = {'github.com', 'bitbucket.org', 'gitlab.com'}
|
|
|
|
for k,service in pairs(common_services) do
|
|
|
|
if (string.find(origin_url, service, 1, true))
|
|
|
|
then
|
|
|
|
-- Common mechanism for managing multiple SSH keys
|
|
|
|
origin_url = string.gsub(origin_url, '://.*' .. service, '://' .. service)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- This part probably only works on github
|
|
|
|
local pr_url
|
|
|
|
if (select('#', ...) == 0)
|
|
|
|
then
|
|
|
|
pr_url = origin_url .. '/compare/' .. vim.fn.FugitiveHead() .. '?expand=1'
|
|
|
|
else
|
|
|
|
pr_url = origin_url .. '/compare/' .. select(1, ...) .. '...' .. vim.fn.FugitiveHead() .. '?expand=1'
|
|
|
|
end
|
|
|
|
return pr_url
|
|
|
|
end
|
|
|
|
|
|
|
|
-------------------------------------------------------------------------------
|
|
|
|
-- - Public API -
|
|
|
|
-------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
local M = {}
|
|
|
|
|
2021-03-08 09:46:42 +00:00
|
|
|
-- Shorcut to push directly to current branch on origin
|
|
|
|
-- Similar to `ggpush` in fish config
|
|
|
|
function M.push_origin()
|
|
|
|
vim.cmd('Git push origin ' .. vim.fn.FugitiveHead())
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Shorcut to pull directly from current branch on origin
|
|
|
|
-- Similar to `ggpull` in fish config
|
|
|
|
function M.pull_origin()
|
|
|
|
vim.cmd('Git pull origin ' .. vim.fn.FugitiveHead())
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2021-03-05 10:08:05 +00:00
|
|
|
-- Prints current branches PR url (not saved to :messages)
|
|
|
|
-- Makes it easy to use terminal for opening url on click
|
2021-03-06 21:23:05 +00:00
|
|
|
function M.print_pr_url(...)
|
2021-03-05 10:08:05 +00:00
|
|
|
vim.cmd('echo "' .. get_pr_url(...) .. '"')
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Copies current branches PR url to system clipboard
|
2021-03-06 21:23:05 +00:00
|
|
|
function M.copy_pr_url(...)
|
2021-03-05 10:08:05 +00:00
|
|
|
vim.cmd('let @+ = "' .. get_pr_url(...) .. '"')
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Opens current banches PR url in default browser
|
|
|
|
-- Utilizes netrw browse, meaning it should behave same as netrw
|
2021-03-06 21:23:05 +00:00
|
|
|
function M.open_new_pr(...)
|
2021-03-05 10:08:05 +00:00
|
|
|
vim.fn['netrw#BrowseX'](get_pr_url(...), 0)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Creates new branch and checks out to it
|
|
|
|
-- Similar to `gcb` in fish config
|
2021-03-06 21:23:05 +00:00
|
|
|
function M.create_branch(branch)
|
2021-03-05 10:08:05 +00:00
|
|
|
vim.cmd('Git checkout -b ' .. branch)
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Switches to branch
|
2021-03-06 21:23:05 +00:00
|
|
|
function M.checkout_branch(branch)
|
2021-03-05 10:08:05 +00:00
|
|
|
vim.cmd('Git checkout ' .. branch)
|
|
|
|
end
|
|
|
|
|
|
|
|
return M
|