enhance: Update git_branch.lua to use GIT_DIR environment variable (#1114)

Fix typo

Remove whitespace

Try GIT_DIR first
This commit is contained in:
Pedro Pombeiro 2023-10-18 06:49:27 +02:00 committed by GitHub
parent 1a3f6bba41
commit c55af3b39c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 17 additions and 5 deletions

View File

@ -56,14 +56,27 @@ local function update_branch()
branch_cache[vim.api.nvim_get_current_buf()] = current_git_branch branch_cache[vim.api.nvim_get_current_buf()] = current_git_branch
end end
---updates the current value of current_git_branch and sets up file watch on HEAD file if value changed
local function update_current_git_dir(git_dir)
if current_git_dir ~= git_dir then
current_git_dir = git_dir
update_branch()
end
end
---returns full path to git directory for dir_path or current directory ---returns full path to git directory for dir_path or current directory
---@param dir_path string|nil ---@param dir_path string|nil
---@return string ---@return string|nil
function M.find_git_dir(dir_path) function M.find_git_dir(dir_path)
local git_dir = vim.env.GIT_DIR
if git_dir then
update_current_git_dir(git_dir)
return git_dir
end
-- get file dir so we can search from that dir -- get file dir so we can search from that dir
local file_dir = dir_path or vim.fn.expand('%:p:h') local file_dir = dir_path or vim.fn.expand('%:p:h')
local root_dir = file_dir local root_dir = file_dir
local git_dir
-- Search upward for .git file or folder -- Search upward for .git file or folder
while root_dir do while root_dir do
if git_dir_cache[root_dir] then if git_dir_cache[root_dir] then
@ -101,9 +114,8 @@ function M.find_git_dir(dir_path)
end end
git_dir_cache[file_dir] = git_dir git_dir_cache[file_dir] = git_dir
if dir_path == nil and current_git_dir ~= git_dir then if dir_path == nil then
current_git_dir = git_dir update_current_git_dir(git_dir)
update_branch()
end end
return git_dir return git_dir
end end