From 3d84264be5fe931f016abea5e6d733efea3fb17e Mon Sep 17 00:00:00 2001 From: olimorris Date: Tue, 7 Sep 2021 19:37:26 +0100 Subject: [PATCH] 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 --- lua/persistence/config.lua | 1 + lua/persistence/init.lua | 20 +++++++++++++++++--- 2 files changed, 18 insertions(+), 3 deletions(-) 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