add git integration

This commit is contained in:
kiyan42
2020-02-18 18:22:05 +01:00
parent 0ed31e065e
commit f8fb6de9c6
6 changed files with 92 additions and 22 deletions

44
lua/lib/git.lua Normal file
View File

@@ -0,0 +1,44 @@
local function system(v) return vim.api.nvim_call_function('system', { v }) end
local function is_git_repo()
local is_git = system('git rev-parse')
return string.match(is_git, 'fatal') == nil
end
local GIT_CMD = 'git status --porcelain=v1 '
local function is_folder_dirty(path)
local ret = system(GIT_CMD..path)
return ret ~= nil and ret ~= ''
end
local function create_git_checker(pattern)
return function(path)
local ret = system(GIT_CMD..path)
return string.match(ret, pattern) ~= nil
end
end
local is_modified = create_git_checker('^ ?MM?')
local is_staged = create_git_checker('^ ?A')
local is_unmerged = create_git_checker('^ ?UU')
local is_untracked = create_git_checker('^%?%?')
local function get_git_attr(path, is_dir)
if is_git_repo() == false then return '' end
if is_dir then
if is_folder_dirty(path) == true then return 'Dirty' end
else
if is_modified(path) then return 'Modified'
elseif is_staged(path) then return 'Staged'
elseif is_unmerged(path) then return 'Unmerged'
elseif is_untracked(path) then return 'Untracked'
end
end
return ''
end
return {
get_git_attr = get_git_attr;
}