Skip to content

Commit

Permalink
Squashed 'home/.vim/bundle/vim-gitgutter/' changes from 83ace20..061258e
Browse files Browse the repository at this point in the history
061258e Add note about solarized-dark.
e5efbaf Fix Markdown typo.
6b8e908 Enable `.` command to work with staging/reverting hunks.
25257c4 Dealt with the case where the preview window is used by another plugin
2c8e82a Add another note for Fish users to README.
1a68abe Update caveat about visual artefacts.
88c7916 Use system() with silent to avoid visual artifact
57342e3 Minor refactor.
7ff7455 Add tests for ambiguous filenames.
f73df41 Handle files with the same name as revisions.
83309ee Cache the fact that a file is known to git.
7f70d52 Refactor the test cleanup.
d56d26b Test runner's exit code reports number of failures.
af1f405 Add test for filenames with square brackets.
a114088 Support symlinked files.
4ca2de6 More tests.
6cef76a Add tests.
dfe985a Use buffer numbers instead of buffer names.
ce4e3d7 Add test for sign column.
6d91198 Drop extension for bash script.
d7a1785 Add test for reverting hunks.
e5add67 Use clearer file extensions for test files.
1f2bb48 Add test for hunk staging.
dd05453 Allow multiple expectations per test.
86cde1a Add basic test stats.
d24686e Extract name manipulation.
7d0492e Add section on limitations of current tests.
4ad4c97 DRY quitting vim after each test.
b9f4173 Refactor tests to be isolated.
a82bc50 Ignore test output.
09134a5 Add basic tests.
b0e9efa Make realtime diff use git-diff.
39f0119 Whitespace.
067160c Remove obsolete comment.
e5eb9e6 Fix staging of hunks coming after deleted lines.
1e82019 Ensure closing preview window deletes previewed buffer.
198faa1 Report updatetime option in debugging buffer.

git-subtree-dir: home/.vim/bundle/vim-gitgutter
git-subtree-split: 061258e04476c0f9f653a969e640bf03b3659594
  • Loading branch information
