Add fugitive#head() and fugitive#repo().head()

fugitive#head() returns the name of the current branch. If the current
HEAD is detached, fugitive#head() will return the empty string, unless
the optional 'len' argument is given, in which case the hash of the
current HEAD will be truncated to 'len' characters.

This makes should make life easier for people who don't want to use the
default provided by fugitive#statusline()
This commit is contained in:
Georgi Valkov 2012-04-17 22:28:11 +03:00 committed by Tim Pope
parent 765c921e1f
commit 43741b550d
2 changed files with 29 additions and 1 deletions

View File

@ -293,6 +293,12 @@ a statusline, this one matches the default when 'ruler' is set:
>
set statusline=%<%f\ %h%m%r%{fugitive#statusline()}%=%-14.(%l,%c%V%)\ %P
<
*fugitive#head(...)*
Use fugitive#head() to return the name of the current branch. If the current
HEAD is detached, fugitive#head() will return the empty string, unless the
optional argument is given, in which case the hash of the current commit will
be truncated to the given number of characters.
ABOUT *fugitive-about*
Grab the latest version or report a bug on GitHub:

View File

@ -275,7 +275,21 @@ function! s:repo_translate(spec) dict abort
endif
endfunction
call s:add_methods('repo',['dir','configured_tree','tree','bare','translate'])
function! s:repo_head(...) dict abort
let head = s:repo().head_ref()
if head =~# '^ref: '
let branch = s:sub(head,'^ref: %(refs/%(heads/|remotes/|tags/)=)=','')
elseif head =~# '^\x\{40\}$'
" truncate hash to a:1 characters if we're in detached head mode
let len = a:0 ? a:1 : 0
let branch = len ? head[0:len-1] : ''
endif
return branch
endfunction
call s:add_methods('repo',['dir','configured_tree','tree','bare','translate','head'])
function! s:repo_git_command(...) dict abort
let git = g:fugitive_git_executable . ' --git-dir='.s:shellesc(self.git_dir)
@ -2386,6 +2400,14 @@ function! fugitive#statusline(...)
endif
endfunction
function! fugitive#head(...)
if !exists('b:git_dir')
return ''
endif
return s:repo().head(a:0 ? a:1 : 0)
endfunction
" }}}1
" Folding {{{1