blob: 58f6dd8dd39eb03f6f09ca94ad5987bc1bd1df89 (
plain)
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
46
47
48
49
50
51
52
53
54
55
56
57
58
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
|