mirror of
https://github.com/airblade/vim-gitgutter.git
synced 2025-11-08 11:33:48 -05:00
Use "undo" instead of "revert" for discarding a hunk.
"Undo" is a better name than "revert" because: - "revert" sounds like it has something to do with `git-revert` but they are entirely different; - "undo" is consistent with vim's "undo": discarding changes and going back to the original. Maintain backwards compatibility and add deprecation warnings. Closes #306.
This commit is contained in:
16
README.mkd
16
README.mkd
@@ -1,6 +1,6 @@
|
||||
## vim-gitgutter
|
||||
|
||||
A Vim plugin which shows a git diff in the 'gutter' (sign column). It shows whether each line has been added, modified, and where lines have been removed. You can also stage and revert individual hunks.
|
||||
A Vim plugin which shows a git diff in the 'gutter' (sign column). It shows whether each line has been added, modified, and where lines have been removed. You can also stage and undo individual hunks.
|
||||
|
||||
Features:
|
||||
|
||||
@@ -8,7 +8,7 @@ Features:
|
||||
* Neovim: runs the diffs asynchronously.
|
||||
* Ensures signs are always as up to date as possible (but without running more than necessary).
|
||||
* Quick jumping between blocks of changed lines ("hunks").
|
||||
* Stage/revert/preview individual hunks.
|
||||
* Stage/undo/preview individual hunks.
|
||||
* Diffs against index (default) or any commit.
|
||||
* Handles line endings correctly, even with repos that do CRLF conversion.
|
||||
* Optional line highlighting.
|
||||
@@ -61,7 +61,7 @@ If you are on Windows you may find the command prompt pops up briefly every time
|
||||
|
||||
When you make a change to a file tracked by git, the diff markers should appear automatically. The delay is governed by vim's `updatetime` option; the default value is 4 seconds but I suggest reducing it to around 250ms (add `set updatetime=250` to your vimrc).
|
||||
|
||||
You can jump between hunks with `[c` and `]c`. You can preview, stage, and revert hunks with `<leader>hp`, `<leader>hs`, and `<leader>hr` respectively.
|
||||
You can jump between hunks with `[c` and `]c`. You can preview, stage, and undo hunks with `<leader>hp`, `<leader>hs`, and `<leader>hu` respectively.
|
||||
|
||||
You cannot currently unstage a staged hunk.
|
||||
|
||||
@@ -112,20 +112,20 @@ nmap ]h <Plug>GitGutterNextHunk
|
||||
nmap [h <Plug>GitGutterPrevHunk
|
||||
```
|
||||
|
||||
You can stage or revert an individual hunk when your cursor is in it:
|
||||
You can stage or undo an individual hunk when your cursor is in it:
|
||||
|
||||
* stage the hunk with `<Leader>hs` or
|
||||
* revert it with `<Leader>hr`.
|
||||
* undo it with `<Leader>hu`.
|
||||
|
||||
See the FAQ if you want to unstage staged changes.
|
||||
|
||||
The `.` command will work with both these if you install [repeat.vim](https://github.com/tpope/vim-repeat).
|
||||
|
||||
To set your own mappings for these, for example if you prefer the mnemonics hunk-add and hunk-undo:
|
||||
To set your own mappings for these, for example if you prefer the mnemonics hunk-add and hunk-revert:
|
||||
|
||||
```viml
|
||||
nmap <Leader>ha <Plug>GitGutterStageHunk
|
||||
nmap <Leader>hu <Plug>GitGutterRevertHunk
|
||||
nmap <Leader>hr <Plug>GitGutterUndoHunk
|
||||
```
|
||||
|
||||
And you can preview a hunk's changes with `<Leader>hp`. You can of course change this mapping, e.g:
|
||||
@@ -273,7 +273,7 @@ To disable all key mappings:
|
||||
let g:gitgutter_map_keys = 0
|
||||
```
|
||||
|
||||
See above for configuring maps for hunk-jumping and staging/reverting.
|
||||
See above for configuring maps for hunk-jumping and staging/undoing.
|
||||
|
||||
|
||||
#### Use a custom `grep` command
|
||||
|
||||
@@ -201,7 +201,7 @@ function! gitgutter#stage_hunk()
|
||||
endif
|
||||
endfunction
|
||||
|
||||
function! gitgutter#revert_hunk()
|
||||
function! gitgutter#undo_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.
|
||||
@@ -212,14 +212,14 @@ function! gitgutter#revert_hunk()
|
||||
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(diff, 'revert')
|
||||
let diff_for_hunk = gitgutter#diff#generate_diff_for_hunk(diff, 'undo')
|
||||
call gitgutter#utility#system(gitgutter#utility#command_in_directory_of_file('git apply --reverse --unidiff-zero - '), diff_for_hunk)
|
||||
|
||||
" reload file
|
||||
silent edit
|
||||
endif
|
||||
|
||||
silent! call repeat#set("\<Plug>GitGutterRevertHunk", -1)<CR>
|
||||
silent! call repeat#set("\<Plug>GitGutterUndoHunk", -1)<CR>
|
||||
endif
|
||||
endfunction
|
||||
|
||||
|
||||
@@ -291,11 +291,11 @@ endfunction
|
||||
" Generates a zero-context diff for the current hunk.
|
||||
"
|
||||
" diff - the full diff for the buffer
|
||||
" type - stage | revert | preview
|
||||
" type - stage | undo | preview
|
||||
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')
|
||||
let diff_for_hunk = gitgutter#diff#discard_hunks(a:diff, a:type == 'stage' || a:type == 'undo')
|
||||
|
||||
if a:type == 'stage' || a:type == 'revert'
|
||||
if a:type == 'stage' || a:type == 'undo'
|
||||
let diff_for_hunk = gitgutter#diff#adjust_hunk_summary(diff_for_hunk, a:type == 'stage')
|
||||
endif
|
||||
|
||||
@@ -330,7 +330,7 @@ endfunction
|
||||
" Adjust hunk summary (from's / to's line number) to ignore changes above/before this one.
|
||||
"
|
||||
" diff_for_hunk - a diff containing only the hunk of interest
|
||||
" staging - truthy if the hunk is to be staged, falsy if it is to be reverted
|
||||
" staging - truthy if the hunk is to be staged, falsy if it is to be undone
|
||||
"
|
||||
" TODO: push this down to #discard_hunks?
|
||||
function! gitgutter#diff#adjust_hunk_summary(diff_for_hunk, staging)
|
||||
|
||||
@@ -98,13 +98,13 @@ Commands for jumping between marked hunks:
|
||||
:GitGutterPrevHunk *:GitGutterPrevHunk*
|
||||
Jump to the previous marked hunk. Takes a count.
|
||||
|
||||
Commands for staging or reverting individual hunks:
|
||||
Commands for staging or undoing individual hunks:
|
||||
|
||||
:GitGutterStageHunk *:GitGutterStageHunk*
|
||||
Stage the hunk the cursor is in.
|
||||
|
||||
:GitGutterRevertHunk *:GitGutterRevertHunk*
|
||||
Revert the hunk the cursor is in.
|
||||
:GitGutterUndoHunk *:GitGutterUndoHunk*
|
||||
Undo the hunk the cursor is in.
|
||||
|
||||
:GitGutterPreviewHunk *:GitGutterPreviewHunk*
|
||||
Preview the hunk the cursor is in.
|
||||
@@ -215,10 +215,10 @@ To change the hunk-jumping maps (defaults shown):
|
||||
nmap ]c <Plug>GitGutterNextHunk
|
||||
<
|
||||
|
||||
To change the hunk-staging/reverting/previewing maps (defaults shown):
|
||||
To change the hunk-staging/undoing/previewing maps (defaults shown):
|
||||
>
|
||||
nmap <Leader>hs <Plug>GitGutterStageHunk
|
||||
nmap <Leader>hr <Plug>GitGutterRevertHunk
|
||||
nmap <Leader>hu <Plug>GitGutterUndoHunk
|
||||
nmap <Leader>hp <Plug>GitGutterPreviewHunk
|
||||
<
|
||||
|
||||
|
||||
@@ -90,7 +90,8 @@ command -bar -count=1 GitGutterNextHunk call gitgutter#hunk#next_hunk(<count>)
|
||||
command -bar -count=1 GitGutterPrevHunk call gitgutter#hunk#prev_hunk(<count>)
|
||||
|
||||
command -bar GitGutterStageHunk call gitgutter#stage_hunk()
|
||||
command -bar GitGutterRevertHunk call gitgutter#revert_hunk()
|
||||
command -bar GitGutterUndoHunk call gitgutter#undo_hunk()
|
||||
command -bar GitGutterRevertHunk echomsg 'GitGutterRevertHunk is deprecated. Use GitGutterUndoHunk'<Bar>call gitgutter#undo_hunk()
|
||||
command -bar GitGutterPreviewHunk call gitgutter#preview_hunk()
|
||||
|
||||
" Returns the git-diff hunks for the file or an empty list if there
|
||||
@@ -141,15 +142,16 @@ endif
|
||||
|
||||
|
||||
nnoremap <silent> <Plug>GitGutterStageHunk :GitGutterStageHunk<CR>
|
||||
nnoremap <silent> <Plug>GitGutterRevertHunk :GitGutterRevertHunk<CR>
|
||||
nnoremap <silent> <Plug>GitGutterUndoHunk :GitGutterUndoHunk<CR>
|
||||
nnoremap <silent> <Plug>GitGutterPreviewHunk :GitGutterPreviewHunk<CR>
|
||||
|
||||
if g:gitgutter_map_keys
|
||||
if !hasmapto('<Plug>GitGutterStageHunk') && maparg('<Leader>hs', 'n') ==# ''
|
||||
nmap <Leader>hs <Plug>GitGutterStageHunk
|
||||
endif
|
||||
if !hasmapto('<Plug>GitGutterRevertHunk') && maparg('<Leader>hr', 'n') ==# ''
|
||||
nmap <Leader>hr <Plug>GitGutterRevertHunk
|
||||
if !hasmapto('<Plug>GitGutterUndoHunk') && maparg('<Leader>hu', 'n') ==# ''
|
||||
nmap <Leader>hu <Plug>GitGutterUndoHunk
|
||||
nmap <Leader>hr <Plug>GitGutterUndoHunk:echomsg '<Leader>hr is deprecated. Use <Leader>hu'<CR>
|
||||
endif
|
||||
if !hasmapto('<Plug>GitGutterPreviewHunk') && maparg('<Leader>hp', 'n') ==# ''
|
||||
nmap <Leader>hp <Plug>GitGutterPreviewHunk
|
||||
|
||||
@@ -7,7 +7,7 @@ execute 'GitGutterStageHunk'
|
||||
call DumpSigns('hunkOutsideNoopStageSigns')
|
||||
call DumpGitDiffStaged('hunkHunkOutsideNoopStageGitDiffStaged')
|
||||
|
||||
execute 'GitGutterRevertHunk'
|
||||
call DumpSigns('hunkOutsideNoopRevertSigns')
|
||||
call DumpGitDiffStaged('hunkHunkOutsideNoopRevertGitDiffStaged')
|
||||
execute 'GitGutterUndoHunk'
|
||||
call DumpSigns('hunkOutsideNoopUndoSigns')
|
||||
call DumpGitDiffStaged('hunkHunkOutsideNoopUndoGitDiffStaged')
|
||||
|
||||
|
||||
@@ -1,7 +0,0 @@
|
||||
source helper.vim
|
||||
call Setup()
|
||||
|
||||
normal 5Gi*
|
||||
execute 'GitGutterRevertHunk'
|
||||
call DumpSigns('hunkRevertSigns')
|
||||
call DumpGitDiffStaged('hunkRevertGitDiff')
|
||||
@@ -1,9 +0,0 @@
|
||||
source helper.vim
|
||||
call Setup()
|
||||
|
||||
execute "normal! 2Gox\<CR>y\<CR>z"
|
||||
normal 2jdd
|
||||
normal k
|
||||
execute 'GitGutterRevertHunk'
|
||||
call DumpSigns('hunkRevertNearbySigns')
|
||||
call DumpGitDiff('hunkRevertNearbyGitDiff')
|
||||
7
test/testHunkUndo.vim
Normal file
7
test/testHunkUndo.vim
Normal file
@@ -0,0 +1,7 @@
|
||||
source helper.vim
|
||||
call Setup()
|
||||
|
||||
normal 5Gi*
|
||||
execute 'GitGutterUndoHunk'
|
||||
call DumpSigns('hunkUndoSigns')
|
||||
call DumpGitDiffStaged('hunkUndoGitDiff')
|
||||
9
test/testHunkUndoNearbyHunk.vim
Normal file
9
test/testHunkUndoNearbyHunk.vim
Normal file
@@ -0,0 +1,9 @@
|
||||
source helper.vim
|
||||
call Setup()
|
||||
|
||||
execute "normal! 2Gox\<CR>y\<CR>z"
|
||||
normal 2jdd
|
||||
normal k
|
||||
execute 'GitGutterUndoHunk'
|
||||
call DumpSigns('hunkUndoNearbySigns')
|
||||
call DumpGitDiff('hunkUndoNearbyGitDiff')
|
||||
Reference in New Issue
Block a user