From debf5816eee21b7d51025ef17ba0647386226cb5 Mon Sep 17 00:00:00 2001 From: Jean-Sebastien Trottier Date: Mon, 22 Jul 2024 04:06:19 -0400 Subject: [PATCH] fix(install): abort installation using git in active git session Problem: Installing a parser using git in an active git session (e.g., when editing a commit message) can corrupt the corresponding repository. Solution: Check for typical environment variables first and abort installation if found. --- .../shell_command_selectors.lua | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/lua/nvim-treesitter/shell_command_selectors.lua b/lua/nvim-treesitter/shell_command_selectors.lua index 9622f5801..f95c84ae8 100644 --- a/lua/nvim-treesitter/shell_command_selectors.lua +++ b/lua/nvim-treesitter/shell_command_selectors.lua @@ -289,6 +289,31 @@ function M.select_download_commands(repo, project_name, cache_folder, revision, local git_folder = utils.join_path(cache_folder, project_name) local clone_error = "Error during download, please verify your internet connection" + -- Running `git clone` or `git checkout` while running under Git (such as + -- editing a `git commit` message) will likely fail to install parsers + -- (such as 'gitcommit') and can also corrupt the index file of the current + -- Git repository. Check for typical git environemnt variables and abort if found. + for _, k in pairs { + "GIT_ALTERNATE_OBJECT_DIRECTORIES", + "GIT_CEILING_DIRECTORIES", + "GIT_DIR", + "GIT_INDEX", + "GIT_INDEX_FILE", + "GIT_OBJECT_DIRECTORY", + "GIT_PREFIX", + "GIT_WORK_TREE", + } do + if vim.uv.os_getenv(k) then + vim.api.nvim_err_writeln( + string.format( + "Cannot install %s with git in an active git session. Exit the session and run ':TSInstall %s' manually", + project_name + ) + ) + return {} + end + end + return { { cmd = "git",