Enable edit commands to work without git dir

This commit is contained in:
Tim Pope
2019-09-03 19:18:51 -04:00
parent 39f9e2d722
commit b8c33aa0a1

View File

@@ -1037,13 +1037,13 @@ endfunction
function! s:Expand(rev, ...) abort function! s:Expand(rev, ...) abort
if a:rev =~# '^:[0-3]$' if a:rev =~# '^:[0-3]$'
let file = a:rev . ':%' let file = len(expand('%')) ? a:rev . ':%' : '%'
elseif a:rev ==# '>' elseif a:rev ==# '>'
let file = '%' let file = '%'
elseif a:rev =~# '^>[~^]' elseif a:rev =~# '^>[~^]'
let file = '!' . a:rev[1:-1] . ':%' let file = len(expand('%')) ? '!' . a:rev[1:-1] . ':%' : '%'
elseif a:rev =~# '^>[> ]\@!' elseif a:rev =~# '^>[> ]\@!'
let file = a:rev[1:-1] . ':%' let file = len(expand('%')) ? a:rev[1:-1] . ':%' : '%'
else else
let file = a:rev let file = a:rev
endif endif
@@ -3907,11 +3907,11 @@ function! s:OpenParse(args) abort
if len(args) if len(args)
let file = join(args) let file = join(args)
elseif empty(expand('%')) elseif empty(expand('%'))
let file = ':' let file = ''
elseif empty(s:DirCommitFile(@%)[1]) && s:Relative('./') !~# '^\./\.git\>' elseif empty(s:DirCommitFile(@%)[1]) && s:Relative('./') !~# '^\./\.git\>'
let file = ':0:%' let file = '>:0'
else else
let file = '%' let file = '>'
endif endif
return [s:Expand(file), join(pre)] return [s:Expand(file), join(pre)]
endfunction endfunction
@@ -3978,7 +3978,6 @@ 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
@@ -4015,7 +4014,6 @@ function! s:ReadCommand(line1, line2, range, count, bang, mods, reg, arg, args)
call fugitive#ReloadStatus() call fugitive#ReloadStatus()
return 'redraw|echo '.string(':!'.s:UserCommand(dir, args)) return 'redraw|echo '.string(':!'.s:UserCommand(dir, 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)
@@ -4055,7 +4053,6 @@ 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 != ''
@@ -4096,6 +4093,7 @@ function! s:WriteCommand(line1, line2, range, count, bang, mods, reg, arg, args)
if file =~# '^fugitive:' if file =~# '^fugitive:'
return 'write' . (a:bang ? '! ' : ' ') . s:fnameescape(file) return 'write' . (a:bang ? '! ' : ' ') . s:fnameescape(file)
endif endif
exe s:DirCheck()
let always_permitted = s:cpath(fugitive#Real(@%), file) && empty(s:DirCommitFile(@%)[1]) let always_permitted = s:cpath(fugitive#Real(@%), file) && empty(s:DirCommitFile(@%)[1])
if !always_permitted && !a:bang && (len(s:TreeChomp('diff', '--name-status', 'HEAD', '--', file)) || len(s:TreeChomp('ls-files', '--others', '--', file))) if !always_permitted && !a:bang && (len(s:TreeChomp('diff', '--name-status', 'HEAD', '--', file)) || len(s:TreeChomp('ls-files', '--others', '--', file)))
let v:errmsg = 'fugitive: file has uncommitted changes (use ! to override)' let v:errmsg = 'fugitive: file has uncommitted changes (use ! to override)'
@@ -4303,7 +4301,6 @@ augroup END
function! s:can_diffoff(buf) abort function! s:can_diffoff(buf) abort
return getwinvar(bufwinnr(a:buf), '&diff') && return getwinvar(bufwinnr(a:buf), '&diff') &&
\ !empty(s:Dir(a:buf)) &&
\ !empty(getwinvar(bufwinnr(a:buf), 'fugitive_diff_restore')) \ !empty(getwinvar(bufwinnr(a:buf), 'fugitive_diff_restore'))
endfunction endfunction
@@ -4406,24 +4403,25 @@ function! s:IsConflicted() abort
endfunction endfunction
function! s:Diff(autodir, keepfocus, mods, ...) abort function! s:Diff(autodir, keepfocus, mods, ...) abort
if exists(':DiffGitCached') && !a:0
return s:Mods(a:mods) . 'DiffGitCached'
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) =~# '^+'
let post = remove(args, 0)[1:-1] let post = remove(args, 0)[1:-1]
endif endif
if exists(':DiffGitCached') && empty(args)
return s:Mods(a:mods) . 'DiffGitCached' . (len(post) ? '|' . post : '')
endif
let commit = s:DirCommitFile(@%)[1] let commit = s:DirCommitFile(@%)[1]
if a:mods =~# '\<tab\>' if a:mods =~# '\<tab\>'
let mods = substitute(a:mods, '\<tab\>', '', 'g') let mods = substitute(a:mods, '\<tab\>', '', 'g')
tab split let pre = 'tab split'
else else
let mods = 'keepalt ' . a:mods let mods = 'keepalt ' . a:mods
let pre = ''
endif endif
let back = exists('*win_getid') ? 'call win_gotoid(' . win_getid() . ')' : 'wincmd p' let back = exists('*win_getid') ? 'call win_gotoid(' . win_getid() . ')' : 'wincmd p'
if (empty(args) || args[0] ==# ':') && a:keepfocus if (empty(args) || args[0] ==# ':') && a:keepfocus
exe s:DirCheck()
if empty(commit) && s:IsConflicted() if empty(commit) && s:IsConflicted()
let parents = [s:Relative(':2:'), s:Relative(':3:')] let parents = [s:Relative(':2:'), s:Relative(':3:')]
elseif empty(commit) elseif empty(commit)
@@ -4437,6 +4435,7 @@ function! s:Diff(autodir, keepfocus, mods, ...) abort
endif endif
try try
if exists('parents') && len(parents) > 1 if exists('parents') && len(parents) > 1
exe pre
let mods = (a:autodir ? s:diff_modifier(len(parents) + 1) : '') . s:Mods(mods, 'leftabove') let mods = (a:autodir ? s:diff_modifier(len(parents) + 1) : '') . s:Mods(mods, 'leftabove')
let nr = bufnr('') let nr = bufnr('')
execute mods 'split' s:fnameescape(s:Generate(parents[0])) execute mods 'split' s:fnameescape(s:Generate(parents[0]))
@@ -4463,11 +4462,14 @@ function! s:Diff(autodir, keepfocus, mods, ...) abort
let arg = join(args, ' ') let arg = join(args, ' ')
if arg ==# '' if arg ==# ''
return post return post
elseif arg ==# '/' elseif arg ==# ':/'
exe s:DirCheck()
let file = s:Relative() let file = s:Relative()
elseif arg ==# ':' elseif arg ==# ':'
exe s:DirCheck()
let file = s:Relative(':0:') let file = s:Relative(':0:')
elseif arg =~# '^:\d$' elseif arg =~# '^:\d$'
exe s:DirCheck()
let file = s:Relative(arg . ':') let file = s:Relative(arg . ':')
else else
try try
@@ -4484,12 +4486,14 @@ function! s:Diff(autodir, keepfocus, mods, ...) abort
let file = s:Relative(':1:') let file = s:Relative(':1:')
let post = 'echohl WarningMsg|echo "Use :Gdiffsplit! for 3 way diff"|echohl NONE|' . post let post = 'echohl WarningMsg|echo "Use :Gdiffsplit! for 3 way diff"|echohl NONE|' . post
else else
exe s:DirCheck()
let file = s:Relative(':0:') let file = s:Relative(':0:')
endif endif
let spec = s:Generate(file) let spec = s:Generate(file)
if spec =~# '^fugitive:' && empty(s:DirCommitFile(spec)[2]) if spec =~# '^fugitive:' && empty(s:DirCommitFile(spec)[2])
let spec = FugitiveVimPath(spec . s:Relative('/')) let spec = FugitiveVimPath(spec . s:Relative('/'))
endif endif
exe pre
let restore = s:diff_restore() let restore = s:diff_restore()
let w:fugitive_diff_restore = restore let w:fugitive_diff_restore = restore
if s:CompareAge(commit, s:DirCommitFile(spec)[1]) < 0 if s:CompareAge(commit, s:DirCommitFile(spec)[1]) < 0