commit dc63e2d8906aed297fc8ba72c8c76b68bb5799c4 (tree)
parent 3a25d0aff613c373b6b900bbba5544da28650755
Author: Tim Pope <code@tpope.net>
Date: Mon, 11 Jan 2010 22:51:54 -0500
Press p in :Gstatus to add/reset --patch
Diffstat:
2 files changed, 54 insertions(+), 1 deletion(-)
diff --git a/doc/fugitive.txt b/doc/fugitive.txt
@@ -50,7 +50,8 @@ that are part of Git repositories).
*fugitive-:Gstatus*
:Gstatus Bring up the output of git-status in the preview
window. Press - to stage or unstage the file on the
- cursor line. Press C to invoke |:Gcommit|.
+ cursor line. Press p to do so on a per hunk basis
+ (--patch). Press C to invoke |:Gcommit|.
*fugitive-:Gcommit*
:Gcommit [args] A wrapper around git-commit. If there is nothing
diff --git a/plugin/fugitive.vim b/plugin/fugitive.vim
@@ -552,6 +552,56 @@ function! s:StageToggle(lnum1,lnum2) abort
return 'checktime'
endfunction
+function! s:StagePatch(lnum1,lnum2) abort
+ let add = []
+ let reset = []
+
+ for lnum in range(a:lnum1,a:lnum2)
+ let line = getline(lnum)
+ if line == '# Changes to be committed:'
+ return 'Git reset --patch'
+ elseif line == '# Changed but not updated:'
+ return 'Git add --patch'
+ endif
+ let filename = matchstr(line,'^#\t[[:alpha:] ]\+: *\zs.*')
+ if filename ==# ''
+ let filename = matchstr(line,'^#\t\zs.*')
+ endif
+ if filename ==# ''
+ continue
+ endif
+ if !exists('first_filename')
+ let first_filename = filename
+ endif
+ execute lnum
+ let section = getline(search('^# .*:$','bnW'))
+ if line =~# '^#\trenamed:' && filename =~ ' -> '
+ let reset += [split(filename,' -> ')[1]]
+ elseif section =~? ' to be '
+ let reset += [filename]
+ elseif line !~# '^#\tdeleted:'
+ let add += [filename]
+ endif
+ endfor
+ try
+ if !empty(add)
+ execute "Git add --patch -- ".join(map(add,'s:shellesc(v:val)'))
+ endif
+ if !empty(reset)
+ execute "Git reset --patch -- ".join(map(add,'s:shellesc(v:val)'))
+ endif
+ if exists('first_filename')
+ silent! edit!
+ 1
+ redraw
+ call search('^#\t\%([[:alpha:] ]\+: *\)\=\V'.first_filename.'\$','W')
+ endif
+ catch /^fugitive:/
+ return 'echoerr v:errmsg'
+ endtry
+ return 'checktime'
+endfunction
+
" }}}1
" Gcommit {{{1
@@ -1183,6 +1233,8 @@ function! s:BufReadIndex()
nnoremap <buffer> <silent> i :<C-U>let b:fugitive_display_format -= 1<Bar>exe <SID>BufReadIndex()<CR>
nnoremap <buffer> <silent> - :<C-U>execute <SID>StageToggle(line('.'),line('.')+v:count1-1)<CR>
xnoremap <buffer> <silent> - :<C-U>execute <SID>StageToggle(line("'<"),line("'>"))<CR>
+ nnoremap <buffer> <silent> p :<C-U>execute <SID>StagePatch(line('.'),line('.')+v:count1-1)<CR>
+ xnoremap <buffer> <silent> p :<C-U>execute <SID>StagePatch(line("'<"),line("'>"))<CR>
call s:JumpInit()
nunmap <buffer> P
nunmap <buffer> ~