Skip to content

Commit 6568cdc

Browse files
authored
Merge pull request vim-airline#1603 from Cimbali/master
Improve fugitive integration
2 parents 1e13620 + 6cb7815 commit 6568cdc

File tree

4 files changed

+77
-66
lines changed

4 files changed

+77
-66
lines changed

autoload/airline/extensions.vim

+6
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,12 @@ function! airline#extensions#load()
229229
call add(loaded_ext, 'bufferline')
230230
endif
231231

232+
if get(g:, 'airline#extensions#fugitiveline#enabled', 1)
233+
\ && exists('*fugitive#head')
234+
call airline#extensions#fugitiveline#init(s:ext)
235+
call add(loaded_ext, 'fugitiveline')
236+
endif
237+
232238
if (get(g:, 'airline#extensions#virtualenv#enabled', 1) && (exists(':VirtualEnvList') || isdirectory($VIRTUAL_ENV)))
233239
call airline#extensions#virtualenv#init(s:ext)
234240
call add(loaded_ext, 'virtualenv')

autoload/airline/extensions/branch.vim

+24-66
Original file line numberDiff line numberDiff line change
@@ -81,46 +81,39 @@ else
8181
endfunction
8282
endif
8383

84-
let s:git_dirs = {}
8584

86-
function! s:update_git_branch(path)
85+
" Fugitive special revisions. call '0' "staging" ?
86+
let s:names = {'0': 'index', '1': 'ancestor', '2':'target', '3':'merged'}
87+
let s:sha1size = get(g:, 'airline#extensions#branch#sha1_len', 7)
88+
89+
function! s:update_git_branch()
8790
if !s:has_fugitive
8891
let s:vcs_config['git'].branch = ''
8992
return
9093
endif
9194

92-
let name = fugitive#head(7)
93-
if empty(name)
94-
if has_key(s:git_dirs, a:path)
95-
let s:vcs_config['git'].branch = s:git_dirs[a:path]
96-
return
97-
endif
95+
let name = fugitive#head(s:sha1size)
9896

99-
let dir = fugitive#extract_git_dir(a:path)
100-
if empty(dir)
101-
let name = ''
102-
else
103-
try
104-
let line = join(readfile(dir . '/HEAD'))
105-
if strpart(line, 0, 16) == 'ref: refs/heads/'
106-
let name = strpart(line, 16)
107-
else
108-
" raw commit hash
109-
let name = strpart(line, 0, 7)
110-
endif
111-
catch
112-
let name = ''
113-
endtry
97+
try
98+
let commit = fugitive#buffer().commit()
99+
100+
if has_key(s:names, commit)
101+
let name = get(s:names, commit)."(".name.")"
102+
elseif !empty(commit)
103+
let ref = fugitive#repo().git_chomp('describe', '--all', '--exact-match', commit)
104+
if ref !~ "^fatal: no tag exactly matches"
105+
let name = s:format_name(substitute(ref, '\v\C^%(heads/|remotes/|tags/)=','',''))."(".name.")"
106+
else
107+
let name = commit[0:s:sha1size-1]."(".name.")"
108+
endif
114109
endif
115-
endif
110+
catch
111+
endtry
116112

117-
let s:git_dirs[a:path] = name
118113
let s:vcs_config['git'].branch = name
119114
endfunction
120115

121-
function! s:update_hg_branch(...)
122-
" path argument is not actually used, so we don't actually care about a:1
123-
" it is just needed, because update_git_branch needs it.
116+
function! s:update_hg_branch()
124117
if s:has_lawrencium
125118
let cmd='LC_ALL=C hg qtop'
126119
let stl=lawrencium#statusline()
@@ -152,10 +145,8 @@ function! s:update_hg_branch(...)
152145
endfunction
153146

154147
function! s:update_branch()
155-
let b:airline_fname_path = get(b:, 'airline_fname_path',
156-
\ exists("*fnamemodify") ? fnamemodify(resolve(@%), ":p:h") : expand("%:p:h"))
157148
for vcs in keys(s:vcs_config)
158-
call {s:vcs_config[vcs].update_branch}(b:airline_fname_path)
149+
call {s:vcs_config[vcs].update_branch}()
159150
if b:buffer_vcs_config[vcs].branch != s:vcs_config[vcs].branch
160151
let b:buffer_vcs_config[vcs].branch = s:vcs_config[vcs].branch
161152
unlet! b:airline_head
@@ -256,9 +247,6 @@ function! airline#extensions#branch#head()
256247
endif
257248
endif
258249

259-
if has_key(heads, 'git') && !s:check_in_path()
260-
let b:airline_head = ''
261-
endif
262250
let minwidth = empty(get(b:, 'airline_hunks', '')) ? 14 : 7
263251
let b:airline_head = airline#util#shorten(b:airline_head, 120, minwidth)
264252
return b:airline_head
@@ -273,35 +261,6 @@ function! airline#extensions#branch#get_head()
273261
\ : printf('%s%s', empty(symbol) ? '' : symbol.(g:airline_symbols.space), head)
274262
endfunction
275263

