Neovim plugin to automatically adjust
git env vars when syncing dotfiles using the "bare git repo" method
1local Scan = {}
2
3---@type BaredotConfig
4local options
5
6-- Modified from ahmedkhalf/project.nvim
7local function get_parent(path)
8 path = path:match("^(.*)/")
9 if path == "" then
10 path = "/"
11 end
12 return path
13end
14
15local function get_files(file_dir)
16 local files = {}
17 local dir = vim.loop.fs_scandir(file_dir)
18 if dir == nil then
19 return files
20 end
21
22 while true do
23 local file = vim.loop.fs_scandir_next(dir)
24 if file == nil then
25 return files
26 end
27
28 table.insert(files, file)
29 end
30end
31
32local function has(dir, pattern)
33 for _, file in ipairs(get_files(dir)) do
34 if file:match(pattern) ~= nil then
35 return true
36 end
37 end
38 return false
39end
40
41function Scan.in_work_tree_no_dot_git()
42 local search_dir = vim.fn.getcwd()
43 local work_tree_root = options.git_work_tree
44 if vim.fn.has("win32") > 0 then
45 search_dir = search_dir:gsub("\\", "/")
46 work_tree_root = work_tree_root:gsub("\\", "/")
47 end
48
49 while true do
50 if search_dir == work_tree_root then
51 return true
52 end
53
54 local pattern = options.disable_pattern
55 if has(search_dir, pattern) then
56 return false
57 end
58
59 local parent = get_parent(search_dir)
60 if parent == nil or parent == search_dir then
61 return false
62 end
63
64 search_dir = parent
65 end
66end
67
68function Scan.setup(opt)
69 options = opt
70end
71
72return Scan