From 1cbf0686b44c9c629e87f328c262372dce7aca2e Mon Sep 17 00:00:00 2001 From: shadmansaleh <13149513+shadmansaleh@users.noreply.github.com> Date: Wed, 25 Aug 2021 12:07:00 +0600 Subject: [PATCH] fix: branch incorrectly assumes .git/ without .git/HEAD to be valid fixes high cpu uses on windows in projects with empty .git folder. --- lua/lualine/components/branch.lua | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/lua/lualine/components/branch.lua b/lua/lualine/components/branch.lua index 367160c..35bb707 100644 --- a/lua/lualine/components/branch.lua +++ b/lua/lualine/components/branch.lua @@ -43,7 +43,7 @@ function Branch.find_git_dir() -- get file dir so we can search from that dir local file_dir = vim.fn.expand('%:p:h') local root_dir = file_dir - local git_file, git_dir + local git_dir -- Search upward for .git file or folder while (root_dir) do if git_dir_cache[root_dir] then @@ -56,24 +56,24 @@ function Branch.find_git_dir() if git_file_stat.type == 'directory' then git_dir = git_path elseif git_file_stat.type == 'file' then - git_file = git_path + -- separate git-dir or submodule is used + local file = io.open(git_path) + git_dir = file:read() + git_dir = git_dir:match('gitdir: (.+)$') + file:close() + -- submodule / relative file path + if git_dir:sub(1, 1) ~= Branch.sep and not git_dir:match('^%a:.*$') then + git_dir = git_path:match('(.*).git') .. git_dir + end end - break + local head_file_stat = vim.loop.fs_stat(git_dir..Branch.sep..'HEAD') + if head_file_stat and head_file_stat.type == 'file' then + break + else git_dir = nil end end root_dir = root_dir:match('(.*)'..Branch.sep..'.-') end - if git_file then - -- separate git-dir or submodule is used - local file = io.open(git_file) - git_dir = file:read() - git_dir = git_dir:match('gitdir: (.+)$') - file:close() - -- submodule / relative file path - if git_dir:sub(1, 1) ~= Branch.sep and not git_dir:match('^%a:.*$') then - git_dir = git_file:match('(.*).git') .. git_dir - end - end git_dir_cache[file_dir] = git_dir if Branch.git_dir ~= git_dir then Branch.git_dir = git_dir