From 2db6a138cd583dd0ece3501d2b9ccc2b143a9001 Mon Sep 17 00:00:00 2001 From: AVGVSTVS96 <122117267+AVGVSTVS96@users.noreply.github.com> Date: Fri, 3 Oct 2025 16:38:10 -0700 Subject: [PATCH] feat: add `branch="any"` option to configure fallback behavior This allows persistence to fallback to another branch's session if the current branch doesn't have an available session. --- README.md | 7 ++++++- lua/persistence/config.lua | 7 ++++++- lua/persistence/init.lua | 28 +++++++++++++++++++++++++++- 3 files changed, 39 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index e338c4f..9edadb4 100644 --- a/README.md +++ b/README.md @@ -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, } ``` diff --git a/lua/persistence/config.lua b/lua/persistence/config.lua index 1589857..c937860 100644 --- a/lua/persistence/config.lua +++ b/lua/persistence/config.lua @@ -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 diff --git a/lua/persistence/init.lua b/lua/persistence/init.lua index 760f6b6..0a89a69 100644 --- a/lua/persistence/init.lua +++ b/lua/persistence/init.lua @@ -73,7 +73,7 @@ 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() @@ -81,6 +81,9 @@ function M.load(opts) 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 @@ -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()