Skip to content

Commit

Permalink
feat(git): added fast Git.get_origin and Git.get_config
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed Jan 8, 2023
1 parent 8a37547 commit a39fa0f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
32 changes: 32 additions & 0 deletions lua/lazy/manage/git.lua
Original file line number Diff line number Diff line change
Expand Up @@ -193,4 +193,36 @@ function M.get_tag_refs(repo, tagref)
return tags
end

---@param repo string
function M.get_origin(repo)
return M.get_config(repo)["remote.origin.url"]
end

---@param repo string
function M.get_config(repo)
local ok, config = pcall(Util.read_file, repo .. "/.git/config")
if not ok then
return {}
end
---@type table<string, string>
local ret = {}
---@type string
local current_section = nil
for line in config:gmatch("[^\n]+") do
-- Check if the line is a section header
local section = line:match("^%s*%[(.+)%]%s*$")
if section then
---@type string
current_section = section:gsub('%s+"', "."):gsub('"+%s*$', "")
else
-- Ignore comments and blank lines
if not line:match("^%s*#") and line:match("%S") then
local key, value = line:match("^%s*(%S+)%s*=%s*(.+)%s*$")
ret[current_section .. "." .. key] = value
end
end
end
return ret
end

return M
4 changes: 4 additions & 0 deletions tests/core/e2e_spec.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
local Git = require("lazy.manage.git")

describe("lazy", function()
before_each(function()
vim.g.lazy_did_setup = false
Expand Down Expand Up @@ -31,5 +33,7 @@ describe("lazy", function()
assert(not neodev)
assert(Config.plugins["neodev.nvim"]._.installed)
assert(not Config.plugins["neodev.nvim"]._.is_local)
assert.equal("https://github.com/folke/neodev.nvim.git", Git.get_origin(Config.plugins["neodev.nvim"].dir))
assert.equal("https://github.com/folke/paint.nvim.git", Git.get_origin(Config.plugins["paint.nvim"].dir))
end)
end)

0 comments on commit a39fa0f

Please sign in to comment.