Fix: Branch not working on directory with whilespace

This commit is contained in:
shadmansaleh 2021-08-03 15:28:05 +06:00
parent ac9a9e0a8b
commit e7b8b42089
1 changed files with 19 additions and 10 deletions

View File

@ -27,15 +27,24 @@ Branch.update_status = function() return Branch.git_branch end
-- returns full path to git directory for current directory -- returns full path to git directory for current directory
function Branch.find_git_dir() function Branch.find_git_dir()
-- get file dir so we can search from that dir -- get file dir so we can search from that dir
local file_dir = vim.fn.expand('%:p:h') .. ';' local file_dir = vim.fn.expand('%:p:h')
-- find .git/ folder genaral case local git_file, git_dir
local git_dir = vim.fn.finddir('.git', file_dir) -- Search upward for .git file or folder
-- find .git file in case of submodules or any other case git dir is in while (file_dir) do
-- any other place than .git/ local git_path = file_dir..Branch.sep..'.git'
local git_file = vim.fn.findfile('.git', file_dir) local git_file_stat = vim.loop.fs_stat(git_path)
-- for some weird reason findfile gives relative path so expand it to fullpath if (git_file_stat) then
if #git_file > 0 then git_file = vim.fn.fnamemodify(git_file, ':p') end if git_file_stat.type == 'directory' then
if #git_file > #git_dir then git_dir = git_path
elseif git_file_stat.type == 'file' then
git_file = git_path
end
break
end
file_dir = file_dir:match('(.*)'..Branch.sep..'.-')
end
if git_file then
-- separate git-dir or submodule is used -- separate git-dir or submodule is used
local file = io.open(git_file) local file = io.open(git_file)
git_dir = file:read() git_dir = file:read()
@ -69,7 +78,7 @@ end
function Branch.update_branch() function Branch.update_branch()
Branch.file_changed:stop() Branch.file_changed:stop()
local git_dir = Branch.find_git_dir() local git_dir = Branch.find_git_dir()
if #git_dir > 0 then if git_dir and #git_dir > 0 then
local head_file = git_dir .. Branch.sep .. 'HEAD' local head_file = git_dir .. Branch.sep .. 'HEAD'
Branch.get_git_head(head_file) Branch.get_git_head(head_file)
Branch.file_changed:start(head_file, {}, vim.schedule_wrap( Branch.file_changed:start(head_file, {}, vim.schedule_wrap(