From f0e2cb5b4a274576cdd157fc57a9500c3465b400 Mon Sep 17 00:00:00 2001 From: Andy Stewart Date: Mon, 8 Feb 2016 15:45:48 +0000 Subject: [PATCH] Avoid unnecessary diff when adding/staging/reverting hunks. --- autoload/gitgutter.vim | 21 ++++++++++++--------- autoload/gitgutter/diff.vim | 7 +++---- 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/autoload/gitgutter.vim b/autoload/gitgutter.vim index 7539378..3053666 100644 --- a/autoload/gitgutter.vim +++ b/autoload/gitgutter.vim @@ -183,13 +183,14 @@ function! gitgutter#stage_hunk() if gitgutter#utility#is_active() " Ensure the working copy of the file is up to date. " It doesn't make sense to stage a hunk otherwise. - " This also updates the signs and hunks. - silent write + noautocmd silent write + let diff = gitgutter#diff#run_diff(0, 1) + call gitgutter#handle_diff(diff) if empty(gitgutter#hunk#current_hunk()) call gitgutter#utility#warn('cursor is not in a hunk') else - let diff_for_hunk = gitgutter#diff#generate_diff_for_hunk('stage') + let diff_for_hunk = gitgutter#diff#generate_diff_for_hunk(diff, 'stage') call gitgutter#utility#system(gitgutter#utility#command_in_directory_of_file('git apply --cached --unidiff-zero - '), diff_for_hunk) " refresh gitgutter's view of buffer @@ -204,13 +205,14 @@ function! gitgutter#revert_hunk() if gitgutter#utility#is_active() " Ensure the working copy of the file is up to date. " It doesn't make sense to stage a hunk otherwise. - " This also updates the signs and hunks. - silent write + noautocmd silent write + let diff = gitgutter#diff#run_diff(0, 1) + call gitgutter#handle_diff(diff) if empty(gitgutter#hunk#current_hunk()) call gitgutter#utility#warn('cursor is not in a hunk') else - let diff_for_hunk = gitgutter#diff#generate_diff_for_hunk('revert') + let diff_for_hunk = gitgutter#diff#generate_diff_for_hunk(diff, 'revert') call gitgutter#utility#system(gitgutter#utility#command_in_directory_of_file('git apply --reverse --unidiff-zero - '), diff_for_hunk) " reload file @@ -225,13 +227,14 @@ function! gitgutter#preview_hunk() if gitgutter#utility#is_active() " Ensure the working copy of the file is up to date. " It doesn't make sense to stage a hunk otherwise. - " This also updates the signs and hunks. - silent write + noautocmd silent write + let diff = gitgutter#diff#run_diff(0, 1) + call gitgutter#handle_diff(diff) if empty(gitgutter#hunk#current_hunk()) call gitgutter#utility#warn('cursor is not in a hunk') else - let diff_for_hunk = gitgutter#diff#generate_diff_for_hunk('preview') + let diff_for_hunk = gitgutter#diff#generate_diff_for_hunk(diff, 'preview') silent! wincmd P if !&previewwindow diff --git a/autoload/gitgutter/diff.vim b/autoload/gitgutter/diff.vim index 3620955..b1d1db4 100644 --- a/autoload/gitgutter/diff.vim +++ b/autoload/gitgutter/diff.vim @@ -286,11 +286,10 @@ endfunction " Generates a zero-context diff for the current hunk. " +" diff - the full diff for the buffer " type - stage | revert | preview -function! gitgutter#diff#generate_diff_for_hunk(type) - " Run a fresh diff. - let diff = gitgutter#diff#run_diff(0, 1) - let diff_for_hunk = gitgutter#diff#discard_hunks(diff, a:type == 'stage' || a:type == 'revert') +function! gitgutter#diff#generate_diff_for_hunk(diff, type) + let diff_for_hunk = gitgutter#diff#discard_hunks(a:diff, a:type == 'stage' || a:type == 'revert') if a:type == 'stage' || a:type == 'revert' let diff_for_hunk = gitgutter#diff#adjust_hunk_summary(diff_for_hunk, a:type == 'stage')