nvim/lua/statusline/git.lua (view raw)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
local M = {}
local sep = package.config:sub(1,1)
local function find_git_dir()
local file_dir = vim.fn.expand('%:p:h') .. ';'
local git_dir = vim.fn.finddir('.git', file_dir)
if #git_dir > 0 then
M.in_git = true
end
return git_dir
end
local function get_git_head(head_file)
local f_head = io.open(head_file)
if f_head then
local HEAD = f_head:read()
f_head:close()
local branch = HEAD:match('ref: refs/heads/(.+)$')
if branch then M.git_branch = branch
else M.git_branch = HEAD:sub(1,7) end
end
return nil
end
-- event watcher to watch head file
local file_changed = vim.loop.new_fs_event()
local function watch_head()
file_changed:stop()
local git_dir = find_git_dir()
if #git_dir > 0 then
local head_file = git_dir..sep..'HEAD'
get_git_head(head_file)
file_changed:start(head_file, {}, vim.schedule_wrap(function()
-- reset file-watch
watch_head()
end))
else
M.git_branch = ''
end
end
watch_head()
return M
|