Add GitHub Enterprise URL support

This commit is contained in:
Sathvik Birudavolu 2022-10-09 13:55:11 -04:00
parent cf9fc379fb
commit c5c85ed23e
2 changed files with 13 additions and 25 deletions

View file

@ -12,12 +12,8 @@ function M.setup()
return return
end end
-- init global variables local gh = utils.parse_gh_remote(repo_url)
local username_and_reponame = utils.get_username_reponame(repo_url) M.repo_url = string.format("https://%s/%s/%s", gh.host, gh.user_or_org, gh.reponame)
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
end end
function M.openFile() function M.openFile()

View file

@ -16,27 +16,19 @@ function M.trim(string)
return (string:gsub("^%s*(.-)%s*$", "%1")) return (string:gsub("^%s*(.-)%s*$", "%1"))
end end
-- returns the username and the reponame form the origin url in a table -- returns a table with the host, user/org and the reponame given a github remote url
function M.get_username_reponame(url) function M.parse_gh_remote(url)
-- ssh has an @ in the url -- 3 capture groups for host, org/user and repo. whitespace is trimmed
if string.find(url, "@") then local http = { string.find(url, "https://([^/]*)/([^/]*)/([^%s/]*)") }
local splitted_user_repo = M.split(url, ":")[2] local ssh = { string.find(url, "git@(.*):([^/]*)/([^%s/]*)") }
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", "")),
}
return username_and_reponame local matches = http[1] and http or ssh
else if matches[1] == nil then
local splitted_username_and_reponame = M.split(url, "/") error("Cannot parse remote 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
end 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 end
-- get the remote default branch -- get the remote default branch