Skip to content
This repository has been archived by the owner on Feb 1, 2022. It is now read-only.

Commit

Permalink
Updated fugitive.vim
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurgeek committed Jan 17, 2012
1 parent 95bd700 commit fe97431
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 26 deletions.
20 changes: 11 additions & 9 deletions doc/fugitive.txt
Expand Up @@ -163,15 +163,17 @@ that are part of Git repositories).
scroll bound vertical split. Press enter on a line to
reblame the file as it was in that commit. You can
give any of ltwfsMC as flags and they will be passed
along to git-blame.

In a blame buffer on any line, one can press <CR> to
reblame at that commit, ~ to reblame at that commit's
[count]th first grandparent (like HEAD~[count]), or P
to reblame at that commit's [count]th parent (like
HEAD^[count]). Press o or O to open up that commit
in a horizontal of vertical split. Pressing q will
close the blame buffer.
along to git-blame. The following maps, which work on
the cursor line commit where sensible, are provided:

q close blame and return to blamed window
gq q, then |:Gedit| to return to work tree version
i q, then open commit
o open commit in horizontal split
O open commit in new tab
<CR> reblame at commit
~ reblame at [count]th first grandparent
P reblame at [count]th parent (like HEAD^[count])

:[range]Gblame [flags] Run git-blame on the given range.

Expand Down
109 changes: 92 additions & 17 deletions plugin/fugitive.vim
Expand Up @@ -116,7 +116,7 @@ function! s:ExtractGitDir(path) abort
let ofn = ""
let nfn = fn
while fn != ofn
if filereadable(fn . '/.git/HEAD')
if isdirectory(fn) && filereadable(fn . '/.git/HEAD')
return s:sub(simplify(fnamemodify(fn . '/.git',':p')),'\W$','')
elseif fn =~ '\.git$' && filereadable(fn . '/HEAD')
return s:sub(simplify(fnamemodify(fn,':p')),'\W$','')
Expand All @@ -128,7 +128,7 @@ function! s:ExtractGitDir(path) abort
endfunction

