mirror of
https://github.com/LazyVim/LazyVim.git
synced 2026-07-25 14:01:06 +00:00
refactor: use patterns instead
This commit is contained in:
parent
24faa32603
commit
6ba1ae87e4
3 changed files with 57 additions and 81 deletions
|
|
@ -31,16 +31,6 @@ vim.g.lazyvim_statuscolumn = {
|
|||
folds_githl = false, -- highlight fold sign with git sign color
|
||||
}
|
||||
|
||||
-- URL Templates for git browse by host, with given placeholders
|
||||
-- * owner: git repo owner
|
||||
-- * repo: git repo name
|
||||
vim.g.lazygit_git_browse = {
|
||||
["azure.com"] = "https://dev.azure.com/${owner}/_git/${repo}",
|
||||
["bitbucket.org"] = "https://bitbucket.org/${owner}/${repo}",
|
||||
["github.com"] = "https://github.com/${owner}/${repo}",
|
||||
["gitlab.com"] = "https://gitlab.com/${owner}/${repo}",
|
||||
}
|
||||
|
||||
-- Optionally setup the terminal to use
|
||||
-- This sets `vim.o.shell` and does some additional configuration for:
|
||||
-- * pwsh
|
||||
|
|
|
|||
|
|
@ -159,63 +159,40 @@ function M.blame_line(opts)
|
|||
return require("lazy.util").float_cmd(cmd, opts)
|
||||
end
|
||||
|
||||
---@class GitRemote
|
||||
---@field host string
|
||||
---@field owner string
|
||||
---@field repo string
|
||||
---@field scheme string
|
||||
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 url string
|
||||
---@return GitRemote
|
||||
function M.parse_remote_url(url)
|
||||
local scheme, host, owner, repo
|
||||
scheme = "ssh"
|
||||
-- handle ssh format: user@host:path/repo.git
|
||||
_, host, owner, repo = url:match("^([^@]+)@([^:]+):(.+)/([^/]+)$")
|
||||
-- handle http(s) format: scheme://host[:port]/path/repo.git
|
||||
if not host then
|
||||
scheme, host, _, owner, repo = url:match("^(%w+)://([^:/]+):?(%d*)(.*)/([^/]+)$")
|
||||
---@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
|
||||
|
||||
-- handle azure.com v3/ prefix
|
||||
if host and host:sub(-#"azure.com") == "azure.com" then
|
||||
owner = owner:gsub("^v3/", "")
|
||||
end
|
||||
|
||||
return {
|
||||
host = host,
|
||||
owner = owner,
|
||||
repo = repo,
|
||||
scheme = scheme,
|
||||
}
|
||||
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}[]
|
||||
local git_browse_urls = vim.g.lazygit_git_browse or {}
|
||||
|
||||
local function get_remote_template(remote_host)
|
||||
for host, browse_url in pairs(git_browse_urls) do
|
||||
if remote_host:sub(-#host) == host then
|
||||
return browse_url
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
for _, line in ipairs(lines) do
|
||||
local name, url = line:match("(%S+)%s+(%S+)%s+%(fetch%)")
|
||||
if name and url then
|
||||
local remote = M.parse_remote_url(url)
|
||||
local git_browse_url = get_remote_template(remote.host)
|
||||
if git_browse_url then
|
||||
local browse_url = git_browse_url:gsub("($%b{})", function(m)
|
||||
return remote[m:sub(3, -2)] or m
|
||||
end)
|
||||
|
||||
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 = browse_url,
|
||||
url = url,
|
||||
})
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -1,33 +1,42 @@
|
|||
---@module "luassert"
|
||||
|
||||
local LazyVim = require("lazyvim.util")
|
||||
_G.LazyVim = require("lazyvim.util")
|
||||
|
||||
-- stylua: ignore
|
||||
local git_remotes_cases = {
|
||||
["https://github.com/lazyvim/lazyvim.git"] = {
|
||||
scheme = "https",
|
||||
host = "github.com",
|
||||
owner = "lazyvim",
|
||||
repo = "lazyvim.git",
|
||||
},
|
||||
["git@github.com/lazyvim/lazyvim"] = {
|
||||
scheme = "ssh",
|
||||
host = "github.com",
|
||||
owner = "lazyvim",
|
||||
repo = "lazyvim",
|
||||
},
|
||||
["git@ssh.dev.azure.com:v3/neovim-org/owner/repo"] = {
|
||||
scheme = "ssh",
|
||||
host = "ssh.dev.azure.com",
|
||||
owner = "neovim-org/owner",
|
||||
repo = "repo",
|
||||
},
|
||||
["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()
|
||||
it("should parse git remotes", function()
|
||||
for url, expected_remote in pairs(git_remotes_cases) do
|
||||
local remote = LazyVim.lazygit.parse_remote_url(url)
|
||||
assert.are.equal(expected_remote, remote)
|
||||
end
|
||||
end)
|
||||
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)
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue