Add Git dir presence guard to all commands

This isn't necessary for the current implementation, but could enable
making the commands (or at least the underlying commands) global.
This commit is contained in:
Tim Pope 2019-08-06 12:31:47 -04:00
parent 35d7590354
commit 9d77fd566c

View File

@ -2207,8 +2207,8 @@ function! s:DirArg(path) abort
endif endif
endfunction endfunction
call s:command("-bar -bang -nargs=? -complete=customlist,s:DirComplete Gcd :exe 'cd<bang>' s:fnameescape(s:DirArg(<q-args>))") call s:command("-bar -bang -nargs=? -complete=customlist,s:DirComplete Gcd :exe s:DirCheck()|exe 'cd<bang>' s:fnameescape(s:DirArg(<q-args>))")
call s:command("-bar -bang -nargs=? -complete=customlist,s:DirComplete Glcd :exe 'lcd<bang>' s:fnameescape(s:DirArg(<q-args>))") call s:command("-bar -bang -nargs=? -complete=customlist,s:DirComplete Glcd :exe s:DirCheck()|exe 'lcd<bang>' s:fnameescape(s:DirArg(<q-args>))")
" Section: :Gstatus " Section: :Gstatus
@ -3676,6 +3676,7 @@ endfunction
function! s:Grep(listnr, bang, arg) abort function! s:Grep(listnr, bang, arg) abort
let dir = s:Dir() let dir = s:Dir()
exe s:DirCheck(dir)
let listnr = a:listnr let listnr = a:listnr
let cmd = s:UserCommandList(dir) + ['--no-pager', 'grep', '-n', '--no-color', '--full-name'] let cmd = s:UserCommandList(dir) + ['--no-pager', 'grep', '-n', '--no-color', '--full-name']
if fugitive#GitVersion(2, 19) if fugitive#GitVersion(2, 19)
@ -3765,6 +3766,7 @@ endfunction
function! s:Log(type, bang, line1, count, args) abort function! s:Log(type, bang, line1, count, args) abort
let dir = s:Dir() let dir = s:Dir()
exe s:DirCheck(dir)
let listnr = a:type =~# '^l' ? 0 : -1 let listnr = a:type =~# '^l' ? 0 : -1
let [args, after] = s:SplitExpandChain(a:args, s:Tree(dir)) let [args, after] = s:SplitExpandChain(a:args, s:Tree(dir))
let split = index(args, '--') let split = index(args, '--')
@ -3905,6 +3907,7 @@ function! s:Open(cmd, bang, mods, arg, args) abort
if a:bang if a:bang
return s:OpenExec(a:cmd, a:mods, s:SplitExpand(a:arg, s:Tree())) return s:OpenExec(a:cmd, a:mods, s:SplitExpand(a:arg, s:Tree()))
endif endif
exe s:DirCheck()
let mods = s:Mods(a:mods) let mods = s:Mods(a:mods)
try try
@ -3942,6 +3945,7 @@ function! s:ReadCommand(line1, line2, range, count, bang, mods, reg, arg, args)
call fugitive#ReloadStatus() call fugitive#ReloadStatus()
return 'redraw|echo '.string(':!'.git.' '.args) return 'redraw|echo '.string(':!'.git.' '.args)
endif endif
exe s:DirCheck()
try try
let [file, pre] = s:OpenParse(a:args) let [file, pre] = s:OpenParse(a:args)
let file = s:Generate(file) let file = s:Generate(file)
@ -3980,6 +3984,7 @@ call s:command("-bar -bang -nargs=* -complete=customlist,fugitive#CompleteObject
call s:command("-bar -bang -nargs=* -complete=customlist,fugitive#CompleteObject Gwq", "Wq") call s:command("-bar -bang -nargs=* -complete=customlist,fugitive#CompleteObject Gwq", "Wq")
function! s:WriteCommand(line1, line2, range, count, bang, mods, reg, arg, args) abort function! s:WriteCommand(line1, line2, range, count, bang, mods, reg, arg, args) abort
exe s:DirCheck()
if exists('b:fugitive_commit_arguments') if exists('b:fugitive_commit_arguments')
return 'write|bdelete' return 'write|bdelete'
elseif expand('%:t') == 'COMMIT_EDITMSG' && $GIT_INDEX_FILE != '' elseif expand('%:t') == 'COMMIT_EDITMSG' && $GIT_INDEX_FILE != ''
@ -4331,6 +4336,7 @@ function! s:Diff(autodir, keepfocus, mods, ...) abort
if exists(':DiffGitCached') && !a:0 if exists(':DiffGitCached') && !a:0
return s:Mods(a:mods) . 'DiffGitCached' return s:Mods(a:mods) . 'DiffGitCached'
endif endif
exe s:DirCheck()
let args = copy(a:000) let args = copy(a:000)
let post = '' let post = ''
if get(args, 0) =~# '^+' if get(args, 0) =~# '^+'
@ -4539,6 +4545,7 @@ function! s:BlameCommand(line1, line2, range, count, bang, mods, reg, arg, args)
if exists('b:fugitive_blamed_bufnr') if exists('b:fugitive_blamed_bufnr')
return 'bdelete' return 'bdelete'
endif endif
exe s:DirCheck()
try try
if empty(s:Relative('/')) if empty(s:Relative('/'))
call s:throw('file or blob required') call s:throw('file or blob required')
@ -4806,6 +4813,7 @@ let s:redirects = {}
function! s:BrowseCommand(line1, line2, range, count, bang, mods, reg, arg, args) abort function! s:BrowseCommand(line1, line2, range, count, bang, mods, reg, arg, args) abort
let dir = s:Dir() let dir = s:Dir()
exe s:DirCheck(dir)
try try
let validremote = '\.\|\.\=/.*\|[[:alnum:]_-]\+\%(://.\{-\}\)\=' let validremote = '\.\|\.\=/.*\|[[:alnum:]_-]\+\%(://.\{-\}\)\='
if a:args ==# ['-'] if a:args ==# ['-']