function! s:Detect(path)
if exists('b:git_dir') && b:git_dir ==# ''
if exists('b:git_dir') && (b:git_dir ==# '' || b:git_dir =~# '/$')
unlet b:git_dir
endif
if !exists('b:git_dir')
Expand Down Expand Up @@ -158,7 +158,7 @@ augroup fugitive
autocmd BufNewFile,BufReadPost * call s:Detect(expand('<amatch>:p'))
autocmd FileType netrw call s:Detect(expand('<afile>:p'))
autocmd VimEnter * if expand('<amatch>')==''|call s:Detect(getcwd())|endif
autocmd BufWinLeave * execute getwinvar(+winnr(), 'fugitive_leave')
autocmd BufWinLeave * execute getwinvar(+bufwinnr(+expand('<abuf>')), 'fugitive_leave')
augroup END

" }}}1
Expand All @@ -181,6 +181,10 @@ function! s:repo(...) abort
call s:throw('not a git repository: '.expand('%:p'))
endfunction

function! fugitive#repo(...)
return call('s:repo', a:000)
endfunction

function! s:repo_dir(...) dict abort
return join([self.git_dir]+a:000,'/')
endfunction
Expand Down Expand Up @@ -441,6 +445,8 @@ function! s:buffer_path(...) dict abort
let rev = matchstr(self.spec(),'^fugitive://.\{-\}//\zs.*')
if rev != ''
let rev = s:sub(rev,'\w*','')
elseif self.repo().bare()
let rev = '/.git'.self.spec()[strlen(self.repo().dir()) : -1]
else
let rev = self.spec()[strlen(self.repo().tree()) : -1]
endif
Expand Down Expand Up @@ -975,7 +981,7 @@ function! s:Edit(cmd,bang,...) abort
diffupdate
return 'redraw|echo '.string(':!'.git.' '.args)
else
let temp = tempname()
let temp = resolve(tempname())
let s:temp_files[temp] = s:repo().dir()
silent execute a:cmd.' '.temp
if a:cmd =~# 'pedit'
Expand Down Expand Up @@ -1222,6 +1228,9 @@ function! s:diffthis()
let w:fugitive_diff_restore .= &l:wrap ? ' wrap' : ' nowrap'
let w:fugitive_diff_restore .= ' foldmethod=' . &l:foldmethod
let w:fugitive_diff_restore .= ' foldcolumn=' . &l:foldcolumn
if has('cursorbind')
let w:fugitive_diff_restore .= (&l:cursorbind ? ' ' : ' no') . 'cursorbind'
endif
diffthis
endif
endfunction
Expand Down Expand Up @@ -1442,7 +1451,7 @@ function! s:Blame(bang,line1,line2,count,args) abort
if a:count
execute 'write !'.substitute(basecmd,' blame ',' blame -L '.a:line1.','.a:line2.' ','g')
else
let error = tempname()
let error = resolve(tempname())
let temp = error.'.fugitiveblame'
if &shell =~# 'csh'
silent! execute '%write !('.basecmd.' > '.temp.') >& '.error
Expand All @@ -1456,6 +1465,12 @@ function! s:Blame(bang,line1,line2,count,args) abort
if v:shell_error
call s:throw(join(readfile(error),"\n"))
endif
for winnr in range(winnr('$'),1,-1)
call setwinvar(winnr, '&scrollbind', 0)
if getbufvar(winbufnr(winnr), 'fugitive_blamed_bufnr')
execute winbufnr(winnr).'bdelete'
endif
endfor
let bufnr = bufnr('')
let restore = 'call setwinvar(bufwinnr('.bufnr.'),"&scrollbind",0)'
if &l:wrap
Expand All @@ -1464,9 +1479,6 @@ function! s:Blame(bang,line1,line2,count,args) abort
if &l:foldenable
let restore .= '|call setwinvar(bufwinnr('.bufnr.'),"&foldenable",1)'
endif
let winnr = winnr()
windo set noscrollbind
exe winnr.'wincmd w'
setlocal scrollbind nowrap nofoldenable
let top = line('w0') + &scrolloff
let current = line('.')
Expand All @@ -1483,11 +1495,15 @@ function! s:Blame(bang,line1,line2,count,args) abort
if exists('+relativenumber')
setlocal norelativenumber
endif
nnoremap <buffer> <silent> q :exe substitute('bdelete<Bar>'.bufwinnr(b:fugitive_blamed_bufnr).' wincmd w','<Bar>-1','','')<CR>
nnoremap <buffer> <silent> gq :exe substitute('bdelete<Bar>'.bufwinnr(b:fugitive_blamed_bufnr).' wincmd w<Bar>if expand("%:p") =~# "^fugitive:[\\/][\\/]"<Bar>Gedit<Bar>endif','<Bar>-1','','')<CR>
nnoremap <buffer> <silent> <CR> :<C-U>exe <SID>BlameJump('')<CR>
nnoremap <buffer> <silent> P :<C-U>exe <SID>BlameJump('^'.v:count1)<CR>
nnoremap <buffer> <silent> ~ :<C-U>exe <SID>BlameJump('~'.v:count1)<CR>
nnoremap <buffer> <silent> o :<C-U>exe <SID>Edit((&splitbelow ? "botright" : "topleft")." split", 0, matchstr(getline('.'),'\x\+'))<CR>
nnoremap <buffer> <silent> O :<C-U>exe <SID>Edit("tabedit", 0, matchstr(getline('.'),'\x\+'))<CR>
nnoremap <buffer> <silent> i :<C-U>exe <SID>BlameCommit("exe 'norm q'<Bar>edit")<CR>
nnoremap <buffer> <silent> o :<C-U>exe <SID>BlameCommit((&splitbelow ? "botright" : "topleft")." split")<CR>
nnoremap <buffer> <silent> O :<C-U>exe <SID>BlameCommit("tabedit")<CR>
redraw
syncbind
endif
finally
Expand All @@ -1501,13 +1517,54 @@ function! s:Blame(bang,line1,line2,count,args) abort
endtry
endfunction

function! s:BlameCommit(cmd) abort
let cmd = s:Edit(a:cmd, 0, matchstr(getline('.'),'\x\+'))
if cmd =~# '^echoerr'
return cmd
endif
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()
endif
execute cmd
if search('^diff .* b/\M'.escape(path,'\').'$','W')
call search('^+++')
let head = line('.')
while search('^@@ \|^diff ') && getline('.') =~# '^@@ '
let top = +matchstr(getline('.'),' +\zs\d\+')
let len = +matchstr(getline('.'),' +\d\+,\zs\d\+')
if lnum >= top && lnum <= top + len
let offset = lnum - top
if &scrolloff
+
normal! zt
else
normal! zt
+
endif
while offset > 0 && line('.') < line('$')
+
if getline('.') =~# '^[ +]'
let offset -= 1
endif
endwhile
return ''
endif
endwhile
execute head
normal! zt
endif
return ''
endfunction

function! s:BlameJump(suffix) abort
let commit = matchstr(getline('.'),'^\^\=\zs\x\+')
if commit =~# '^0\+$'
let commit = ':0'
endif
let lnum = matchstr(getline('.'),'\d\+\ze\s\+[([:digit:]]')
let path = matchstr(getline('.'),'^\^\=\zs\x\+\s\+\zs.\{-\}\ze\s*\d\+ ')
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()
endif
Expand All @@ -1519,16 +1576,17 @@ function! s:BlameJump(suffix) abort
exe winnr.'wincmd w'
endif
execute s:Edit('edit', 0, commit.a:suffix.':'.path)
execute lnum
if winnr > 0
exe bufnr.'bdelete'
endif
execute 'Gblame '.args
execute lnum
let delta = line('.') - line('w0') - offset
if delta > 0
execute 'norm! 'delta."\<C-E>"
execute 'normal! '.delta."\<C-E>"
elseif delta < 0
execute 'norm! '(-delta)."\<C-Y>"
execute 'normal! '.(-delta)."\<C-Y>"
endif
syncbind
return ''
Expand Down Expand Up @@ -1818,7 +1876,7 @@ function! s:BufReadIndex()
endtry
set ft=gitcommit
endif
setlocal ro noma nomod nomodeline bufhidden=wipe
setlocal ro noma nomod nomodeline noswapfile bufhidden=wipe
call s:JumpInit()
nunmap <buffer> P
nunmap <buffer> ~
Expand Down Expand Up @@ -2000,7 +2058,9 @@ augroup END
" }}}1
" Temp files {{{1

let s:temp_files = {}
if !exists('s:temp_files')
let s:temp_files = {}
endif

augroup fugitive_temp
autocmd!
Expand All @@ -2010,7 +2070,7 @@ augroup fugitive_temp
\ let b:git_type = 'temp' |
\ call s:Detect(expand('<amatch>:p')) |
\ setlocal bufhidden=delete |
\ nnoremap <buffer> <silent> q :<C-U>bdelete<CR> |
\ nnoremap <buffer> <silent> q :<C-U>bdelete<CR>|
\ endif
augroup END

Expand Down Expand Up @@ -2131,6 +2191,21 @@ function! s:GF(mode) abort
elseif getline('.') =~# '^[+-]\{3\} [ab/]'
let ref = getline('.')[4:]

elseif getline('.') =~# '^[+-]' && search('^@@ -\d\+,\d\+ +\d\+,','bnW')
let type = getline('.')[0]
let lnum = line('.') - 1
let offset = -1
while getline(lnum) !~# '^@@ -\d\+,\d\+ +\d\+,'
if getline(lnum) =~# '^[ '.type.']'
let offset += 1
endif
let lnum -= 1
endwhile
let offset += matchstr(getline(lnum), type.'\zs\d\+')
let ref = getline(search('^'.type.'\{3\} [ab]/','bnW'))[4:-1]
let dcmd = '+'.offset.'|foldopen'
let dref = ''

elseif getline('.') =~# '^rename from '
let ref = 'a/'.getline('.')[12:]
elseif getline('.') =~# '^rename to '
Expand Down

0 comments on commit fe97431

Please sign in to comment.