Skip to content

Commit a6daf50

Browse files
authored
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
1 parent ef305a8 commit a6daf50

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

lua/nvim-tree/explorer/watch.lua

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,17 @@ local M = {
88
}
99

1010
local function is_git(path)
11-
return vim.fn.fnamemodify(path, ":t") == ".git"
11+
-- If $GIT_DIR is set, consider its value to be equivalent to '.git'.
12+
-- Expand $GIT_DIR (and `path`) to a full path (see :help filename-modifiers), since
13+
-- it's possible to set it to a relative path. We want to make our best
14+
-- effort to expand that to a valid absolute path.
15+
if vim.fn.fnamemodify(path, ":p") == vim.fn.fnamemodify(vim.env.GIT_DIR, ":p") then
16+
return true
17+
elseif vim.fn.fnamemodify(path, ":t") == ".git" then
18+
return true
19+
else
20+
return false
21+
end
1222
end
1323

1424
local IGNORED_PATHS = {

lua/nvim-tree/git/init.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,8 @@ function M.load_project_status(cwd)
186186
end)
187187
end
188188

189-
watcher = Watcher:new(utils.path_join { project_root, ".git" }, WATCHED_FILES, callback, {
189+
local git_dir = vim.env.GIT_DIR or utils.path_join { project_root, ".git" }
190+
watcher = Watcher:new(git_dir, WATCHED_FILES, callback, {
190191
project_root = project_root,
191192
})
192193
end

0 commit comments

Comments
 (0)