From f0093ceaa61be132d39e5fa23d09c27b8a0a86b8 Mon Sep 17 00:00:00 2001 From: Cimbali Date: Wed, 22 Nov 2017 19:12:29 +0100 Subject: [PATCH 1/5] Remove code redundant with fugitive#head() It which requires a path, and just performed the same as fugitive#head() does internally. --- autoload/airline/extensions/branch.vim | 38 ++++---------------------- 1 file changed, 5 insertions(+), 33 deletions(-) diff --git a/autoload/airline/extensions/branch.vim b/autoload/airline/extensions/branch.vim index c589bdd..f2798bc 100644 --- a/autoload/airline/extensions/branch.vim +++ b/autoload/airline/extensions/branch.vim @@ -83,44 +83,18 @@ endif let s:git_dirs = {} -function! s:update_git_branch(path) +function! s:update_git_branch() if !s:has_fugitive let s:vcs_config['git'].branch = '' return endif let name = fugitive#head(7) - if empty(name) - if has_key(s:git_dirs, a:path) - let s:vcs_config['git'].branch = s:git_dirs[a:path] - return - endif - let dir = fugitive#extract_git_dir(a:path) - if empty(dir) - let name = '' - else - try - let line = join(readfile(dir . '/HEAD')) - if strpart(line, 0, 16) == 'ref: refs/heads/' - let name = strpart(line, 16) - else - " raw commit hash - let name = strpart(line, 0, 7) - endif - catch - let name = '' - endtry - endif - endif - - let s:git_dirs[a:path] = name let s:vcs_config['git'].branch = name endfunction -function! s:update_hg_branch(...) - " path argument is not actually used, so we don't actually care about a:1 - " it is just needed, because update_git_branch needs it. +function! s:update_hg_branch() if s:has_lawrencium let cmd='LC_ALL=C hg qtop' let stl=lawrencium#statusline() @@ -152,10 +126,8 @@ function! s:update_hg_branch(...) endfunction function! s:update_branch() - let b:airline_fname_path = get(b:, 'airline_fname_path', - \ exists("*fnamemodify") ? fnamemodify(resolve(@%), ":p:h") : expand("%:p:h")) for vcs in keys(s:vcs_config) - call {s:vcs_config[vcs].update_branch}(b:airline_fname_path) + call {s:vcs_config[vcs].update_branch}() if b:buffer_vcs_config[vcs].branch != s:vcs_config[vcs].branch let b:buffer_vcs_config[vcs].branch = s:vcs_config[vcs].branch unlet! b:airline_head @@ -329,8 +301,8 @@ function! airline#extensions#branch#init(ext) call airline#parts#define_function('branch', 'airline#extensions#branch#get_head') autocmd BufReadPost * unlet! b:airline_file_in_root - autocmd ShellCmdPost,CmdwinLeave * unlet! b:airline_head b:airline_do_mq_check b:airline_fname_path - autocmd User AirlineBeforeRefresh unlet! b:airline_head b:airline_do_mq_check b:airline_fname_path + autocmd ShellCmdPost,CmdwinLeave * unlet! b:airline_head b:airline_do_mq_check + autocmd User AirlineBeforeRefresh unlet! b:airline_head b:airline_do_mq_check autocmd BufWritePost * call s:reset_untracked_cache(0) autocmd ShellCmdPost * call s:reset_untracked_cache(1) endfunction From 009f7932fb8da73dad1f74a61cfabc85df6fd8bb Mon Sep 17 00:00:00 2001 From: Cimbali Date: Wed, 22 Nov 2017 18:54:29 +0100 Subject: [PATCH 2/5] Show #branch#head() as "ref-or-sha1(current-branch)" Only changes anything in the case of using fugitive to show a file that is not from the current working directory. --- autoload/airline/extensions/branch.vim | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/autoload/airline/extensions/branch.vim b/autoload/airline/extensions/branch.vim index f2798bc..7e9625b 100644 --- a/autoload/airline/extensions/branch.vim +++ b/autoload/airline/extensions/branch.vim @@ -81,7 +81,9 @@ else endfunction endif -let s:git_dirs = {} + +" Fugitive special revisions. call '0' "staging" ? +let s:names = {'0': 'index', '1': 'ancestor', '2':'target', '3':'merged'} function! s:update_git_branch() if !s:has_fugitive @@ -91,6 +93,22 @@ function! s:update_git_branch() let name = fugitive#head(7) + try + let commit = fugitive#buffer().commit() + + if has_key(s:names, commit) + let name = get(s:names, commit)."(".name.")" + elseif !empty(commit) + let ref = fugitive#repo().git_chomp('describe', '--all', '--exact-match', commit) + if ref !~ "^fatal: no tag exactly matches" + let name = s:format_name(substitute(ref, '\v\C^%(heads/|remotes/|tags/)=','',''))."(".name.")" + else + let name = commit[:6]."(".name.")" + endif + endif + catch + endtry + let s:vcs_config['git'].branch = name endfunction From 33663d7df2d2f47b63bc4849c3a1c051f609d255 Mon Sep 17 00:00:00 2001 From: Cimbali Date: Wed, 22 Nov 2017 22:11:58 +0100 Subject: [PATCH 3/5] Show buffer name relative to git folder --- autoload/airline/extensions.vim | 6 ++++ autoload/airline/extensions/fugitiveline.vim | 37 ++++++++++++++++++++ doc/airline.txt | 7 ++++ 3 files changed, 50 insertions(+) create mode 100644 autoload/airline/extensions/fugitiveline.vim diff --git a/autoload/airline/extensions.vim b/autoload/airline/extensions.vim index 79f6bec..6de9c83 100644 --- a/autoload/airline/extensions.vim +++ b/autoload/airline/extensions.vim @@ -229,6 +229,12 @@ function! airline#extensions#load() call add(loaded_ext, 'bufferline') endif + if get(g:, 'airline#extensions#fugitiveline#enabled', 1) + \ && exists('*fugitive#head') + call airline#extensions#fugitiveline#init(s:ext) + call add(loaded_ext, 'fugitiveline') + endif + if (get(g:, 'airline#extensions#virtualenv#enabled', 1) && (exists(':VirtualEnvList') || isdirectory($VIRTUAL_ENV))) call airline#extensions#virtualenv#init(s:ext) call add(loaded_ext, 'virtualenv') diff --git a/autoload/airline/extensions/fugitiveline.vim b/autoload/airline/extensions/fugitiveline.vim new file mode 100644 index 0000000..e2fcad7 --- /dev/null +++ b/autoload/airline/extensions/fugitiveline.vim @@ -0,0 +1,37 @@ +" MIT License. Copyright (c) 2017 Cimbali. +" vim: et ts=2 sts=2 sw=2 + +scriptencoding utf-8 + +if !exists('*fugitive#head') + finish +endif + + +if exists("+autochdir") && &autochdir == 1 + let s:fmod = ':p' +else + let s:fmod = ':.' +endif + +function! airline#extensions#fugitiveline#bufname() + try + let buffer = fugitive#buffer() + if buffer.type('blob') + return fnamemodify(buffer.repo().translate(buffer.path()), s:fmod) + endif + catch + endtry + + return fnamemodify(bufname('%'), s:fmod) +endfunction + +function! airline#extensions#fugitiveline#init(ext) + if exists("+autochdir") && &autochdir == 1 + " if 'acd' is set, vim-airline uses the path section, so we need to redefine this here as well + call airline#parts#define_raw('path', '%<%{airline#extensions#fugitiveline#bufname()}%m') + else + call airline#parts#define_raw('file', '%<%{airline#extensions#fugitiveline#bufname()}%m') + endif +endfunction + diff --git a/doc/airline.txt b/doc/airline.txt index ae43254..051b5d2 100644 --- a/doc/airline.txt +++ b/doc/airline.txt @@ -400,6 +400,13 @@ vim-bufferline * determine whether bufferline will overwrite customization variables > let g:airline#extensions#bufferline#overwrite_variables = 1 < +------------------------------------- *airline-fugitiveline* +This extension hides the fugitive://**// part of the buffer names, to only +show the file name as if it were in the current working tree. + +* enable/disable bufferline integration > + let g:airline#extensions#fugitiveline#enabled = 1 +< ------------------------------------- *airline-branch* vim-airline will display the branch-indicator together with the branch name in From 8c4bf37588fb52f0aa3237d491d81f74061140c1 Mon Sep 17 00:00:00 2001 From: Cimbali Date: Wed, 22 Nov 2017 22:41:00 +0100 Subject: [PATCH 4/5] Configurability: parametrizable sha1 length --- autoload/airline/extensions/branch.vim | 5 +++-- doc/airline.txt | 3 +++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/autoload/airline/extensions/branch.vim b/autoload/airline/extensions/branch.vim index 7e9625b..c46c3c5 100644 --- a/autoload/airline/extensions/branch.vim +++ b/autoload/airline/extensions/branch.vim @@ -84,6 +84,7 @@ endif " Fugitive special revisions. call '0' "staging" ? let s:names = {'0': 'index', '1': 'ancestor', '2':'target', '3':'merged'} +let s:sha1size = get(g:, 'airline#extensions#branch#sha1_len', 7) function! s:update_git_branch() if !s:has_fugitive @@ -91,7 +92,7 @@ function! s:update_git_branch() return endif - let name = fugitive#head(7) + let name = fugitive#head(s:sha1size) try let commit = fugitive#buffer().commit() @@ -103,7 +104,7 @@ function! s:update_git_branch() if ref !~ "^fatal: no tag exactly matches" let name = s:format_name(substitute(ref, '\v\C^%(heads/|remotes/|tags/)=','',''))."(".name.")" else - let name = commit[:6]."(".name.")" + let name = commit[0:s:sha1size-1]."(".name.")" endif endif catch diff --git a/doc/airline.txt b/doc/airline.txt index 051b5d2..c9c6578 100644 --- a/doc/airline.txt +++ b/doc/airline.txt @@ -453,6 +453,9 @@ notexists symbol will be displayed after the branch name. return '[' . a:name . ']' endfunction < +* truncate sha1 commits at this number of characters > + let g:airline#extensions#branch#sha1_len = 10 +< ------------------------------------- *airline-syntastic* syntastic From e415c5301f1ba1c53fac8308d1d2e8c8f4b348ba Mon Sep 17 00:00:00 2001 From: Cimbali Date: Wed, 22 Nov 2017 23:55:20 +0100 Subject: [PATCH 5/5] Trusting fugitive instead of tracking paths manually The initial reason in #237 is not valid anymore, as vim-gitgutter functions as expected for editing files that are not part of a repo, whether they are: - in a different repo - in a parent repo (cwd being a submodule) - outside of the repo Furthermore, removing this check allows to show relevant info for specific fugitive file names that are fugitive://..../sha1//... which are hard to parse manually, especially in complicated situations such as submodules. --- autoload/airline/extensions/branch.vim | 33 -------------------------- 1 file changed, 33 deletions(-) diff --git a/autoload/airline/extensions/branch.vim b/autoload/airline/extensions/branch.vim index c46c3c5..b85c155 100644 --- a/autoload/airline/extensions/branch.vim +++ b/autoload/airline/extensions/branch.vim @@ -247,9 +247,6 @@ function! airline#extensions#branch#head() endif endif - if has_key(heads, 'git') && !s:check_in_path() - let b:airline_head = '' - endif let minwidth = empty(get(b:, 'airline_hunks', '')) ? 14 : 7 let b:airline_head = airline#util#shorten(b:airline_head, 120, minwidth) return b:airline_head @@ -264,35 +261,6 @@ function! airline#extensions#branch#get_head() \ : printf('%s%s', empty(symbol) ? '' : symbol.(g:airline_symbols.space), head) endfunction -function! s:check_in_path() - if !exists('b:airline_file_in_root') - let root = get(b:, 'git_dir', get(b:, 'mercurial_dir', '')) - let bufferpath = resolve(fnamemodify(expand('%'), ':p')) - - if !filereadable(root) "not a file - " if .git is a directory, it's the old submodule format - if match(root, '\.git$') >= 0 - let root = expand(fnamemodify(root, ':h')) - else - " else it's the newer format, and we need to guesstimate - " 1) check for worktrees - if match(root, 'worktrees') > -1 - " worktree can be anywhere, so simply assume true here - return 1 - endif - " 2) check for submodules - let pattern = '\.git[\\/]\(modules\)[\\/]' - if match(root, pattern) >= 0 - let root = substitute(root, pattern, '', '') - endif - endif - endif - - let b:airline_file_in_root = stridx(bufferpath, root) > -1 - endif - return b:airline_file_in_root -endfunction - function! s:reset_untracked_cache(shellcmdpost) " shellcmdpost - whether function was called as a result of ShellCmdPost hook if !g:airline#init#vim_async && !has('nvim') @@ -319,7 +287,6 @@ endfunction function! airline#extensions#branch#init(ext) call airline#parts#define_function('branch', 'airline#extensions#branch#get_head') - autocmd BufReadPost * unlet! b:airline_file_in_root autocmd ShellCmdPost,CmdwinLeave * unlet! b:airline_head b:airline_do_mq_check autocmd User AirlineBeforeRefresh unlet! b:airline_head b:airline_do_mq_check autocmd BufWritePost * call s:reset_untracked_cache(0)