diff --git a/doc/fugitive.txt b/doc/fugitive.txt index be2ec07..a098297 100644 --- a/doc/fugitive.txt +++ b/doc/fugitive.txt @@ -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: diff --git a/plugin/fugitive.vim b/plugin/fugitive.vim index 08cd41b..d587e88 100644 --- a/plugin/fugitive.vim +++ b/plugin/fugitive.vim @@ -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