nvim: autoload/ftplugin: git: Add helper for format-patch

This commit is contained in:
Sanchayan Maity 2022-02-10 12:30:40 +05:30
parent 9c16443e10
commit 4edfa99a21
2 changed files with 25 additions and 0 deletions

View file

@ -5,6 +5,8 @@ nnoremap <buffer> <Leader>gB :call git#git_branch_delete()<CR>
nnoremap <buffer> <Leader>gc :call git#git_cherry_pick()<CR> nnoremap <buffer> <Leader>gc :call git#git_cherry_pick()<CR>
xnoremap <buffer> <Leader>gC :<C-U>call git#git_cherry_pick_range()<CR> xnoremap <buffer> <Leader>gC :<C-U>call git#git_cherry_pick_range()<CR>
nnoremap <buffer> <Leader>gd :call git#git_diff_commit()<CR> nnoremap <buffer> <Leader>gd :call git#git_diff_commit()<CR>
nnoremap <buffer> <Leader>gf :call git#git_format_patch()<CR>
xnoremap <buffer> <Leader>gf :call git#git_format_patch_range()<CR>
nnoremap <buffer> <Leader>gl :call git#git_branch_log()<CR> nnoremap <buffer> <Leader>gl :call git#git_branch_log()<CR>
nnoremap <buffer> <Leader>gL :call git#git_branch_log_pretty()<CR> nnoremap <buffer> <Leader>gL :call git#git_branch_log_pretty()<CR>
nnoremap <buffer> <Leader>gr :call git#git_rebase_branch()<CR> nnoremap <buffer> <Leader>gr :call git#git_rebase_branch()<CR>

View file

@ -110,6 +110,29 @@ function! git#git_diff_commit() abort
execute "Git difftool -y " . commit[1] . " " . commit[1] . "~1" execute "Git difftool -y " . commit[1] . " " . commit[1] . "~1"
endfunction endfunction
" To be used after running some variation of Git log.
function! git#git_format_patch() abort
" A line in Git log is of the form commit <SHA>
let line = trim(getline('.'))
let commit = split(line, " ")
execute "Git format-patch " . commit[1] . "~1"
endfunction
" Git format-patch a range of commits. To be run on output of Git log
" --pretty=oneline. See git_branch_log_pretty.
function! git#git_format_patch_range() abort
let [lnum1, col1] = getpos("'<")[1:2]
let [lnum2, col2] = getpos("'>")[1:2]
let lines = reverse(getline(lnum1, lnum2))
if len(lines) == 0
return ''
endif
let oldCommit = split(lines[0], " ")[:0][0]
let newCommit = split(lines[-1], " ")[:0][0]
execute "Git format-patch " . oldCommit . "~1.." . newCommit
silent execute "bw"
endfunction
" The next two functions allow scoping diffs by line range of a file and named " The next two functions allow scoping diffs by line range of a file and named
" block in a file. Inspired by reading the following article. " block in a file. Inspired by reading the following article.
" https://susanpotter.net/software/tracking-diffs-by-scoping-to-file-range-function-method-or-class-changes-in-git/ " https://susanpotter.net/software/tracking-diffs-by-scoping-to-file-range-function-method-or-class-changes-in-git/