mirror of
https://github.com/LazyVim/LazyVim.git
synced 2026-07-25 14:01:06 +00:00
fix(lazygit): improve git browse
This commit is contained in:
parent
427b641eb8
commit
f3ed1a78b3
3 changed files with 94 additions and 3 deletions
|
|
@ -31,6 +31,16 @@ 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,17 +159,65 @@ 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
|
||||
|
||||
---@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*)(.*)/([^/]+)$")
|
||||
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,
|
||||
}
|
||||
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
|
||||
if url:find("git@") == 1 then
|
||||
url = url:gsub("git@(%S+):", "https://%1/"):gsub(".git$", "")
|
||||
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)
|
||||
|
||||
table.insert(remotes, {
|
||||
name = name,
|
||||
url = browse_url,
|
||||
})
|
||||
end
|
||||
table.insert(remotes, { name = name, url = url })
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
|||
33
tests/util/lazygit_spec.lua
Normal file
33
tests/util/lazygit_spec.lua
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
---@module "luassert"
|
||||
|
||||
local LazyVim = require("lazyvim.util")
|
||||
|
||||
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",
|
||||
},
|
||||
}
|
||||
|
||||
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)
|
||||
end)
|
||||
Loading…
Add table
Reference in a new issue