Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

git_status { use_file_path = true } on worktree #3409

Open
P4Cu opened this issue Feb 10, 2025 · 6 comments · May be fixed by #3417
Open

git_status { use_file_path = true } on worktree #3409

P4Cu opened this issue Feb 10, 2025 · 6 comments · May be fixed by #3417
Labels
bug Something isn't working

Comments

@P4Cu
Copy link

P4Cu commented Feb 10, 2025

Description

I've noticed that when I try to :lua require("telescope.builtin").git_status { use_file_path = true } for a git-worktree I get:

E5108: Error executing lua: ...data/lazy/telescope.nvim/lua/telescope/builtin/__git.lua:443: attempt to concatenate local 'worktrees' (a nil value)
stack traceback:
...data/lazy/telescope.nvim/lua/telescope/builtin/__git.lua:443: in function 'try_worktrees'...data/lazy/telescope.nvim/lua/telescope/builtin/__git.lua:477: in function 'set_opts_cwd'
...data/lazy/telescope.nvim/lua/telescope/builtin/__git.lua:507: in function 'v'
...-data/lazy/telescope.nvim/lua/telescope/builtin/init.lua:587: in function 'git_status'
...-data/lazy/AstroNvim/lua/astronvim/plugins/telescope.lua:27: in function <...-data/lazy/AstroNvim/lua/astronvim/plugins/telescope.lua:27>

Doing :lua require("telescope.builtin").git_status { use_file_path = false } works just fine.

use_file_path = true unfortunately is a default of Astronvim <leader>gt binding.

Neovim version

NVIM v0.10.2
Build type: Release
LuaJIT 2.1.1713484068
Run "nvim -V1 -v" for more info

Operating system and version

windows 11

Telescope version / branch / rev

415af52

checkhealth telescope

==============================================================================
telescope: health#telescope#check

Checking for required plugins ~
- OK plenary installed.
- OK nvim-treesitter installed.

Checking external dependencies ~
- OK rg: found ripgrep 14.1.1 (rev 4649aa9700)
- OK fd: found fd 10.2.0

===== Installed extensions ===== ~

Telescope Extension: `aerial` ~
- No healthcheck provided

Telescope Extension: `dap` ~
- No healthcheck provided

Telescope Extension: `fzf` ~
- OK lib working as expected
- OK file_sorter correctly configured
- OK generic_sorter correctly configured

Telescope Extension: `notify` ~
- No healthcheck provided

Steps to reproduce

  • nvim -nu minimal.lu
  • cd /some/git/worktree/path
  • :lua require("telescope.builtin").git_status { use_file_path = true } FAIL
  • :lua require("telescope.builtin").git_status { use_file_path = true } WORKS

Expected behavior

<worktree>/.git file is resolved and path for git is found.

Actual behavior

Fails with message.

Minimal config

local root = vim.fn.fnamemodify("./.repro", ":p")

-- set stdpaths to use .repro
for _, name in ipairs { "config", "data", "state", "cache" } do
  vim.env[("XDG_%s_HOME"):format(name:upper())] = root .. "/" .. name
end

-- bootstrap lazy
local lazypath = root .. "/plugins/lazy.nvim"
if not vim.uv.fs_stat(lazypath) then
  vim.fn.system {
    "git",
    "clone",
    "--filter=blob:none",
    "https://github.com/folke/lazy.nvim.git",
    lazypath,
  }
end
vim.opt.runtimepath:prepend(lazypath)

-- install plugins
local plugins = {
  {
    "nvim-telescope/telescope.nvim",
    dependencies = {
      "nvim-lua/plenary.nvim",
    },
    config = function()
      -- ADD INIT.LUA SETTINGS THAT ARE _NECESSARY_ FOR REPRODUCING THE ISSUE
      require("telescope").setup {}
    end,
  },
}

require("lazy").setup(plugins, {
  root = root .. "/plugins",
})
@P4Cu P4Cu added the bug Something isn't working label Feb 10, 2025
@P4Cu
Copy link
Author

P4Cu commented Feb 10, 2025

If somebody points me how the path should be resolved I might prepare a PR or maybe it's actually a good case to try out aider.chat for a first time :)

@P4Cu
Copy link
Author

P4Cu commented Feb 12, 2025

So the solution is super simple and only bound to that command. We're looking for .git directory while for worktrees it's a .git file.
IMHO better solution is to use git rev-parse --show-toplevel which we already have used in another place.
I'll create a PR with that change soon.

diff --git a/lua/telescope/builtin/__git.lua b/lua/telescope/builtin/__git.lua
index b242670..23294b0 100644
--- a/lua/telescope/builtin/__git.lua
+++ b/lua/telescope/builtin/__git.lua
@@ -458,11 +458,13 @@ local try_worktrees = function(opts)
 end

 local current_path_toplevel = function()
-  local gitdir = vim.fn.finddir(".git", vim.fn.expand "%:p" .. ";")
-  if gitdir == "" then
+  local gitdir, ret, _ = utils.get_os_command_output(
+    {"git", "rev-parse", "--show-toplevel"},
+    vim.fn.expand("%:p:h"))
+  if ret ~= 0 or gitdir == nil then
     return nil
   end
-  return Path:new(gitdir):parent():absolute()
+  return Path:new(gitdir[1]):absolute()
 end

 local set_opts_cwd = function(opts)

@xieyonn
Copy link

xieyonn commented Feb 14, 2025

neovim has a builtin api vim.fs.root({source}, {marker}) since 0.9.0, used to search parrent dir with marker, marker can be dir or file

So a simpler method:

local gitdir = vim.fs.root(0, '.git')

this work both on dir .git/ and file .git

@P4Cu
Copy link
Author

P4Cu commented Feb 15, 2025

Simpler, but a bit less portable due to that fact. Either of those fixes is good to me so I can update PR.

@P4Cu P4Cu linked a pull request Feb 16, 2025 that will close this issue
4 tasks
@P4Cu
Copy link
Author

P4Cu commented Feb 16, 2025

@xieyonn Yeah, your approach is a lot shorter == better : #3417

@P4Cu
Copy link
Author

P4Cu commented Feb 16, 2025

Hey @Conni2461 I think this is ready for review/merge.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
2 participants