@@ -12,10 +12,32 @@ local M = {}
1212local  cwd_watcher  --- @type  uv.uv_fs_event_t ? 
1313
1414---  @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 
1738    return 
1839  end 
40+ 
1941  local  paths  =  vim .fs .find (' .git' 
2042    limit  =  1 ,
2143    upward  =  true ,
@@ -26,37 +48,7 @@ local function update_cwd_head()
2648    return 
2749  end 
2850
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 ()
6052
6153  api .nvim_exec_autocmds (' User' 
6254    pattern  =  ' GitSignsUpdate' 
@@ -71,6 +63,12 @@ local function update_cwd_head()
7163
7264  local  towatch  =  gitdir  ..  ' /HEAD' 
7365
66+   if  cwd_watcher  then 
67+     cwd_watcher :stop ()
68+   else 
69+     cwd_watcher  =  assert (uv .new_fs_event ())
70+   end 
71+ 
7472  if  cwd_watcher :getpath () ==  towatch  then 
7573    --  Already watching
7674    return 
@@ -81,6 +79,7 @@ local function update_cwd_head()
8179  local  update_head  =  debounce_trailing (
8280    100 ,
8381    async .create (function ()
82+       local  git  =  require (' gitsigns.git' 
8483      local  new_head  =  git .get_repo_info (cwd ).abbrev_head 
8584      async .scheduler ()
8685      vim .g .gitsigns_head  =  new_head 
@@ -102,7 +101,7 @@ local function update_cwd_head()
102101      update_head ()
103102    end )
104103  )
105- end 
104+ end ) 
106105
107106local  function  setup_cli ()
108107  api .nvim_create_user_command (' Gitsigns' function (params )
@@ -128,8 +127,6 @@ local function setup_attach()
128127    return 
129128  end 
130129
131-   async .scheduler ()
132- 
133130  local  attach_autocmd_disabled  =  false 
134131
135132  api .nvim_create_autocmd ({ ' BufRead' ' BufNewFile' ' BufWritePost'  
@@ -165,13 +162,11 @@ local function setup_attach()
165162  end 
166163end 
167164
168- ---  @async 
169165local  function  setup_cwd_head ()
170-   async .scheduler ()
171-   update_cwd_head ()
172- 
173166  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 ()
175170
176171  --  Need to debounce in case some plugin changes the cwd too often
177172  --  (like vim-grepper)
185180
186181---  Setup and start Gitsigns.
187182--- 
188- ---  Attributes: ~
189- ---      {async}
190- --- 
191183---  @param  cfg  table | nil Configuration for Gitsigns. 
192184---      See |gitsigns-usage| for more details.
193- M .setup   =   async . create ( 1 ,  function (cfg )
185+ function   M .setup (cfg )
194186  gs_config .build (cfg )
195187
196188  if  vim .fn .executable (' git' ==  0  then 
@@ -200,16 +192,12 @@ M.setup = async.create(1, function(cfg)
200192
201193  api .nvim_create_augroup (' gitsigns' 
202194
203-   if  vim .fn .has (' nvim-0.9' ==  0  then 
204-     require (' gitsigns.git.version' check ()
205-   end 
206- 
207195  setup_debug ()
208196  setup_cli ()
209197  require (' gitsigns.highlight' setup ()
210198  setup_attach ()
211199  setup_cwd_head ()
212- end ) 
200+ end 
213201
214202return  setmetatable (M , {
215203  __index  =  function (_ , f )
0 commit comments