From 4edfa99a21d79020f9f574707c63f347ea1cfc22 Mon Sep 17 00:00:00 2001 From: Sanchayan Maity Date: Thu, 10 Feb 2022 12:30:40 +0530 Subject: [PATCH] nvim: autoload/ftplugin: git: Add helper for format-patch --- nvim/.config/nvim/after/ftplugin/git.vim | 2 ++ nvim/.config/nvim/autoload/git.vim | 23 +++++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/nvim/.config/nvim/after/ftplugin/git.vim b/nvim/.config/nvim/after/ftplugin/git.vim index 1eff8d0..92cc9c4 100644 --- a/nvim/.config/nvim/after/ftplugin/git.vim +++ b/nvim/.config/nvim/after/ftplugin/git.vim @@ -5,6 +5,8 @@ nnoremap gB :call git#git_branch_delete() nnoremap gc :call git#git_cherry_pick() xnoremap gC :call git#git_cherry_pick_range() nnoremap gd :call git#git_diff_commit() +nnoremap gf :call git#git_format_patch() +xnoremap gf :call git#git_format_patch_range() nnoremap gl :call git#git_branch_log() nnoremap gL :call git#git_branch_log_pretty() nnoremap gr :call git#git_rebase_branch() diff --git a/nvim/.config/nvim/autoload/git.vim b/nvim/.config/nvim/autoload/git.vim index 9457e12..1cf57ae 100644 --- a/nvim/.config/nvim/autoload/git.vim +++ b/nvim/.config/nvim/autoload/git.vim @@ -110,6 +110,29 @@ function! git#git_diff_commit() abort execute "Git difftool -y " . commit[1] . " " . commit[1] . "~1" 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 + 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 " 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/