diff --git a/autoload/fugitive.vim b/autoload/fugitive.vim index 5e30096..2569f0d 100644 --- a/autoload/fugitive.vim +++ b/autoload/fugitive.vim @@ -202,6 +202,22 @@ function! fugitive#Prepare(cmd, ...) abort return pre . g:fugitive_git_executable . ' ' . args endfunction +function! fugitive#Head(...) abort + let dir = a:0 > 1 ? a:2 : get(b:, 'git_dir', '') + if empty(dir) || !filereadable(dir . '/HEAD') + return '' + endif + let head = readfile(dir . '/HEAD')[0] + if head =~# '^ref: ' + return substitute(head, '\C^ref: \%(refs/\%(heads/\|remotes/\|tags/\)\=\)\=', '', '') + elseif head =~# '^\x\{40\}$' + let len = a:0 ? a:1 : 0 + return len < 0 ? head : len ? head[0:len-1] : '' + else + return '' + endif +endfunction + function! fugitive#RevParse(rev, ...) abort let hash = system(s:Prepare(a:0 ? a:1 : b:git_dir, 'rev-parse', '--verify', a:rev))[0:-2] if !v:shell_error && hash =~# '^\x\{40\}$' @@ -408,22 +424,7 @@ function! s:Generate(rev, ...) abort endfunction function! s:repo_head(...) dict abort - if !filereadable(self.dir('HEAD')) - return '' - endif - let head = readfile(self.dir('HEAD'))[0] - - 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 < 0 ? head : len ? head[0:len-1] : '' - else - return '' - endif - - return branch + return fugitive#Head(a:0 ? a:1 : 0, self.git_dir) endfunction call s:add_methods('repo',['dir','tree','bare','translate','head']) @@ -3543,7 +3544,7 @@ function! fugitive#head(...) abort return '' endif - return fugitive#repo().head(a:0 ? a:1 : 0) + return fugitive#Head(a:0 ? a:1 : 0) endfunction " Section: Folding diff --git a/plugin/fugitive.vim b/plugin/fugitive.vim index 11b9fe1..00d38df 100644 --- a/plugin/fugitive.vim +++ b/plugin/fugitive.vim @@ -85,7 +85,7 @@ function! FugitiveHead(...) abort if empty(dir) return '' endif - return fugitive#repo(dir).head(a:0 ? a:1 : 0) + return fugitive#Head(a:0 ? a:1 : 0, dir) endfunction function! FugitiveStatusline(...) abort