Skip to content

Commit

Permalink
feat(setup)!: make setup() synchronous
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
lewis6991 committed May 28, 2024
1 parent cdfcd9d commit 720061a
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 61 deletions.
5 changes: 1 addition & 4 deletions doc/gitsigns.txt
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,9 @@ Note functions with the {async} attribute are run asynchronously and accept
an optional {callback} argument.


setup({cfg}, {callback?}) *gitsigns.setup()*
setup({cfg}) *gitsigns.setup()*
Setup and start Gitsigns.

Attributes: ~
{async}

Parameters: ~
{cfg} (table|nil): Configuration for Gitsigns.
See |gitsigns-usage| for more details.
Expand Down
88 changes: 38 additions & 50 deletions lua/gitsigns.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,32 @@ local M = {}
local cwd_watcher ---@type uv.uv_fs_event_t?

--- @async
local function update_cwd_head()
if not uv.cwd() then
--- @return string gitdir
--- @return string head
local function get_gitdir_and_head()
local cwd = assert(uv.cwd())

-- Look in the cache first
for _, bcache in pairs(require('gitsigns.cache').cache) do
local repo = bcache.git_obj.repo
if repo.toplevel == cwd then
return repo.gitdir, repo.abbrev_head
end
end

local info = require('gitsigns.git').get_repo_info(cwd)
async.scheduler()

return info.gitdir, info.abbrev_head
end

local update_cwd_head = async.create(function()
local cwd = uv.cwd()

if not cwd then
return
end

local paths = vim.fs.find('.git', {
limit = 1,
upward = true,
Expand All @@ -26,37 +48,7 @@ local function update_cwd_head()
return
end

if cwd_watcher then
cwd_watcher:stop()
else
cwd_watcher = assert(uv.new_fs_event())
end

local cwd = assert(uv.cwd())
--- @type string, string
local gitdir, head

local gs_cache = require('gitsigns.cache')

-- Look in the cache first
for _, bcache in pairs(gs_cache.cache) do
local repo = bcache.git_obj.repo
if repo.toplevel == cwd then
head = repo.abbrev_head
gitdir = repo.gitdir
break
end
end

local git = require('gitsigns.git')

if not head or not gitdir then
local info = git.get_repo_info(cwd)
gitdir = info.gitdir
head = info.abbrev_head
end

async.scheduler()
local gitdir, head = get_gitdir_and_head()

api.nvim_exec_autocmds('User', {
pattern = 'GitSignsUpdate',
Expand All @@ -71,6 +63,12 @@ local function update_cwd_head()

local towatch = gitdir .. '/HEAD'

if cwd_watcher then
cwd_watcher:stop()
else
cwd_watcher = assert(uv.new_fs_event())
end

if cwd_watcher:getpath() == towatch then
-- Already watching
return
Expand All @@ -81,6 +79,7 @@ local function update_cwd_head()
local update_head = debounce_trailing(
100,
async.create(function()
local git = require('gitsigns.git')
local new_head = git.get_repo_info(cwd).abbrev_head
async.scheduler()
vim.g.gitsigns_head = new_head
Expand All @@ -102,7 +101,7 @@ local function update_cwd_head()
update_head()
end)
)
end
end)

local function setup_cli()
api.nvim_create_user_command('Gitsigns', function(params)
Expand All @@ -128,8 +127,6 @@ local function setup_attach()
return
end

async.scheduler()

local attach_autocmd_disabled = false

api.nvim_create_autocmd({ 'BufRead', 'BufNewFile', 'BufWritePost' }, {
Expand Down Expand Up @@ -165,13 +162,11 @@ local function setup_attach()
end
end

--- @async
local function setup_cwd_head()
async.scheduler()
update_cwd_head()

local debounce = require('gitsigns.debounce').debounce_trailing
local update_cwd_head_debounced = debounce(100, async.create(update_cwd_head))
local update_cwd_head_debounced = debounce(100, update_cwd_head)

update_cwd_head_debounced()

-- Need to debounce in case some plugin changes the cwd too often
-- (like vim-grepper)
Expand All @@ -185,12 +180,9 @@ end

--- Setup and start Gitsigns.
---
--- Attributes: ~
--- {async}
---
--- @param cfg table|nil Configuration for Gitsigns.
--- See |gitsigns-usage| for more details.
M.setup = async.create(1, function(cfg)
function M.setup(cfg)
gs_config.build(cfg)

if vim.fn.executable('git') == 0 then
Expand All @@ -200,16 +192,12 @@ M.setup = async.create(1, function(cfg)

api.nvim_create_augroup('gitsigns', {})

if vim.fn.has('nvim-0.9') == 0 then
require('gitsigns.git.version').check()
end

setup_debug()
setup_cli()
require('gitsigns.highlight').setup()
setup_attach()
setup_cwd_head()
end)
end

return setmetatable(M, {
__index = function(_, f)
Expand Down
2 changes: 2 additions & 0 deletions lua/gitsigns/git/version.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ local function parse_version(version)
return ret
end

--- @async
local function set_version()
local version = gs_config.config._git_version
if version ~= 'auto' then
Expand Down Expand Up @@ -70,6 +71,7 @@ local function set_version()
M.version = parse_version(parts[3])
end

--- @async
--- Usage: check_version{2,3}
--- @param version {[1]: integer, [2]:integer, [3]:integer}?
--- @return boolean
Expand Down
8 changes: 1 addition & 7 deletions test/gs_helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -259,17 +259,11 @@ function M.setup_gitsigns(config, on_attach)
return false
end
end
_SETUP_DONE = false
require('gitsigns').setup(config, function()
_SETUP_DONE = true
end)
require('gitsigns').setup(config)
]],
config,
on_attach
)
M.expectf(function()
return exec_lua([[return _SETUP_DONE]])
end)
end

--- @param status table<string,string|integer>
Expand Down

0 comments on commit 720061a

Please sign in to comment.