How to conditionally load gitsigns only when current working directory is a git repository?

I manage gitsigns.nvim using packer.nvim & I would like to load gitsigns only when the current working directory is a local git repository. I tried configuring packer with the following code:

use({
    "lewis6991/gitsigns.nvim",
    config = function()
        require("gitsigns").setup()
    end,
    cond = function()
        if vim.api.nvim_command_output("!git rev-parse --is-inside-work-tree") == true then
            return true
        end
    end,
})

Yet, packer still loads gitsigns even when the current working directory isn’t a local git repository.

So, how do I go about configuring packer to only load gitsigns when the conditions are right?

Can you check if gitsigns is in ~/.local/share/nvim/site/pack/packer/start ?
If yes, I believe you should run a PackerUpdate so that it moves gitsigns to ~/.local/share/nvim/site/pack/packer/opt so that it can be sourced on demand.

I’m pretty sure packer downloaded gitsigns to the right folder i.e ~/.local/share/nvim/site/pack/packer/opt/gitsigns.nvim. See the screenshot(s) below:

gitsigns is in the opt folder:

image

gitsigns doesn’t exist in the start folder:

image

Yet, gitsigns is loaded in a “non-git repository”, see screenshot below for reference.

Also for reference, here’s proof of this said directory which is not a local git repository.

image

I’m probably misinterpreting something here (I guess?) because everything appears to be properly in place :sweat_smile: