Skip to content

Commit a39fa0f

Browse files
committed
feat(git): added fast Git.get_origin and Git.get_config
1 parent 8a37547 commit a39fa0f

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

lua/lazy/manage/git.lua

+32
Original file line numberDiff line numberDiff line change
@@ -193,4 +193,36 @@ function M.get_tag_refs(repo, tagref)
193193
return tags
194194
end
195195

196+
---@param repo string
197+
function M.get_origin(repo)
198+
return M.get_config(repo)["remote.origin.url"]
199+
end
200+
201+
---@param repo string
202+
function M.get_config(repo)
203+
local ok, config = pcall(Util.read_file, repo .. "/.git/config")
204+
if not ok then
205+
return {}
206+
end
207+
---@type table<string, string>
208+
local ret = {}
209+
---@type string
210+
local current_section = nil
211+
for line in config:gmatch("[^\n]+") do
212+
-- Check if the line is a section header
213+
local section = line:match("^%s*%[(.+)%]%s*$")
214+
if section then
215+
---@type string
216+
current_section = section:gsub('%s+"', "."):gsub('"+%s*$', "")
217+
else
218+
-- Ignore comments and blank lines
219+
if not line:match("^%s*#") and line:match("%S") then
220+
local key, value = line:match("^%s*(%S+)%s*=%s*(.+)%s*$")
221+
ret[current_section .. "." .. key] = value
222+
end
223+
end
224+
end
225+
return ret
226+
end
227+
196228
return M

tests/core/e2e_spec.lua

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
local Git = require("lazy.manage.git")
2+
13
describe("lazy", function()
24
before_each(function()
35
vim.g.lazy_did_setup = false
@@ -31,5 +33,7 @@ describe("lazy", function()
3133
assert(not neodev)
3234
assert(Config.plugins["neodev.nvim"]._.installed)
3335
assert(not Config.plugins["neodev.nvim"]._.is_local)
36+
assert.equal("https://github.com/folke/neodev.nvim.git", Git.get_origin(Config.plugins["neodev.nvim"].dir))
37+
assert.equal("https://github.com/folke/paint.nvim.git", Git.get_origin(Config.plugins["paint.nvim"].dir))
3438
end)
3539
end)

0 commit comments

Comments
 (0)