Files
dotfiles/nvim/lua/tracer/plugins/fugitive.lua
2026-03-31 23:13:00 +02:00

91 lines
2.3 KiB
Lua

local M = {}
local function homepage_for_remote(remote)
local patterns = {
"^gitea@([^:]+):(.+)%.git$",
"^git@([^:]+):(.+)%.git$",
"^ssh://git@([^/]+)/(.+)%.git$",
"^https?://([^/]+)/(.+)%.git$",
"^https?://([^/]+)/(.+)$",
}
for _, pattern in ipairs(patterns) do
local host, repo = remote:match(pattern)
if host and repo then
return ("https://%s/%s"):format(host, repo)
end
end
return nil
end
local function url_escape(value)
return (value:gsub("#", "%%23"):gsub(" ", "%%20"))
end
local function gitea_browse(opts)
local root = homepage_for_remote(opts.remote or "")
if not root then
return nil
end
local path = (opts.path or ""):gsub("^/", "")
local ref = (opts.path or ""):match("^/?%.git/(refs/.*)")
if ref and ref:match("^refs/heads/") then
local branch = ref:gsub("^refs/heads/", "")
return root .. "/commits/branch/" .. url_escape(branch)
end
if ref and ref:match("^refs/tags/") then
local tag = ref:gsub("^refs/tags/", "")
return root .. "/releases/tag/" .. url_escape(tag)
end
if ref and ref:match("^refs/remotes/[^/]+/.+") then
local branch = ref:match("^refs/remotes/[^/]+/(.+)$")
return root .. "/commits/branch/" .. url_escape(branch)
end
if (opts.path or ""):match("^/?%.git") then
return root
end
local commit = url_escape(opts.commit or "HEAD")
local url
if opts.type == "tree" or path:match("/$") then
url = root .. "/src/commit/" .. commit
if path ~= "" then
url = url .. "/" .. url_escape(path):gsub("/$", "")
end
elseif opts.type == "blob" or path:match("[^/]$") then
url = root .. "/src/commit/" .. commit
if path ~= "" then
url = url .. "/" .. url_escape(path)
end
if opts.line2 and opts.line1 and opts.line1 > 0 and opts.line1 == opts.line2 then
url = url .. "#L" .. opts.line1
elseif opts.line1 and opts.line2 and opts.line1 > 0 and opts.line2 > 0 then
url = url .. "#L" .. opts.line1 .. "-L" .. opts.line2
end
else
url = root .. "/commit/" .. commit
end
return url
end
function M.setup()
if vim.g.tracer_fugitive_gitea_handler_loaded then
return
end
vim.g.tracer_fugitive_gitea_handler_loaded = true
vim.g.fugitive_browse_handlers = vim.g.fugitive_browse_handlers or {}
table.insert(vim.g.fugitive_browse_handlers, 1, gitea_browse)
end
return M