Skip to content

Commit 29f80e9

Browse files
authored
Support autoloading session for the current git repository (#128)
1 parent b3058f6 commit 29f80e9

File tree

3 files changed

+37
-13
lines changed

3 files changed

+37
-13
lines changed

README.md

+15-13
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@ The plugin saves the sessions in the specified folder (see [configuration](#conf
1212

1313
Use the command `:SessionManager[!]` with one of the following arguments:
1414

15-
| Argument | Description |
16-
| -----------------------------| ------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
17-
| `load_session` | Select and load session. (Your current session won't appear on the list). |
18-
| `load_last_session` | Removes all buffers and tries to `:source` the last saved session. Returns `true` if the session was restored and `false` otherwise. |
19-
| `load_current_dir_session` | Removes all buffers and tries to `:source` the last saved session file of the current directory. Returns `true` if the session was restored and `false` otherwise. |
20-
| `save_current_session` | Works like `:mksession`, but saves/creates current directory as a session in `sessions_dir`. |
21-
| `delete_session` | Select and delete session. |
22-
| `delete_current_dir_session` | Deletes the session associated with the current directory. |
15+
| Argument | Description |
16+
| -----------------------------| --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
17+
| `load_session` | Select and load session. (Your current session won't appear on the list). |
18+
| `load_last_session` | Removes all buffers and tries to `:source` the last saved session. Returns `true` if the session was restored and `false` otherwise. |
19+
| `load_current_dir_session` | Removes all buffers and tries to `:source` the last saved session of the current directory. Returns `true` if the session was restored and `false` otherwise. |
20+
| `load_git_session` | When in a git repo, removes all buffers and tries to `:source` the last saved session of the git repo root directory. Returns `true` if the session was restored and `false` otherwise. |
21+
| `save_current_session` | Works like `:mksession`, but saves/creates current directory as a session in `sessions_dir`. |
22+
| `delete_session` | Select and delete session. |
23+
| `delete_current_dir_session` | Deletes the session associated with the current directory. |
2324

2425
When `!` is specified, the modified buffers will not be saved.
2526

@@ -54,11 +55,12 @@ require('session_manager').setup({
5455

5556
If Neovim is started without arguments the value of the autoload_mode option is used to determine which session to initially load. The following modes are supported:
5657

57-
| Mode | Description |
58-
| ----------- | ------------------------------------------------------------ |
59-
| Disabled | No session will be loaded. |
60-
| CurrentDir | The session in the current working directory will be loaded. |
61-
| LastSession | The last session will be loaded. This is the default. |
58+
| Mode | Description |
59+
| ----------- | --------------------------------------------------------------- |
60+
| Disabled | No session will be loaded. |
61+
| CurrentDir | The session in the current working directory will be loaded. |
62+
| LastSession | The last session will be loaded. This is the default. |
63+
| GitSession | If in a git repo the session for repository root will be loaded |
6264

6365
`autoload_mode` can be set to either a single mode or an array of modes, in which
6466
case each mode will be tried until one succeeds e.g.

lua/session_manager/config.lua

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ local config = {
99
'Disabled',
1010
'CurrentDir',
1111
'LastSession',
12+
'GitSession',
1213
}),
1314
}
1415

lua/session_manager/init.lua

+21
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
local config = require('session_manager.config')
22
local AutoloadMode = require('session_manager.config').AutoloadMode
33
local utils = require('session_manager.utils')
4+
local Job = require('plenary.job')
45
local session_manager = {}
56

67
--- Apply user settings.
@@ -66,6 +67,25 @@ function session_manager.load_current_dir_session(discard_current)
6667
return false
6768
end
6869

70+
--- If in a git repo, tries to load a session for the repo's root directory
71+
---@return boolean: `true` if session was loaded, `false` otherwise.
72+
function session_manager.load_git_session(discard_current)
73+
local job = Job:new({
74+
command = 'git',
75+
args = { 'rev-parse', '--show-toplevel' },
76+
})
77+
job:sync()
78+
local git_dir = job:result()[1]
79+
if git_dir then
80+
local session = config.dir_to_session_filename(git_dir)
81+
if session:exists() then
82+
utils.load_session(session.filename, discard_current)
83+
return true
84+
end
85+
end
86+
return false
87+
end
88+
6989
--- Saves a session for the current working directory.
7090
function session_manager.save_current_session()
7191
local cwd = vim.uv.cwd()
@@ -78,6 +98,7 @@ local autoloaders = {
7898
[AutoloadMode.Disabled] = function() return true end,
7999
[AutoloadMode.CurrentDir] = session_manager.load_current_dir_session,
80100
[AutoloadMode.LastSession] = session_manager.load_last_session,
101+
[AutoloadMode.GitSession] = session_manager.load_git_session,
81102
}
82103

83104
--- Loads a session based on settings. Executed after starting the editor.

0 commit comments

Comments
 (0)