diff --git a/lua/openingh/init.lua b/lua/openingh/init.lua index b37af1a..6eb16b2 100644 --- a/lua/openingh/init.lua +++ b/lua/openingh/init.lua @@ -33,9 +33,9 @@ function M.openFile() return end - local current_branch_name = utils.get_current_branch() + local current_branch_name_or_commit_hash = utils.get_current_branch_or_commit() - local file_page_url = M.repo_url .. "/blob/" .. current_branch_name .. file_path + local file_page_url = M.repo_url .. "/blob/" .. current_branch_name_or_commit_hash .. file_path local result = utils.open_url(file_page_url) @@ -52,9 +52,9 @@ function M.openRepo() return end - local current_branch_name = utils.get_current_branch() + local current_branch_name_or_commit_hash = utils.get_current_branch_or_commit() - local repo_page_url = M.repo_url .. "/tree/" .. current_branch_name + local repo_page_url = M.repo_url .. "/tree/" .. current_branch_name_or_commit_hash local result = utils.open_url(repo_page_url) diff --git a/lua/openingh/utils.lua b/lua/openingh/utils.lua index 7660297..032af26 100644 --- a/lua/openingh/utils.lua +++ b/lua/openingh/utils.lua @@ -48,10 +48,17 @@ function M.get_defualt_branch() return M.trim(branch_name) end --- get the active local branch -function M.get_current_branch() - local current_branch_name = vim.fn.system("git rev-parse --abbrev-ref HEAD") - return M.trim(current_branch_name) +-- 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")) + + -- HEAD is detached returing commit hash + if current_branch_name ~= "HEAD" then + return current_branch_name + end + + local current_commit_hash = vim.fn.system("git rev-parse HEAD") + return M.trim(current_commit_hash) end -- get the active buf relative file path form the .git