stevearc / resession.nvim

A replacement for mksession with a better API
MIT License
175 stars 13 forks source link

bug: "Create one session per git branch" example puts newlines in filenames #22

Closed mrjones2014 closed 10 months ago

mrjones2014 commented 10 months ago

Did you check the docs and existing issues?

Neovim version (nvim -v)

v0.10.0-dev-cffdf10

Operating system/version

MacOS 12

Describe the bug

Using the "Create one session per git branch" example from README.md can result in having a \n character in the session file name. Modifying the get_session_name() function like so fixes it:

local function get_session_name()
  local name = vim.fn.getcwd()
  local branch = vim.fn.system('git branch --show-current')
  if vim.v.shell_error ~= 0 then
    branch = ''
  end
  vim.trim(string.format('%s%s', name, branch))
end

Steps To Reproduce

Create a per directory, per branch, session.

Expected Behavior

It sanitizes the session file name to exclude newlines.

Directory structure

No response

Repro

-- save as repro.lua
-- run with nvim -u repro.lua
-- DO NOT change the paths
local root = vim.fn.fnamemodify("./.repro", ":p")

-- set stdpaths to use .repro
for _, name in ipairs({ "config", "data", "state", "runtime", "cache" }) do
  vim.env[("XDG_%s_HOME"):format(name:upper())] = root .. "/" .. name
end

-- bootstrap lazy
local lazypath = root .. "/plugins/lazy.nvim"
if not vim.loop.fs_stat(lazypath) then
  vim.fn.system({
    "git",
    "clone",
    "--filter=blob:none",
    "--single-branch",
    "https://github.com/folke/lazy.nvim.git",
    lazypath,
  })
end
vim.opt.runtimepath:prepend(lazypath)

-- install plugins
local plugins = {
  "folke/tokyonight.nvim",
  {
    "stevearc/resession.nvim",
    config = function()
      require("resession").setup({
        -- add any needed settings here
      })
      local function get_session_name()
        local name = vim.fn.getcwd()
        local branch = vim.fn.system("git branch --show-current")
        if vim.v.shell_error == 0 then
          return name .. branch
        else
          return name
        end
      end
      local resession = require("resession")
      resession.add_hook("post_load", function()
        -- if loading a session, there are likely multiple windows, and loading smart-splits.nvim is inexpensive anyway
        require("smart-splits")
      end)
      -- Only load the session if nvim was started with no args, or if command was to open a directory, not a file
      local num_args = vim.fn.argc(-1)
      if num_args == 0 or (num_args == 1 and vim.fn.isdirectory(vim.fn.argv(0) or "") ~= 0) then
        resession.load(
          get_session_name(),
          { dir = "dirsession", silence_errors = true, notify = false }
        )
      end

      vim.api.nvim_create_autocmd("VimLeavePre", {
        callback = function()
          require("resession").save(
            get_session_name(),
            { dir = "dirsession", notify = false }
          )
        end,
      })
    end,
  },
  -- add any other plugins here
}
require("lazy").setup(plugins, {
  root = root .. "/plugins",
})

vim.cmd.colorscheme("tokyonight")
-- add anything else here

Did you check the bug with a clean config?

stevearc commented 10 months ago

Thanks for the report!