Fix: set_opts_cwd function will only call git once (#256)

This commit is contained in:
YIQUN
2020-11-17 18:35:06 +08:00
committed by GitHub
parent 1b12520e99
commit e16712f22e

View File

@@ -144,20 +144,19 @@ git.status = function(opts)
end end
local set_opts_cwd = function(opts) local set_opts_cwd = function(opts)
local is_git_dir = function(path)
vim.fn.system('cd ' .. path .. ' && git rev-parse HEAD >/dev/null 2>&1')
if vim.v.shell_error ~= 0 then
error(path .. ' is not a git directory')
end
end
if opts.cwd then if opts.cwd then
opts.cwd = vim.fn.expand(opts.cwd) opts.cwd = vim.fn.expand(opts.cwd)
is_git_dir(opts.cwd)
else else
is_git_dir(vim.fn.expand('%:p:h')) opts.cwd = vim.fn.expand('%:p:h')
--- Find root of git directory and remove trailing newline characters end
opts.cwd = vim.fn.systemlist("git rev-parse --show-toplevel")[1]
-- Find root of git directory and remove trailing newline characters
local git_root = vim.fn.systemlist("git -C " .. opts.cwd .. " rev-parse --show-toplevel")[1]
if vim.v.shell_error ~= 0 then
error(opts.cwd .. ' is not a git directory')
else
opts.cwd = git_root
end end
end end