Skip to content

Commit

Permalink
feat: improved git matching
Browse files Browse the repository at this point in the history
Supersedes #24

* Allow optional .git for ssh remotes
* Detect owner in owner#issue format
  • Loading branch information
sportshead committed Feb 24, 2024
1 parent cfe87ca commit fb38fc9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
11 changes: 8 additions & 3 deletions lua/gx/git.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ local function parse_git_output(result)
return
end

local domain, repository = result[1]:match("@(.*%..*):(.*)%.git$")
local domain, repository = result[1]:gsub("%.git%s*$", ""):match("@(.*%..*):(.*)$")
if domain and repository then
return "https://" .. domain .. "/" .. repository
end
Expand All @@ -16,10 +16,11 @@ local function parse_git_output(result)
end
end

function M.get_remote_url()
function M.get_remote_url(owner)
local notifier = require("gx.notifier")

local return_val, result = require("gx.shell").execute("git", { "remote", "get-url", "--push", "origin" })
local return_val, result =
require("gx.shell").execute("git", { "remote", "get-url", "--push", "origin" })

if return_val ~= 0 then
notifier.warn("No git information available!")
Expand All @@ -31,6 +32,10 @@ function M.get_remote_url()
notifier.warn("No remote git repository found!")
return
end
if type(owner) == "string" and owner ~= "" then
local domain, repository = url:match("^https?://([^/]+)/[^/]+/([^/]*)")
url = string.format("https://%s/%s/%s", domain, owner, repository)
end

return url
end
Expand Down
14 changes: 9 additions & 5 deletions lua/gx/handlers/github.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,20 @@ local M = {

-- navigate to neovim github plugin url
function M.handle(mode, line, _)
local pattern = "%a*%s#(%d*)"
local github_issue = helper.find(line, mode, pattern)
if not github_issue then
local match = helper.find(line, mode, "%a*%s([%w-_]+#%d+)")
if not match then
match = helper.find(line, mode, "%a*%s(#%d+)")
end
if not match then
return
end
local git_url = require("gx.git").get_remote_url()
local owner, issue = match:match("(.*)#(.+)")

local git_url = require("gx.git").get_remote_url(owner)
if not git_url then
return
end
return git_url .. "/issues/" .. github_issue
return git_url .. "/issues/" .. issue
end

return M

0 comments on commit fb38fc9

Please sign in to comment.