Skip to content

fix(git): git watcher showing error for bare repositories #1819

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions lua/nvim-tree/git/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ function M.load_project_status(cwd)
return {}
end

local absolutegitdir = git_utils.get_absolutegitdir(cwd)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will break existing behavior since get_project_root() has a few more checks

function M.get_project_root(cwd)
if not M.config.git.enable then
return nil
end

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#1809 will be merged shortly, with some significant refactoring in this area.

We must merge master before testing this PR.


local status = M.projects[project_root]
if status then
return status
Expand All @@ -153,14 +155,15 @@ function M.load_project_status(cwd)
log.line("watcher", "git start")

local callback = function(w)
log.line("watcher", "git event scheduled '%s'", w.project_root)
utils.debounce("git:watcher:" .. w.project_root, M.config.filesystem_watchers.debounce_delay, function()
log.line("watcher", "git event scheduled '%s'", w.absolutegitdir)
utils.debounce("git:watcher:" .. w.absolutegitdir, M.config.filesystem_watchers.debounce_delay, function()
reload_tree_at(w.project_root)
end)
end

watcher = Watcher:new(utils.path_join { project_root, ".git" }, WATCHED_FILES, callback, {
watcher = Watcher:new(absolutegitdir, WATCHED_FILES, callback, {
project_root = project_root,
absolutegitdir = absolutegitdir,
})
end

Expand Down
31 changes: 31 additions & 0 deletions lua/nvim-tree/git/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,37 @@ local log = require "nvim-tree.log"

local has_cygpath = vim.fn.executable "cygpath" == 1

function M.get_absolutegitdir(cwd)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as mentioned, I think this should partially replace get_project_root

local ps = log.profile_start("git absolutegitdir %s", cwd)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you.


local cmd = { "git", "-C", cwd, "rev-parse", "--absolute-git-dir" }
log.line("git", "%s", vim.inspect(cmd))

local absolutegitdir = vim.fn.system(cmd)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this can return different errors, what's the plan for handling them? maybe check if the result is a valid dir?


log.raw("git", absolutegitdir)
log.profile_end(ps, "git absolutegitdir %s", cwd)

if vim.v.shell_error ~= 0 or not absolutegitdir or #absolutegitdir == 0 or absolutegitdir:match "fatal" then
return nil
end

-- git always returns path with forward slashes
if vim.fn.has "win32" == 1 then
-- msys2 git support
if has_cygpath then
absolutegitdir = vim.fn.system("cygpath -w " .. vim.fn.shellescape(absolutegitdir))
if vim.v.shell_error ~= 0 then
return nil
end
end
absolutegitdir = absolutegitdir:gsub("/", "\\")
end

-- remove newline
return absolutegitdir:sub(0, -2)
end

function M.get_toplevel(cwd)
local ps = log.profile_start("git toplevel %s", cwd)

Expand Down