Skip to content

Commit 1d14453

Browse files
committed
Live Grep from Git root falls back to cwd on special buffers
1 parent bced1c2 commit 1d14453

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

init.lua

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -313,18 +313,21 @@ pcall(require('telescope').load_extension, 'fzf')
313313
local function find_git_root()
314314
-- Use the current buffer's path as the starting point for the git search
315315
local current_file = vim.api.nvim_buf_get_name(0)
316+
local current_dir
317+
local cwd = vim.fn.getcwd()
316318
-- If the buffer is not associated with a file, return nil
317319
if current_file == "" then
318-
print("Buffer is not associated with a file")
319-
return nil
320+
current_dir = cwd
321+
else
322+
-- Extract the directory from the current file's path
323+
current_dir = vim.fn.fnamemodify(current_file, ":h")
320324
end
321-
-- Extract the directory from the current file's path
322-
local current_dir = vim.fn.fnamemodify(current_file, ":h")
325+
323326
-- Find the Git root directory from the current file's path
324327
local git_root = vim.fn.systemlist("git -C " .. vim.fn.escape(current_dir, " ") .. " rev-parse --show-toplevel")[1]
325328
if vim.v.shell_error ~= 0 then
326-
print("Not a git repository")
327-
return nil
329+
print("Not a git repository. Searching on current working directory")
330+
return cwd
328331
end
329332
return git_root
330333
end

0 commit comments

Comments
 (0)