From 19e36774e7e0e1c72b6e68d543485b2d78983b21 Mon Sep 17 00:00:00 2001 From: Solomon Victorino Date: Thu, 1 Aug 2024 16:13:07 -0600 Subject: [PATCH] feat: support forgejo URLs --- README.md | 1 + lua/gitblame/config.lua | 3 ++- lua/gitblame/git.lua | 19 ++++++++++++------- 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 7f490fa..020e49d 100644 --- a/README.md +++ b/README.md @@ -289,6 +289,7 @@ A map of domain names to forge software, used to generate commit and file URLs. - `github` - `gitlab` - `sourcehut` +- `forgejo` - `azure` - `bitbucket` diff --git a/lua/gitblame/config.lua b/lua/gitblame/config.lua index 0ab0262..2cdf268 100644 --- a/lua/gitblame/config.lua +++ b/lua/gitblame/config.lua @@ -19,7 +19,8 @@ M.default_opts = { remote_domains = { ["git.sr.ht"] = "sourcehut", ["dev.azure.com"] = "azure", - ["bitbucket.org"] = "bitbucket" + ["bitbucket.org"] = "bitbucket", + ["codeberg.org"] = "forgejo" } } diff --git a/lua/gitblame/git.lua b/lua/gitblame/git.lua index c27f250..6aad0ed 100644 --- a/lua/gitblame/git.lua +++ b/lua/gitblame/git.lua @@ -101,26 +101,31 @@ local function get_repo_url(remote_url) end ---@param remote_url string ----@param branch string +---@param ref_type "commit"|"branch" +---@param ref string ---@param filepath string ---@param line1 number? ---@param line2 number? ---@return string -local function get_file_url(remote_url, branch, filepath, line1, line2) +local function get_file_url(remote_url, ref_type, ref, filepath, line1, line2) local repo_url = get_repo_url(remote_url) local domain = get_http_domain(repo_url) local forge = vim.g.gitblame_remote_domains[domain] - local file_path = "/blob/" .. branch .. "/" .. filepath + local file_path = "/blob/" .. ref .. "/" .. filepath if forge == "sourcehut" then - file_path = "/tree/" .. branch .. "/" .. filepath + file_path = "/tree/" .. ref .. "/" .. filepath + end + if forge == "forgejo" then + file_path = "/src/" .. ref_type .. "/" .. ref .. "/" .. filepath end if forge == "bitbucket" then file_path = "/src/" .. ref .. "/" .. filepath end if forge == "azure" then - -- Can't use branch here since the URL wouldn't work in cases it's a commit sha + -- Can't use ref here if it's a commit sha + -- FIXME: add branch names to the URL file_path = "?path=%2F" .. filepath end @@ -190,13 +195,13 @@ function M.get_file_url(filepath, sha, line1, line2, callback) if sha == nil then get_current_branch(function(branch) M.get_remote_url(function(remote_url) - local url = get_file_url(remote_url, branch, relative_filepath, line1, line2) + local url = get_file_url(remote_url, "branch", branch, relative_filepath, line1, line2) callback(url) end) end) else M.get_remote_url(function(remote_url) - local url = get_file_url(remote_url, sha, relative_filepath, line1, line2) + local url = get_file_url(remote_url, "commit", sha, relative_filepath, line1, line2) callback(url) end) end