From 99be35eef8385c0cc2451af72675ee81e9140173 Mon Sep 17 00:00:00 2001 From: Sathvik Birudavolu <8377116+BSathvik@users.noreply.github.com> Date: Sun, 9 Oct 2022 15:34:46 -0400 Subject: [PATCH] Fix error, support other remotes - `table.unpack` -> `unpack` - Add support for `http://` and `ssh://` remotes - Gracefully handle error while parsing --- 1 | 15 +++++++++++++++ lua/openingh/init.lua | 6 ++++++ lua/openingh/utils.lua | 18 +++++++++++------- 3 files changed, 32 insertions(+), 7 deletions(-) create mode 100644 1 diff --git a/1 b/1 new file mode 100644 index 0000000..b34562b --- /dev/null +++ b/1 @@ -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 +# diff --git a/lua/openingh/init.lua b/lua/openingh/init.lua index 68e31b8..3bcbccd 100644 --- a/lua/openingh/init.lua +++ b/lua/openingh/init.lua @@ -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 diff --git a/lua/openingh/utils.lua b/lua/openingh/utils.lua index f5376b6..250374f 100644 --- a/lua/openingh/utils.lua +++ b/lua/openingh/utils.lua @@ -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