Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature Request] Create different sessions for different git branches #68

Closed
lucasvianav opened this issue Sep 22, 2021 · 12 comments
Closed

Comments

@lucasvianav
Copy link

I'd like to have different sessions (with auto-save and auto-restore) for multiple branches on the same directory, so I don't keep overwriting the session whenever changing branches. I could maybe make a PR for this if you'd be ok with that

@rmagatti
Copy link
Owner

This is somewhat similar to this: #60

You'd create git workspaces and each one of those would have it's different session. I'll close this as a dupe, feel free to reopen it if you're thinking of something different.

Dupe of #60

@lucasvianav
Copy link
Author

lucasvianav commented Sep 23, 2021

That's not really what I was looking for, I also like this plugin's quality of not having to think about it, so I wouldn't like to have project-local directory/files to manage. Besides that, for my specific use-case, I think it wouldn't work, as in most project I'm working on I can't sync that kind of files to the repo.

I was actually thinking of something simpler, like just appending the branch name to the session's filename, so a single directory could have multiple sessions, one for each branch. What do you think?

@lucasvianav
Copy link
Author

@rmagatti I'm not able to reopen the issue, but hope you take a look at this

@rmagatti rmagatti reopened this Sep 23, 2021
@rmagatti
Copy link
Owner

There are three main concerns I have with that.

  1. That would mean this plugin would effectively require git as a dependency
  2. Even if this works, at least for my particular workflow it would be a tad frustrating to not have a session just because I switched branches. But I do understand how one might want to keep different branches with separate sessions.
  3. When one still has Neovim open and switches to a new branch, what would you expect to happen? This poses a few problems, the plugin would need to know when that happens and either switch to the session for that particular branch or at the very least stop itself from saving the active session to what would then essentially be the wrong one.

That said, I think this could be a good optional feature, I just wouldn't want it as part of the core functionality.
The dependency on git is arguably okay too, if error handling from not having git is done gracefully.

Point number 3 there is really the most concerning part imo, this can prove difficult to handle.

@lucasvianav
Copy link
Author

I wrote a big reply and then closed my browser on mistake :c
I'll rewrite it tomorrow!

@lucasvianav
Copy link
Author

lucasvianav commented Sep 26, 2021

Hey @rmagatti, sorry for the delay. Those are all valid points, but I think making the behavior I'm proposing optional
solves 1 and 2, and agree that 3 is trickier.

Point 3 has two aspects: how does autorestore behave when changing branches and how does autosave behave. For this, I'll
call the original branch (before change) branch-1 and the new one branch-2.

Autorestore

I think it shouldn't do anything.

There's no good way for us to know exactly when a user switches branches. The only one I can think of is running a check on autocmd (BufRead maybe?) to check if the changed occurred, but this doesn't seem nice to me.

Even if we could know when the change happens, if a user changed branches without closing Neovim, it's either one of two cases:

  • They are ok with/want to continue using branch-1's session (the active one) in branch-2.
    • Then it'd be bad/unexpected for the plugin to autorestore branch-2's session.
  • They do want to restore branch-2's session.

Seeing as there is correct options and we can't know what behavior the user prefers each time they change branches, I think opting for the less intrusive way (of doing nothing) is better --- in which case, if they want branch-2's session, they can just manually run :RestoreSession to get it, which I think is fair.

Autosaving

Knowing that a user changed branches (that the branch in which they're quitting Neovim is different than the one they started it in) is easier than knowing when they did it. We can just store the startup branch on a global variable (vim's? lua's? don't which would be more appropriate) and check, on VimLeave, check the active branch against it.

For this I see three options, when changing from branch-1 to branch-2 and closing Neovim in branch-2.

  1. Save the active session as branch-2's
  2. Save the active session as branch-1s
  3. Don't save the active session

Again, as we can't know for sure what the user prefers each time, so maybe this could be an option.

Honestly tho, I really think it's safe to assume that if the user changed branches without closing Neovim and didn't restore branch-2's session, they'll keep working on that session on branch-2, possibly opening files that are not available on branch-1, etc. In this case, overwriting branch-2's session with the active one seems like the best behavior to me.

Also, the plugin could maybe expose a command to quit Neovim without triggering the autosave. I think this'd be cool regardless of which of the above behaviors is default --- many times I've restored a session to do something quickly and would rather not overwrite it after.

Optimal scenario

Now that I think of it, maybe the best-case scenario (if we knew exactly when the user changed branches) would be to immediately autosave branch-1's session and then autorestore branch-2's session. But I don't know about that autocmd tho...

@xbot
Copy link
Contributor

xbot commented Mar 8, 2022

Any progress? Need this feature too. Every time I quit nvim → switch to another branch → open nvim, files in the previous branch become 'no name', even if I switch back won't recover the session. So I have to operate carefully to prevent my session from being destroyed.

@lucasvianav
Copy link
Author

I actually never got around to trying this, life got busy after my last comment =/

@calebdw
Copy link

calebdw commented Mar 9, 2022

I actually came here to request this same idea. I could care less for detecting the git checkout and autosaving/reloading. If I want to change branches then I'd shutdown nvim (:qa), checkout, and then reopen nvim.

@WhoIsSethDaniel
Copy link
Contributor

If no one else is working on this I may be able to do it. I recently realized this is a feature I can really use.

@WhoIsSethDaniel
Copy link
Contributor

WhoIsSethDaniel commented Mar 13, 2022

I've submitted PR #121. A very bare-bones effort at this feature.
If the branch is changed while in the editor the session will save to the session for the new branch. There may be a good way to detect branch changes, but I don't know what it is. At some point this can be changed to cache the current branch, check that the cached branch is the same as the current branch and do the right thing.
Only works with git.
Only works with a git named 'git' (and is in your path).
Only works if you do not have a disconnected .git dir and/or do not require special arguments to get git to run correctly.
Is opt-in. Set the 'auto_session_use_git_branch' option to true. By default it is false (i.e. off).
If my other PR (#120) is accepted one easy way of changing sessions (and branches) would be to (this assumes you have a session saved for the new branch already):

  • :SaveSession
  • git checkout <branch>
  • :RestoreSessionFromFile <session>

@rmagatti
Copy link
Owner

PR #121 is merged. Thanks for working on it @WhoIsSethDaniel! 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants