commit f91c34069e55f93d550d8b07fea2875e5cb86bc8 (tree)
parent 40e2dcba05635442d147394c2369c360c931bcfb
Author: Tim Pope <code@tpope.net>
Date: Mon, 6 Aug 2018 01:36:37 -0400
Decouple Head() from repo object
Diffstat:
2 files changed, 19 insertions(+), 18 deletions(-)
diff --git 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
@@ -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