Skip to content

Commit a70e094

Browse files
committed
lots of bugfixes
1 parent 8d1cb06 commit a70e094

4 files changed

Lines changed: 74 additions & 55 deletions

File tree

autoload/diffstat.vim

Lines changed: 50 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ if exists('g:loaded_diffstat')
88
finish
99
endif
1010

11+
let s:GIT_ERROR = "GIT ERROR"
1112

1213
" Maps visible file names to actual system paths.
1314
let s:diff_files_list = {}
1415

15-
function! g:DiffStatOpenFile()
16+
function! s:DiffStatOpenFile()
1617
let l:name = s:DiffStatFileNameForLine('.')
1718
if strlen(l:name)
1819
execute "wincmd w"
@@ -33,36 +34,39 @@ function! s:DisplayWindow(show)
3334

3435
if l:nr < 0
3536
split __diffstat__
37+
setlocal filetype=diffstat
38+
setlocal bufhidden=delete
39+
setlocal buftype=nofile
40+
setlocal foldcolumn=0
41+
setlocal nobuflisted
42+
setlocal nofoldenable
43+
setlocal nolist
44+
setlocal nonumber
45+
setlocal nospell
46+
setlocal noswapfile
47+
setlocal nowrap
48+
setlocal statusline=DiffStat
49+
setlocal textwidth=0
50+
setlocal winfixwidth
51+
if exists('+colorcolumn')
52+
setlocal colorcolumn=
53+
endif
54+
if exists('+relativenumber')
55+
setlocal norelativenumber
56+
endif
3657
else
3758
execute l:nr . " wincmd w"
3859
endif
60+
3961
setlocal noreadonly modifiable
4062
normal! gg"_dGA
41-
setlocal filetype=diffstat
42-
setlocal bufhidden=hide
43-
setlocal buftype=nofile
44-
setlocal foldcolumn=0
45-
setlocal nobuflisted
46-
setlocal nofoldenable
47-
setlocal nolist
48-
setlocal nonumber
49-
setlocal nospell
50-
setlocal noswapfile
51-
setlocal nowrap
52-
setlocal statusline=DiffStat
53-
setlocal textwidth=0
54-
setlocal winfixwidth
55-
if exists('+colorcolumn')
56-
setlocal colorcolumn=
57-
endif
58-
if exists('+relativenumber')
59-
setlocal norelativenumber
60-
endif
6163

6264
setlocal foldmethod=expr
6365
setlocal foldexpr=s:DiffStatFold(v:lnum)
6466

65-
nnoremap <script> <buffer> <silent> <cr> :call g:DiffStatOpenFile()<cr>
67+
if !hasmapto("DiffStatOpenFile", "\n")
68+
nnoremap <script> <buffer> <silent> <cr> :call <SID>DiffStatOpenFile()<cr>
69+
endif
6670

6771
endfunction
6872

@@ -83,6 +87,9 @@ function! s:DiffStatCommand(command)
8387

8488
let l:command_result =
8589
\ system(g:diff_stat_git_command . " diff " . a:command . " --numstat")
90+
if v:shell_error
91+
throw s:GIT_ERROR
92+
endif
8693

8794
let files_list = {}
8895
let l:max_deltas = 0
@@ -133,10 +140,15 @@ function! s:GetTotalsString(files_list)
133140
let l:deletes_total += value['deletes']
134141
endif
135142
endfor
136-
return printf("%d files changed, %d insertions(+), %d deletions",
137-
\ len(a:files_list), l:inserts_total, l:deletes_total)
143+
return printf("%s changed, %s(+), %s(-)",
144+
\ s:FormatCountString('file', len(a:files_list)),
145+
\ s:FormatCountString('insertion', l:inserts_total),
146+
\ s:FormatCountString('deletion', l:deletes_total))
138147
endfunction
139148

149+
function! s:FormatCountString(str, count)
150+
return a:count . " " . a:str . (a:count == 1 ? "" : "s")
151+
endfunction
140152

