diff --git a/README.md b/README.md index 0b54b0c..e4ec303 100644 --- a/README.md +++ b/README.md @@ -48,6 +48,7 @@ local opts = { auto_save_enabled = nil, auto_restore_enabled = nil, auto_session_suppress_dirs = nil, + auto_session_use_git_branch = nil, -- the configs below are lua only bypass_session_save_file_types = nil } @@ -83,6 +84,7 @@ require('lualine').setup{ | auto_restore_enabled | false, true, nil | nil | Enables/disables auto restoring | | auto_session_suppress_dirs | ["list", "of paths"] | nil | Suppress session create/restore if in one of the list of dirs | | auto_session_allowed_dirs | ["list", "of paths"] | nil | Allow session create/restore if in one of the list of dirs | +| auto_session_use_git_branch | false, true, nil | nil | Use the git branch to differentiate the session name | ### Lua Only Options | Config | Options | Default | Description | diff --git a/lua/auto-session.lua b/lua/auto-session.lua index 5a75124..3862aac 100644 --- a/lua/auto-session.lua +++ b/lua/auto-session.lua @@ -35,6 +35,7 @@ local defaultConf = { auto_restore_enabled = nil, -- Enables/disables auto restore feature auto_session_suppress_dirs = nil, -- Suppress session restore/create in certain directories auto_session_allowed_dirs = nil, -- Allow session restore/create in certain directories + auto_session_use_git_branch = vim.g.auto_session_use_git_branch or false -- use the current git branch name as part of the session name } local luaOnlyConf = { @@ -89,6 +90,20 @@ local function is_auto_create_enabled() return true end +-- get the current git branch name, if any, and only if configured to do so +local function get_branch_name() + if AutoSession.conf.auto_session_use_git_branch then + local out = vim.fn.systemlist('git rev-parse --abbrev-ref HEAD') + if vim.v.shell_error ~= 0 then + vim.api.nvim_err_writeln(string.format("git failed with: %s", table.concat(out, "\n"))) + return "" + end + return out[1] + end + + return "" +end + local pager_mode = nil local in_pager_mode = function() if pager_mode ~= nil then @@ -196,7 +211,13 @@ local function get_session_file_name(sessions_dir) -- When we get here session and sessions_dir either both point to a file or do not exist return session else - local session_name = Lib.conf.last_loaded_session or Lib.escaped_session_name_from_cwd() + local session_name = Lib.conf.last_loaded_session + if not session_name then + session_name = Lib.escaped_session_name_from_cwd() + local branch_name = get_branch_name() + branch_name = branch_name ~= "" and "_"..branch_name or "" + session_name = string.format("%s%s", session_name, branch_name) + end return string.format(sessions_dir .. "%s.vim", session_name) end end @@ -346,9 +367,15 @@ function AutoSession.RestoreSession(sessions_dir_or_file) -- I still don't like reading this chunk, please cleanup if sessions_dir then Lib.logger.debug "==== Using session DIR" - local session_name = Lib.conf.last_loaded_session or Lib.escaped_session_name_from_cwd() + local session_name = Lib.conf.last_loaded_session + local session_file_path + if not session_name then + session_file_path = get_session_file_name(sessions_dir) + session_name = vim.fn.fnamemodify(session_file_path, ':t:r') + else + session_file_path = string.format(sessions_dir .. "%s.vim", session_name) + end Lib.logger.debug("==== Session Name", session_name) - local session_file_path = string.format(sessions_dir .. "%s.vim", session_name) local legacy_session_name = Lib.legacy_session_name_from_cwd() local legacy_file_path = string.format(sessions_dir .. "%s.vim", legacy_session_name)