From 720061aa152faedfe4099dfb92d2b3fcb0e55edc Mon Sep 17 00:00:00 2001 From: Lewis Russell Date: Thu, 23 May 2024 11:02:42 +0100 Subject: [PATCH] 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. --- doc/gitsigns.txt | 5 +- lua/gitsigns.lua | 88 ++++++++++++++++-------------------- lua/gitsigns/git/version.lua | 2 + test/gs_helpers.lua | 8 +--- 4 files changed, 42 insertions(+), 61 deletions(-) diff --git a/doc/gitsigns.txt b/doc/gitsigns.txt index 4393273a2..abfc63e72 100644 --- a/doc/gitsigns.txt +++ b/doc/gitsigns.txt @@ -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. diff --git a/lua/gitsigns.lua b/lua/gitsigns.lua index 9823593f1..ce158e6af 100644 --- a/lua/gitsigns.lua +++ b/lua/gitsigns.lua @@ -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, @@ -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', @@ -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 @@ -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 @@ -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) @@ -128,8 +127,6 @@ local function setup_attach() return end - async.scheduler() - local attach_autocmd_disabled = false api.nvim_create_autocmd({ 'BufRead', 'BufNewFile', 'BufWritePost' }, { @@ -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) @@ -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 @@ -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) diff --git a/lua/gitsigns/git/version.lua b/lua/gitsigns/git/version.lua index fedcc954d..92f9c8ba7 100644 --- a/lua/gitsigns/git/version.lua +++ b/lua/gitsigns/git/version.lua @@ -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 @@ -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 diff --git a/test/gs_helpers.lua b/test/gs_helpers.lua index afae0e4a4..2841c0c62 100644 --- a/test/gs_helpers.lua +++ b/test/gs_helpers.lua @@ -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