From 87c73b77964b582900c50308eddd9518948eb895 Mon Sep 17 00:00:00 2001 From: Adam Hovorka Date: Sun, 7 Oct 2018 09:54:18 -0600 Subject: Customize vim statusline --- base/vim/autoload/gitbranch.vim | 59 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 base/vim/autoload/gitbranch.vim (limited to 'base/vim/autoload/gitbranch.vim') diff --git a/base/vim/autoload/gitbranch.vim b/base/vim/autoload/gitbranch.vim new file mode 100644 index 0000000..58f6dd8 --- /dev/null +++ b/base/vim/autoload/gitbranch.vim @@ -0,0 +1,59 @@ +" ============================================================================= +" Filename: autoload/gitbranch.vim +" Author: itchyny +" License: MIT License +" Last Change: 2015/02/26 00:34:03. +" ============================================================================= + +let s:save_cpo = &cpo +set cpo&vim + +function! gitbranch#name() abort + if get(b:, 'gitbranch_pwd', '') !=# expand('%:p:h') || !has_key(b:, 'gitbranch_path') + call gitbranch#detect(expand('%:p:h')) + endif + if has_key(b:, 'gitbranch_path') && filereadable(b:gitbranch_path) + let branch = get(readfile(b:gitbranch_path), 0, '') + if branch =~# '^ref: ' + return substitute(branch, '^ref: \%(refs/\%(heads/\|remotes/\|tags/\)\=\)\=', '', '') + elseif branch =~# '^\x\{20\}' + return branch[:6] + endif + endif + return '' +endfunction + +function! gitbranch#dir(path) abort + let path = a:path + let prev = '' + while path !=# prev + let dir = path . '/.git' + let type = getftype(dir) + if type ==# 'dir' && isdirectory(dir.'/objects') && isdirectory(dir.'/refs') && getfsize(dir.'/HEAD') > 10 + return dir + elseif type ==# 'file' + let reldir = get(readfile(dir), 0, '') + if reldir =~# '^gitdir: ' + return simplify(path . '/' . reldir[8:]) + endif + endif + let prev = path + let path = fnamemodify(path, ':h') + endwhile + return '' +endfunction + +function! gitbranch#detect(path) abort + unlet! b:gitbranch_path + let b:gitbranch_pwd = expand('%:p:h') + let dir = gitbranch#dir(a:path) + if dir !=# '' + let path = dir . '/HEAD' + if filereadable(path) + let b:gitbranch_path = path + endif + endif +endfunction + +let &cpo = s:save_cpo +unlet s:save_cpo -- cgit v1.2.3-70-g09d2