From 133ed492daf43218f3cb1c7cc3120d6ad13f3540 Mon Sep 17 00:00:00 2001 From: Avimitin Date: Tue, 4 Jan 2022 15:46:44 +0800 Subject: [PATCH] refactor: delete the wrong and useless path check The file path check is incorrect. It gives the main repository path to lazygit, not the submodule path. Lazygit itself can do well for the path-checking job, so just let lazygit deal with it. Symlink check functionality is kept. Signed-off-by: Avimitin --- lua/lazygit.lua | 32 ++++++++++---------------------- 1 file changed, 10 insertions(+), 22 deletions(-) diff --git a/lua/lazygit.lua b/lua/lazygit.lua index 5ce99bb..504b0f1 100644 --- a/lua/lazygit.lua +++ b/lua/lazygit.lua @@ -16,6 +16,11 @@ local function is_lazygit_available() return fn.executable('lazygit') == 1 end +local function is_symlink() + local resolved = fn.resolve(fn.expand('%:p')) + return resolved ~= fn.expand('%:p') +end + --- Get project_root_dir for git repository local function project_root_dir() @@ -25,21 +30,6 @@ local function project_root_dir() vim.o.shell = 'bash' end - -- try submodule first - local gitdir = fn.system('cd "' .. fn.expand('%:p:h') .. '" && git rev-parse --show-superproject-working-tree') - if gitdir ~= '' and fn.matchstr(gitdir, '^fatal:.*') == '' then - vim.o.shell = oldshell - return trim(gitdir) - end - - -- try file location first - local gitdir = fn.system('cd "' .. fn.expand('%:p:h') .. '" && git rev-parse --show-toplevel') - local isgitdir = fn.matchstr(gitdir, '^fatal:.*') == '' - if isgitdir then - vim.o.shell = oldshell - return trim(gitdir) - end - -- try symlinked file location instead gitdir = fn.system( 'cd "' .. fn.fnamemodify(fn.resolve(fn.expand('%:p')), ':h') .. '" && git rev-parse --show-toplevel') @@ -163,18 +153,16 @@ local function lazygit(path) return end if path == nil then - path = project_root_dir() - end - if path == nil or fn.isdirectory(path .. '/.git/') == 0 then - print('Not in a git repository') - return + if is_symlink() then + path = project_root_dir() + end end open_floating_window() local cmd = 'lazygit' - if not vim.env.GIT_DIR then + if path ~= nil and not vim.env.GIT_DIR then cmd = cmd .. ' -g "' .. path .. '/.git/"' end - if not vim.env.GIT_WORK_TREE then + if path ~= nil and not vim.env.GIT_WORK_TREE then cmd = cmd .. ' -w "' .. path .. '"' end exec_lazygit_command(cmd)