Skip to content

Commit

Permalink
feat: add handler_options.git_remote_push option
Browse files Browse the repository at this point in the history
Also executes `git remote get-url` with a `-C` param of
`vim.fn.expand("%:p:h")`
  • Loading branch information
sportshead committed Apr 28, 2024
1 parent 7259a45 commit a7628f4
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 5 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ require("lazy").setup({
end
return { "upstream", "origin" }
end,

git_remote_push = false, -- use the push url for git issue linking,
git_remote_push = function(fname) -- you can also pass in a function
return fname:match("myproject")
end,
},
} end,
},
Expand Down
10 changes: 7 additions & 3 deletions lua/gx/git.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,17 @@ local function parse_git_output(result)
end
end

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

local url = nil
local path = vim.fn.expand("%:p:h")
for _, remote in ipairs(remotes) do
local exit_code, result =
require("gx.shell").execute("git", { "remote", "get-url", "--push", remote })
local args = { "-C", path, "remote", "get-url", remote }
if push then
table.insert(args, "--push")
end
local exit_code, result = require("gx.shell").execute("git", args)
if exit_code == 0 then
url = parse_git_output(result)
if url then
Expand Down
7 changes: 6 additions & 1 deletion lua/gx/handlers/commit.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ function M.handle(mode, line, handler_options)
remotes = remotes(vim.fn.expand("%:p"))
end

local git_url = require("gx.git").get_remote_url(remotes)
local push = handler_options.push
if type(push) == "function" then
push = push(vim.fn.expand("%:p"))
end

local git_url = require("gx.git").get_remote_url(remotes, push)
if not git_url then
return
end
Expand Down
7 changes: 6 additions & 1 deletion lua/gx/handlers/github.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ function M.handle(mode, line, handler_options)
remotes = remotes(vim.fn.expand("%:p"))
end

local git_url = require("gx.git").get_remote_url(remotes, owner)
local push = handler_options.push
if type(push) == "function" then
push = push(vim.fn.expand("%:p"))
end

local git_url = require("gx.git").get_remote_url(remotes, push, owner)
if not git_url then
return
end
Expand Down
1 change: 1 addition & 0 deletions lua/gx/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ local function with_defaults(options)
search_engine = options.handler_options.search_engine or "google",
select_for_search = options.handler_options.select_for_search or false,
git_remotes = options.handler_options.git_remotes or { "upstream", "origin" },
git_remote_push = options.handler_options.git_remote_push or false,
},
}
end
Expand Down

0 comments on commit a7628f4

Please sign in to comment.