276-
function! s:check_in_path()
277-
if !exists('b:airline_file_in_root')
278-
let root = get(b:, 'git_dir', get(b:, 'mercurial_dir', ''))
279-
let bufferpath = resolve(fnamemodify(expand('%'), ':p'))
280-
281-
if !filereadable(root) "not a file
282-
" if .git is a directory, it's the old submodule format
283-
if match(root, '\.git$') >= 0
284-
let root = expand(fnamemodify(root, ':h'))
285-
else
286-
" else it's the newer format, and we need to guesstimate
287-
" 1) check for worktrees
288-
if match(root, 'worktrees') > -1
289-
" worktree can be anywhere, so simply assume true here
290-
return 1
291-
endif
292-
" 2) check for submodules
293-
let pattern = '\.git[\\/]\(modules\)[\\/]'
294-
if match(root, pattern) >= 0
295-
let root = substitute(root, pattern, '', '')
296-
endif
297-
endif
298-
endif
299-
300-
let b:airline_file_in_root = stridx(bufferpath, root) > -1
301-
endif
302-
return b:airline_file_in_root
303-
endfunction
304-
305264
function! s:reset_untracked_cache(shellcmdpost)
306265
" shellcmdpost - whether function was called as a result of ShellCmdPost hook
307266
if !g:airline#init#vim_async && !has('nvim')
@@ -328,9 +287,8 @@ endfunction
328287
function! airline#extensions#branch#init(ext)
329288
call airline#parts#define_function('branch', 'airline#extensions#branch#get_head')
330289

331-
autocmd BufReadPost * unlet! b:airline_file_in_root
332-
autocmd ShellCmdPost,CmdwinLeave * unlet! b:airline_head b:airline_do_mq_check b:airline_fname_path
333-
autocmd User AirlineBeforeRefresh unlet! b:airline_head b:airline_do_mq_check b:airline_fname_path
290+
autocmd ShellCmdPost,CmdwinLeave * unlet! b:airline_head b:airline_do_mq_check
291+
autocmd User AirlineBeforeRefresh unlet! b:airline_head b:airline_do_mq_check
334292
autocmd BufWritePost * call s:reset_untracked_cache(0)
335293
autocmd ShellCmdPost * call s:reset_untracked_cache(1)
336294
endfunction
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
" MIT License. Copyright (c) 2017 Cimbali.
2+
" vim: et ts=2 sts=2 sw=2
3+
4+
scriptencoding utf-8
5+
6+
if !exists('*fugitive#head')
7+
finish
8+
endif
9+
10+
11+
if exists("+autochdir") && &autochdir == 1
12+
let s:fmod = ':p'
13+
else
14+
let s:fmod = ':.'
15+
endif
16+
17+
function! airline#extensions#fugitiveline#bufname()
18+
try
19+
let buffer = fugitive#buffer()
20+
if buffer.type('blob')
21+
return fnamemodify(buffer.repo().translate(buffer.path()), s:fmod)
22+
endif
23+
catch
24+
endtry
25+
26+
return fnamemodify(bufname('%'), s:fmod)
27+
endfunction
28+
29+
function! airline#extensions#fugitiveline#init(ext)
30+
if exists("+autochdir") && &autochdir == 1
31+
" if 'acd' is set, vim-airline uses the path section, so we need to redefine this here as well
32+
call airline#parts#define_raw('path', '%<%{airline#extensions#fugitiveline#bufname()}%m')
33+
else
34+
call airline#parts#define_raw('file', '%<%{airline#extensions#fugitiveline#bufname()}%m')
35+
endif
36+
endfunction
37+

doc/airline.txt

+10
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,13 @@ vim-bufferline <https://github.com/bling/vim-bufferline>
400400
* determine whether bufferline will overwrite customization variables >
401401
let g:airline#extensions#bufferline#overwrite_variables = 1
402402
<
403+
------------------------------------- *airline-fugitiveline*
404+
This extension hides the fugitive://**// part of the buffer names, to only
405+
show the file name as if it were in the current working tree.
406+
407+
* enable/disable bufferline integration >
408+
let g:airline#extensions#fugitiveline#enabled = 1
409+
<
403410
------------------------------------- *airline-branch*
404411

405412
vim-airline will display the branch-indicator together with the branch name in
@@ -446,6 +453,9 @@ notexists symbol will be displayed after the branch name.
446453
return '[' . a:name . ']'
447454
endfunction
448455
<
456+
* truncate sha1 commits at this number of characters >
457+
let g:airline#extensions#branch#sha1_len = 10
458+
<
449459
------------------------------------- *airline-syntastic*
450460
syntastic <https://github.com/vim-syntastic/syntastic>
451461

0 commit comments

Comments
 (0)