diff --git a/autoload/fugitive.vim b/autoload/fugitive.vim index bd37750..3c94b45 100644 --- a/autoload/fugitive.vim +++ b/autoload/fugitive.vim @@ -554,6 +554,13 @@ function! s:buffer_relative(...) dict abort return s:sub(s:sub(rev,'.\zs/$',''),'^/',a:0 ? a:1 : '') endfunction +function! s:buffer_path(...) dict abort + if a:0 + return self.relative(a:1) + endif + return self.relative() +endfunction + function! s:buffer_rev() dict abort let rev = matchstr(self.spec(),'^fugitive:\%(//\)\=.\{-\}\%(//\|::\)\zs.*') if rev =~ '^\x/' @@ -565,7 +572,7 @@ function! s:buffer_rev() dict abort elseif self.spec() =~ '\.git/refs/\|\.git/.*HEAD$' return self.spec()[strlen(self.repo().dir())+1 : -1] else - return self.path('/') + return self.relative('/') endif endfunction @@ -579,21 +586,21 @@ endfunction function! s:buffer_expand(rev) dict abort if a:rev =~# '^:[0-3]$' - let file = a:rev.self.path(':') + let file = a:rev.self.relative(':') elseif a:rev =~# '^[-:]/$' - let file = '/'.self.path() + let file = '/'.self.relative() elseif a:rev =~# '^-' - let file = 'HEAD^{}'.a:rev[1:-1].self.path(':') + let file = 'HEAD^{}'.a:rev[1:-1].self.relative(':') elseif a:rev =~# '^@{' - let file = 'HEAD'.a:rev.self.path(':') + let file = 'HEAD'.a:rev.self.relative(':') elseif a:rev =~# '^[~^]' let commit = s:sub(self.commit(),'^\d=$','HEAD') - let file = commit.a:rev.self.path(':') + let file = commit.a:rev.self.relative(':') else let file = a:rev endif return s:sub(substitute(file, - \ '%$\|\\\([[:punct:]]\)','\=len(submatch(1)) ? submatch(1) : self.path()','g'), + \ '%$\|\\\([[:punct:]]\)','\=len(submatch(1)) ? submatch(1) : self.relative()','g'), \ '\.\@<=/$','') endfunction @@ -631,8 +638,7 @@ function! s:buffer_up(...) dict abort return rev endfunction -call s:add_methods('buffer',['getvar','setvar','getline','repo','type','spec','name','commit','relative','rev','sha1','expand','containing_commit','up']) -let s:buffer_prototype.path = s:function('s:buffer_relative') +call s:add_methods('buffer',['getvar','setvar','getline','repo','type','spec','name','commit','path','relative','rev','sha1','expand','containing_commit','up']) " Section: Git @@ -1287,7 +1293,7 @@ function! s:Grep(cmd,bang,arg) abort endfunction function! s:Log(cmd, line1, line2, ...) abort - let path = s:buffer().path('/') + let path = s:buffer().relative('/') if path =~# '^/\.git\%(/\|$\)' || index(a:000,'--') != -1 let path = '' endif @@ -1296,11 +1302,11 @@ function! s:Log(cmd, line1, line2, ...) abort if empty(filter(a:000[0 : index(a:000,'--')],'v:val !~# "^-"')) if s:buffer().commit() =~# '\x\{40\}' let cmd += [s:buffer().commit()] - elseif s:buffer().path() =~# '^\.git/refs/\|^\.git/.*HEAD$' - let cmd += [s:buffer().path()[5:-1]] + elseif s:buffer().relative() =~# '^\.git/refs/\|^\.git/.*HEAD$' + let cmd += [s:buffer().relative()[5:-1]] endif end - let cmd += map(copy(a:000),'s:sub(v:val,"^\\%(%(:\\w)*)","\\=fnamemodify(s:buffer().path(),submatch(1))")') + let cmd += map(copy(a:000),'s:sub(v:val,"^\\%(%(:\\w)*)","\\=fnamemodify(s:buffer().relative(),submatch(1))")') if path =~# '/.' if a:line2 let cmd += ['-L', a:line1 . ',' . a:line2 . ':' . path[1:-1]] @@ -1404,10 +1410,10 @@ function! s:Edit(cmd, bang, mods, ...) abort let file = buffer.expand(join(a:000, ' ')) elseif expand('%') ==# '' let file = ':' - elseif buffer.commit() ==# '' && buffer.path('/') !~# '^/.git\>' - let file = buffer.path(':') + elseif buffer.commit() ==# '' && buffer.relative('/') !~# '^/.git\>' + let file = buffer.relative(':') else - let file = buffer.path('/') + let file = buffer.relative('/') endif try let file = buffer.repo().translate(file) @@ -1457,7 +1463,7 @@ function! s:Write(force,...) abort return 'wq' elseif s:buffer().type() == 'index' return 'Gcommit' - elseif s:buffer().path() ==# '' && getline(4) =~# '^+++ ' + elseif s:buffer().relative() ==# '' && getline(4) =~# '^+++ ' let filename = getline(4)[6:-1] setlocal buftype= silent write @@ -1478,14 +1484,14 @@ function! s:Write(force,...) abort endif let mytab = tabpagenr() let mybufnr = bufnr('') - let path = a:0 ? join(a:000, ' ') : s:buffer().path() + let path = a:0 ? join(a:000, ' ') : s:buffer().relative() if empty(path) return 'echoerr '.string('fugitive: cannot determine file path') endif if path =~# '^:\d\>' return 'write'.(a:force ? '! ' : ' ').s:fnameescape(s:repo().translate(s:buffer().expand(path))) endif - let always_permitted = (s:buffer().path() ==# path && s:buffer().commit() =~# '^0\=$') + let always_permitted = (s:buffer().relative() ==# path && s:buffer().commit() =~# '^0\=$') if !always_permitted && !a:force && s:repo().git_chomp_in_tree('diff','--name-status','HEAD','--',path) . s:repo().git_chomp_in_tree('ls-files','--others','--',path) !=# '' let v:errmsg = 'fugitive: file has uncommitted changes (use ! to override)' return 'echoerr v:errmsg' @@ -1542,7 +1548,7 @@ function! s:Write(force,...) abort let v:errmsg = 'fugitive: '.error return 'echoerr v:errmsg' endif - if s:buffer().path() ==# path && s:buffer().commit() =~# '^\d$' + if s:buffer().relative() ==# path && s:buffer().commit() =~# '^\d$' set nomodified endif @@ -1765,7 +1771,7 @@ function! s:Diff(vert,keepfocus,...) abort let vert = empty(a:vert) ? s:diff_modifier(2) : a:vert if exists(':DiffGitCached') return 'DiffGitCached' - elseif (empty(args) || args[0] == ':') && s:buffer().commit() =~# '^[0-1]\=$' && s:repo().git_chomp_in_tree('ls-files', '--unmerged', '--', s:buffer().path()) !=# '' + elseif (empty(args) || args[0] ==# ':') && s:buffer().commit() =~# '^[0-1]\=$' && s:repo().git_chomp_in_tree('ls-files', '--unmerged', '--', s:buffer().relative()) !=# '' let vert = empty(a:vert) ? s:diff_modifier(3) : a:vert let nr = bufnr('') execute 'leftabove '.vert.'split' s:fnameescape(fugitive#repo().translate(s:buffer().expand(':2'))) @@ -1787,12 +1793,12 @@ function! s:Diff(vert,keepfocus,...) abort if arg ==# '' return post elseif arg ==# '/' - let file = s:buffer().path('/') + let file = s:buffer().relative('/') elseif arg ==# ':' - let file = s:buffer().path(':0:') + let file = s:buffer().relative(':0:') elseif arg =~# '^:/.' try - let file = s:repo().rev_parse(arg).s:buffer().path(':') + let file = s:repo().rev_parse(arg).s:buffer().relative(':') catch /^fugitive:/ return 'echoerr v:errmsg' endtry @@ -1800,10 +1806,10 @@ function! s:Diff(vert,keepfocus,...) abort let file = s:buffer().expand(arg) endif if file !~# ':' && file !~# '^/' && s:repo().git_chomp('cat-file','-t',file) =~# '^\%(tag\|commit\)$' - let file = file.s:buffer().path(':') + let file = file.s:buffer().relative(':') endif else - let file = s:buffer().path(s:buffer().commit() == '' ? ':0:' : '/') + let file = s:buffer().relative(empty(s:buffer().commit()) ? ':0:' : '/') endif try let spec = s:repo().translate(file) @@ -1839,7 +1845,7 @@ function! s:Move(force, rename, destination) abort if a:destination =~# '^/' let destination = a:destination[1:-1] elseif a:rename - let destination = fnamemodify(s:buffer().path(), ':h') . '/' . a:destination + let destination = fnamemodify(s:buffer().relative(), ':h') . '/' . a:destination else let destination = s:shellslash(fnamemodify(s:sub(a:destination,'[%#]%(:\w)*','\=expand(submatch(0))'),':p')) if destination[0:strlen(s:repo().tree())] ==# s:repo().tree('') @@ -1850,7 +1856,7 @@ function! s:Move(force, rename, destination) abort " Work around Vim parser idiosyncrasy let discarded = s:buffer().setvar('&swapfile',0) endif - let message = call(s:repo().git_chomp_in_tree,['mv']+(a:force ? ['-f'] : [])+['--', s:buffer().path(), destination], s:repo()) + let message = call(s:repo().git_chomp_in_tree,['mv']+(a:force ? ['-f'] : [])+['--', s:buffer().relative(), destination], s:repo()) if v:shell_error let v:errmsg = 'fugitive: '.message return 'echoerr v:errmsg' @@ -1885,7 +1891,7 @@ function! s:RenameComplete(A,L,P) abort if a:A =~# '^/' return s:repo().superglob(a:A) else - let pre = '/'. fnamemodify(s:buffer().path(), ':h') . '/' + let pre = '/'. fnamemodify(s:buffer().relative(), ':h') . '/' return map(s:repo().superglob(pre.a:A), 'strpart(v:val, len(pre))') endif endfunction @@ -1902,7 +1908,7 @@ function! s:Remove(after, force) abort if a:force let cmd += ['--force'] endif - let message = call(s:repo().git_chomp_in_tree,cmd+['--',s:buffer().path()],s:repo()) + let message = call(s:repo().git_chomp_in_tree,cmd+['--',s:buffer().relative()],s:repo()) if v:shell_error let v:errmsg = 'fugitive: '.s:sub(message,'error:.*\zs\n\(.*-f.*',' (add ! to force)') return 'echoerr '.string(v:errmsg) @@ -1948,7 +1954,7 @@ function! s:Blame(bang,line1,line2,count,args) abort return 'bdelete' endif try - if s:buffer().path() == '' + if empty(s:buffer().relative()) call s:throw('file or blob required') endif if filter(copy(a:args),'v:val !~# "^\\%(--root\|--show-name\\|-\\=\\%([ltfnsew]\\|[MC]\\d*\\)\\+\\)$"') != [] @@ -1961,7 +1967,7 @@ function! s:Blame(bang,line1,line2,count,args) abort else let cmd += ['--contents', '-'] endif - let cmd += ['--', s:buffer().path()] + let cmd += ['--', s:buffer().relative()] let basecmd = escape(call(s:repo().git_command,cmd,s:repo()),'!%#') try let cd = exists('*haslocaldir') && haslocaldir() ? 'lcd' : 'cd' @@ -2070,7 +2076,7 @@ function! s:BlameCommit(cmd) abort let lnum = matchstr(getline('.'),' \zs\d\+\ze\s\+[([:digit:]]') let path = matchstr(getline('.'),'^\^\=\x\+\s\+\zs.\{-\}\ze\s*\d\+ ') if path ==# '' - let path = s:buffer(b:fugitive_blamed_bufnr).path() + let path = s:buffer(b:fugitive_blamed_bufnr).relative() endif execute cmd if search('^diff .* b/\M'.escape(path,'\').'$','W') @@ -2111,7 +2117,7 @@ function! s:BlameJump(suffix) abort let lnum = matchstr(getline('.'),' \zs\d\+\ze\s\+[([:digit:]]') let path = matchstr(getline('.'),'^\^\=\x\+\s\+\zs.\{-\}\ze\s*\d\+ ') if path ==# '' - let path = s:buffer(b:fugitive_blamed_bufnr).path() + let path = s:buffer(b:fugitive_blamed_bufnr).relative() endif let args = b:fugitive_blame_arguments let offset = line('.') - line('w0') @@ -2219,7 +2225,7 @@ function! s:Browse(bang,line1,count,...) abort if rev ==# '' let expanded = s:buffer().rev() elseif rev ==# ':' - let expanded = s:buffer().path('/') + let expanded = s:buffer().relative('/') else let expanded = s:buffer().expand(rev) endif @@ -2775,8 +2781,8 @@ function! fugitive#MapJumps(...) abort nnoremap S :exe GF("vsplit") nnoremap O :exe GF("tabedit") nnoremap - :exe Edit('edit',0,'',buffer().up(v:count1)) if fugitive#buffer().type('tree')call search('^'.escape(expand('#:t'),'.*[]~\').'/\=$','wc')endif - nnoremap P :exe Edit('edit',0,'',buffer().commit().'^'.v:count1.buffer().path(':')) - nnoremap ~ :exe Edit('edit',0,'',buffer().commit().'~'.v:count1.buffer().path(':')) + nnoremap P :exe Edit('edit',0,'',buffer().commit().'^'.v:count1.buffer().relative(':')) + nnoremap ~ :exe Edit('edit',0,'',buffer().commit().'~'.v:count1.buffer().relative(':')) nnoremap C :exe Edit('edit',0,'',buffer().containing_commit()) nnoremap cc :exe Edit('edit',0,'',buffer().containing_commit()) nnoremap co :exe Edit('split',0,'',buffer().containing_commit()) @@ -2798,9 +2804,9 @@ function! s:cfile() abort if buffer.type('tree') let showtree = (getline(1) =~# '^tree ' && getline(2) == "") if showtree && line('.') > 2 - return [buffer.commit().':'.s:buffer().path().(buffer.path() =~# '^$\|/$' ? '' : '/').s:sub(getline('.'),'/$','')] + return [buffer.commit().':'.s:buffer().relative().(buffer.relative() =~# '^$\|/$' ? '' : '/').s:sub(getline('.'),'/$','')] elseif getline('.') =~# '^\d\{6\} \l\{3,8\} \x\{40\}\t' - return [buffer.commit().':'.s:buffer().path().(buffer.path() =~# '^$\|/$' ? '' : '/').s:sub(matchstr(getline('.'),'\t\zs.*'),'/$','')] + return [buffer.commit().':'.s:buffer().relative().(buffer.relative() =~# '^$\|/$' ? '' : '/').s:sub(matchstr(getline('.'),'\t\zs.*'),'/$','')] endif elseif buffer.type('blob')