Skip to content

Commit a770e21

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 a770e21

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

autoload/fugitive.vim

+35-1
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,7 +1032,40 @@ 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+
if head =~# '^ref: refs/heads/.invalid$'
1036+
" reftable
1037+
let file = FugitiveActualDir(dir) . '/reftable/tables.list'
1038+
let ftime = getftime(file)
1039+
if ftime == -1
1040+
return ''
1041+
elseif ftime != get(s:head_reftable_cache, file, [-1])[0]
1042+
let res = s:ChompDefault('', [dir, 'rev-parse', 'HEAD', '--symbolic-full-name', 'HEAD'])
1043+
if res ==# ''
1044+
" unborn branch
1045+
let oid = ''
1046+
let head = s:ChompDefault('', [dir, 'symbolic-ref', 'HEAD'])
1047+
else
1048+
let lines = split(res, '\n')
1049+
let oid = lines[0]
1050+
let head = lines[1]
1051+
endif
1052+
let s:head_reftable_cache[file] = [ftime, oid, head]
1053+
endif
1054+
let oid = s:head_reftable_cache[file][1]
1055+
let head = s:head_reftable_cache[file][2]
1056+
let len = a:0 ? a:1 : 0
1057+
if head !=# 'HEAD'
1058+
if len < 0
1059+
return head
1060+
else
1061+
return substitute(head, '\C^\%(refs/\%(heads/\|remotes/\|tags/\)\=\)\=', '', '')
1062+
endif
1063+
elseif oid =~# '^\x\{40,\}$'
1064+
return len < 0 ? oid : strpart(oid, 0, len)
1065+
else
1066+
return ''
1067+
endif
1068+
elseif head =~# '^ref: '
10351069
if len < 0
10361070
return strpart(head, 5)
10371071
else

0 commit comments

Comments
 (0)