fix(lazygit): support monorepo structure and other git providers (#3701)

## What is this PR for?
- The current implementation doesn't support multi-module/monorepo
project where `<repo>/.git` can be `gitdir` alias to the main `.git`
folder. This PR uses the Git CLI to resolve remote URLs to better
support flexible project structure.
- This PR also adds the support to Bitbucket and GitLab

What's the problem of reading `.git/config`?
- Not all information available there. For instance, some large projects
will split a monorepo into smaller submodules, where `.git` is a file
that contains the `gitdir` alias.
- There's no promise that `.git/config` has to be existed. Git supports
multiple ways to store the git info outside of the default git directory
like using `GIT_DIR` env variable.
- Have to do lot of reading and parsing logic.

Why `git remote -v`?
- Only contains remote info with explicit format. 
- Don't have to filter out other config info. 
- Don't have to deal with lots of weird edge cases.



## Does this PR fix an existing issue?
No.

## Checklist
- [x] I've read the
[CONTRIBUTING](https://github.com/LazyVim/LazyVim/blob/main/CONTRIBUTING.md)
guidelines.
This commit is contained in:
morland
2024-06-18 03:52:40 +10:00
committed by GitHub
parent a69e626d7a
commit 1f034e1600

View File

@ -160,13 +160,17 @@ function M.blame_line(opts)
end
function M.browse()
local config = require("lazy.manage.git").get_config(LazyVim.root.detectors.pattern(0, { ".git" })[1])
local lines = require("lazy.manage.process").exec({ "git", "remote", "-v" })
local remotes = {} ---@type {name:string, url:string}[]
for name, url in pairs(config) do
name = name:match("^remote%.(.-)%.url$")
if name then
url = url:gsub("git@github.com:", "https://github.com/"):gsub("%.git$", "") --[[@as 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@github.com") or url:find("git@bitbucket.org") or url:find("git@gitlab.com") then
url = url:gsub("git@(%S+):", "https://%1/"):gsub(".git$", "")
end
table.insert(remotes, { name = name, url = url })
LazyVim.info(("Found remote %s %s)"):format(name, url))
end
end