lazy.nvim icon indicating copy to clipboard operation
lazy.nvim copied to clipboard

bug: lazy unset GIT_DIR and GIT_WORK_TREE environment variables

Open luigir-it opened this issue 1 year ago • 2 comments

Did you check docs and existing issues?

  • [X] I have read all the lazy.nvim docs
  • [X] I have updated the plugin to the latest version before submitting this issue
  • [X] I have searched the existing issues of lazy.nvim
  • [X] I have searched the existing issues of plugins related to this issue

Neovim version (nvim -v)

0.10.1

Operating system/version

Linux

Describe the bug

Since this commit lazy.nvim unsets GIT_DIR. This was done to not interfere with its internal use of git, as stated in #434. Few months later, this unset GIT_WORK_TREE.

While this is a sane default behavior, it breaks my workflow. I, just like the user of #434, use those variables to manage my dotfiles, but use nvim to edit them, and plugins like gitsigns or the one used to blame lines need those variables to be set.

Adding a way to alter this behavior would be appreciated. For example, right now I'm using this hack:

~/.local/share/nvim/lazy/lazy.nvim/lua/lazy/manage/process.lua
131,132c131,134
<   env.GIT_DIR = nil
<   env.GIT_WORK_TREE = nil
---
>   if not env.DISISDOT then
>     env.GIT_DIR = nil
>     env.GIT_WORK_TREE = nil
>   end

So if I set DISISDOT set to true, lazy.nvim would not overwrite the other two variables.


If the suggested change is not implemented, this is the workaround I've been using for few weeks and works just fine. Patch ~/.local/share/nvim/lazy/lazy.nvim/lua/lazy/manage/process.lua with dots_patch.txt using patch -bf ~/.local/share/nvim/lazy/lazy.nvim/lua/lazy/manage/process.lua -i /path/to/dots_patch.txt Remember that lazy.nvim will complain about tampering it's code when updating itself. So you should reverse the patch when applying an update.

If you want to automate the entire process, edit ~/.config/nvim/lua/config/autocmd.lua and add the following autocommands

  1. This will set DISISDOT to true if working with a dotfile
vim.api.nvim_create_autocmd({ "BufReadPost" }, {
  pattern = { "*" },
  callback = function()
    local file = vim.api.nvim_buf_get_name(0)
    if
      os.execute(
        "/usr/bin/git --git-dir=$HOME/.dotfiles/ --work-tree=$HOME ls-files --error-unmatch "
          .. file
          .. " >/dev/null 2>&1"
      ) == 0
    then
      -- $DISISDOT will signal lazy.nvim to not overwrite $GIT_DIR and $GIT_WORK_TREE
      -- REMEMBER that this works because you changed the behaviour of ~/.local/share/nvim/lazy/lazy.nvim/lua/lazy/manage/process.lua
      vim.env.DISISDOT = true
      vim.env.GIT_DIR = vim.env.HOME .. "/.dotfiles/"
      vim.env.GIT_WORK_TREE = vim.env.HOME
    end
  end,
})

  1. This will deal with lazy.nvim updates
vim.api.nvim_create_autocmd({ "User" }, {
  pattern = { "LazyUpdatePre" },
  callback = function()
    os.execute(
      "patch -bfR ~/.local/share/nvim/lazy/lazy.nvim/lua/lazy/manage/process.lua -i ~/dots_patch.txt"
    )
  end,
})
vim.api.nvim_create_autocmd({ "User" }, {
  pattern = { "LazyUpdate" },
  callback = function()
    os.execute(
      "patch -bf ~/.local/share/nvim/lazy/lazy.nvim/lua/lazy/manage/process.lua -i ~/dots_patch.txt"
    )
    print("Patched!")
  end,
})

Steps To Reproduce

  1. set GIT_DIR and GIT_WORK_TREE
  2. use a plugin that use those variables

Expected Behavior

The variable should be unset by default, but the user should have the possibility to set them.

luigir-it avatar Sep 03 '24 22:09 luigir-it

I don't really understand your use-case. You can still set those variables in your env and they would be used by those plugins.

The lines you are referring to, just don't use those env vars for lazy specifically.

What's the problem you're trying to solve here?

folke avatar Sep 16 '24 08:09 folke

I'm sorry I didn't explain the issue properly. The plugins will use those env indeed. The issue persist only for 'Git Blame Line' (space+g+b) and similar options.

This is probably because 'Git Blame Line' uses float_cmd from lazy.util and when spawning the floating terminal, lazy unset those git related env vars

This will prompt an error:

git -C . log -n 3 -u -L 99,+1:/home/pcino/dotfiles.conf`

## Error
fatal: not a git repository (or any parent up to mount point /)
Stopping at filesystem boundary (GIT_DISCOVERY_ACROSS_FILESYSTEM not set).

That prevents me from using 'Git Blame Line'

luigir-it avatar Sep 29 '24 21:09 luigir-it

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 7 days.

github-actions[bot] avatar Oct 30 '24 02:10 github-actions[bot]

This issue was closed because it has been stalled for 7 days with no activity.

github-actions[bot] avatar Nov 07 '24 01:11 github-actions[bot]