11local vim = vim
22local events = require (" neo-tree.events" )
33local log = require (" neo-tree.log" )
4+ local git = require (" neo-tree.git" )
5+ local utils = require (" neo-tree.utils" )
46
57local M = {}
68
@@ -20,6 +22,18 @@ local flags = {
2022
2123local watched = {}
2224
25+ local get_dot_git_folder = function (path )
26+ local git_root = git .get_repository_root (path )
27+ if git_root then
28+ local git_folder = utils .path_join (git_root , " .git" )
29+ local stat = vim .loop .fs_stat (git_folder )
30+ if stat and stat .type == " directory" then
31+ return git_folder , git_root
32+ end
33+ end
34+ return nil , nil
35+ end
36+
2337M .show_watched = function ()
2438 local items = {}
2539 for _ , handle in pairs (watched ) do
3044
3145--- Watch a directory for changes to it's children. Not recursive.
3246--- @param path string The directory to watch.
33- M .watch_folder = function (path )
34- if path :find (" /%.git$" ) or path :find (" /%.git/" ) then
35- -- git folders seem to throw off fs events constantly.
36- log .debug (" watch_folder(path): Skipping git folder: " , path )
37- return
47+ M .watch_folder = function (path , git_watch_callback )
48+ if not git_watch_callback then
49+ if path :find (" /%.git$" ) or path :find (" /%.git/" ) then
50+ -- git folders seem to throw off fs events constantly.
51+ log .debug (" watch_folder(path): Skipping git folder: " , path )
52+ return
53+ end
3854 end
3955 local h = watched [path ]
4056 if h == nil then
@@ -45,13 +61,31 @@ M.watch_folder = function(path)
4561 path = path ,
4662 references = 1 ,
4763 }
48- w :start (path , flags , fs_event_callback )
64+ w :start (path , flags , git_watch_callback or fs_event_callback )
4965 else
5066 log .trace (" Incrementing references for fs watch on: " , path )
5167 h .references = h .references + 1
5268 end
5369end
5470
71+ M .watch_git_index = function (path )
72+ local git_folder , git_root = get_dot_git_folder (path )
73+ if git_folder then
74+ local git_event_callback = vim .schedule_wrap (function (err , fname )
75+ if fname and fname :match (" ^.+%.lock$" ) then
76+ return
77+ end
78+ if err then
79+ log .error (" git_event_callback: " , err )
80+ return
81+ end
82+ events .fire_event (events .GIT_EVENT , { path = fname , repository = git_root })
83+ end )
84+
85+ M .watch_folder (git_folder , git_event_callback )
86+ end
87+ end
88+
5589--- Stop watching a directory. If there are no more references to the handle,
5690--- it will be destroyed. Otherwise, the reference count will be decremented.
5791--- @param path string The directory to stop watching.
@@ -72,6 +106,13 @@ M.unwatch_folder = function(path)
72106 end
73107end
74108
109+ M .unwatch_git_index = function (path )
110+ local git_folder = get_dot_git_folder (path )
111+ if git_folder then
112+ M .unwatch_folder (git_folder )
113+ end
114+ end
115+
75116--- Stop watching all directories. This is the nuclear option and it affects all
76117--- sources.
77118M .unwatch_all = function ()
0 commit comments