feat(snacks): use gitbrowse

This commit is contained in:
Folke Lemaitre 2024-11-04 12:14:02 +01:00
parent ad698dd0aa
commit 7ec2d7f22d
No known key found for this signature in database
GPG key ID: 41F8B1FBACAE2040
4 changed files with 1 additions and 113 deletions

View file

@ -133,7 +133,7 @@ end
map("n", "<leader>gg", function() Snacks.lazygit( { cwd = LazyVim.root.git() }) end, { desc = "Lazygit (Root Dir)" })
map("n", "<leader>gG", function() Snacks.lazygit() end, { desc = "Lazygit (cwd)" })
map("n", "<leader>gb", function() Snacks.git.blame_line() end, { desc = "Git Blame Line" })
map("n", "<leader>gB", LazyVim.lazygit.browse, { desc = "Git Browse" })
map("n", "<leader>gB", function() Snacks.gitbrowse() end, { desc = "Git Browse" })
map("n", "<leader>gf", function() Snacks.lazygit.log_file() end, { desc = "Lazygit Current File History" })
map("n", "<leader>gl", function() Snacks.lazygit.log({ cwd = LazyVim.root.git() }) end, { desc = "Lazygit Log" })
map("n", "<leader>gL", function() Snacks.lazygit.log() end, { desc = "Lazygit Log (cwd)" })

View file

@ -6,7 +6,6 @@ local LazyUtil = require("lazy.core.util")
---@field lsp lazyvim.util.lsp
---@field root lazyvim.util.root
---@field terminal lazyvim.util.terminal
---@field lazygit lazyvim.util.lazygit
---@field toggle lazyvim.util.toggle
---@field format lazyvim.util.format
---@field plugin lazyvim.util.plugin

View file

@ -1,69 +0,0 @@
---@class lazyvim.util.lazygit
local M = {}
-- 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, 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
end
end
local function open(remote)
if remote then
LazyVim.info(("Opening [%s](%s)"):format(remote.name, remote.url))
if vim.fn.has("nvim-0.10") == 0 then
require("lazy.util").open(remote.url, { system = true })
return
end
vim.ui.open(remote.url)
end
end
if #remotes == 0 then
return LazyVim.error("No git remotes found")
elseif #remotes == 1 then
return open(remotes[1])
end
vim.ui.select(remotes, {
prompt = "Select remote to browse",
format_item = function(item)
return item.name .. (" "):rep(8 - #item.name) .. " 🔗 " .. item.url
end,
}, open)
end
return M

View file

@ -1,42 +0,0 @@
---@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)