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
|
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
|
-- Optionally setup the terminal to use
|
||||||
-- This sets `vim.o.shell` and does some additional configuration for:
|
-- This sets `vim.o.shell` and does some additional configuration for:
|
||||||
-- * pwsh
|
-- * pwsh
|
||||||
|
|
|
||||||
|
|
@ -159,63 +159,40 @@ function M.blame_line(opts)
|
||||||
return require("lazy.util").float_cmd(cmd, opts)
|
return require("lazy.util").float_cmd(cmd, opts)
|
||||||
end
|
end
|
||||||
|
|
||||||
---@class GitRemote
|
M.remote_patterns = {
|
||||||
---@field host string
|
{ "^(https?://.*)%.git$", "%1" },
|
||||||
---@field owner string
|
{ "^git@(.+):(.+)%.git$", "https://%1/%2" },
|
||||||
---@field repo string
|
{ "^git@(.+):(.+)$", "https://%1/%2" },
|
||||||
---@field scheme string
|
{ "^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
|
---@param remote string
|
||||||
---@return GitRemote
|
function M.get_url(remote)
|
||||||
function M.parse_remote_url(url)
|
local ret = remote
|
||||||
local scheme, host, owner, repo
|
for _, pattern in ipairs(M.remote_patterns) do
|
||||||
scheme = "ssh"
|
ret = ret:gsub(pattern[1], pattern[2])
|
||||||
-- 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*)(.*)/([^/]+)$")
|
|
||||||
end
|
end
|
||||||
|
return ret:find("https://") == 1 and ret or ("https://%s"):format(ret)
|
||||||
-- 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,
|
|
||||||
}
|
|
||||||
end
|
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}[]
|
||||||
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
|
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
|
||||||
local remote = M.parse_remote_url(url)
|
local url = M.get_url(remote)
|
||||||
local git_browse_url = get_remote_template(remote.host)
|
if url then
|
||||||
if git_browse_url then
|
|
||||||
local browse_url = git_browse_url:gsub("($%b{})", function(m)
|
|
||||||
return remote[m:sub(3, -2)] or m
|
|
||||||
end)
|
|
||||||
|
|
||||||
table.insert(remotes, {
|
table.insert(remotes, {
|
||||||
name = name,
|
name = name,
|
||||||
url = browse_url,
|
url = url,
|
||||||
})
|
})
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -1,33 +1,42 @@
|
||||||
---@module "luassert"
|
---@module "luassert"
|
||||||
|
|
||||||
local LazyVim = require("lazyvim.util")
|
_G.LazyVim = require("lazyvim.util")
|
||||||
|
|
||||||
|
-- stylua: ignore
|
||||||
local git_remotes_cases = {
|
local git_remotes_cases = {
|
||||||
["https://github.com/lazyvim/lazyvim.git"] = {
|
["https://github.com/LazyVim/LazyVim.git"] = "https://github.com/LazyVim/LazyVim",
|
||||||
scheme = "https",
|
["https://github.com/LazyVim/LazyVim"] = "https://github.com/LazyVim/LazyVim",
|
||||||
host = "github.com",
|
["git@github.com:LazyVim/LazyVim"] = "https://github.com/LazyVim/LazyVim",
|
||||||
owner = "lazyvim",
|
["git@ssh.dev.azure.com:v3/neovim-org/owner/repo"] = "https://dev.azure.com/neovim-org/owner/_git/repo",
|
||||||
repo = "lazyvim.git",
|
["https://folkelemaitre@bitbucket.org/samiulazim/neovim.git"] = "https://bitbucket.org/samiulazim/neovim",
|
||||||
},
|
["git@bitbucket.org:samiulazim/neovim.git"] = "https://bitbucket.org/samiulazim/neovim",
|
||||||
["git@github.com/lazyvim/lazyvim"] = {
|
["git@gitlab.com:inkscape/inkscape.git"] = "https://gitlab.com/inkscape/inkscape",
|
||||||
scheme = "ssh",
|
["https://gitlab.com/inkscape/inkscape.git"] = "https://gitlab.com/inkscape/inkscape",
|
||||||
host = "github.com",
|
["git@github.com:torvalds/linux.git"] = "https://github.com/torvalds/linux",
|
||||||
owner = "lazyvim",
|
["https://github.com/torvalds/linux.git"] = "https://github.com/torvalds/linux",
|
||||||
repo = "lazyvim",
|
["git@bitbucket.org:team/repo.git"] = "https://bitbucket.org/team/repo",
|
||||||
},
|
["https://bitbucket.org/team/repo.git"] = "https://bitbucket.org/team/repo",
|
||||||
["git@ssh.dev.azure.com:v3/neovim-org/owner/repo"] = {
|
["git@gitlab.com:example-group/example-project.git"] = "https://gitlab.com/example-group/example-project",
|
||||||
scheme = "ssh",
|
["https://gitlab.com/example-group/example-project.git"] = "https://gitlab.com/example-group/example-project",
|
||||||
host = "ssh.dev.azure.com",
|
["git@ssh.dev.azure.com:v3/org/project/repo"] = "https://dev.azure.com/org/project/_git/repo",
|
||||||
owner = "neovim-org/owner",
|
["https://username@dev.azure.com/org/project/_git/repo"] = "https://dev.azure.com/org/project/_git/repo",
|
||||||
repo = "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()
|
describe("util.lazygit", function()
|
||||||
it("should parse git remotes", function()
|
for remote, expected in pairs(git_remotes_cases) do
|
||||||
for url, expected_remote in pairs(git_remotes_cases) do
|
it("should parse git remote " .. remote, function()
|
||||||
local remote = LazyVim.lazygit.parse_remote_url(url)
|
local url = LazyVim.lazygit.get_url(remote)
|
||||||
assert.are.equal(expected_remote, remote)
|
assert.are.equal(expected, url)
|
||||||
end
|
end)
|
||||||
end)
|
end
|
||||||
end)
|
end)
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue