Return empty string on FugitiveFind() with no Git dir

Previously, we would return a path from the current working directory in
this case, which was a good fallback for :Gedit but unhelpful for
general programmatic usage.
This commit is contained in:
Tim Pope
2021-04-13 07:25:48 -04:00
parent 7866e83cc2
commit 054fca2269

View File

@@ -914,7 +914,7 @@ function! s:Owner(path, ...) abort
elseif commit ==# '0'
return ''
endif
let merge_head = s:MergeHead()
let merge_head = s:MergeHead(dir)
if empty(merge_head)
return ''
endif
@@ -1023,10 +1023,10 @@ function! fugitive#Find(object, ...) abort
endif
let dir = a:0 ? a:1 : s:Dir()
if empty(dir)
let file = matchstr(a:object, '^\%(:\d:\|[^:]*:\)\zs.*', '', '')
let file = matchstr(a:object, '^\%(:\d:\|[^:]*:\)\zs\%(\.\.\=$\|\.\.\=/.*\|/.*\|\w:/.*\)')
let dir = FugitiveExtractGitDir(file)
if empty(dir)
return fnamemodify(FugitiveVimPath(len(file) ? file : a:object), ':p')
return ''
endif
endif
let tree = s:Tree(dir)
@@ -1049,7 +1049,7 @@ function! fugitive#Find(object, ...) abort
let f = simplify(dir . f)
endif
elseif rev ==# ':/'
let f = base
let f = tree
elseif rev =~# '^\.\%(/\|$\)'
let f = base . rev[1:-1]
elseif rev =~# '^::\%(/\|\a\+\:\)'
@@ -1080,6 +1080,8 @@ function! fugitive#Find(object, ...) abort
let f = matchstr(rev, ')\zs.*')
if f=~# '^\.\.\=\%(/\|$\)'
let f = simplify(getcwd() . '/' . f)
elseif empty(f)
let f = base
elseif f !~# '^/\|^\%(\a\a\+:\).*\%(//\|::\)' . (has('win32') ? '\|^\a:/' : '')
let f = base . '/' . f
endif
@@ -1106,7 +1108,7 @@ function! fugitive#Find(object, ...) abort
call map(commits, 'empty(v:val) || v:val ==# "@" ? "HEAD" : v:val')
let commit = matchstr(s:ChompDefault('', [dir, 'merge-base'] + commits + ['--']), '\<[0-9a-f]\{40,\}\>')
endif
if commit !~# '^[0-9a-f]\{40,\}$'
if commit !~# '^[0-9a-f]\{40,\}$\|^$'
let commit = matchstr(s:ChompDefault('', [dir, 'rev-parse', '--verify', commit . (len(file) ? '^{}' : ''), '--']), '\<[0-9a-f]\{40,\}\>')
if empty(commit) && len(file)
let commit = repeat('0', 40)
@@ -1122,8 +1124,16 @@ function! fugitive#Find(object, ...) abort
return FugitiveVimPath(f)
endfunction
function! s:Generate(rev, ...) abort
return fugitive#Find(a:rev, a:0 ? a:1 : s:Dir())
function! s:Generate(object, ...) abort
let dir = a:0 ? a:1 : s:Dir()
let f = fugitive#Find(a:object, dir)
if !empty(f)
return f
elseif a:object ==# ':/'
return len(dir) ? FugitiveVimPath('fugitive://' . dir . '//0') : '.'
endif
let file = matchstr(a:object, '^\%(:\d:\|[^:]*:\)\zs.*')
return fnamemodify(FugitiveVimPath(len(file) ? file : a:object), ':p')
endfunction
function! s:DotRelative(path, ...) abort
@@ -4890,7 +4900,7 @@ function! s:OpenParse(string, wants_cmd) abort
endif
let dir = s:Dir()
let efile = s:Expand(file)
let url = fugitive#Find(efile, dir)
let url = s:Generate(efile, dir)
if a:wants_cmd && file[0] ==# '>' && efile[0] !=# '>' && get(b:, 'fugitive_type', '') isnot# 'tree' && &filetype !=# 'netrw'
let line = line('.')
@@ -5133,9 +5143,9 @@ function! fugitive#WriteCommand(line1, line2, range, bang, mods, arg, args) abor
setlocal nomodified
endif
let one = s:Generate(':1:'.file)
let two = s:Generate(':2:'.file)
let three = s:Generate(':3:'.file)
let one = fugitive#Find(':1:'.file)
let two = fugitive#Find(':2:'.file)
let three = fugitive#Find(':3:'.file)
for nr in range(1,bufnr('$'))
let name = fnamemodify(bufname(nr), ':p')
if bufloaded(nr) && !getbufvar(nr,'&modified') && (name ==# one || name ==# two || name ==# three)
@@ -5144,7 +5154,7 @@ function! fugitive#WriteCommand(line1, line2, range, bang, mods, arg, args) abor
endfor
unlet! restorewinnr
let zero = s:Generate(':0:'.file)
let zero = fugitive#Find(':0:'.file)
silent exe s:DoAutocmd('BufWritePost ' . s:fnameescape(zero))
for tab in range(1,tabpagenr('$'))
for winnr in range(1,tabpagewinnr(tab,'$'))
@@ -5356,7 +5366,7 @@ function! fugitive#Diffsplit(autodir, keepfocus, mods, arg, args) abort
exe pre
let mods = (a:autodir ? s:diff_modifier(len(parents) + 1) : '') . s:Mods(mods, 'leftabove')
let nr = bufnr('')
execute mods 'split' s:fnameescape(s:Generate(parents[0]))
execute mods 'split' s:fnameescape(fugitive#Find(parents[0]))
call s:Map('n', 'dp', ':diffput '.nr.'<Bar>diffupdate<CR>', '<silent>')
let nr2 = bufnr('')
call s:diffthis()
@@ -5364,7 +5374,7 @@ function! fugitive#Diffsplit(autodir, keepfocus, mods, arg, args) abort
call s:Map('n', 'd2o', ':diffget '.nr2.'<Bar>diffupdate<CR>', '<silent>')
let mods = substitute(mods, '\Cleftabove\|rightbelow\|aboveleft\|belowright', '\=submatch(0) =~# "f" ? "rightbelow" : "leftabove"', '')
for i in range(len(parents)-1, 1, -1)
execute mods 'split' s:fnameescape(s:Generate(parents[i]))
execute mods 'split' s:fnameescape(fugitive#Find(parents[i]))
call s:Map('n', 'dp', ':diffput '.nr.'<Bar>diffupdate<CR>', '<silent>')
let nrx = bufnr('')
call s:diffthis()
@@ -6561,7 +6571,7 @@ function! s:StatusCfile(...) abort
endfunction
function! fugitive#StatusCfile() abort
let file = s:Generate(s:StatusCfile()[0])
let file = fugitive#Find(s:StatusCfile()[0])
return empty(file) ? fugitive#Cfile() : s:fnameescape(file)
endfunction
@@ -6591,7 +6601,7 @@ function! s:MessageCfile(...) abort
endfunction
function! fugitive#MessageCfile() abort
let file = s:Generate(s:MessageCfile())
let file = fugitive#Find(s:MessageCfile())
return empty(file) ? fugitive#Cfile() : s:fnameescape(file)
endfunction