diff --git a/lua/lazyvim/util/lazygit.lua b/lua/lazyvim/util/lazygit.lua index 2e030cb6..2532ad08 100644 --- a/lua/lazyvim/util/lazygit.lua +++ b/lua/lazyvim/util/lazygit.lua @@ -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 diff --git a/tests/util/lazygit_spec.lua b/tests/util/lazygit_spec.lua new file mode 100644 index 00000000..518e886a --- /dev/null +++ b/tests/util/lazygit_spec.lua @@ -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)