From c5c85ed23ec6a2bee54ae06d576bb3c5df35544b Mon Sep 17 00:00:00 2001 From: Sathvik Birudavolu <8377116+BSathvik@users.noreply.github.com> Date: Sun, 9 Oct 2022 13:55:11 -0400 Subject: [PATCH] Add GitHub Enterprise URL support --- lua/openingh/init.lua | 8 ++------ lua/openingh/utils.lua | 30 +++++++++++------------------- 2 files changed, 13 insertions(+), 25 deletions(-) diff --git a/lua/openingh/init.lua b/lua/openingh/init.lua index 4a02daa..68e31b8 100644 --- a/lua/openingh/init.lua +++ b/lua/openingh/init.lua @@ -12,12 +12,8 @@ function M.setup() return end - -- init global variables - local username_and_reponame = utils.get_username_reponame(repo_url) - M.username = username_and_reponame.username - M.reponame = username_and_reponame.reponame - M.gh_url = "https://github.com/" - M.repo_url = M.gh_url .. M.username .. "/" .. M.reponame + local gh = utils.parse_gh_remote(repo_url) + M.repo_url = string.format("https://%s/%s/%s", gh.host, gh.user_or_org, gh.reponame) end function M.openFile() diff --git a/lua/openingh/utils.lua b/lua/openingh/utils.lua index 82b7f7f..f5376b6 100644 --- a/lua/openingh/utils.lua +++ b/lua/openingh/utils.lua @@ -16,27 +16,19 @@ function M.trim(string) return (string:gsub("^%s*(.-)%s*$", "%1")) end --- returns the username and the reponame form the origin url in a table -function M.get_username_reponame(url) - -- ssh has an @ in the url - if string.find(url, "@") then - local splitted_user_repo = M.split(url, ":")[2] - local splitted_username_and_reponame = M.split(splitted_user_repo, "/") - local username_and_reponame = { - username = splitted_username_and_reponame[1], - reponame = M.trim(string.gsub(splitted_username_and_reponame[2], ".git", "")), - } +-- returns a table with the host, user/org and the reponame given a github remote url +function M.parse_gh_remote(url) + -- 3 capture groups for host, org/user and repo. whitespace is trimmed + local http = { string.find(url, "https://([^/]*)/([^/]*)/([^%s/]*)") } + local ssh = { string.find(url, "git@(.*):([^/]*)/([^%s/]*)") } - return username_and_reponame - else - local splitted_username_and_reponame = M.split(url, "/") - local username_and_reponame = { - username = splitted_username_and_reponame[3], - reponame = M.trim(string.gsub(splitted_username_and_reponame[4], ".git", "")), - } - - return username_and_reponame + local matches = http[1] and http or ssh + if matches[1] == nil then + error("Cannot parse remote url") end + + local _, _, host, user_or_org, reponame = table.unpack(matches) + return { host = host, user_or_org = user_or_org, reponame = string.gsub(reponame, ".git", "") } end -- get the remote default branch