fix(lazygit): improve git browse (#3941)

## Description
Improves git browse command by handling different types of remotes, and
allows user to extend to other git hosts.

## Related Issue(s)
Fixes #3886

## Checklist
- [x] I've read the
[CONTRIBUTING](https://github.com/LazyVim/LazyVim/blob/main/CONTRIBUTING.md)
guidelines.

---------

Co-authored-by: Folke Lemaitre <folke.lemaitre@gmail.com>
This commit is contained in:
med8bra
2024-07-07 20:25:45 +01:00
committed by GitHub
parent 8b2eacb6ac
commit 28805d1a4c
2 changed files with 73 additions and 5 deletions

View File

@ -159,17 +159,43 @@ function M.blame_line(opts)
return require("lazy.util").float_cmd(cmd, opts)
end
-- stylua: ignore
M.remote_patterns = {
{ "^(https?://.*)%.git$" , "%1" },
{ "^git@(.+):(.+)%.git$" , "https://%1/%2" },
{ "^git@(.+):(.+)$" , "https://%1/%2" },
{ "^git@(.+)/(.+)$" , "https://%1/%2" },
{ "^ssh://git@(.*)$" , "https://%1" },
{ "ssh%.dev%.azure%.com/v3/(.*)/(.*)$", "dev.azure.com/%1/_git/%2" },
{ "^https://%w*@(.*)" , "https://%1" },
{ "^git@(.*)" , "https://%1" },
{ ":%d+" , "" },
{ "%.git$" , "" },
}
---@param remote string
function M.get_url(remote)
local ret = remote
for _, pattern in ipairs(M.remote_patterns) do
ret = ret:gsub(pattern[1], pattern[2])
end
return ret:find("https://") == 1 and ret or ("https://%s"):format(ret)
end
function M.browse()
local lines = require("lazy.manage.process").exec({ "git", "remote", "-v" })
local remotes = {} ---@type {name:string, url:string}[]
for _, line in ipairs(lines) do
local name, url = line:match("(%S+)%s+(%S+)%s+%(fetch%)")
if name and url then
if url:find("git@") == 1 then
url = url:gsub("git@(%S+):", "https://%1/"):gsub(".git$", "")
local name, remote = line:match("(%S+)%s+(%S+)%s+%(fetch%)")
if name and remote then
local url = M.get_url(remote)
if url then
table.insert(remotes, {
name = name,
url = url,
})
end
table.insert(remotes, { name = name, url = url })
end
end