Skip to content

Commit

Permalink
refactor!: do not append main to non-git repo sessions
Browse files Browse the repository at this point in the history
  • Loading branch information
olimorris committed Dec 20, 2023
1 parent ff261c2 commit 66d540f
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 33 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ require("persisted").setup({
save_dir = vim.fn.expand(vim.fn.stdpath("data") .. "/sessions/"), -- directory where session files are saved
silent = false, -- silent nvim message when sourcing session file
use_git_branch = false, -- create session files based on the branch of a git enabled repository
default_branch = "main", -- the branch to load if a session file is not found for the current branch
autosave = true, -- automatically save session files when exiting Neovim
should_autosave = nil, -- function to determine if a session should be autosaved
autoload = false, -- automatically load the session for the cwd on Neovim startup
Expand Down Expand Up @@ -186,9 +187,7 @@ require("persisted").setup({
})
```

> **Note**: If git branching is enabled on a non git enabled repo, then `main` will be used as the default branch
If you switch branches in a repository, the plugin will try to load a session which corresponds to that branch. If it can't find one, then it will load the session from the `main` branch.
> **Note**: If you initiate git in a repository which has an existing session file, you'll need to add it's branch name to the session name. To do this from within Neovim, use the `:Sessions` command, navigate to the session and press `<C-a>`.
### Autosaving

Expand Down
12 changes: 6 additions & 6 deletions doc/persisted.nvim.txt
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,14 @@ The plugin comes with a number of commands:

TELESCOPE EXTENSION ~

The Telescope extension may be opened via `:Telescope persisted`.
The Telescope extension may be opened via `:Telescope persisted`. The available
actions are:

Once opened, the available keymaps are:


- `<CR>` - Source the session file
- `<CR>` - Open/source the session file
- `<C-b>` - Add/update the git branch for the session file
- `<C-c>` - Copy the session file
- `<C-d>` - Delete the session file
- `<C-a>` - Add/update a git branch to the session file


GLOBAL VARIABLES ~
Expand Down Expand Up @@ -161,7 +161,7 @@ The plugin comes with the following defaults:
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
telescope = {
reset_prompt = true, -- Reset prompt after a telescope action?
reset_prompt = true, -- Reset the Telescope prompt after an action?
},
})
<
Expand Down
6 changes: 5 additions & 1 deletion lua/persisted/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ local M = {}
local defaults = {
save_dir = vim.fn.expand(vim.fn.stdpath("data") .. "/sessions/"), -- directory where session files are saved
silent = false, -- silent nvim message when sourcing session file
use_git_branch = false, -- create session files based on the branch of the git enabled repository

use_git_branch = false, -- create session files based on the branch of a git enabled repository
branch_separator = "@@", -- string used to separate session directory name from branch name
default_branch = "main", -- the branch to load if a session file is not found for the current branch

autosave = true, -- automatically save session files when exiting Neovim
should_autosave = nil, -- function to determine if a session should be autosaved (resolve to a boolean)

Expand All @@ -17,6 +20,7 @@ local defaults = {

telescope = {
reset_prompt = true, -- Reset prompt after a telescope action?
--TODO: We should add a deprecation notice for the old API here
},
}

Expand Down
78 changes: 55 additions & 23 deletions lua/persisted/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,19 @@ local config = require("persisted.config")
local M = {}

local e = vim.fn.fnameescape
local default_branch = "main"

---Escapes special characters before performing string substitution
---@param str string
---@param pattern string
---@param replace string
---@param n? integer
---@return string
---@return integer count
local function escape_pattern(str, pattern, replace, n)
pattern = string.gsub(pattern, "[%(%)%.%+%-%*%?%[%]%^%$%%]", "%%%1") -- escape pattern
replace = string.gsub(replace, "[%%]", "%%%%") -- escape replacement
return string.gsub(str, pattern, replace, n)
end

---Does the current working directory allow for the auto-saving and loading?
---@return boolean
Expand All @@ -14,6 +26,7 @@ local function allow_dir()
if allowed_dirs == nil then
return true
end

return utils.dirs_match(vim.fn.getcwd(), allowed_dirs)
end

Expand All @@ -33,6 +46,7 @@ end
---@return string
local function get_last()
local sessions = vim.fn.glob(config.options.save_dir .. "*.vim", true, true)

table.sort(sessions, function(a, b)
return vim.loop.fs_stat(a).mtime.sec > vim.loop.fs_stat(b).mtime.sec
end)
Expand All @@ -41,40 +55,57 @@ local function get_last()
end

---Get the current Git branch
---@return string
---@return string|nil
function M.get_branch()
if config.options.use_git_branch then
vim.fn.system([[git rev-parse 2> /dev/null]])

local git_enabled = (vim.v.shell_error == 0)

if git_enabled then
local branch = vim.fn.systemlist([[git rev-parse --abbrev-ref HEAD 2>/dev/null]])
local git_branch = vim.fn.systemlist([[git rev-parse --abbrev-ref HEAD 2>/dev/null]])

if vim.v.shell_error == 0 then
branch = config.options.branch_separator .. branch[1]:gsub("/", "%%")
local branch = config.options.branch_separator .. git_branch[1]:gsub("/", "%%")
local branch_session = config.options.save_dir
.. vim.fn.getcwd():gsub(utils.get_dir_pattern(), "%%")
.. branch
.. ".vim"

-- Try to load the session for the current branch and if not, use the value of default_branch
-- Try to load the session for the current branch
if vim.fn.filereadable(branch_session) ~= 0 then
return branch
else
vim.api.nvim_echo({
{ "[Persisted.nvim]\n", "Question" },
{ "Could not load a session for branch " },
{ git_branch[1] .. "\n", "WarningMsg" },
{ "Trying to load a session for branch " },
{ config.options.default_branch, "Title" },
{ " ..." },
}, true, {})

vim.g.persisted_branch_session = branch_session
return config.options.branch_separator .. default_branch
return config.options.branch_separator .. config.options.default_branch
end
end
end
end

return config.options.branch_separator .. default_branch
-- -- INFO: This allows users who have `@@main` in their session name to load
-- -- repositories that are not git enabled
-- if config.options.use_old_branching then
-- return config.options.branch_separator .. config.options.default_branch
-- end
end
end

---Get the current session for the current working directory and git branch
---@return string
local function get_current()
local name = vim.fn.getcwd():gsub(utils.get_dir_pattern(), "%%")
return config.options.save_dir .. name .. M.get_branch() .. ".vim"
local branch = M.get_branch()

return config.options.save_dir .. name .. (branch or "") .. ".vim"
end

---Determine if a session for the current wording directory, exists
Expand Down Expand Up @@ -105,15 +136,28 @@ function M.load(opt)
opt = opt or {}
local session = opt.session or (opt.last and get_last() or get_current())

local session_exists = vim.fn.filereadable(session) ~= 0

if session then
if vim.fn.filereadable(session) ~= 0 then
if session_exists then
vim.g.persisting_session = config.options.follow_cwd and nil or session
utils.load_session(session, config.options.silent)
elseif type(config.options.on_autoload_no_session) == "function" then
config.options.on_autoload_no_session()
end
end

if session and not session_exists then
vim.api.nvim_echo({
{ "[Persisted.nvim]\n", "Question" },
{ "Could not find a session for " },
{ vim.fn.getcwd() .. "\n", "WarningMsg" },
{ "As per " },
{ "https://github.com/olimorris/persisted.nvim/discussions/103", "WarningMsg" },
{ " you may need to remove the branch from the name" },
}, true, {})
end

if config.options.autosave and (allow_dir() and not ignore_dir()) then
M.start()
end
Expand Down Expand Up @@ -212,19 +256,6 @@ function M.toggle()
return M.start()
end

---Escapes special characters before performing string substitution
---@param str string
---@param pattern string
---@param replace string
---@param n? integer
---@return string
---@return integer count
local function escape_pattern(str, pattern, replace, n)
pattern = string.gsub(pattern, "[%(%)%.%+%-%*%?%[%]%^%$%%]", "%%%1") -- escape pattern
replace = string.gsub(replace, "[%%]", "%%%%") -- escape replacement
return string.gsub(str, pattern, replace, n)
end

---List all of the sessions
---@return table
function M.list()
Expand Down Expand Up @@ -265,6 +296,7 @@ function M.list()
["dir_path"] = dir_path,
})
end

return sessions
end

Expand Down

0 comments on commit 66d540f

Please sign in to comment.