diff --git a/lua/persistence/config.lua b/lua/persistence/config.lua index 6d17901..9fd382e 100644 --- a/lua/persistence/config.lua +++ b/lua/persistence/config.lua @@ -3,6 +3,7 @@ local M = {} ---@class PersistenceOptions local defaults = { dir = vim.fn.expand(vim.fn.stdpath("config") .. "/sessions/"), -- directory where session files are saved + use_git_branch = false, -- create session files based on the branch of the git enabled repository options = { "buffers", "curdir", "tabpages", "winsize" }, -- sessionoptions used for saving } diff --git a/lua/persistence/init.lua b/lua/persistence/init.lua index 05fcd60..6b1702a 100644 --- a/lua/persistence/init.lua +++ b/lua/persistence/init.lua @@ -6,7 +6,21 @@ local e = vim.fn.fnameescape function M.get_current() local name = vim.fn.getcwd():gsub("/", "%%") - return Config.options.dir .. name .. ".vim" + return Config.options.dir .. name .. M.get_branch() ..".vim" +end + +function M.get_branch() + if Config.options.use_git_branch == true then + local branch = vim.api.nvim_exec([[!git rev-parse --abbrev-ref HEAD]], true) + -- The command returns two lines. We only need the second one + lines = {} + for s in branch:gmatch("[^\r\n]+") do + table.insert(lines, '_' .. s) + end + return lines[2]:gsub("/", "%%") + end + + return '' end function M.get_last() @@ -33,8 +47,8 @@ end function M.stop() vim.cmd([[ - autocmd! Persistence - augroup! Persistence + autocmd! Persistence + augroup! Persistence ]]) end