From a39fa0f0ced7324800eff0a4eb0ed68bf13452d1 Mon Sep 17 00:00:00 2001 From: Folke Lemaitre Date: Sun, 8 Jan 2023 08:04:34 +0100 Subject: [PATCH] feat(git): added fast `Git.get_origin` and `Git.get_config` --- lua/lazy/manage/git.lua | 32 ++++++++++++++++++++++++++++++++ tests/core/e2e_spec.lua | 4 ++++ 2 files changed, 36 insertions(+) diff --git a/lua/lazy/manage/git.lua b/lua/lazy/manage/git.lua index f104c5ee..76f7009f 100644 --- a/lua/lazy/manage/git.lua +++ b/lua/lazy/manage/git.lua @@ -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 + 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 diff --git a/tests/core/e2e_spec.lua b/tests/core/e2e_spec.lua index db591551..a7d02fc5 100644 --- a/tests/core/e2e_spec.lua +++ b/tests/core/e2e_spec.lua @@ -1,3 +1,5 @@ +local Git = require("lazy.manage.git") + describe("lazy", function() before_each(function() vim.g.lazy_did_setup = false @@ -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)