Add ability to session based on cwd and git branch

This creates session files like:

%Users%Oli%Code%Projects%persistance_feature%add-git-branch-to-session

enabling you to segment your project directory by git branch
main
olimorris 2021-09-07 19:37:26 +01:00
parent 2f2b0cc69d
commit 3d84264be5
2 changed files with 18 additions and 3 deletions

View File

@ -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
}

View File

@ -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