Skip to content

Commit d055749

Browse files
committed
Make HEAD lookups work with reftable
Git has a new ref storage backend called reftable that stores all refs, including HEAD, as well as all reflogs, in a binary format under .git/reftable. Because the HEAD file is important in determining whether a directory is a Git repository, Git retains this file, but it always contains "ref: refs/heads/.invalid", thus pointing to an invalid ref, since ref components may not start with a dot. In such a configuration, the only practical possibility is to invoke a Git command to resolve HEAD for us, so use git rev-parse to do so if we find this invalid ref in the HEAD file. Look up the object ID and the value for HEAD at the same time to avoid the overhead of two calls and use the former if the latter is HEAD (that is, we're not on a branch). Unfortunately, this doesn't work when we have an unborn branch without any commits, such as when a repository is newly initialized. Fall back to git symbolic-ref in such a case. Use the regular head cache, as well as a new reftable head cache that tracks .git/reftable/tables.list. The reftable format uses several binary files plus a list of tables and the table list will change whenever a regular ref or symref (including HEAD) changes, which is what we want to know.
1 parent 4a745ea commit d055749

File tree

1 file changed

+37
-2
lines changed

1 file changed

+37
-2
lines changed

autoload/fugitive.vim

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,6 +1016,7 @@ function! s:StdoutToFile(out, cmd, ...) abort
10161016
endfunction
10171017

10181018
let s:head_cache = {}
1019+
let s:head_reftable_cache = {}
10191020

10201021
function! fugitive#Head(...) abort
10211022
let dir = a:0 > 1 ? a:2 : s:Dir()
@@ -1031,11 +1032,45 @@ function! fugitive#Head(...) abort
10311032
endif
10321033
let head = s:head_cache[file][1]
10331034
let len = a:0 ? a:1 : 0
1034-
if head =~# '^ref: '
1035+
let ref_pattern = '\%(refs/\%(heads/\|remotes/\|tags/\)\=\)\='
1036+
if head =~# '^ref: refs/heads/.invalid$'
1037+
" reftable
1038+
let file = FugitiveActualDir(dir) . '/reftable/tables.list'
1039+
let ftime = getftime(file)
1040+
if ftime == -1
1041+
return ''
1042+
elseif ftime != get(s:head_reftable_cache, file, [-1])[0]
1043+
let res = s:ChompDefault('', [dir, 'rev-parse', 'HEAD', '--symbolic-full-name', 'HEAD'])
1044+
if res ==# ''
1045+
" unborn branch
1046+
let oid = ''
1047+
let head = s:ChompDefault('', [dir, 'symbolic-ref', 'HEAD'])
1048+
else
1049+
let lines = split(res, '\n')
1050+
let oid = lines[0]
1051+
let head = lines[1]
1052+
endif
1053+
let s:head_reftable_cache[file] = [ftime, oid, head]
1054+
endif
1055+
let oid = s:head_reftable_cache[file][1]
1056+
let head = s:head_reftable_cache[file][2]
1057+
let len = a:0 ? a:1 : 0
1058+
if head !=# 'HEAD'
1059+
if len < 0
1060+
return head
1061+
else
1062+
return substitute(head, '\C^' . ref_pattern, '', '')
1063+
endif
1064+
elseif oid =~# '^\x\{40,\}$'
1065+
return len < 0 ? oid : strpart(oid, 0, len)
1066+
else
1067+
return ''
1068+
endif
1069+
elseif head =~# '^ref: '
10351070
if len < 0
10361071
return strpart(head, 5)
10371072
else
1038-
return substitute(head, '\C^ref: \%(refs/\%(heads/\|remotes/\|tags/\)\=\)\=', '', '')
1073+
return substitute(head, '\C^ref: ' . ref_pattern, '', '')
10391074
endif
10401075
elseif head =~# '^\x\{40,\}$'
10411076
return len < 0 ? head : strpart(head, 0, len)

0 commit comments

Comments
 (0)