Roughly create a commit-priority implementation
This commit is contained in:
parent
871c1f837d
commit
5e742c3d7f
3 changed files with 54 additions and 9 deletions
|
|
@ -22,7 +22,18 @@ function M.setup()
|
|||
M.repo_url = string.format("http://%s/%s/%s", gh.host, gh.user_or_org, gh.reponame)
|
||||
end
|
||||
|
||||
local get_current_branch_or_commit_with_priority = function(priority)
|
||||
if (not priority) or (priority and priority == 'branch') then
|
||||
return utils.get_current_branch_or_commit()
|
||||
elseif (priority and priority == 'branch') then
|
||||
return utils.get_current_commit_or_branch()
|
||||
else
|
||||
return utils.get_current_branch_or_commit()
|
||||
end
|
||||
end
|
||||
|
||||
function M.open_file(
|
||||
priority,
|
||||
--[[optional]]
|
||||
range_start,
|
||||
--[[optional]]
|
||||
|
|
@ -43,7 +54,8 @@ function M.open_file(
|
|||
return
|
||||
end
|
||||
|
||||
local file_page_url = M.repo_url .. "/blob/" .. utils.get_current_branch_or_commit() .. file_path
|
||||
|
||||
local file_page_url = M.repo_url .. "/blob/" .. get_current_branch_or_commit_with_priority(priority) .. file_path
|
||||
|
||||
if range_start and not range_end then
|
||||
file_page_url = file_page_url .. "#L" .. range_start
|
||||
|
|
@ -58,7 +70,7 @@ function M.open_file(
|
|||
end
|
||||
end
|
||||
|
||||
function M.open_repo()
|
||||
function M.open_repo(priority)
|
||||
-- make sure to update the current directory
|
||||
M.setup()
|
||||
if M.is_no_git_origin then
|
||||
|
|
@ -66,7 +78,8 @@ function M.open_repo()
|
|||
return
|
||||
end
|
||||
|
||||
local url = M.repo_url .. "/tree/" .. utils.get_current_branch_or_commit()
|
||||
|
||||
local url = M.repo_url .. "/tree/" .. get_current_branch_or_commit_with_priority(priority)
|
||||
if not utils.open_url(url) then
|
||||
utils.notify("Could not open the built URL " .. url, vim.log.levels.ERROR)
|
||||
end
|
||||
|
|
|
|||
|
|
@ -96,6 +96,23 @@ function M.get_current_branch_or_commit()
|
|||
return M.encode_uri_component(M.get_default_branch())
|
||||
end
|
||||
|
||||
-- Returns the current commit or branch if they are available on remote
|
||||
-- otherwise this will return the default branch of the repo
|
||||
-- (This function prioritizes commit than branch)
|
||||
function M.get_current_commit_or_branch()
|
||||
local commit_hash = get_current_commit_hash()
|
||||
if M.is_commit_upstreamed(commit_hash) then
|
||||
return commit_hash
|
||||
end
|
||||
|
||||
local current_branch = get_current_branch()
|
||||
if current_branch ~= "HEAD" and M.is_branch_upstreamed(current_branch) then
|
||||
return M.encode_uri_component(current_branch)
|
||||
end
|
||||
|
||||
return M.encode_uri_component(M.get_default_branch())
|
||||
end
|
||||
|
||||
-- get the active buf relative file path form the .git
|
||||
function M.get_current_relative_file_path()
|
||||
-- we only want the active buffer name
|
||||
|
|
|
|||
|
|
@ -5,26 +5,41 @@ vim.g.openingh = true
|
|||
|
||||
local openingh = require("openingh")
|
||||
|
||||
local complete_list = { 'branch-priority', 'commit-priority', }
|
||||
local complete_func = function(arg_lead, _, _)
|
||||
return vim.tbl_filter(function(item)
|
||||
return vim.startswith(item, arg_lead)
|
||||
end, complete_list)
|
||||
end
|
||||
|
||||
vim.api.nvim_create_user_command("OpenInGHFile", function(opts)
|
||||
if opts.range == 0 then -- Nothing was selected
|
||||
openingh.open_file()
|
||||
openingh.open_file(opts.args)
|
||||
else -- Current line or block was selected
|
||||
openingh.open_file(opts.line1, opts.line2)
|
||||
openingh.open_file(opts.args, opts.line1, opts.line2)
|
||||
end
|
||||
end, {
|
||||
range = true,
|
||||
nargs = '?',
|
||||
complete = complete_func,
|
||||
})
|
||||
|
||||
vim.api.nvim_create_user_command("OpenInGHFileLines", function(opts)
|
||||
if opts.range == 0 then -- Nothing was selected
|
||||
openingh.open_file(opts.line1)
|
||||
openingh.open_file(opts.args, opts.line1)
|
||||
else -- Current line or block was selected
|
||||
openingh.open_file(opts.line1, opts.line2)
|
||||
openingh.open_file(opts.args, opts.line1, opts.line2)
|
||||
end
|
||||
end, {
|
||||
range = true,
|
||||
nargs = '?',
|
||||
complete = complete_func,
|
||||
})
|
||||
|
||||
vim.api.nvim_create_user_command("OpenInGHRepo", function()
|
||||
openingh.open_repo()
|
||||
end, {})
|
||||
print(opts)
|
||||
openingh.open_repo(opts.args)
|
||||
end, {
|
||||
nargs = '?',
|
||||
complete = complete_func,
|
||||
})
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue