@@ -8,11 +8,12 @@ if exists('g:loaded_diffstat')
88 finish
99endif
1010
11+ let s: GIT_ERROR = " GIT ERROR"
1112
1213" Maps visible file names to actual system paths.
1314let 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
6771endfunction
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 ))
138147endfunction
139148
149+ function ! s: FormatCountString (str, count )
150+ return a: count . " " . a: str . (a: count == 1 ? " " : " s" )
151+ endfunction
140152
141153let s: DiffStatRegex = ' \v^\s*(\S+)\s*\|\s*\d+\s*[+-]+\s*$'
142154function ! s: DiffStatFileNameForLine (lnum)
@@ -159,12 +171,12 @@ endfunction
159171function ! 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
192213endfunction
193214
215+ let g: loaded_diffstat = 1
0 commit comments