Defaults to main branch if all other options aren't upstreamed

This commit is contained in:
PlaiyTiziano 2023-04-09 01:35:54 +02:00
parent 8c28db76c9
commit 4b7cf31af2
4 changed files with 86 additions and 32 deletions

View file

@ -39,7 +39,7 @@ function M.open_file(
-- if there is no buffer opened
if file_path == "/" then
print("There is no active file to open!")
utils.notify("There is no active file to open!", vim.log.levels.ERROR)
return
end
@ -49,10 +49,8 @@ function M.open_file(
file_page_url = file_page_url .. "#L" .. range_start .. "-L" .. range_end
end
local result = utils.open_url(file_page_url)
if result == false then
print("Unknown OS please open report")
if not utils.open_url(file_page_url) then
utils.notify("Could not open the built URL " .. file_page_url, vim.log.levels.ERROR)
end
end
@ -64,14 +62,9 @@ function M.open_repo()
return
end
local current_branch_name_or_commit_hash = utils.get_current_branch_or_commit()
local repo_page_url = M.repo_url .. "/tree/" .. current_branch_name_or_commit_hash
local result = utils.open_url(repo_page_url)
if result == false then
print("Unknown OS please open report")
local url = M.repo_url .. "/tree/" .. utils.get_current_branch_or_commit()
if not utils.open_url(url) then
utils.notify("Could not open the built URL " .. url, vim.log.levels.ERROR)
end
end

View file

@ -1,5 +1,11 @@
local M = {}
-- Notify the user that something went wrong
function M.notify(message, log_level)
print(message)
vim.notify({ message }, log_level, { title = "openingh.nvim" })
end
-- the missing split lua method to split a string
function M.split(string, char)
local array = {}
@ -46,17 +52,42 @@ function M.get_default_branch()
return M.trim(branch_name)
end
-- get the active local branch or commit when HEAD is detached
function M.get_current_branch_or_commit()
local current_branch_name = M.trim(vim.fn.system("git rev-parse --abbrev-ref HEAD"))
-- Checks if the supplied branch is available on the remote
function M.is_branch_upstreamed(branch)
local output = M.trim(vim.fn.system("git ls-remote --exit-code --heads origin " .. branch))
return output ~= ""
end
-- HEAD is detached
if current_branch_name ~= "HEAD" then
return current_branch_name
-- Get the current working branch
local function get_current_branch()
return M.trim(vim.fn.system("git rev-parse --abbrev-ref HEAD"))
end
-- Get the commit hash of the most recent commit
local function get_current_commit_hash()
return M.trim(vim.fn.system("git rev-parse HEAD"))
end
-- Checks if the supplied commit is available on the remote
function M.is_commit_upstreamed(commit_sha)
local output = M.trim(vim.fn.system('git log --format="%H" --remotes'))
return output:match(commit_sha) ~= nil
end
-- Returns the current branch or commit if they are available on remote
-- otherwise this will return the default branch of the repo
function M.get_current_branch_or_commit()
local current_branch = get_current_branch()
if current_branch ~= "HEAD" and M.is_branch_upstreamed(current_branch) then
return current_branch
end
local current_commit_hash = vim.fn.system("git rev-parse HEAD")
return M.trim(current_commit_hash)
local commit_hash = get_current_commit_hash()
if current_branch == "HEAD" and M.is_commit_upstreamed(commit_hash) then
return commit_hash
end
return M.get_default_branch()
end
-- get the active buf relative file path form the .git