feat(util): add find_git_root function

Add utility function to traverse up directory tree and find git repository root by checking for .git directory.
This commit is contained in:
林玮 (Jade Lin) 2025-07-01 21:54:25 +08:00
commit 6db4274a36
No known key found for this signature in database
GPG key ID: 8F30F127640DE1CC

View file

@ -228,6 +228,17 @@ function M.walk(path, fn)
end)
end
---@param path string
function M.find_git_root(path)
local current = path
while current and current ~= "/" do
if vim.fn.isdirectory(current .. "/.git") == 1 then
return current
end
current = vim.fn.fnamemodify(current, ":h")
end
end
---@param root string
---@param fn fun(modname:string, modpath:string)
---@param modname? string