avdgaag committed Feb 26, 2015
1 parent 2e45359 commit 422a5d9
Show file tree
Hide file tree
Showing 47 changed files with 465 additions and 160 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,2 +1,3 @@
/doc/tags
/misc
/test/*.actual
16 changes: 14 additions & 2 deletions README.mkd
Expand Up @@ -34,6 +34,8 @@ In the screenshot above you can see:

Before installation, please check your Vim supports signs by running `:echo has('signs')`. `1` means you're all set; `0` means you need to install a Vim with signs support. If you're compiling Vim yourself you need the 'big' or 'huge' feature set. [MacVim][] supports signs.

Your vim `shell` option needs to point to a POSIX-compatible shell. For example if you use Fish, add `set shell=/bin/bash` to your vimrc.

If you don't have a preferred installation method, I recommend installing [pathogen.vim][pathogen], and then simply copy and paste:

```
Expand Down Expand Up @@ -107,6 +109,8 @@ You can stage or revert an individual hunk when your cursor is in it:
* stage the hunk with `<Leader>hs` or
* revert it with `<Leader>hr`.

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:

```viml
Expand Down Expand Up @@ -145,7 +149,7 @@ By default the signs are updated as follows:
| Save a buffer | So non-realtime signs are up to date | [always] |
| Change a file outside Vim | To notice `git stash` | [always] |

The length of time Vim waits after you stop typing before it triggers the plugin is governed by the setting `updatetime`. This defaults to `4000` milliseconds which is rather too long. I recommend around `750` milliseconds but it depends on your system and your preferences. Note that in terminal Vim an `updatetime` of less than approximately `1000` milliseconds can lead to random highlighting glitches; the lower the `updatetime`, the more glitches. This is due to a bug in Vim.
The length of time Vim waits after you stop typing before it triggers the plugin is governed by the setting `updatetime`. This defaults to `4000` milliseconds which is rather too long. I recommend around `750` milliseconds but it depends on your system and your preferences. Note that in terminal Vim pre-7.4.427 an `updatetime` of less than approximately `1000` milliseconds can lead to random highlighting glitches; the lower the `updatetime`, the more glitches.

If you experience a lag, you can trade speed for accuracy:

Expand Down Expand Up @@ -191,6 +195,14 @@ To change your sign column's appearance, update your colorscheme or `~/.vimrc` l
* For a specific appearance on terminal Vim: `highlight SignColumn ctermbg=whatever`
* For a specific appearance on gVim/MacVim: `highlight SignColumn guibg=whatever`

If you use [solarized](https://github.com/altercation/vim-colors-solarized)-dark, try the following snippet in your `~/.vimrc`:

```viml
set background=dark
colorscheme solarized
highlight clear SignColumn
```

By default the sign column will appear when there are signs to show and disappear when there aren't. If you would always like the sign column to be there, add `let g:gitgutter_sign_column_always = 1` to your `~/.vimrc`.


Expand Down Expand Up @@ -297,7 +309,7 @@ Here are some things you can check:
* Your git config is compatible with the version of git which your Vim is calling (`:echo system('git --version')`).
* Your Vim supports signs (`:echo has('signs')` should give `1`).
* Your file is being tracked by git and has unstaged, saved changes.
* If you use the Fish shell, add `set shell=/bin/bash` to your `~/.vimrc`.
* If you use the Fish shell, or another non-POSIX shell, add `set shell=/bin/bash` to your `~/.vimrc`.


### Shameless Plug
Expand Down
60 changes: 23 additions & 37 deletions autoload/gitgutter.vim
Expand Up @@ -4,36 +4,36 @@ function! gitgutter#all()
for buffer_id in tabpagebuflist()
let file = expand('#' . buffer_id . ':p')
if !empty(file)
call gitgutter#process_buffer(file, 0)
call gitgutter#process_buffer(buffer_id, 0)
endif
endfor
endfunction

" file: (string) the file to process.
" bufnr: (integer) the buffer to process.
" realtime: (boolean) when truthy, do a realtime diff; otherwise do a disk-based diff.
function! gitgutter#process_buffer(file, realtime)
call gitgutter#utility#set_file(a:file)
function! gitgutter#process_buffer(bufnr, realtime)
call gitgutter#utility#set_buffer(a:bufnr)
if gitgutter#utility#is_active()
if g:gitgutter_sign_column_always
call gitgutter#sign#add_dummy_sign()
endif
try
if !a:realtime || gitgutter#utility#has_fresh_changes(a:file)
let diff = gitgutter#diff#run_diff(a:realtime || gitgutter#utility#has_unsaved_changes(a:file), 1)
if !a:realtime || gitgutter#utility#has_fresh_changes()
let diff = gitgutter#diff#run_diff(a:realtime || gitgutter#utility#has_unsaved_changes(), 1, 0)
call gitgutter#hunk#set_hunks(gitgutter#diff#parse_diff(diff))
let modified_lines = gitgutter#diff#process_hunks(gitgutter#hunk#hunks())

if len(modified_lines) > g:gitgutter_max_signs
call gitgutter#utility#warn('exceeded maximum number of signs (configured by g:gitgutter_max_signs).')
call gitgutter#sign#clear_signs(a:file)
call gitgutter#sign#clear_signs()
return
endif

if g:gitgutter_signs || g:gitgutter_highlight_lines
call gitgutter#sign#update_signs(a:file, modified_lines)
call gitgutter#sign#update_signs(modified_lines)
endif

call gitgutter#utility#save_last_seen_change(a:file)
call gitgutter#utility#save_last_seen_change()
endif
catch /diff failed/
call gitgutter#hunk#reset()
Expand All @@ -53,8 +53,8 @@ function! gitgutter#disable()
for buffer_id in buflist
let file = expand('#' . buffer_id . ':p')
if !empty(file)
call gitgutter#utility#set_file(file)
call gitgutter#sign#clear_signs(gitgutter#utility#file())
call gitgutter#utility#set_buffer(buffer_id)
call gitgutter#sign#clear_signs()
call gitgutter#sign#remove_dummy_sign(1)
call gitgutter#hunk#reset()
endif
Expand Down Expand Up @@ -85,7 +85,7 @@ function! gitgutter#line_highlights_disable()
call gitgutter#highlight#define_sign_line_highlights()

if !g:gitgutter_signs
call gitgutter#sign#clear_signs(gitgutter#utility#file())
call gitgutter#sign#clear_signs()
call gitgutter#sign#remove_dummy_sign(0)
endif

Expand Down Expand Up @@ -133,7 +133,7 @@ function! gitgutter#signs_disable()
call gitgutter#highlight#define_sign_text_highlights()

if !g:gitgutter_highlight_lines
call gitgutter#sign#clear_signs(gitgutter#utility#file())
call gitgutter#sign#clear_signs()
call gitgutter#sign#remove_dummy_sign(0)
endif
endfunction
Expand All @@ -156,20 +156,16 @@ function! gitgutter#stage_hunk()
" It doesn't make sense to stage a hunk otherwise.
silent write

" find current hunk
let current_hunk = gitgutter#hunk#current_hunk()
if empty(current_hunk)
return
endif

" construct a diff
let diff_for_hunk = gitgutter#diff#generate_diff_for_hunk(current_hunk, 1)
let diff_for_hunk = gitgutter#diff#generate_diff_for_hunk(1, 1)

" apply the diff
call gitgutter#utility#system(gitgutter#utility#command_in_directory_of_file('git apply --cached --unidiff-zero - '), diff_for_hunk)
call gitgutter#utility#system(gitgutter#utility#command_in_directory_of_file('git apply --cached --recount --allow-overlap - '), diff_for_hunk)

" refresh gitgutter's view of buffer
silent execute "GitGutter"

silent! call repeat#set("\<Plug>GitGutterStageHunk", -1)<CR>
endif
endfunction

Expand All @@ -179,44 +175,34 @@ function! gitgutter#revert_hunk()
" It doesn't make sense to stage a hunk otherwise.
silent write

" find current hunk
let current_hunk = gitgutter#hunk#current_hunk()
if empty(current_hunk)
return
endif

" construct a diff
let diff_for_hunk = gitgutter#diff#generate_diff_for_hunk(current_hunk, 1)
let diff_for_hunk = gitgutter#diff#generate_diff_for_hunk(1, 1)

" apply the diff
call gitgutter#utility#system(gitgutter#utility#command_in_directory_of_file('git apply --reverse --unidiff-zero - '), diff_for_hunk)
call gitgutter#utility#system(gitgutter#utility#command_in_directory_of_file('git apply --reverse - '), diff_for_hunk)

" reload file
silent edit

silent! call repeat#set("\<Plug>GitGutterRevertHunk", -1)<CR>
endif
endfunction

function! gitgutter#preview_hunk()
if gitgutter#utility#is_active()
silent write

" find current hunk
let current_hunk = gitgutter#hunk#current_hunk()
if empty(current_hunk)
return
endif

" construct a diff
let diff_for_hunk = gitgutter#diff#generate_diff_for_hunk(current_hunk, 0)
let diff_for_hunk = gitgutter#diff#generate_diff_for_hunk(0, 0)

" preview the diff
silent! wincmd P
if !&previewwindow
execute 'bo ' . &previewheight . ' new'
set previewwindow
setlocal filetype=diff buftype=nowrite
endif

setlocal noro modifiable filetype=diff buftype=nofile bufhidden=delete noswapfile
execute "%delete_"
call append(0, split(diff_for_hunk, "\n"))

Expand Down
1 change: 1 addition & 0 deletions autoload/gitgutter/debug.vim
Expand Up @@ -12,6 +12,7 @@ function! gitgutter#debug#debug()
call gitgutter#debug#git_version()
call gitgutter#debug#separator()

call gitgutter#debug#option('updatetime')
call gitgutter#debug#option('shell')
call gitgutter#debug#option('shellcmdflag')
call gitgutter#debug#option('shellpipe')
Expand Down
53 changes: 36 additions & 17 deletions autoload/gitgutter/diff.vim
@@ -1,19 +1,29 @@
let s:grep_available = executable('grep')
let s:grep_command = ' | ' . (g:gitgutter_escape_grep ? '\grep' : 'grep') . ' -e ' . gitgutter#utility#shellescape('^@@ ')
let s:grep_command = ' | '.(g:gitgutter_escape_grep ? '\grep' : 'grep').' -e '.gitgutter#utility#shellescape('^@@ ')
let s:hunk_re = '^@@ -\(\d\+\),\?\(\d*\) +\(\d\+\),\?\(\d*\) @@'


function! gitgutter#diff#run_diff(realtime, use_external_grep)
" Wrap compound command in parentheses to make Windows happy.
let cmd = '(git ls-files --error-unmatch ' . gitgutter#utility#shellescape(gitgutter#utility#filename()) . ' && ('
function! gitgutter#diff#run_diff(realtime, use_external_grep, lines_of_context)
" Wrap compound commands in parentheses to make Windows happy.
let cmd = '('

let bufnr = gitgutter#utility#bufnr()
let tracked = getbufvar(bufnr, 'gitgutter_tracked') " i.e. tracked by git
if !tracked
let cmd .= 'git ls-files --error-unmatch '.gitgutter#utility#shellescape(gitgutter#utility#filename()).' && ('
endif

if a:realtime
let blob_name = ':' . gitgutter#utility#shellescape(gitgutter#utility#file_relative_to_repo_root())
let blob_name = ':'.gitgutter#utility#shellescape(gitgutter#utility#file_relative_to_repo_root())
let blob_file = tempname()
let cmd .= 'git show ' . blob_name . ' > ' . blob_file .
\ ' && diff -U0 ' . g:gitgutter_diff_args . ' ' . blob_file . ' - '
let cmd .= 'git show '.blob_name.' > '.blob_file.' && '
endif

let cmd .= 'git diff --no-ext-diff --no-color -U'.a:lines_of_context.' '.g:gitgutter_diff_args.' -- '
if a:realtime
let cmd .= blob_file.' - '
else
let cmd .= 'git diff --no-ext-diff --no-color -U0 ' . g:gitgutter_diff_args . ' ' . gitgutter#utility#shellescape(gitgutter#utility#filename())
let cmd .= gitgutter#utility#shellescape(gitgutter#utility#filename())
endif

if a:use_external_grep && s:grep_available
Expand All @@ -28,7 +38,11 @@ function! gitgutter#diff#run_diff(realtime, use_external_grep)
let cmd.= ' || exit 0'
endif

let cmd .= '))'
let cmd .= ')'

if !tracked
let cmd .= ')'
endif

if a:realtime
let diff = gitgutter#utility#system(gitgutter#utility#command_in_directory_of_file(cmd), gitgutter#utility#buffer_contents())
Expand All @@ -37,11 +51,14 @@ function! gitgutter#diff#run_diff(realtime, use_external_grep)
endif

if gitgutter#utility#shell_error()
" A shell error indicates the file is not tracked by git (unless something
" bizarre is going on).
" A shell error indicates the file is not tracked by git (unless something bizarre is going on).
throw 'diff failed'
endif

if !tracked
call setbufvar(bufnr, 'gitgutter_tracked', 1)
endif

return diff
endfunction

Expand Down Expand Up @@ -182,22 +199,24 @@ function! gitgutter#diff#process_modified_and_removed(modifications, from_count,
let a:modifications[-1] = [a:to_line + offset - 1, 'modified_removed']
endfunction

function! gitgutter#diff#generate_diff_for_hunk(hunk, keep_header)
let diff = gitgutter#diff#discard_hunks(gitgutter#diff#run_diff(0, 0), a:hunk, a:keep_header)
function! gitgutter#diff#generate_diff_for_hunk(keep_header, lines_of_context)
let diff = gitgutter#diff#run_diff(0, 0, a:lines_of_context)
let diff_for_hunk = gitgutter#diff#discard_hunks(diff, a:keep_header)
if !a:keep_header
" Discard summary line
let diff = join(split(diff, '\n')[1:-1], "\n")
let diff_for_hunk = join(split(diff_for_hunk, '\n')[1:-1], "\n")
endif
return diff
return diff_for_hunk
endfunction

function! gitgutter#diff#discard_hunks(diff, hunk_to_keep, keep_header)
" diff - with non-zero lines of context
function! gitgutter#diff#discard_hunks(diff, keep_header)
let modified_diff = []
let keep_line = a:keep_header
for line in split(a:diff, '\n')
let hunk_info = gitgutter#diff#parse_hunk(line)
if len(hunk_info) == 4 " start of new hunk
let keep_line = (hunk_info == a:hunk_to_keep)
let keep_line = gitgutter#hunk#cursor_in_hunk(hunk_info)
endif
if keep_line
call add(modified_diff, line)
Expand Down
40 changes: 20 additions & 20 deletions autoload/gitgutter/highlight.vim
Expand Up @@ -44,11 +44,11 @@ function! gitgutter#highlight#define_signs()
endfunction

function! gitgutter#highlight#define_sign_text()
execute "sign define GitGutterLineAdded text=" . g:gitgutter_sign_added
execute "sign define GitGutterLineModified text=" . g:gitgutter_sign_modified
execute "sign define GitGutterLineRemoved text=" . g:gitgutter_sign_removed
execute "sign define GitGutterLineAdded text=" . g:gitgutter_sign_added
execute "sign define GitGutterLineModified text=" . g:gitgutter_sign_modified
execute "sign define GitGutterLineRemoved text=" . g:gitgutter_sign_removed
execute "sign define GitGutterLineRemovedFirstLine text=" . g:gitgutter_sign_removed_first_line
execute "sign define GitGutterLineModifiedRemoved text=" . g:gitgutter_sign_modified_removed
execute "sign define GitGutterLineModifiedRemoved text=" . g:gitgutter_sign_modified_removed
endfunction

function! gitgutter#highlight#define_sign_text_highlights()
Expand All @@ -57,33 +57,33 @@ function! gitgutter#highlight#define_sign_text_highlights()
" off or disabling) we make them invisible by setting their foreground colours
" to the background's.
if g:gitgutter_signs
sign define GitGutterLineAdded texthl=GitGutterAdd
sign define GitGutterLineModified texthl=GitGutterChange
sign define GitGutterLineRemoved texthl=GitGutterDelete
sign define GitGutterLineAdded texthl=GitGutterAdd
sign define GitGutterLineModified texthl=GitGutterChange
sign define GitGutterLineRemoved texthl=GitGutterDelete
sign define GitGutterLineRemovedFirstLine texthl=GitGutterDelete
sign define GitGutterLineModifiedRemoved texthl=GitGutterChangeDelete
sign define GitGutterLineModifiedRemoved texthl=GitGutterChangeDelete
else
sign define GitGutterLineAdded texthl=GitGutterAddInvisible
sign define GitGutterLineModified texthl=GitGutterChangeInvisible
sign define GitGutterLineRemoved texthl=GitGutterDeleteInvisible
sign define GitGutterLineAdded texthl=GitGutterAddInvisible
sign define GitGutterLineModified texthl=GitGutterChangeInvisible
sign define GitGutterLineRemoved texthl=GitGutterDeleteInvisible
sign define GitGutterLineRemovedFirstLine texthl=GitGutterDeleteInvisible
sign define GitGutterLineModifiedRemoved texthl=GitGutterChangeDeleteInvisible
sign define GitGutterLineModifiedRemoved texthl=GitGutterChangeDeleteInvisible
endif
endfunction

function! gitgutter#highlight#define_sign_line_highlights()
if g:gitgutter_highlight_lines
sign define GitGutterLineAdded linehl=GitGutterAddLine
sign define GitGutterLineModified linehl=GitGutterChangeLine
sign define GitGutterLineRemoved linehl=GitGutterDeleteLine
sign define GitGutterLineAdded linehl=GitGutterAddLine
sign define GitGutterLineModified linehl=GitGutterChangeLine
sign define GitGutterLineRemoved linehl=GitGutterDeleteLine
sign define GitGutterLineRemovedFirstLine linehl=GitGutterDeleteLine
sign define GitGutterLineModifiedRemoved linehl=GitGutterChangeDeleteLine
sign define GitGutterLineModifiedRemoved linehl=GitGutterChangeDeleteLine
else
sign define GitGutterLineAdded linehl=
sign define GitGutterLineModified linehl=
sign define GitGutterLineRemoved linehl=
sign define GitGutterLineAdded linehl=
sign define GitGutterLineModified linehl=
sign define GitGutterLineRemoved linehl=
sign define GitGutterLineRemovedFirstLine linehl=
sign define GitGutterLineModifiedRemoved linehl=
sign define GitGutterLineModifiedRemoved linehl=
endif
endfunction

Expand Down

0 comments on commit 422a5d9

Please sign in to comment.