Skip to content

Commit

Permalink
config: support custom forge domains
Browse files Browse the repository at this point in the history
  • Loading branch information
sgvictorino committed Aug 2, 2024
1 parent 3ed6338 commit f5baf31
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 10 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ A git blame plugin for Neovim written in Lua
- [Start virtual text at column](#start-virtual-text-at-column)
- [Better Performance](#better-performance)
- [Use blame commit file URLs](#use-blame-commit-file-urls)
- [Remote forge domains](#forge-domains)
- [Commands](#commands)
- [Open the commit URL in browser](#open-the-commit-url-in-browser)
- [Enable/Disable git blame messages](#enabledisable-git-blame-messages)
Expand Down Expand Up @@ -281,6 +282,22 @@ Default: `+`
let g:gitblame_clipboard_register = "*"
```

### Remote forge domains

A map of domain names to forge software, used to generate commit and file URLs. Currently supported software:

- `github`
- `gitlab`
- `sourcehut`
- `azure`
- `bitbucket`

```lua
vim.g.gitblame_remote_domains = {
"git.sr.ht" = "sourcehut"
}
```

## Commands

### Open the commit URL in browser
Expand Down
5 changes: 5 additions & 0 deletions lua/gitblame/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ M.default_opts = {
schedule_event = "CursorMoved",
clear_event = "CursorMovedI",
clipboard_register = "+",
remote_domains = {
["git.sr.ht"] = "sourcehut",
["dev.azure.com"] = "azure",
["bitbucket.org"] = "bitbucket"
}
}

---@param opts SetupOptions?
Expand Down
19 changes: 9 additions & 10 deletions lua/gitblame/git.lua
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ end
local function get_commit_path(sha, repo_url)
local domain = get_http_domain(repo_url)

if domain == "bitbucket.org" then
local forge = vim.g.gitblame_remote_domains[domain]
if forge == "bitbucket" then
return "/commits/" .. sha
end

Expand Down Expand Up @@ -109,26 +110,24 @@ local function get_file_url(remote_url, branch, filepath, line1, line2)
local repo_url = get_repo_url(remote_url)
local domain = get_http_domain(repo_url)

local isSrcHut = domain == "git.sr.ht"
local isBitbucket = domain == "bitbucket.org"
local isAzure = domain == "dev.azure.com"
local forge = vim.g.gitblame_remote_domains[domain]

local file_path = "/blob/" .. branch .. "/" .. filepath
if isSrcHut then
if forge == "sourcehut" then
file_path = "/tree/" .. branch .. "/" .. filepath
end
if isBitbucket then
if forge == "bitbucket" then
file_path = "/src/" .. ref .. "/" .. filepath
end
if isAzure then
if forge == "azure" then
-- Can't use branch here since the URL wouldn't work in cases it's a commit sha
file_path = "?path=%2F" .. filepath
end

if line1 == nil then
return repo_url .. file_path
elseif line2 == nil or line1 == line2 then
if isAzure then
if forge == "azure" then
return repo_url .. file_path .. "&line=" .. line1 .. "&lineEnd=" .. line1 + 1 .. "&lineStartColumn=1&lineEndColumn=1"
end

Expand All @@ -138,11 +137,11 @@ local function get_file_url(remote_url, branch, filepath, line1, line2)

return repo_url .. file_path .. "#L" .. line1
else
if isSrcHut then
if forge == "sourcehut" then
return repo_url .. file_path .. "#L" .. line1 .. "-" .. line2
end

if isAzure then
if forge == "azure" then
return repo_url .. file_path .. "&line=" .. line1 .. "&lineEnd=" .. line2 + 1 .. "&lineStartColumn=1&lineEndColumn=1"
end

Expand Down

0 comments on commit f5baf31

Please sign in to comment.