From d0557495c705e3d98b003c48c60c5f55f77bf03c Mon Sep 17 00:00:00 2001 From: "brian m. carlson" Date: Mon, 7 Apr 2025 08:32:08 +0000 Subject: [PATCH] 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. --- autoload/fugitive.vim | 39 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/autoload/fugitive.vim b/autoload/fugitive.vim index 1cbc2ebce..9b0cccd28 100644 --- a/autoload/fugitive.vim +++ b/autoload/fugitive.vim @@ -1016,6 +1016,7 @@ function! s:StdoutToFile(out, cmd, ...) abort endfunction let s:head_cache = {} +let s:head_reftable_cache = {} function! fugitive#Head(...) abort let dir = a:0 > 1 ? a:2 : s:Dir() @@ -1031,11 +1032,45 @@ function! fugitive#Head(...) abort endif let head = s:head_cache[file][1] let len = a:0 ? a:1 : 0 - if head =~# '^ref: ' + let ref_pattern = '\%(refs/\%(heads/\|remotes/\|tags/\)\=\)\=' + if head =~# '^ref: refs/heads/.invalid$' + " reftable + let file = FugitiveActualDir(dir) . '/reftable/tables.list' + let ftime = getftime(file) + if ftime == -1 + return '' + elseif ftime != get(s:head_reftable_cache, file, [-1])[0] + let res = s:ChompDefault('', [dir, 'rev-parse', 'HEAD', '--symbolic-full-name', 'HEAD']) + if res ==# '' + " unborn branch + let oid = '' + let head = s:ChompDefault('', [dir, 'symbolic-ref', 'HEAD']) + else + let lines = split(res, '\n') + let oid = lines[0] + let head = lines[1] + endif + let s:head_reftable_cache[file] = [ftime, oid, head] + endif + let oid = s:head_reftable_cache[file][1] + let head = s:head_reftable_cache[file][2] + let len = a:0 ? a:1 : 0 + if head !=# 'HEAD' + if len < 0 + return head + else + return substitute(head, '\C^' . ref_pattern, '', '') + endif + elseif oid =~# '^\x\{40,\}$' + return len < 0 ? oid : strpart(oid, 0, len) + else + return '' + endif + elseif head =~# '^ref: ' if len < 0 return strpart(head, 5) else - return substitute(head, '\C^ref: \%(refs/\%(heads/\|remotes/\|tags/\)\=\)\=', '', '') + return substitute(head, '\C^ref: ' . ref_pattern, '', '') endif elseif head =~# '^\x\{40,\}$' return len < 0 ? head : strpart(head, 0, len)