From a6daf50b9d1aa426f557e51b37ee854b99782d3f Mon Sep 17 00:00:00 2001 From: Zach Himsel Date: Sat, 15 Jul 2023 22:44:21 -0400 Subject: [PATCH] feat: support custom $GIT_DIR (#2263) * feat: Watch $GIT_DIR for git changes, if set While rarely used, it's possible to set the $GIT_DIR environment variable to instruct git to use a directory other than `.git`. This checks if that environment variable is set; if it is, the plugin will watch that directory. If it's not set, it'll fall back to the default `.git` directory. * fix: Don't create two watchers for $GIT_DIR This will ignore a path for watching if EITHER it's '.git', or the value of $GIT_DIR (if it's set). If $GIT_DIR is not set, the vim.env object returns `nil`, which will never match `path`. * fix: Attempt to make a relative $GIT_DIR absolute --- lua/nvim-tree/explorer/watch.lua | 12 +++++++++++- lua/nvim-tree/git/init.lua | 3 ++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/lua/nvim-tree/explorer/watch.lua b/lua/nvim-tree/explorer/watch.lua index dad1738c..cbebe3b1 100644 --- a/lua/nvim-tree/explorer/watch.lua +++ b/lua/nvim-tree/explorer/watch.lua @@ -8,7 +8,17 @@ local M = { } local function is_git(path) - return vim.fn.fnamemodify(path, ":t") == ".git" + -- If $GIT_DIR is set, consider its value to be equivalent to '.git'. + -- Expand $GIT_DIR (and `path`) to a full path (see :help filename-modifiers), since + -- it's possible to set it to a relative path. We want to make our best + -- effort to expand that to a valid absolute path. + if vim.fn.fnamemodify(path, ":p") == vim.fn.fnamemodify(vim.env.GIT_DIR, ":p") then + return true + elseif vim.fn.fnamemodify(path, ":t") == ".git" then + return true + else + return false + end end local IGNORED_PATHS = { diff --git a/lua/nvim-tree/git/init.lua b/lua/nvim-tree/git/init.lua index 2714e39e..4a727779 100644 --- a/lua/nvim-tree/git/init.lua +++ b/lua/nvim-tree/git/init.lua @@ -186,7 +186,8 @@ function M.load_project_status(cwd) end) end - watcher = Watcher:new(utils.path_join { project_root, ".git" }, WATCHED_FILES, callback, { + local git_dir = vim.env.GIT_DIR or utils.path_join { project_root, ".git" } + watcher = Watcher:new(git_dir, WATCHED_FILES, callback, { project_root = project_root, }) end