Skip to content

Commit 720061a

Browse files
committed
feat(setup)!: make setup() synchronous
Previously `setup()` was asynchronous in order to run a system command to check the git version. As support for v0.8 is dropped, this is no longer required.
1 parent cdfcd9d commit 720061a

File tree

4 files changed

+42
-61
lines changed

4 files changed

+42
-61
lines changed

doc/gitsigns.txt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,12 +93,9 @@ Note functions with the {async} attribute are run asynchronously and accept
9393
an optional {callback} argument.
9494

9595

96-
setup({cfg}, {callback?}) *gitsigns.setup()*
96+
setup({cfg}) *gitsigns.setup()*
9797
Setup and start Gitsigns.
9898

99-
Attributes: ~
100-
{async}
101-
10299
Parameters: ~
103100
{cfg} (table|nil): Configuration for Gitsigns.
104101
See |gitsigns-usage| for more details.

lua/gitsigns.lua

Lines changed: 38 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,32 @@ local M = {}
1212
local 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

107106
local 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
166163
end
167164

168-
--- @async
169165
local 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)
@@ -185,12 +180,9 @@ end
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

214202
return setmetatable(M, {
215203
__index = function(_, f)

lua/gitsigns/git/version.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ local function parse_version(version)
3434
return ret
3535
end
3636

37+
--- @async
3738
local function set_version()
3839
local version = gs_config.config._git_version
3940
if version ~= 'auto' then
@@ -70,6 +71,7 @@ local function set_version()
7071
M.version = parse_version(parts[3])
7172
end
7273

74+
--- @async
7375
--- Usage: check_version{2,3}
7476
--- @param version {[1]: integer, [2]:integer, [3]:integer}?
7577
--- @return boolean

test/gs_helpers.lua

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -259,17 +259,11 @@ function M.setup_gitsigns(config, on_attach)
259259
return false
260260
end
261261
end
262-
_SETUP_DONE = false
263-
require('gitsigns').setup(config, function()
264-
_SETUP_DONE = true
265-
end)
262+
require('gitsigns').setup(config)
266263
]],
267264
config,
268265
on_attach
269266
)
270-
M.expectf(function()
271-
return exec_lua([[return _SETUP_DONE]])
272-
end)
273267
end
274268

275269
--- @param status table<string,string|integer>

0 commit comments

Comments
 (0)