141153
let s:DiffStatRegex = '\v^\s*(\S+)\s*\|\s*\d+\s*[+-]+\s*$'
142154
function! s:DiffStatFileNameForLine(lnum)
@@ -159,12 +171,12 @@ endfunction
159171
function! diffstat#run(...)
160172
let s:toplevel = split(system(
161173
\ g:diff_stat_git_command . " rev-parse --show-toplevel"), "\n")[0]
162-
if s:toplevel =~# '\v^\s*$' || !isdirectory(s:toplevel)
174+
if v:shell_error
163175
call s:DisplayWindow(0)
164176
echohl WarningMsg
165177
echomsg "DiffStat can only be run in a .git repo"
166178
echohl None
167-
return
179+
return 0
168180
endif
169181
call s:DisplayWindow(1)
170182
let s:diff_files_list = {}
@@ -177,8 +189,16 @@ function! diffstat#run(...)
177189
if !empty(l:lines)
178190
call add(l:lines, "")
179191
endif
180-
call add(l:lines, "#" . g:diff_stat_git_command . " diff " . l:commit)
181-
let l:files_list = s:DiffStatCommand(l:commit)
192+
call add(l:lines, "# " . g:diff_stat_git_command . " diff " . l:commit)
193+
try
194+
let l:files_list = s:DiffStatCommand(l:commit)
195+
catch
196+
call s:DisplayWindow(0)
197+
echohl ErrorMsg
198+
echomsg "Unknown revision or path " . l:commit . " in working tree."
199+
echohl None
200+
return 0
201+
endtry
182202
for [key, value] in items(l:files_list)
183203
call add(l:lines, value['string'])
184204
endfor
@@ -189,5 +209,7 @@ function! diffstat#run(...)
189209
call setpos(1, 1)
190210
setlocal readonly nomodifiable
191211
setlocal statusline="DiffStat " . join(a:000, ' ')
212+
return 1
192213
endfunction
193214

215+
let g:loaded_diffstat = 1

doc/diffstat.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ Use an alternate git command.
5656

5757
let g:diff_stat_git_command = '/usr/local/bin/git1.8.0'
5858

59-
Path simplifications. *g:diff_stat_path_simplifications*
59+
Path simplifications. *g:diff_stat_path_simplifications*
6060

6161
Define custom path simplifications for use in longer file paths. The keys in
6262
this dictionary are path patters, and the values are replacements. This can be

plugin/diffstat.vim

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@ if !exists('g:diff_stat_git_command')
88
let g:diff_stat_git_command = 'git'
99
endif
1010

11-
if !exists('g:diff_stat_secondary_git_command')
12-
let g:diff_stat_git_secondary_command = 'git'
13-
endif
14-
1511
if !exists('g:diff_stat_path_simplifications')
1612
" These are useful for Guava, but probably not much else. Change them to suit
1713
" your needs if you have very long paths.

syntax/diffstat.vim

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,34 @@
1-
if exists("b:diffstat_syntax")
2-
"finish
1+
scriptencoding utf-8
2+
if exists('b:diffstat_syntax')
3+
finish
34
endif
45

5-
syntax match diffStatBranch "\v\S+$"
6-
highlight link diffStatBranch Keyword
7-
8-
syn region diffStatComment start="\v^#" end="\n" contains=diffStatBranch
9-
highlight link diffStatComment Comment
6+
syntax match diffStatBranch '\v\S+$'
7+
highlight default link diffStatBranch Keyword
108

119
syntax keyword diffStatBin Bin
12-
highlight link diffStatBin Comment
13-
14-
syntax match diffStatSeparator "\v\|"
15-
highlight link diffStatSeparator Comment
10+
highlight default link diffStatBin Comment
1611

17-
highlight DiffStatAdd ctermfg=green
18-
highlight DiffStatRemove ctermfg=red
12+
highlight default DiffStatAddHl ctermfg=green
13+
highlight default DiffStatRemoveHl ctermfg=red
1914

20-
syntax match diffStatRemove "\v\-+"
21-
highlight link diffStatRemove DiffStatRemove
22-
syntax match diffStatAdd "\v\++"
23-
highlight link diffStatAdd DiffStatAdd
15+
syntax match diffStatRemove '\v\-+'
16+
highlight default link diffStatRemove DiffStatRemoveHl
17+
syntax match diffStatAdd '\v\++'
18+
highlight default link diffStatAdd DiffStatAddHl
19+
syntax match diffStatSeparator '\v\|'
20+
highlight default link diffStatSeparator Comment
2421

25-
syn region diffStatLine start="|" end="\n"
26-
\ contains=diffStatAdd,diffStatRemove,diffStatSeparator,diffStatBin
22+
syn region diffStatLine start='|' end='\n'
23+
\ contains=diffStatAdd,diffStatRemove,diffStatSeparator,diffStatBin
2724

28-
syn region diffStatFile start="\v^[^#]" end="|\@=" skip=/\\|/
29-
highlight link diffStatFile Normal
25+
syn region diffStatFile start='\v^[^#]' end='|\@=' skip=/\\|/
26+
highlight default link diffStatFile Normal
3027

28+
syn region diffStatComment start='\v^#' end='\n' contains=diffStatBranch
29+
highlight default link diffStatComment Comment
3130

31+
syntax match diffStatTotals '\v^.*file.*change.*insertion.*deletion.*$'
32+
highlight default link diffStatTotals Comment
3233

33-
let b:diffstat_syntax = "diffstat"
34+
let b:diffstat_syntax = 'diffstat'

0 commit comments

Comments
 (0)