fix: #120 session loading for branches with slashes

main
Oli 2024-05-03 10:09:50 +01:00 committed by GitHub
parent 96bf597778
commit 9c5fc98a4c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 20 additions and 15 deletions

View File

@ -29,32 +29,28 @@ end
---Form a table of session data ---Form a table of session data
---@param session string ---@param session string
---@return table ---@return table|nil
function M.make_session_data(session) function M.make_session_data(session)
local config = require("persisted.config").options local config = require("persisted.config").options
local home local home = os.getenv("HOME") or os.getenv("USERPROFILE") or ""
if os.getenv("HOME") then
home = os.getenv("HOME") -- Unix-based systems (Linux, macOS) -- Split the session string into path and branch parts
elseif os.getenv("USERPROFILE") then local separator_index = session:find(config.branch_separator)
home = os.getenv("USERPROFILE") -- Windows if not separator_index then
else return nil
home = ""
end end
-- Form the branch local branch = session:sub(separator_index + 2):gsub("%.vim$", ""):gsub("%%", "/")
local pattern = config.branch_separator .. "(.-)%.vim"
local branch = session:match(pattern) or ""
-- Form the name -- Removing the home directory from the path and cleaning leading `/`
local name = session:gsub(config.save_dir, ""):gsub("%%", "/"):gsub(home, "") local name = session:gsub(config.save_dir, ""):gsub("%%", "/"):gsub(home, "")
name = name:sub(1, #name - 4) -- Remove the .vim extension -- Remove the .vim extension
name = name:sub(1, #name - 4)
if name:sub(1, 1) == "/" then if name:sub(1, 1) == "/" then
name = name:sub(2) name = name:sub(2)
end end
-- Form the dir_path
local dir_path = name:gsub(branch, ""):gsub(config.branch_separator, ""):gsub(home, "") local dir_path = name:gsub(branch, ""):gsub(config.branch_separator, ""):gsub(home, "")
return { return {

View File

@ -66,3 +66,12 @@ async.describe("With default settings:", function()
assert.equals("0", vim.fn.system("ls tests/default_data | wc -l"):gsub("%s+", "")) assert.equals("0", vim.fn.system("ls tests/default_data | wc -l"):gsub("%s+", ""))
end) end)
end) end)
describe("Utilities", function()
it("can make derive the session name", function()
local session = "%home%username%projects%front@@user%fix-analytics-export-null-values.vim"
local data = require("persisted.utils").make_session_data(session)
assert.equals("home/username/projects/front@@user/fix-analytics-export-null-values", data.name)
end)
end)