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
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
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) return require("lazy.util").float_cmd(cmd, opts)
end 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() function M.browse()
local lines = require("lazy.manage.process").exec({ "git", "remote", "-v" }) local lines = require("lazy.manage.process").exec({ "git", "remote", "-v" })
local remotes = {} ---@type {name:string, url:string}[] local remotes = {} ---@type {name:string, url:string}[]
for _, line in ipairs(lines) do for _, line in ipairs(lines) do
local name, url = line:match("(%S+)%s+(%S+)%s+%(fetch%)") local name, remote = line:match("(%S+)%s+(%S+)%s+%(fetch%)")
if name and url then if name and remote then
if url:find("git@") == 1 then local url = M.get_url(remote)
url = url:gsub("git@(%S+):", "https://%1/"):gsub(".git$", "") if url then
table.insert(remotes, {
name = name,
url = url,
})
end end
table.insert(remotes, { name = name, url = url })
end end
end end

View file

@ -0,0 +1,42 @@
---@module "luassert"
_G.LazyVim = require("lazyvim.util")
-- stylua: ignore
local git_remotes_cases = {
["https://github.com/LazyVim/LazyVim.git"] = "https://github.com/LazyVim/LazyVim",
["https://github.com/LazyVim/LazyVim"] = "https://github.com/LazyVim/LazyVim",
["git@github.com:LazyVim/LazyVim"] = "https://github.com/LazyVim/LazyVim",
["git@ssh.dev.azure.com:v3/neovim-org/owner/repo"] = "https://dev.azure.com/neovim-org/owner/_git/repo",
["https://folkelemaitre@bitbucket.org/samiulazim/neovim.git"] = "https://bitbucket.org/samiulazim/neovim",
["git@bitbucket.org:samiulazim/neovim.git"] = "https://bitbucket.org/samiulazim/neovim",
["git@gitlab.com:inkscape/inkscape.git"] = "https://gitlab.com/inkscape/inkscape",
["https://gitlab.com/inkscape/inkscape.git"] = "https://gitlab.com/inkscape/inkscape",
["git@github.com:torvalds/linux.git"] = "https://github.com/torvalds/linux",
["https://github.com/torvalds/linux.git"] = "https://github.com/torvalds/linux",
["git@bitbucket.org:team/repo.git"] = "https://bitbucket.org/team/repo",
["https://bitbucket.org/team/repo.git"] = "https://bitbucket.org/team/repo",
["git@gitlab.com:example-group/example-project.git"] = "https://gitlab.com/example-group/example-project",
["https://gitlab.com/example-group/example-project.git"] = "https://gitlab.com/example-group/example-project",
["git@ssh.dev.azure.com:v3/org/project/repo"] = "https://dev.azure.com/org/project/_git/repo",
["https://username@dev.azure.com/org/project/_git/repo"] = "https://dev.azure.com/org/project/_git/repo",
["ssh://git@ghe.example.com:2222/org/repo.git"] = "https://ghe.example.com/org/repo",
["https://ghe.example.com/org/repo.git"] = "https://ghe.example.com/org/repo",
["git-codecommit.us-east-1.amazonaws.com/v1/repos/MyDemoRepo"] = "https://git-codecommit.us-east-1.amazonaws.com/v1/repos/MyDemoRepo",
["https://git-codecommit.us-east-1.amazonaws.com/v1/repos/MyDemoRepo"] = "https://git-codecommit.us-east-1.amazonaws.com/v1/repos/MyDemoRepo",
["ssh://git@source.developers.google.com:2022/p/project/r/repo"] = "https://source.developers.google.com/p/project/r/repo",
["https://source.developers.google.com/p/project/r/repo"] = "https://source.developers.google.com/p/project/r/repo",
["git@git.sr.ht:~user/repo"] = "https://git.sr.ht/~user/repo",
["https://git.sr.ht/~user/repo"] = "https://git.sr.ht/~user/repo",
["git@git.sr.ht:~user/another-repo"] = "https://git.sr.ht/~user/another-repo",
["https://git.sr.ht/~user/another-repo"] = "https://git.sr.ht/~user/another-repo",
}
describe("util.lazygit", function()
for remote, expected in pairs(git_remotes_cases) do
it("should parse git remote " .. remote, function()
local url = LazyVim.lazygit.get_url(remote)
assert.are.equal(expected, url)
end)
end
end)