From 9a05b9e9f928856ca23dbf876fab372003180c3f Mon Sep 17 00:00:00 2001 From: Tomasz N Date: Tue, 5 Aug 2025 05:23:51 +0200 Subject: [PATCH] perf(#3171): use vim.system() instead of vim.fn.system() to execute git toplevel (#3175) * fix(#3171): use vim.system() to determine git toplevel * Don't use vim.trim * Ensure sdtout is a string * Keep Nvim 0.9 compatibility * Use vim.system to query git config for status.showUntrackedFiles too --- lua/nvim-tree/git/utils.lua | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/lua/nvim-tree/git/utils.lua b/lua/nvim-tree/git/utils.lua index b805ebfe..1e3444c7 100644 --- a/lua/nvim-tree/git/utils.lua +++ b/lua/nvim-tree/git/utils.lua @@ -5,6 +5,19 @@ local M = { use_cygpath = false, } +--- Execute system command +---@param cmd string[] +---@return string stdout +---@return integer exit code +local function system(cmd) + if vim.fn.has("nvim-0.10") == 1 then + local obj = vim.system(cmd):wait() + return obj.stdout or "", obj.code + else + return vim.fn.system(cmd), vim.v.shell_error + end +end + --- Retrieve the git toplevel directory ---@param cwd string path ---@return string|nil toplevel absolute path @@ -16,12 +29,12 @@ function M.get_toplevel(cwd) local cmd = { "git", "-C", cwd, "rev-parse", "--show-toplevel", "--absolute-git-dir" } log.line("git", "%s", table.concat(cmd, " ")) - local out = vim.fn.system(cmd) + local out, exitCode = system(cmd) log.raw("git", out) log.profile_end(profile) - if vim.v.shell_error ~= 0 or not out or #out == 0 or out:match("fatal") then + if exitCode ~= 0 or not out or #out == 0 or out:match("fatal") then return nil, nil end @@ -73,7 +86,7 @@ function M.should_show_untracked(cwd) local cmd = { "git", "-C", cwd, "config", "status.showUntrackedFiles" } log.line("git", table.concat(cmd, " ")) - local has_untracked = vim.fn.system(cmd) + local has_untracked = system(cmd) log.raw("git", has_untracked) log.profile_end(profile)