Retool $GIT_INDEX_FILE handling

* Don't attempt to handle relative paths, as there's no guarantee the
  current working directory is the one Vim was started with.  In
  practice, the only relative path I've seen is `.git/index`, which is
  already the default and thus harmless to ignore.
* Cache the result of FugitiveVimPath(), to allow for slow
  implementations.
This commit is contained in:
Tim Pope
2022-05-31 15:26:11 -04:00
parent 8fba012775
commit fb07620878

View File

@@ -578,11 +578,19 @@ function! s:PreparePathArgs(cmd, dir, literal, explicit) abort
return a:cmd
endfunction
let s:git_index_file_env = {}
function! s:GitIndexFileEnv() abort
if $GIT_INDEX_FILE =~# '^/\|^\a:' && !has_key(s:git_index_file_env, $GIT_INDEX_FILE)
let s:git_index_file_env[$GIT_INDEX_FILE] = s:Slash(FugitiveVimPath($GIT_INDEX_FILE))
endif
return get(s:git_index_file_env, $GIT_INDEX_FILE, '')
endfunction
function! s:PrepareEnv(env, dir) abort
if len($GIT_INDEX_FILE) && len(s:Tree(a:dir)) && !has_key(a:env, 'GIT_INDEX_FILE')
let index_dir = substitute($GIT_INDEX_FILE, '[^/]\+$', '', '')
let index_dir = substitute(s:GitIndexFileEnv(), '[^/]\+$', '', '')
let our_dir = fugitive#Find('.git/', a:dir)
if !s:cpath(index_dir, our_dir) && !s:cpath(resolve(FugitiveVimPath(index_dir)), our_dir)
if !s:cpath(index_dir, our_dir) && !s:cpath(resolve(index_dir), our_dir)
let a:env['GIT_INDEX_FILE'] = FugitiveGitPath(fugitive#Find('.git/index', a:dir))
endif
endif
@@ -1823,11 +1831,11 @@ function! fugitive#Find(object, ...) abort
let fdir = dir . '/'
let f = fdir . 'index'
if len($GIT_INDEX_FILE)
let index_dir = substitute($GIT_INDEX_FILE, '[^/]\+$', '', '')
let index_dir = substitute(s:GitIndexFileEnv(), '[^/]\+$', '', '')
if s:cpath(index_dir, fdir)
let f = FugitiveVimPath($GIT_INDEX_FILE)
elseif s:cpath(resolve(FugitiveVimPath(index_dir)), fdir)
let f = resolve(FugitiveVimPath($GIT_INDEX_FILE))
let f = s:GitIndexFileEnv()
elseif s:cpath(resolve(index_dir), fdir)
let f = resolve(s:GitIndexFileEnv())
endif
endif
elseif rev =~# '^:(\%(top\|top,literal\|literal,top\|literal\))'
@@ -2646,7 +2654,7 @@ function! fugitive#BufReadStatus(...) abort
let cmd = [s:Dir()]
setlocal noreadonly modifiable nomodeline buftype=nowrite
if amatch !~# '^fugitive:' && s:cpath(fnamemodify($GIT_INDEX_FILE !=# '' ? FugitiveVimPath($GIT_INDEX_FILE) : fugitive#Find('.git/index'), ':p')) !=# s:cpath(amatch)
if amatch !~# '^fugitive:' && s:cpath($GIT_INDEX_FILE !=# '' ? resolve(s:GitIndexFileEnv()) : fugitive#Find('.git/index')) !=# s:cpath(amatch)
let cmd += [{'env': {'GIT_INDEX_FILE': FugitiveGitPath(amatch)}}]
endif