Fix error, support other remotes

- `table.unpack` -> `unpack`
- Add support for `http://` and `ssh://` remotes
- Gracefully handle error while parsing
This commit is contained in:
Sathvik Birudavolu 2022-10-09 15:34:46 -04:00
parent c5c85ed23e
commit 99be35eef8
3 changed files with 32 additions and 7 deletions

15
1 Normal file
View file

@ -0,0 +1,15 @@
Fix issue with error, support other remotes
- `table.unpack` -> `unpack`
- Add support for `http://` and `ssh://` remotes
- Gracefully handle error while parsing
# Please enter the commit message for your changes. Lines starting
# with '#' will be ignored, and an empty message aborts the commit.
#
# On branch vik/add_ghe_support
# Your branch is up to date with 'origin/vik/add_ghe_support'.
#
# Changes to be committed:
# modified: lua/openingh/init.lua
# modified: lua/openingh/utils.lua
#

View file

@ -13,6 +13,12 @@ function M.setup()
end
local gh = utils.parse_gh_remote(repo_url)
if gh == nil then
print("Error parsing GitHub remote URL")
vim.g.openingh = false
return
end
M.repo_url = string.format("https://%s/%s/%s", gh.host, gh.user_or_org, gh.reponame)
end

View file

@ -17,17 +17,21 @@ function M.trim(string)
end
-- returns a table with the host, user/org and the reponame given a github remote url
-- nil is returned when the url cannot be parsed
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/]*)") }
-- when cloning with http://, gh redirects to https://, but remote is stays http
local http = { string.find(url, "https?://([^/]*)/([^/]*)/([^%s/]*)") }
-- ssh url can be of type:
-- git@some.github.com:user_or_org/reponame.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/]*)") }
local matches = http[1] and http or ssh
if matches[1] == nil then
error("Cannot parse remote url")
end
local matches = http[1] == nil and ssh or http
if matches[1] == nil then return nil end
local _, _, host, user_or_org, reponame = table.unpack(matches)
local _, _, host, user_or_org, reponame = unpack(matches)
return { host = host, user_or_org = user_or_org, reponame = string.gsub(reponame, ".git", "") }
end