@@ -12,10 +12,32 @@ local M = {}
12
12
local cwd_watcher --- @type uv.uv_fs_event_t ?
13
13
14
14
--- @async
15
- local function update_cwd_head ()
16
- if not uv .cwd () then
15
+ --- @return string gitdir
16
+ --- @return string head
17
+ local function get_gitdir_and_head ()
18
+ local cwd = assert (uv .cwd ())
19
+
20
+ -- Look in the cache first
21
+ for _ , bcache in pairs (require (' gitsigns.cache' ).cache ) do
22
+ local repo = bcache .git_obj .repo
23
+ if repo .toplevel == cwd then
24
+ return repo .gitdir , repo .abbrev_head
25
+ end
26
+ end
27
+
28
+ local info = require (' gitsigns.git' ).get_repo_info (cwd )
29
+ async .scheduler ()
30
+
31
+ return info .gitdir , info .abbrev_head
32
+ end
33
+
34
+ local update_cwd_head = async .create (function ()
35
+ local cwd = uv .cwd ()
36
+
37
+ if not cwd then
17
38
return
18
39
end
40
+
19
41
local paths = vim .fs .find (' .git' , {
20
42
limit = 1 ,
21
43
upward = true ,
@@ -26,37 +48,7 @@ local function update_cwd_head()
26
48
return
27
49
end
28
50
29
- if cwd_watcher then
30
- cwd_watcher :stop ()
31
- else
32
- cwd_watcher = assert (uv .new_fs_event ())
33
- end
34
-
35
- local cwd = assert (uv .cwd ())
36
- --- @type string , string
37
- local gitdir , head
38
-
39
- local gs_cache = require (' gitsigns.cache' )
40
-
41
- -- Look in the cache first
42
- for _ , bcache in pairs (gs_cache .cache ) do
43
- local repo = bcache .git_obj .repo
44
- if repo .toplevel == cwd then
45
- head = repo .abbrev_head
46
- gitdir = repo .gitdir
47
- break
48
- end
49
- end
50
-
51
- local git = require (' gitsigns.git' )
52
-
53
- if not head or not gitdir then
54
- local info = git .get_repo_info (cwd )
55
- gitdir = info .gitdir
56
- head = info .abbrev_head
57
- end
58
-
59
- async .scheduler ()
51
+ local gitdir , head = get_gitdir_and_head ()
60
52
61
53
api .nvim_exec_autocmds (' User' , {
62
54
pattern = ' GitSignsUpdate' ,
@@ -71,6 +63,12 @@ local function update_cwd_head()
71
63
72
64
local towatch = gitdir .. ' /HEAD'
73
65
66
+ if cwd_watcher then
67
+ cwd_watcher :stop ()
68
+ else
69
+ cwd_watcher = assert (uv .new_fs_event ())
70
+ end
71
+
74
72
if cwd_watcher :getpath () == towatch then
75
73
-- Already watching
76
74
return
@@ -81,6 +79,7 @@ local function update_cwd_head()
81
79
local update_head = debounce_trailing (
82
80
100 ,
83
81
async .create (function ()
82
+ local git = require (' gitsigns.git' )
84
83
local new_head = git .get_repo_info (cwd ).abbrev_head
85
84
async .scheduler ()
86
85
vim .g .gitsigns_head = new_head
@@ -102,7 +101,7 @@ local function update_cwd_head()
102
101
update_head ()
103
102
end )
104
103
)
105
- end
104
+ end )
106
105
107
106
local function setup_cli ()
108
107
api .nvim_create_user_command (' Gitsigns' , function (params )
@@ -128,8 +127,6 @@ local function setup_attach()
128
127
return
129
128
end
130
129
131
- async .scheduler ()
132
-
133
130
local attach_autocmd_disabled = false
134
131
135
132
api .nvim_create_autocmd ({ ' BufRead' , ' BufNewFile' , ' BufWritePost' }, {
@@ -165,13 +162,11 @@ local function setup_attach()
165
162
end
166
163
end
167
164
168
- --- @async
169
165
local function setup_cwd_head ()
170
- async .scheduler ()
171
- update_cwd_head ()
172
-
173
166
local debounce = require (' gitsigns.debounce' ).debounce_trailing
174
- local update_cwd_head_debounced = debounce (100 , async .create (update_cwd_head ))
167
+ local update_cwd_head_debounced = debounce (100 , update_cwd_head )
168
+
169
+ update_cwd_head_debounced ()
175
170
176
171
-- Need to debounce in case some plugin changes the cwd too often
177
172
-- (like vim-grepper)
185
180
186
181
--- Setup and start Gitsigns.
187
182
---
188
- --- Attributes: ~
189
- --- {async}
190
- ---
191
183
--- @param cfg table | nil Configuration for Gitsigns.
192
184
--- See |gitsigns-usage| for more details.
193
- M .setup = async . create ( 1 , function (cfg )
185
+ function M .setup (cfg )
194
186
gs_config .build (cfg )
195
187
196
188
if vim .fn .executable (' git' ) == 0 then
@@ -200,16 +192,12 @@ M.setup = async.create(1, function(cfg)
200
192
201
193
api .nvim_create_augroup (' gitsigns' , {})
202
194
203
- if vim .fn .has (' nvim-0.9' ) == 0 then
204
- require (' gitsigns.git.version' ).check ()
205
- end
206
-
207
195
setup_debug ()
208
196
setup_cli ()
209
197
require (' gitsigns.highlight' ).setup ()
210
198
setup_attach ()
211
199
setup_cwd_head ()
212
- end )
200
+ end
213
201
214
202
return setmetatable (M , {
215
203
__index = function (_ , f )
0 commit comments