From 1f034e16000692d1d0ec8e02110d37cf2089bd4c Mon Sep 17 00:00:00 2001 From: morland Date: Tue, 18 Jun 2024 03:52:40 +1000 Subject: [PATCH] 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 `/.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. --- lua/lazyvim/util/lazygit.lua | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/lua/lazyvim/util/lazygit.lua b/lua/lazyvim/util/lazygit.lua index 1786eefd..b74e987d 100644 --- a/lua/lazyvim/util/lazygit.lua +++ b/lua/lazyvim/util/lazygit.lua @@ -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