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

[Enhancement] add git branches support #17

Open
mac-hel opened this issue Oct 15, 2022 · 1 comment
Open

[Enhancement] add git branches support #17

mac-hel opened this issue Oct 15, 2022 · 1 comment

Comments

@mac-hel
Copy link

mac-hel commented Oct 15, 2022

This is best sessions plugin I have found (better then Persistence/Persisted or auto-session)
Cleanest and best configuration, custom session names (all other plugins just save session per working dir, no multiple sessions for single dir allowed)

One thing that may be lacking is command to auto name session after current git branch. Of course I can name session manually, but it would be nice to have this automated - maybe some option added to PossessionSave command
It could be even better if configuration option exist to auto load session on branch change

This is just idea, thanks for great plugin

@jedrzejboczar
Copy link
Owner

Thanks!

Naming session after current git branch may a nice option, but it probably shouldn't be the default for PossessionSave, maybe we could add a separate PossessionSaveGit command. There is an issue with name duplication as most often the branch is master or main.

You can try the following (uses dir+branch as name):

local function current_git_branch()
    local output = vim.fn.system('git rev-parse --abbrev-ref HEAD ')
    local status = vim.api.nvim_get_vvar('shell_error')
    if status ~= 0 then
        vim.notify('Cannot get current git branch: ' .. output, vim.log.levels.ERROR)
        return
    end

    local lines = vim.split(output, '\n', { plain = true, trimempty = true })
    local words = #lines > 0 and vim.split(vim.trim(lines[1]), '%s+')
    local branch = words and words[1]

    if #lines > 1 or not words or #words > 1 or branch == 'HEAD' then
        vim.notify('Incorrect git branch: ' .. output, vim.log.levels.ERROR)
        return
    end
    return words[1]
end

local function current_dir_name()
    local cwd = vim.fn.getcwd()
    return vim.fn.fnamemodify(cwd, ':t')
end

vim.api.nvim_create_user_command('PossessionSaveGit', function()
    local branch = current_git_branch()
    local repo = current_dir_name()
    if branch then
        branch = branch:gsub('/', '_')
        local session_name = repo .. '-' .. branch
        require('possession.commands').save(session_name)
    end
end, { nargs = 0 })

and if it works well I can add it as separate command.

Regarding auto-changing session on branch change - this seems a bit complicated, it would require setting up some libuv file-watches on .git/ or something like this, and it might be out of scope of this plugin. Maybe some other plugin like fugitive/gitsigns/etc. exposes some autocommand/callback on branch change - then you could try loading the correct session in this callback.

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

2 participants