diff --git a/lua/lib/git.lua b/lua/lib/git.lua index 9aadef01..356f880a 100644 --- a/lua/lib/git.lua +++ b/lua/lib/git.lua @@ -1,3 +1,4 @@ +local utils = require'lib.utils' local M = {} local roots = {} @@ -26,10 +27,6 @@ function M.reload_roots() end end -local function path_to_matching_str(path) - return path:gsub('(%-)', '(%%-)'):gsub('(%.)', '(%%.)') -end - local function get_git_root(path) if roots[path] then return path, roots[path] @@ -37,7 +34,7 @@ local function get_git_root(path) for name, status in pairs(roots) do if status ~= not_git then - if path:match(path_to_matching_str(name)) then + if path:match(utils.path_to_matching_str(name)) then return name, status end end @@ -65,7 +62,7 @@ function M.update_status(entries, cwd) return end - local matching_cwd = path_to_matching_str(git_root..'/') + local matching_cwd = utils.path_to_matching_str(git_root..'/') for _, node in pairs(entries) do local relpath = node.absolute_path:gsub(matching_cwd, '') if node.entries ~= nil then @@ -77,7 +74,7 @@ function M.update_status(entries, cwd) if status then node.git_status = status elseif node.entries ~= nil then - local matcher = '^'..path_to_matching_str(relpath) + local matcher = '^'..utils.path_to_matching_str(relpath) for key, _ in pairs(git_status) do if key:match(matcher) then node.git_status = 'dirty' diff --git a/lua/lib/populate.lua b/lua/lib/populate.lua index f07eeabe..bfeef983 100644 --- a/lua/lib/populate.lua +++ b/lua/lib/populate.lua @@ -7,9 +7,7 @@ local luv = vim.loop local M = {} -local function path_to_matching_str(path) - return path:gsub('(%-)', '(%%-)'):gsub('(%.)', '(%%.)') -end +local path_to_matching_str = require'lib.utils'.path_to_matching_str local function dir_new(cwd, name) local absolute_path = cwd..'/'..name diff --git a/lua/lib/utils.lua b/lua/lib/utils.lua new file mode 100644 index 00000000..6147c7d2 --- /dev/null +++ b/lua/lib/utils.lua @@ -0,0 +1,7 @@ +local M = {} + +function M.path_to_matching_str(path) + return path:gsub('(%-)', '(%%-)'):gsub('(%.)', '(%%.)'):gsub('(%_)', '(%%_)') +end + +return M