Merge pull request #19 from msr1k/feature/commit-priority-arg

This commit is contained in:
Ali Almohaya 2023-06-19 23:02:52 +03:00 committed by GitHub
commit 2719e5759e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 56 additions and 10 deletions

View file

@ -22,7 +22,20 @@ function M.setup()
M.repo_url = string.format("http://%s/%s/%s", gh.host, gh.user_or_org, gh.reponame)
end
M.priority = { BRANCH = 'branch-priority', COMMIT = 'commit-priority', }
local function get_current_branch_or_commit_with_priority(priority)
if priority == M.priority.BRANCH then
return utils.get_current_branch_or_commit()
elseif priority == M.priority.COMMIT 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 +56,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 +72,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 +80,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

View file

@ -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

View file

@ -5,26 +5,40 @@ vim.g.openingh = true
local openingh = require("openingh")
local complete_list = { openingh.priority.BRANCH, openingh.priority.COMMIT, }
local function complete_func(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, {})
vim.api.nvim_create_user_command("OpenInGHRepo", function(opts)
openingh.open_repo(opts.args)
end, {
nargs = '?',
complete = complete_func,
})