lambdalisue / vim-gina

👣 Asynchronously control git repositories in Neovim/Vim 8
http://www.vim.org/scripts/script.php?script_id=5531
MIT License
689 stars 27 forks source link

Allow silent gina#component#repo#branch failures #299

Closed ixti closed 2 years ago

ixti commented 2 years ago

I use gina with lightline:

let g:lightline =
  \ {
  \   'active': {
  \     'left': [
  \       [ 'mode', 'paste' ],
  \       [ 'gitbranch', 'readonly', 'filename', 'modified' ]
  \     ]
  \   },
  \   'component': { 'lineinfo': ' %3l:%-2v' },
  \   'component_function': { 'gitbranch': 'gina#component#repo#branch' },
  \   'separator': { 'left': '', 'right': '' },
  \   'subseparator': { 'left': '', 'right': '' }
  \ }

And it works perfect. But when I lack permissions to read .git (e.g. while using etckeeper for /etc) it becomes a problem:

"/etc/profile" [readonly] 50L, 1661B
Error detected while processing function gina#component#repo#branch:
line   14:
E484: Can't open file /etc/.git/HEAD
ixti commented 2 years ago

Found solution (inspired by your dotfiles' s:safe function) :D

" file: ~/.vim/plugin.d/lightline.vim

function! lightline#gitbranch() abort
  try
    return gina#component#repo#branch()
  catch
    return ''
  endtry
endfunction

let g:lightline =
  \ {
  \   'active': {
  \     'left': [
  \       [ 'mode', 'paste' ],
  \       [ 'gitbranch', 'readonly', 'filename', 'modified' ]
  \     ]
  \   },
  \   'component': { 'lineinfo': ' %3l:%-2v' },
  \   'component_function': { 'gitbranch': 'lightline#safe_branch' },
  \   'separator': { 'left': '', 'right': '' },
  \   'subseparator': { 'left': '', 'right': '' }
  \ }