Skip to content

Commit

Permalink
feat: support forgejo URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
sgvictorino committed Aug 2, 2024
1 parent f5baf31 commit 19e3677
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 8 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`

Expand Down
3 changes: 2 additions & 1 deletion lua/gitblame/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}

Expand Down
19 changes: 12 additions & 7 deletions lua/gitblame/git.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 19e3677

Please sign in to comment.