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 1/4] 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 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 2/4] 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 From dcd5aad28c323ccf5ac9df1eaf0abccfdafbb181 Mon Sep 17 00:00:00 2001 From: Sathvik Birudavolu <8377116+BSathvik@users.noreply.github.com> Date: Sun, 9 Oct 2022 15:36:16 -0400 Subject: [PATCH 3/4] remove accidentally added file --- 1 | 15 --------------- 1 file changed, 15 deletions(-) delete mode 100644 1 diff --git a/1 b/1 deleted file mode 100644 index b34562b..0000000 --- a/1 +++ /dev/null @@ -1,15 +0,0 @@ -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 -# From 16dc19bd5c7f3977eda5853d5392223593964eae Mon Sep 17 00:00:00 2001 From: Sathvik Birudavolu <8377116+BSathvik@users.noreply.github.com> Date: Sun, 9 Oct 2022 15:43:14 -0400 Subject: [PATCH 4/4] nit --- lua/openingh/utils.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/openingh/utils.lua b/lua/openingh/utils.lua index 250374f..90f2a0d 100644 --- a/lua/openingh/utils.lua +++ b/lua/openingh/utils.lua @@ -20,7 +20,7 @@ end -- 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 - -- when cloning with http://, gh redirects to https://, but remote is stays http + -- when cloning with http://, gh redirects to https://, but remote stays http local http = { string.find(url, "https?://([^/]*)/([^/]*)/([^%s/]*)") } -- ssh url can be of type: -- git@some.github.com:user_or_org/reponame.git