Merge pull request #3 from BSathvik/vik/add_ghe_support

This commit is contained in:
Ali Almohaya 2022-10-10 00:59:48 +03:00 committed by GitHub
commit 65d0d11a5c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 25 deletions

View file

@ -12,12 +12,14 @@ 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) if gh == nil then
M.username = username_and_reponame.username print("Error parsing GitHub remote URL")
M.reponame = username_and_reponame.reponame vim.g.openingh = false
M.gh_url = "https://github.com/" return
M.repo_url = M.gh_url .. M.username .. "/" .. M.reponame end
M.repo_url = string.format("https://%s/%s/%s", gh.host, gh.user_or_org, gh.reponame)
end end
function M.openFile() function M.openFile()

View file

@ -16,27 +16,23 @@ 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) -- nil is returned when the url cannot be parsed
-- ssh has an @ in the url function M.parse_gh_remote(url)
if string.find(url, "@") then -- 3 capture groups for host, org/user and repo. whitespace is trimmed
local splitted_user_repo = M.split(url, ":")[2] -- when cloning with http://, gh redirects to https://, but remote stays http
local splitted_username_and_reponame = M.split(splitted_user_repo, "/") local http = { string.find(url, "https?://([^/]*)/([^/]*)/([^%s/]*)") }
local username_and_reponame = { -- ssh url can be of type:
username = splitted_username_and_reponame[1], -- git@some.github.com:user_or_org/reponame.git
reponame = M.trim(string.gsub(splitted_username_and_reponame[2], ".git", "")), -- ssh://git@some.github.com/user_or_org/reponame.git
} -- .* is used for ssh:// since lua matching doesn't support optional groups, only chars
local ssh = { string.find(url, ".*git@(.*)[:/]([^/]*)/([^%s/]*)") }
return username_and_reponame local matches = http[1] == nil and ssh or http
else if matches[1] == nil then return nil end
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 _, _, host, user_or_org, reponame = unpack(matches)
end 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