Skip to content

Commit 517dee6

Browse files
Damien Mehalaalex-courtis
Damien Mehala
andauthored
fix(#1976): support non-standard $GIT_DIR (#2012)
* Improve $GIT_DIR handling - Retrieve $GIT_DIR using `git rev-parse --absolute-git-dir` - Move $GIT_DIR ignore in the project exploration part Resolves #1976 * Code review - move norm_path to utils.lua - fix comment #2012 * add comments for utils.norm_path * get_git_directory use norm_path from utils * watcher improvements --------- Co-authored-by: Alexander Courtis <[email protected]>
1 parent f8bb6b4 commit 517dee6

File tree

4 files changed

+82
-38
lines changed

4 files changed

+82
-38
lines changed

lua/nvim-tree/explorer/watch.lua

+7-13
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,19 @@ local Watcher = require("nvim-tree.watcher").Watcher
44

55
local M = {}
66

7-
local function is_git(path)
8-
return vim.fn.fnamemodify(path, ":t") == ".git"
9-
end
10-
11-
local IGNORED_PATHS = {
7+
M.ignore_dirs = {
128
-- disable watchers on kernel filesystems
139
-- which have a lot of unwanted events
1410
"/sys",
1511
"/proc",
1612
"/dev",
1713
}
1814

19-
local function is_folder_ignored(path)
20-
for _, folder in ipairs(IGNORED_PATHS) do
21-
if vim.startswith(path, folder) then
22-
return true
23-
end
24-
end
15+
function M.ignore_dir(path)
16+
table.insert(M.ignore_dirs, path)
17+
end
2518

19+
local function is_folder_ignored(path)
2620
for _, ignore_dir in ipairs(M.ignore_dirs) do
2721
if vim.fn.match(path, ignore_dir) ~= -1 then
2822
return true
@@ -44,7 +38,7 @@ function M.create_watcher(node)
4438
path = node.absolute_path
4539
end
4640

47-
if is_git(path) or is_folder_ignored(path) then
41+
if is_folder_ignored(path) then
4842
return nil
4943
end
5044

@@ -74,7 +68,7 @@ end
7468
function M.setup(opts)
7569
M.enabled = opts.filesystem_watchers.enable
7670
M.debounce_delay = opts.filesystem_watchers.debounce_delay
77-
M.ignore_dirs = opts.filesystem_watchers.ignore_dirs
71+
M.ignore_dirs = vim.tbl_extend("force", M.ignore_dirs, opts.filesystem_watchers.ignore_dirs)
7872
M.uid = 0
7973
end
8074

lua/nvim-tree/git/init.lua

+25-12
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ local log = require "nvim-tree.log"
22
local utils = require "nvim-tree.utils"
33
local git_utils = require "nvim-tree.git.utils"
44
local Runner = require "nvim-tree.git.runner"
5+
local Watch = require "nvim-tree.explorer.watch"
56
local Watcher = require("nvim-tree.watcher").Watcher
67
local Iterator = require "nvim-tree.iterators.node-iterator"
78
local explorer_node = require "nvim-tree.explorer.node"
@@ -164,22 +165,34 @@ function M.load_project_status(cwd)
164165
}
165166

166167
local watcher = nil
168+
167169
if M.config.filesystem_watchers.enable then
168170
log.line("watcher", "git start")
169171

170-
local callback = function(w)
171-
log.line("watcher", "git event scheduled '%s'", w.project_root)
172-
utils.debounce("git:watcher:" .. w.project_root, M.config.filesystem_watchers.debounce_delay, function()
173-
if w.destroyed then
174-
return
175-
end
176-
reload_tree_at(w.project_root)
177-
end)
178-
end
172+
local git_directory = git_utils.get_git_directory(project_root)
173+
174+
if git_directory == nil then
175+
log.line("watcher", "could not found the location of .git folder")
176+
else
177+
local callback = function(w)
178+
log.line("watcher", "git event scheduled '%s'", w.project_root)
179+
utils.debounce("git:watcher:" .. w.project_root, M.config.filesystem_watchers.debounce_delay, function()
180+
if w.destroyed then
181+
return
182+
end
183+
reload_tree_at(w.project_root)
184+
end)
185+
end
186+
187+
-- Add GIT_DIR to the list of directory to ignore
188+
-- local base_gitdir = utils.path_basename(git_directory)
189+
-- Watch.ignore_dir(base_gitdir)
190+
Watch.ignore_dir(git_directory)
179191

180-
watcher = Watcher:new(utils.path_join { project_root, ".git" }, WATCHED_FILES, callback, {
181-
project_root = project_root,
182-
})
192+
watcher = Watcher:new(git_directory, WATCHED_FILES, callback, {
193+
project_root = project_root,
194+
})
195+
end
183196
end
184197

185198
M.projects[project_root] = {

lua/nvim-tree/git/utils.lua

+28-12
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
local M = {}
22
local log = require "nvim-tree.log"
3-
4-
local has_cygpath = vim.fn.executable "cygpath" == 1
3+
local utils = require "nvim-tree.utils"
54

65
function M.get_toplevel(cwd)
76
local profile = log.profile_start("git toplevel %s", cwd)
@@ -18,16 +17,9 @@ function M.get_toplevel(cwd)
1817
return nil
1918
end
2019

21-
-- git always returns path with forward slashes
22-
if vim.fn.has "win32" == 1 then
23-
-- msys2 git support
24-
if has_cygpath then
25-
toplevel = vim.fn.system("cygpath -w " .. vim.fn.shellescape(toplevel))
26-
if vim.v.shell_error ~= 0 then
27-
return nil
28-
end
29-
end
30-
toplevel = toplevel:gsub("/", "\\")
20+
toplevel = utils.norm_path(toplevel)
21+
if toplevel == nil then
22+
return nil
3123
end
3224

3325
-- remove newline
@@ -94,4 +86,28 @@ function M.file_status_to_dir_status(status, cwd)
9486
return r
9587
end
9688

89+
function M.get_git_directory(cwd)
90+
local profile = log.profile_start("git directory %s", cwd)
91+
92+
local cmd = { "git", "-C", cwd, "rev-parse", "--absolute-git-dir" }
93+
log.line("git", vim.inspect(cmd))
94+
95+
local git_dir = vim.fn.system(cmd)
96+
97+
log.raw("git", git_dir)
98+
log.profile_end(profile)
99+
100+
if vim.v.shell_error ~= 0 or not git_dir or #git_dir == 0 or git_dir:match "fatal" then
101+
return nil
102+
end
103+
104+
git_dir = utils.norm_path(git_dir)
105+
if git_dir == nil then
106+
return nil
107+
end
108+
109+
-- remove newline
110+
return git_dir:sub(0, -2)
111+
end
112+
97113
return M

lua/nvim-tree/utils.lua

+22-1
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ local M = {
55
debouncers = {},
66
}
77

8+
local has_cygpath = vim.fn.executable "cygpath" == 1
9+
810
M.is_unix = vim.fn.has "unix" == 1
911
M.is_macos = vim.fn.has "mac" == 1 or vim.fn.has "macunix" == 1
1012
M.is_wsl = vim.fn.has "wsl" == 1
11-
-- false for WSL
1213
M.is_windows = vim.fn.has "win32" == 1 or vim.fn.has "win32unix" == 1
1314

1415
function M.str_find(haystack, needle)
@@ -38,6 +39,26 @@ function M.path_split(path)
3839
return path:gmatch("[^" .. path_separator .. "]+" .. path_separator .. "?")
3940
end
4041

42+
--- Normalise a path:
43+
--- windows: replace slashes with backslashes
44+
--- cygwin: resolve path first via cygpath
45+
--- @param path string
46+
--- @return string|nil nil on cygpath failure
47+
function M.norm_path(path)
48+
if M.is_windows then
49+
-- msys2 git support
50+
if has_cygpath then
51+
path = vim.fn.system("cygpath -w " .. vim.fn.shellescape(path))
52+
if vim.v.shell_error ~= 0 then
53+
return nil
54+
end
55+
end
56+
path = path:gsub("/", "\\")
57+
end
58+
59+
return path
60+
end
61+
4162
---Get the basename of the given path.
4263
---@param path string
4364
---@return string

0 commit comments

Comments
 (0)