aboutsummaryrefslogtreecommitdiff
path: root/base/vim
diff options
context:
space:
mode:
Diffstat (limited to 'base/vim')
-rw-r--r--base/vim/autoload/gitbranch.vim59
-rw-r--r--base/vim/plugin/gitbranch.vim23
2 files changed, 82 insertions, 0 deletions
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
diff --git a/base/vim/plugin/gitbranch.vim b/base/vim/plugin/gitbranch.vim
new file mode 100644
index 0000000..a6f776a
--- /dev/null
+++ b/base/vim/plugin/gitbranch.vim
@@ -0,0 +1,23 @@
+" =============================================================================
+" Filename: plugin/gitbranch.vim
+" Author: itchyny
+" License: MIT License
+" Last Change: 2015/05/12 08:16:47.
+" =============================================================================
+
+if exists('g:loaded_gitbranch') || v:version < 700
+ finish
+endif
+let g:loaded_gitbranch = 1
+
+let s:save_cpo = &cpo
+set cpo&vim
+
+augroup GitBranch
+ autocmd!
+ autocmd BufNewFile,BufReadPost * call gitbranch#detect(expand('<amatch>:p:h'))
+ autocmd BufEnter * call gitbranch#detect(expand('%:p:h'))
+augroup END
+
+let &cpo = s:save_cpo
+unlet s:save_cpo