feat: Add ability to ignore branches (#114)
parent
18fda8136f
commit
ce9d621683
14
README.md
14
README.md
|
|
@ -161,6 +161,7 @@ require("persisted").setup({
|
||||||
follow_cwd = true, -- change session file name to match current working directory if it changes
|
follow_cwd = true, -- change session file name to match current working directory if it changes
|
||||||
allowed_dirs = nil, -- table of dirs that the plugin will auto-save and auto-load from
|
allowed_dirs = nil, -- table of dirs that the plugin will auto-save and auto-load from
|
||||||
ignored_dirs = nil, -- table of dirs that are ignored when auto-saving and auto-loading
|
ignored_dirs = nil, -- table of dirs that are ignored when auto-saving and auto-loading
|
||||||
|
ignored_branches = nil, -- table of branch patterns that are ignored for auto-saving and auto-loading
|
||||||
telescope = {
|
telescope = {
|
||||||
reset_prompt = true, -- Reset the Telescope prompt after an action?
|
reset_prompt = true, -- Reset the Telescope prompt after an action?
|
||||||
mappings = { -- table of mappings for the Telescope extension
|
mappings = { -- table of mappings for the Telescope extension
|
||||||
|
|
@ -322,6 +323,19 @@ require("persisted").setup({
|
||||||
|
|
||||||
In this setup, `~/.config` and `~/.local/nvim` are still going to behave in their default setting (ignoring all listed directory and its children), however `/` and `/tmp` will only ignore those directories exactly.
|
In this setup, `~/.config` and `~/.local/nvim` are still going to behave in their default setting (ignoring all listed directory and its children), however `/` and `/tmp` will only ignore those directories exactly.
|
||||||
|
|
||||||
|
### Ignored branches
|
||||||
|
|
||||||
|
You may specify a table of patterns that match against braches for which the plugin will **never** autosave and autoload from. For example:
|
||||||
|
|
||||||
|
```lua
|
||||||
|
require("persisted").setup({
|
||||||
|
ignored_branches = {
|
||||||
|
"^master",
|
||||||
|
"feature/%u"
|
||||||
|
},
|
||||||
|
})
|
||||||
|
```
|
||||||
|
|
||||||
### Events / Callbacks
|
### Events / Callbacks
|
||||||
|
|
||||||
The plugin fires events at various points during its lifecycle:
|
The plugin fires events at various points during its lifecycle:
|
||||||
|
|
|
||||||
|
|
@ -17,6 +17,7 @@ local defaults = {
|
||||||
follow_cwd = true, -- change session file name with changes in current working directory
|
follow_cwd = true, -- change session file name with changes in current working directory
|
||||||
allowed_dirs = nil, -- table of dirs that the plugin will auto-save and auto-load from
|
allowed_dirs = nil, -- table of dirs that the plugin will auto-save and auto-load from
|
||||||
ignored_dirs = nil, -- table of dirs that are ignored for auto-saving and auto-loading
|
ignored_dirs = nil, -- table of dirs that are ignored for auto-saving and auto-loading
|
||||||
|
ignored_branches = nil, -- table of branch patterns that are ignored for auto-saving and auto-loading
|
||||||
|
|
||||||
telescope = {
|
telescope = {
|
||||||
reset_prompt = true, -- Reset prompt after a telescope action?
|
reset_prompt = true, -- Reset prompt after a telescope action?
|
||||||
|
|
|
||||||
|
|
@ -79,6 +79,19 @@ local function ignore_dir(dir)
|
||||||
return utils.dirs_match(dir, ignored_dirs)
|
return utils.dirs_match(dir, ignored_dirs)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
---Is the current branch ignored for auto-saving and loading?
|
||||||
|
---@param dir string Branch to be used for the session
|
||||||
|
---@return boolean
|
||||||
|
local function ignore_branch(branch)
|
||||||
|
local ignored_branches = config.options.ignored_branches
|
||||||
|
|
||||||
|
if ignored_branches == nil then
|
||||||
|
return false
|
||||||
|
end
|
||||||
|
|
||||||
|
return utils.table_match(branch, ignored_branches) ~= nil
|
||||||
|
end
|
||||||
|
|
||||||
---Get the session that was saved last
|
---Get the session that was saved last
|
||||||
---@return string
|
---@return string
|
||||||
local function get_last()
|
local function get_last()
|
||||||
|
|
@ -91,6 +104,24 @@ local function get_last()
|
||||||
return sessions[1]
|
return sessions[1]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
---Get the current Git branch name, untouched
|
||||||
|
---@param dir? string Directory to be used for the session
|
||||||
|
---@return string|nil
|
||||||
|
local function get_branchname(dir)
|
||||||
|
dir = dir or session_dir()
|
||||||
|
vim.fn.system('git -C "' .. dir .. '" rev-parse 2>/dev/null')
|
||||||
|
|
||||||
|
local git_enabled = (vim.v.shell_error == 0)
|
||||||
|
|
||||||
|
if git_enabled then
|
||||||
|
local git_branch = vim.fn.systemlist('git -C "' .. dir .. '" rev-parse --abbrev-ref HEAD 2>/dev/null')
|
||||||
|
return git_branch[1]
|
||||||
|
end
|
||||||
|
|
||||||
|
return nil
|
||||||
|
end
|
||||||
|
|
||||||
---Get the current Git branch
|
---Get the current Git branch
|
||||||
---@param dir? string Directory to be used for the session
|
---@param dir? string Directory to be used for the session
|
||||||
---@return string|nil
|
---@return string|nil
|
||||||
|
|
@ -154,10 +185,12 @@ end
|
||||||
function M.setup(opts)
|
function M.setup(opts)
|
||||||
config.setup(opts)
|
config.setup(opts)
|
||||||
local dir = session_dir()
|
local dir = session_dir()
|
||||||
|
local branch = get_branchname()
|
||||||
|
|
||||||
if
|
if
|
||||||
config.options.autosave
|
config.options.autosave
|
||||||
and (allow_dir(dir) and not ignore_dir(dir) and vim.g.persisting == nil)
|
and (allow_dir(dir) and not ignore_dir(dir) and vim.g.persisting == nil)
|
||||||
|
and not ignore_branch(branch)
|
||||||
and args_check()
|
and args_check()
|
||||||
then
|
then
|
||||||
M.start()
|
M.start()
|
||||||
|
|
@ -171,6 +204,7 @@ end
|
||||||
function M.load(opt, dir)
|
function M.load(opt, dir)
|
||||||
opt = opt or {}
|
opt = opt or {}
|
||||||
dir = dir or session_dir()
|
dir = dir or session_dir()
|
||||||
|
local branch = get_branchname()
|
||||||
|
|
||||||
local session = opt.session or (opt.last and get_last() or get_current(dir))
|
local session = opt.session or (opt.last and get_last() or get_current(dir))
|
||||||
|
|
||||||
|
|
@ -185,7 +219,11 @@ function M.load(opt, dir)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if config.options.autosave and (allow_dir(dir) and not ignore_dir(dir)) then
|
if
|
||||||
|
config.options.autosave
|
||||||
|
and (allow_dir(dir) and not ignore_dir(dir))
|
||||||
|
and not ignore_branch(branch)
|
||||||
|
then
|
||||||
M.start()
|
M.start()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
@ -194,9 +232,10 @@ end
|
||||||
---@return nil
|
---@return nil
|
||||||
function M.autoload()
|
function M.autoload()
|
||||||
local dir = session_dir()
|
local dir = session_dir()
|
||||||
|
local branch = get_branchname()
|
||||||
|
|
||||||
if config.options.autoload and args_check() then
|
if config.options.autoload and args_check() then
|
||||||
if allow_dir(dir) and not ignore_dir(dir) then
|
if allow_dir(dir) and not ignore_dir(dir) and not ignore_branch(branch) then
|
||||||
M.load({}, dir)
|
M.load({}, dir)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -82,7 +82,15 @@ end
|
||||||
---@return boolean
|
---@return boolean
|
||||||
function M.dirs_match(dir, dirs_table)
|
function M.dirs_match(dir, dirs_table)
|
||||||
dir = vim.fn.expand(dir)
|
dir = vim.fn.expand(dir)
|
||||||
return dirs_table
|
return M.table_match(dir, dirs_table, function(pattern) return escape_pattern(vim.fn.expand(pattern)) end)
|
||||||
|
end
|
||||||
|
|
||||||
|
---Check if a string matches and entry in a given table
|
||||||
|
---@param needle string
|
||||||
|
---@param heystack table
|
||||||
|
---@return boolean
|
||||||
|
function M.table_match(needle, heystack, escape_fct)
|
||||||
|
return heystack
|
||||||
and next(vim.tbl_filter(function(pattern)
|
and next(vim.tbl_filter(function(pattern)
|
||||||
if pattern.exact then
|
if pattern.exact then
|
||||||
-- The pattern is actually a table
|
-- The pattern is actually a table
|
||||||
|
|
@ -92,11 +100,14 @@ function M.dirs_match(dir, dirs_table)
|
||||||
if pattern:sub(-1) == fp_sep and pattern:len() > 1 then
|
if pattern:sub(-1) == fp_sep and pattern:len() > 1 then
|
||||||
pattern = pattern:sub(1, -2)
|
pattern = pattern:sub(1, -2)
|
||||||
end
|
end
|
||||||
return dir == pattern
|
return needle == pattern
|
||||||
else
|
else
|
||||||
return dir:find(escape_pattern(vim.fn.expand(pattern)))
|
if escape_fct then
|
||||||
|
pattern = escape_fct(pattern)
|
||||||
|
end
|
||||||
|
return needle:match(pattern)
|
||||||
end
|
end
|
||||||
end, dirs_table))
|
end, heystack))
|
||||||
end
|
end
|
||||||
|
|
||||||
---Get the directory pattern based on OS
|
---Get the directory pattern based on OS
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue