local remote_format_file = "%s/blob/%s%s" local bb_remote_format_file = "%s/src/%s%s" local forgejo_remote_format_file = "%s/src/%s%s" local remote_format_branch = "%s/tree/%s" local bb_remote_format_branch = "%s/branch/%s" local bb_remote_format_commit = "%s/commits/%s" local forgejo_remote_format_branch = "%s/src/branch/%s" local forgejo_remote_format_commit = "%s/commit/%s" local remote_format_line = "%s#L%s" local remote_format_lines = "%s#L%s-L%s" local bb_remote_format_line = "%s#lines-%s" local bb_remote_format_lines = "%s#lines-%s:%s" local utils = require("openingh.utils") local M = {} function M.setup() -- get the current working directory and set the url local current_buffer = vim.fn.expand("%:p:h"):gsub("%[", "\\["):gsub("%]", "\\]") local repo_url = vim.fn.system("git -C " .. current_buffer .. " config --get remote.origin.url") if repo_url:len() == 0 then M.is_no_git_origin = true vim.g.openingh = false return end local remote = utils.parse_remote(repo_url) if remote == nil then print("Error parsing remote URL") vim.g.openingh = false return end M.repo_url = string.format("http://%s/%s/%s", remote.host, remote.user_or_org, remote.reponame) if string.find(M.repo_url, "bitbucket") then M.remote = M.remotes.BB M.format_file = bb_remote_format_file M.format_branch = bb_remote_format_branch M.format_commit = bb_remote_format_commit M.format_single_line = bb_remote_format_line M.format_multiple_lines = bb_remote_format_lines elseif string.find(M.repo_url, "forgejo") or string.find(M.repo_url, "gitea") then M.remote = M.remotes.FORGEJO M.format_file = forgejo_remote_format_file M.format_branch = forgejo_remote_format_branch M.format_commit = forgejo_remote_format_commit M.format_single_line = remote_format_line M.format_multiple_lines = remote_format_lines else M.remote = M.remotes.GH M.format_file = remote_format_file M.format_branch = remote_format_branch M.format_commit = remote_format_branch M.format_single_line = remote_format_line M.format_multiple_lines = remote_format_lines end end M.priority = { BRANCH = 1, COMMIT = 2 } M.remotes = { GH = 1, BB = 2, FORGEJO = 3 } 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.get_file_url( priority, --[[optional]] branch, --[[optional]] range_start, --[[optional]] range_end ) -- make sure to update the current directory M.setup() if M.is_no_git_origin then utils.print_no_remote_message() return end local file_path = utils.get_current_relative_file_path() -- if there is no buffer opened if file_path == "/" then utils.notify("There is no active file to open!", vim.log.levels.ERROR) return end local rev = get_current_branch_or_commit_with_priority(priority) if branch ~= nil then rev = branch end local file_page_url = string.format(M.format_file, M.repo_url, rev, file_path) if range_start and not range_end then file_page_url = string.format(M.format_single_line, file_page_url, range_start) end if range_start and range_end then file_page_url = string.format(M.format_multiple_lines, file_page_url, range_start, range_end) end return file_page_url end function M.get_repo_url(priority) -- make sure to update the current directory M.setup() if M.is_no_git_origin then utils.print_no_remote_message() return end local url if priority == M.priority.BRANCH then url = string.format(M.format_branch, M.repo_url, get_current_branch_or_commit_with_priority(priority)) else url = string.format(M.format_commit, M.repo_url, get_current_branch_or_commit_with_priority(priority)) end return url end function M.open_url(url) if not utils.open_url(url) then utils.notify("Could not open the built URL " .. url, vim.log.levels.ERROR) end end return M