Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,12 @@ Persistence comes with the following defaults:
-- minimum number of file buffers that need to be open to save
-- Set to 0 to always save
need = 1,
branch = true, -- use git branch to save session

-- use git branch to save session (true, false, or "any")
-- false: single session per directory, not branch-specific
-- true (default): branch-specific session, won't fall back to other branches
-- "any": branch-specific session, falls back to any other branch
branch = true,
}
```

Expand Down
7 changes: 6 additions & 1 deletion lua/persistence/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ local defaults = {
-- minimum number of file buffers that need to be open to save
-- Set to 0 to always save
need = 1,
branch = true, -- use git branch to save session

-- use git branch to save session (true, false, or "any")
-- false: single session per directory, not branch-specific
-- true (default): branch-specific session, won't fall back to other branches
-- "any": branch-specific session, falls back to any other branch
branch = true,
}

---@type Persistence.Config
Expand Down
28 changes: 27 additions & 1 deletion lua/persistence/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,17 @@ end
---@param opts? { last?: boolean }
function M.load(opts)
opts = opts or {}
---@type string
---@type string?
local file
if opts.last then
file = M.last()
else
file = M.current()
if vim.fn.filereadable(file) == 0 then
file = M.current({ branch = false })
if vim.fn.filereadable(file) == 0 and Config.options.branch == "any" then
file = M.find_any_branch_session()
end
end
end
if file and vim.fn.filereadable(file) ~= 0 then
Expand Down Expand Up @@ -134,6 +137,29 @@ function M.select()
end)
end

---@return string?
function M.find_any_branch_session()
local cwd_name = vim.fn.getcwd():gsub("[\\/:]+", "%%")
local pattern = Config.options.dir .. cwd_name .. "%%*.vim"
local candidates = vim.fn.glob(pattern, true, true)

if #candidates == 0 then
return nil
end

-- Sort by modification time (most recent first)
table.sort(candidates, function(a, b)
local stat_a = uv.fs_stat(a)
local stat_b = uv.fs_stat(b)
if not stat_a or not stat_b then
return false
end
return stat_a.mtime.sec > stat_b.mtime.sec
end)

return candidates[1]
end

--- get current branch name
---@return string?
function M.branch()
Expand Down