From 8c076e3f68a5f77dc95cdc83ee28df8fba885099 Mon Sep 17 00:00:00 2001 From: Tim Pope Date: Thu, 19 Jul 2018 00:34:04 -0400 Subject: [PATCH] Drop some silly instance methods --- autoload/fugitive.vim | 131 ++++++++++++++++++------------------------ plugin/fugitive.vim | 5 +- 2 files changed, 60 insertions(+), 76 deletions(-) diff --git a/autoload/fugitive.vim b/autoload/fugitive.vim index 6188a62..9c2bdf7 100644 --- a/autoload/fugitive.vim +++ b/autoload/fugitive.vim @@ -202,14 +202,14 @@ function! fugitive#Init() abort endif let buffer = fugitive#buffer() if expand('%:p') =~# ':[\/][\/]' - call buffer.setvar('&path', s:sub(buffer.getvar('&path'), '^\.%(,|$)', '')) + let &l:path = s:sub(&path, '^\.%(,|$)', '') endif - if stridx(buffer.getvar('&tags'), escape(b:git_dir, ', ')) == -1 + if stridx(&tags, escape(b:git_dir, ', ')) == -1 if filereadable(b:git_dir.'/tags') - call buffer.setvar('&tags', escape(b:git_dir.'/tags', ', ').','.buffer.getvar('&tags')) + let &l:tags = escape(b:git_dir.'/tags', ', ').','.&tags endif if &filetype !=# '' && filereadable(b:git_dir.'/'.&filetype.'.tags') - call buffer.setvar('&tags', escape(b:git_dir.'/'.&filetype.'.tags', ', ').','.buffer.getvar('&tags')) + let &l:tags = escape(b:git_dir.'/'.&filetype.'.tags', ', ').','.&tags endif endif try @@ -320,14 +320,17 @@ function! s:repo_translate(spec) dict abort endfunction function! s:repo_head(...) dict abort - let head = s:repo().head_ref() + if !filereadable(self.dir('HEAD')) + return '' + endif + let head = readfile(self.dir('HEAD'))[0] if head =~# '^ref: ' let branch = s:sub(head,'^ref: %(refs/%(heads/|remotes/|tags/)=)=','') elseif head =~# '^\x\{40\}$' " truncate hash to a:1 characters if we're in detached head mode let len = a:0 ? a:1 : 0 - let branch = len ? head[0:len-1] : '' + let branch = len < 0 ? head : len ? head[0:len-1] : '' else return '' endif @@ -441,17 +444,6 @@ endfunction call s:add_methods('repo',['config', 'user', 'aliases']) -function! s:repo_keywordprg() dict abort - let args = ' --git-dir='.escape(self.dir(),"\\\"' ") - if has('gui_running') && !has('win32') - return s:git_command() . ' --no-pager' . args . ' log -1' - else - return s:git_command() . args . ' show' - endif -endfunction - -call s:add_methods('repo',['keywordprg']) - " Section: Buffer function! s:DirCommitFile(path) abort @@ -684,10 +676,6 @@ function! s:buffer_getvar(var) dict abort return getbufvar(self['#'],a:var) endfunction -function! s:buffer_setvar(var,value) dict abort - return setbufvar(self['#'],a:var,a:value) -endfunction - function! s:buffer_getline(lnum) dict abort return get(getbufline(self['#'], a:lnum), 0, '') endfunction @@ -794,14 +782,6 @@ function! s:buffer_rev() dict abort endif endfunction -function! s:buffer_sha1() dict abort - if self.spec() =~? '^fugitive:' || self.spec() =~ '\.git/refs/\|\.git/.*HEAD$' - return self.repo().rev_parse(self.rev()) - else - return '' - endif -endfunction - function! s:buffer_expand(rev) dict abort if a:rev =~# '^:[0-3]$' let file = a:rev.self.relative(':') @@ -822,16 +802,6 @@ function! s:buffer_expand(rev) dict abort \ '\.\@<=/$','') endfunction -function! s:buffer_containing_commit() dict abort - if self.commit() =~# '^\d$' - return ':' - elseif self.commit() =~# '.' - return self.commit() - else - return 'HEAD' - endif -endfunction - function! s:buffer_up(...) dict abort let rev = self.rev() let c = a:0 ? a:1 : 1 @@ -856,7 +826,7 @@ function! s:buffer_up(...) dict abort return rev endfunction -call s:add_methods('buffer',['getvar','setvar','getline','repo','type','spec','name','commit','path','relative','rev','sha1','expand','containing_commit','up']) +call s:add_methods('buffer',['getvar','getline','repo','type','spec','name','commit','path','relative','rev','expand','up']) " Section: Git @@ -1963,28 +1933,28 @@ function! s:diffoff_all(dir) abort execute curwin.'wincmd w' endfunction -function! s:buffer_compare_age(commit) dict abort +function! s:CompareAge(mine, theirs) abort let scores = {':0': 1, ':1': 2, ':2': 3, ':': 4, ':3': 5} - let my_score = get(scores, ':'.self.commit(), 0) - let their_score = get(scores, ':'.substitute(a:commit, '^:', '', ''), 0) + let mine = substitute(a:mine, '^:', '', '') + let theirs = substitute(a:theirs, '^:', '', '') + let my_score = get(scores, ':'.mine, 0) + let their_score = get(scores, ':'.theirs, 0) if my_score || their_score return my_score < their_score ? -1 : my_score != their_score - elseif self.commit() ==# a:commit + elseif mine ==# theirs return 0 endif - let base = self.repo().git_chomp('merge-base',self.commit(),a:commit) - if base ==# self.commit() + let base = s:repo().git_chomp('merge-base', mine, theirs) + if base ==# mine return -1 - elseif base ==# a:commit + elseif base ==# theirs return 1 endif - let my_time = +self.repo().git_chomp('log','--max-count=1','--pretty=format:%at',self.commit()) - let their_time = +self.repo().git_chomp('log','--max-count=1','--pretty=format:%at',a:commit) + let my_time = +s:repo().git_chomp('log','--max-count=1','--pretty=format:%at',a:mine) + let their_time = +s:repo().git_chomp('log','--max-count=1','--pretty=format:%at',a:theirs) return my_time < their_time ? -1 : my_time != their_time endfunction -call s:add_methods('buffer',['compare_age']) - function! s:Diff(vert,keepfocus,...) abort let args = copy(a:000) let post = '' @@ -2041,7 +2011,7 @@ function! s:Diff(vert,keepfocus,...) abort setlocal cursorbind endif let w:fugitive_diff_restore = restore - if s:buffer().compare_age(s:DirCommitFile(spec)[1]) < 0 + if s:CompareAge(s:buffer().commit(), s:DirCommitFile(spec)[1]) < 0 execute 'rightbelow '.vert.'diffsplit '.s:fnameescape(spec) else execute 'leftabove '.vert.'diffsplit '.s:fnameescape(spec) @@ -2076,8 +2046,7 @@ function! s:Move(force, rename, destination) abort endif endif if isdirectory(s:buffer().spec()) - " Work around Vim parser idiosyncrasy - let discarded = s:buffer().setvar('&swapfile',0) + setlocal noswapfile endif let message = call(s:repo().git_chomp_in_tree,['mv']+(a:force ? ['-f'] : [])+['--', s:buffer().relative(), destination], s:repo()) if v:shell_error @@ -2153,9 +2122,18 @@ augroup END " Section: Gblame +function! s:Keywordprg() abort + let args = ' --git-dir='.escape(b:git_dir,"\\\"' ") + if has('gui_running') && !has('win32') + return s:git_command() . ' --no-pager' . args . ' log -1' + else + return s:git_command() . args . ' show' + endif +endfunction + augroup fugitive_blame autocmd! - autocmd FileType fugitiveblame setlocal nomodeline | if exists('b:git_dir') | let &l:keywordprg = s:repo().keywordprg() | endif + autocmd FileType fugitiveblame setlocal nomodeline | if exists('b:git_dir') | let &l:keywordprg = s:Keywordprg() | endif autocmd Syntax fugitiveblame call s:BlameSyntax() autocmd User Fugitive if s:buffer().type('file', 'blob', 'blame') | exe "command! -buffer -bar -bang -range=0 -nargs=* Gblame :execute s:Blame(0,,,,[])" | endif autocmd ColorScheme,GUIEnter * call s:RehighlightBlame() @@ -2655,7 +2633,7 @@ function! s:instaweb_url(opts) abort call s:throw('fugitive: cannot browse uncommitted file') endtry endif - let root .= ';hb=' . matchstr(a:opts.repo.head_ref(),'[^ ]\+$') + let root .= ';hb=' . a:opts.repo.head(-1) endif if a:opts.path !=# '' let url .= ';f=' . a:opts.path @@ -2996,6 +2974,15 @@ function! fugitive#MapCfile(...) abort endif endfunction +function! s:ContainingCommit() abort + let commit = s:DirCommitFile(@%)[1] + if commit =~# '^\d\=$' + return 'HEAD' + else + return commit + endif +endfunction + function! fugitive#MapJumps(...) abort nnoremap :exe GF("edit") if !&modifiable @@ -3003,14 +2990,14 @@ 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().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()) - nnoremap cS :exe Edit('vsplit',0,'',buffer().containing_commit()) - nnoremap cO :exe Edit('tabedit',0,'',buffer().containing_commit()) - nnoremap cP :exe Edit('pedit',0,'',buffer().containing_commit()) + nnoremap P :exe Edit('edit',0,'',ContainingCommit().'^'.v:count1.buffer().relative(':')) + nnoremap ~ :exe Edit('edit',0,'',ContainingCommit().'~'.v:count1.buffer().relative(':')) + nnoremap C :exe Edit('edit',0,'',ContainingCommit()) + nnoremap cc :exe Edit('edit',0,'',ContainingCommit()) + nnoremap co :exe Edit('split',0,'',ContainingCommit()) + nnoremap cS :exe Edit('vsplit',0,'',ContainingCommit()) + nnoremap cO :exe Edit('tabedit',0,'',ContainingCommit()) + nnoremap cP :exe Edit('pedit',0,'',ContainingCommit()) nnoremap . : =fnameescape(recall()) endif endfunction @@ -3018,7 +3005,12 @@ endfunction function! s:cfile() abort try let buffer = s:buffer() - let myhash = buffer.sha1() + + if buffer.spec() =~? '^fugitive:' || buffer.spec() =~# '\.git/refs/\|\.git/.*HEAD$' + let myhash = buffer.repo().rev_parse(buffer.rev()) + else + let myhash = '' + endif if myhash ==# '' && getline(1) =~# '^\%(commit\|tag\) \w' let myhash = matchstr(getline(1),'^\w\+ \zs\S\+') endif @@ -3229,15 +3221,6 @@ endfunction " Section: Statusline -function! s:repo_head_ref() dict abort - if !filereadable(self.dir('HEAD')) - return '' - endif - return readfile(self.dir('HEAD'))[0] -endfunction - -call s:add_methods('repo',['head_ref']) - function! fugitive#Statusline(...) abort if !exists('b:git_dir') return '' diff --git a/plugin/fugitive.vim b/plugin/fugitive.vim index 3b0e6ee..01c3750 100644 --- a/plugin/fugitive.vim +++ b/plugin/fugitive.vim @@ -132,10 +132,11 @@ function! FugitiveStatusline(...) abort endfunction function! FugitiveHead(...) abort - if !exists('b:git_dir') + let dir = a:0 > 1 ? a:2 : get(b:, 'git_dir', '') + if empty(dir) return '' endif - return fugitive#repo().head(a:0 ? a:1 : 0) + return fugitive#repo(dir).head(a:0 ? a:1 : 0) endfunction function! FugitivePath(...) abort