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(...)
|
2022-04-28 13:53:54 +00:00
|
|
|
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://")
|
2021-03-05 10:08:05 +00:00
|
|
|
|
2022-04-28 13:53:54 +00:00
|
|
|
-- Remove prefix if it is available, for some of common git services
|
|
|
|
local common_services = { "github.com", "bitbucket.org", "gitlab.com" }
|
2022-04-29 07:46:45 +00:00
|
|
|
for _, service in pairs(common_services) do
|
2022-04-28 13:53:54 +00:00
|
|
|
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
|
2021-03-05 10:08:05 +00:00
|
|
|
|
2022-04-28 13:53:54 +00:00
|
|
|
-- 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
|
2021-03-05 10:08:05 +00:00
|
|
|
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()
|
2022-04-28 13:53:54 +00:00
|
|
|
vim.cmd("Git push origin " .. vim.fn.FugitiveHead())
|
2021-03-08 09:46:42 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Shorcut to pull directly from current branch on origin
|
|
|
|
-- Similar to `ggpull` in fish config
|
|
|
|
function M.pull_origin()
|
2022-04-28 13:53:54 +00:00
|
|
|
vim.cmd("Git pull origin " .. vim.fn.FugitiveHead())
|
2021-03-08 09:46:42 +00:00
|
|
|
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(...)
|
2022-04-28 13:53:54 +00:00
|
|
|
vim.cmd('echo "' .. get_pr_url(...) .. '"')
|
2021-03-05 10:08:05 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Copies current branches PR url to system clipboard
|
2021-03-06 21:23:05 +00:00
|
|
|
function M.copy_pr_url(...)
|
2022-04-28 13:53:54 +00:00
|
|
|
vim.cmd('let @+ = "' .. get_pr_url(...) .. '"')
|
2021-03-05 10:08:05 +00:00
|
|
|
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(...)
|
2022-04-28 13:53:54 +00:00
|
|
|
vim.fn["netrw#BrowseX"](get_pr_url(...), 0)
|
2021-03-05 10:08:05 +00:00
|
|
|
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)
|
2022-04-28 13:53:54 +00:00
|
|
|
vim.cmd("Git checkout -b " .. branch)
|
2021-03-05 10:08:05 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Switches to branch
|
2021-03-06 21:23:05 +00:00
|
|
|
function M.checkout_branch(branch)
|
2022-04-28 13:53:54 +00:00
|
|
|
vim.cmd("Git checkout " .. branch)
|
2021-03-05 10:08:05 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return M
|