Skip to content

Commit 5860ac3

Browse files
author
Damien MEHALA
committed
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
1 parent bcb969c commit 5860ac3

File tree

4 files changed

+65
-28
lines changed

4 files changed

+65
-28
lines changed

lua/nvim-tree/explorer/explore.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ local function populate_children(handle, cwd, node, git_status)
3030

3131
t = get_type_from(t, abs)
3232
if
33-
not filters.should_filter(abs, filter_status)
33+
abs ~= git_status.git_dir
34+
and not filters.should_filter(abs, filter_status)
3435
and not nodes_by_path[abs]
3536
and Watcher.is_fs_event_capable(abs)
3637
then

lua/nvim-tree/explorer/watch.lua

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@ 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-
117
local IGNORED_PATHS = {
128
-- disable watchers on kernel filesystems
139
-- which have a lot of unwanted events
@@ -44,7 +40,7 @@ function M.create_watcher(node)
4440
path = node.absolute_path
4541
end
4642

47-
if is_git(path) or is_folder_ignored(path) then
43+
if is_folder_ignored(path) then
4844
return nil
4945
end
5046

lua/nvim-tree/git/init.lua

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -143,29 +143,36 @@ function M.load_project_status(cwd)
143143
timeout = M.config.git.timeout,
144144
}
145145

146+
local git_directory = git_utils.get_git_directory(project_root)
147+
146148
local watcher = nil
147149
if M.config.filesystem_watchers.enable then
148150
log.line("watcher", "git start")
149151

150-
local callback = function(w)
151-
log.line("watcher", "git event scheduled '%s'", w.project_root)
152-
utils.debounce("git:watcher:" .. w.project_root, M.config.filesystem_watchers.debounce_delay, function()
153-
if w.destroyed then
154-
return
155-
end
156-
reload_tree_at(w.project_root)
157-
end)
158-
end
152+
if git_directory == nil then
153+
log.line("watcher", "could not found the location of .git folder")
154+
else
155+
local callback = function(w)
156+
log.line("watcher", "git event scheduled '%s'", w.project_root)
157+
utils.debounce("git:watcher:" .. w.project_root, M.config.filesystem_watchers.debounce_delay, function()
158+
if w.destroyed then
159+
return
160+
end
161+
reload_tree_at(w.project_root)
162+
end)
163+
end
159164

160-
watcher = Watcher:new(utils.path_join { project_root, ".git" }, WATCHED_FILES, callback, {
161-
project_root = project_root,
162-
})
165+
watcher = Watcher:new(git_directory, WATCHED_FILES, callback, {
166+
project_root = project_root,
167+
})
168+
end
163169
end
164170

165171
M.projects[project_root] = {
166172
files = git_status,
167173
dirs = git_utils.file_status_to_dir_status(git_status, project_root),
168174
watcher = watcher,
175+
git_dir = git_directory,
169176
}
170177
return M.projects[project_root]
171178
end

lua/nvim-tree/git/utils.lua

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,22 @@ local log = require "nvim-tree.log"
33

44
local has_cygpath = vim.fn.executable "cygpath" == 1
55

6+
local function norm_path(path)
7+
-- git always returns path with forward slashes
8+
if vim.fn.has "win32" == 1 then
9+
-- msys2 git support
10+
if has_cygpath then
11+
path = vim.fn.system("cygpath -w " .. vim.fn.shellescape(path))
12+
if vim.v.shell_error ~= 0 then
13+
return nil
14+
end
15+
end
16+
path = path:gsub("/", "\\")
17+
end
18+
19+
return path
20+
end
21+
622
function M.get_toplevel(cwd)
723
local profile = log.profile_start("git toplevel %s", cwd)
824

@@ -18,16 +34,9 @@ function M.get_toplevel(cwd)
1834
return nil
1935
end
2036

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("/", "\\")
37+
toplevel = norm_path(toplevel)
38+
if toplevel == nil then
39+
return nil
3140
end
3241

3342
-- remove newline
@@ -94,4 +103,28 @@ function M.file_status_to_dir_status(status, cwd)
94103
return r
95104
end
96105

106+
function M.get_git_directory(cwd)
107+
local profile = log.profile_start("git directory %s", cwd)
108+
109+
local cmd = { "git", "-C", cwd, "rev-parse", "--absolute-git-dir" }
110+
log.line("git", vim.inspect(cmd))
111+
112+
local git_dir = vim.fn.system(cmd)
113+
114+
log.raw("git", git_dir)
115+
log.profile_end(profile)
116+
117+
if vim.v.shell_error ~= 0 or not git_dir or #git_dir == 0 or git_dir:match "fatal" then
118+
return nil
119+
end
120+
121+
git_dir = norm_path(git_dir)
122+
if git_dir == nil then
123+
return nil
124+
end
125+
126+
-- remove newline
127+
return git_dir:sub(0, -2)
128+
end
129+
97130
return M

0 commit comments

Comments
 (0)