Skip to content

Commit 03990e4

Browse files
authored
fix: do not pass sanitized directories into utils.dirs_match (#148)
1 parent b0de69d commit 03990e4

File tree

2 files changed

+12
-16
lines changed

2 files changed

+12
-16
lines changed

Diff for: lua/persisted/init.lua

+4-9
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,18 @@ function M.fire(event)
1212
vim.api.nvim_exec_autocmds("User", { pattern = "Persisted" .. event })
1313
end
1414

15-
---Get the current working directory
16-
---@return string
17-
function M.cwd()
18-
return utils.sanitize_dir(vim.fn.getcwd())
19-
end
20-
2115
---Get the current session for the current working directory and git branch
2216
---@param opts? {branch?: boolean}
2317
---@return string
2418
function M.current(opts)
2519
opts = opts or {}
26-
local name = M.cwd()
20+
local name = utils.make_fs_safe(vim.fn.getcwd())
2721

2822
if config.use_git_branch and opts.branch ~= false then
2923
local branch = M.branch()
3024
if branch then
31-
name = name .. "@@" .. branch:gsub("[\\/:]+", "%%")
25+
branch = utils.make_fs_safe(branch)
26+
name = name .. "@@" .. branch
3227
end
3328
end
3429

@@ -164,7 +159,7 @@ function M.allowed_dir(opts)
164159
end
165160

166161
opts = opts or {}
167-
local dir = opts.dir or M.cwd()
162+
local dir = opts.dir or vim.fn.getcwd()
168163

169164
return utils.dirs_match(dir, config.allowed_dirs) and not utils.dirs_match(dir, config.ignored_dirs)
170165
end

Diff for: lua/persisted/utils.lua

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
local M = {}
22

3-
--- Escape special pattern matching characters in a string
4-
---@param dir string
5-
function M.sanitize_dir(dir)
6-
return dir:gsub("[\\/:]+", "%%")
3+
--- Escapes the given text to be safe for use in file-system paths/names,
4+
--- accounting for cross-platform use.
5+
---@param text string
6+
function M.make_fs_safe(text)
7+
return text:gsub("[\\/:]+", "%%")
78
end
89

910
---Get the directory pattern based on OS
@@ -29,17 +30,17 @@ end
2930
---@param dirs table The table of directories to search in
3031
---@return boolean
3132
function M.dirs_match(dir, dirs)
32-
dir = M.sanitize_dir(vim.fn.expand(dir))
33+
dir = M.make_fs_safe(vim.fn.expand(dir))
3334

3435
for _, search in ipairs(dirs) do
3536
if type(search) == "string" then
36-
search = M.sanitize_dir(vim.fn.expand(search))
37+
search = M.make_fs_safe(vim.fn.expand(search))
3738
if M.is_subdirectory(search, dir) then
3839
return true
3940
end
4041
elseif type(search) == "table" then
4142
if search.exact then
42-
search = M.sanitize_dir(vim.fn.expand(search[1]))
43+
search = M.make_fs_safe(vim.fn.expand(search[1]))
4344
if dir == search then
4445
return true
4546
end

0 commit comments

Comments
 (0)