Skip to content

Commit 666c036

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. 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. Note that the head cache cannot be preserved here because the .git/HEAD file does not have to change when the branch changes with reftable (in fact, it will not), so remove that code here.
1 parent 4a745ea commit 666c036

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

autoload/fugitive.vim

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,23 +1022,24 @@ function! fugitive#Head(...) abort
10221022
if empty(dir)
10231023
return ''
10241024
endif
1025-
let file = FugitiveActualDir(dir) . '/HEAD'
1026-
let ftime = getftime(file)
1027-
if ftime == -1
1028-
return ''
1029-
elseif ftime != get(s:head_cache, file, [-1])[0]
1030-
let s:head_cache[file] = [ftime, readfile(file)[0]]
1025+
let res = s:ChompDefault('', [dir, 'rev-parse', 'HEAD', '--symbolic-full-name', 'HEAD'])
1026+
if res ==# ''
1027+
let oid = ''
1028+
let head = s:ChompDefault('', [dir, 'symbolic-ref', 'HEAD'])
1029+
else
1030+
let lines = split(res, '\n')
1031+
let oid = lines[0]
1032+
let head = lines[1]
10311033
endif
1032-
let head = s:head_cache[file][1]
10331034
let len = a:0 ? a:1 : 0
1034-
if head =~# '^ref: '
1035+
if head !=# 'HEAD'
10351036
if len < 0
1036-
return strpart(head, 5)
1037+
return head
10371038
else
1038-
return substitute(head, '\C^ref: \%(refs/\%(heads/\|remotes/\|tags/\)\=\)\=', '', '')
1039+
return substitute(head, '\C^\%(refs/\%(heads/\|remotes/\|tags/\)\=\)\=', '', '')
10391040
endif
1040-
elseif head =~# '^\x\{40,\}$'
1041-
return len < 0 ? head : strpart(head, 0, len)
1041+
elseif oid =~# '^\x\{40,\}$'
1042+
return len < 0 ? oid : strpart(oid, 0, len)
10421043
else
10431044
return ''
10441045
endif

0 commit comments

Comments
 (0)