Debian Codegraph @

contrib/vim/patchreview.vim

raw
1 " VIM plugin for doing single, multi-patch or diff code reviews {{{
2 " Home: http://www.vim.org/scripts/script.php?script_id=1563
3
4 " Version : 0.2.2 "{{{
5 " Author : Manpreet Singh < [email protected] >
6 " Copyright : 2006-2010 by Manpreet Singh
7 " License : This file is placed in the public domain.
8 " No warranties express or implied. Use at your own risk.
9 "
10 " Changelog :
11 "
12 " 0.2.2 - Security fixes by removing custom tempfile creation
13 " - Removed need for DiffReviewCleanup/PatchReviewCleanup
14 " - Better command execution error detection and display
15 " - Improved diff view and folding by ignoring modelines
16 " - Improved tab labels display
17 "
18 " 0.2.1 - Minor temp directory autodetection logic and cleanup
19 "
20 " 0.2 - Removed the need for filterdiff by implementing it in pure vim script
21 " - Added DiffReview command for reverse (changed repository to
22 " pristine state) reviews.
23 " (PatchReview does pristine repository to patch review)
24 " - DiffReview does automatic detection and generation of diffs for
25 " various Source Control systems
26 " - Skip load if VIM 7.0 or higher unavailable
27 "
28 " 0.1 - First released
29 "}}}
30
31 " Documentation: "{{{
32 " ===========================================================================
33 " This plugin allows single or multiple, patch or diff based code reviews to
34 " be easily done in VIM. VIM has :diffpatch command to do single file reviews
35 " but a) can not handle patch files containing multiple patches or b) do
36 " automated diff generation for various version control systems. This plugin
37 " attempts to provide those functionalities. It opens each changed / added or
38 " removed file diff in new tabs.
39 "
40 " Installing:
41 "
42 " For a quick start, unzip patchreview.zip into your ~/.vim directory and
43 " restart Vim.
44 "
45 " Details:
46 "
47 " Requirements:
48 "
49 " 1) VIM 7.0 or higher built with +diff option.
50 "
51 " 2) A gnu compatible patch command installed. This is the standard patch
52 " command on Linux, Mac OS X, *BSD, Cygwin or /usr/bin/gpatch on newer
53 " Solaris.
54 "
55 " 3) Optional (but recommended for speed)
56 "
57 " Install patchutils ( http://cyberelk.net/tim/patchutils/ ) for your
58 " OS. For windows it is available from Cygwin
59 "
60 " http://www.cygwin.com
61 "
62 " or GnuWin32
63 "
64 " http://gnuwin32.sourceforge.net/
65 "
66 " Install:
67 "
68 " 1) Extract the zip in your $HOME/.vim or $VIM/vimfiles directory and
69 " restart vim. The directory location relevant to your platform can be
70 " seen by running :help add-global-plugin in vim.
71 "
72 " 2) Restart vim.
73 "
74 " Configuration:
75 "
76 " Optionally, specify the locations to these filterdiff and patch commands
77 " and location of a temporary directory to use in your .vimrc.
78 "
79 " let g:patchreview_patch = '/path/to/gnu/patch'
80 "
81 " " If you are using filterdiff
82 " let g:patchreview_filterdiff = '/path/to/filterdiff'
83 "
84 "
85 " Usage:
86 "
87 " Please see :help patchreview or :help diffreview for details.
88 "
89 ""}}}
90
91 " Enabled only during development
92 " unlet! g:loaded_patchreview " DEBUG
93 " unlet! g:patchreview_patch " DEBUG
94 " unlet! g:patchreview_filterdiff " DEBUG
95 " let g:patchreview_patch = 'patch' " DEBUG
96
97 if v:version < 700
98 finish
99 endif
100 if ! has('diff')
101 call confirm('patchreview.vim plugin needs (G)VIM built with +diff support to work.')
102 finish
103 endif
104
105 " load only once
106 if (! exists('g:patchreview_debug') && exists('g:loaded_patchreview')) || &compatible
107 finish
108 endif
109 let g:loaded_patchreview="0.2.2"
110
111 let s:msgbufname = '-PatchReviewMessages-'
112
113 function! <SID>Debug(str) "{{{
114 if exists('g:patchreview_debug')
115 Pecho 'DEBUG: ' . a:str
116 endif
117 endfunction
118 command! -nargs=+ -complete=expression Debug call s:Debug(<args>)
119 "}}}
120
121 function! <SID>PR_wipeMsgBuf() "{{{
122 let winnum = bufwinnr(s:msgbufname)
123 if winnum != -1 " If the window is already open, jump to it
124 let cur_winnr = winnr()
125 if winnr() != winnum
126 exe winnum . 'wincmd w'
127 exe 'bw'
128 exe cur_winnr . 'wincmd w'
129 endif
130 endif
131 endfunction
132 "}}}
133
134 function! <SID>Pecho(...) "{{{
135 " Usage: Pecho(msg, [return_to_original_window_flag])
136 " default return_to_original_window_flag = 0
137 "
138 let cur_winnr = winnr()
139 let winnum = bufwinnr(s:msgbufname)
140 if winnum != -1 " If the window is already open, jump to it
141 if winnr() != winnum
142 exe winnum . 'wincmd w'
143 endif
144 else
145 let bufnum = bufnr(s:msgbufname)
146 if bufnum == -1
147 let wcmd = s:msgbufname
148 else
149 let wcmd = '+buffer' . bufnum
150 endif
151 exe 'silent! botright 5split ' . wcmd
152 endif
153 setlocal modifiable
154 setlocal buftype=nofile
155 setlocal bufhidden=delete
156 setlocal noswapfile
157 setlocal nowrap
158 setlocal nobuflisted
159 if a:0 != 0
160 silent! $put =a:1
161 endif
162 exe ':$'
163 setlocal nomodifiable
164 if a:0 > 1 && a:2
165 exe cur_winnr . 'wincmd w'
166 endif
167 endfunction
168
169 command! -nargs=+ -complete=expression Pecho call s:Pecho(<args>)
170 "}}}
171
172 function! <SID>PR_checkBinary(BinaryName) "{{{
173 " Verify that BinaryName is specified or available
174 if ! exists('g:patchreview_' . a:BinaryName)
175 if executable(a:BinaryName)
176 let g:patchreview_{a:BinaryName} = a:BinaryName
177 return 1
178 else
179 Pecho 'g:patchreview_' . a:BinaryName . ' is not defined and ' . a:BinaryName . ' command could not be found on path.'
180 Pecho 'Please define it in your .vimrc.'
181 return 0
182 endif
183 elseif ! executable(g:patchreview_{a:BinaryName})
184 Pecho 'Specified g:patchreview_' . a:BinaryName . ' [' . g:patchreview_{a:BinaryName} . '] is not executable.'
185 return 0
186 else
187 return 1
188 endif
189 endfunction
190 "}}}
191
192 function! <SID>ExtractDiffsNative(...) "{{{
193 " Sets g:patches = {'reason':'', 'patch':[
194 " {
195 " 'filename': filepath
196 " 'type' : '+' | '-' | '!'
197 " 'content' : patch text for this file
198 " },
199 " ...
200 " ]}
201 let g:patches = {'reason' : '', 'patch' : []}
202 " TODO : User pointers into lines list rather then use collect
203 if a:0 == 0
204 let g:patches['reason'] = "ExtractDiffsNative expects at least a patchfile argument"
205 return
206 endif
207 let patchfile = expand(a:1, ':p')
208 if a:0 > 1
209 let patch = a:2
210 endif
211 if ! filereadable(patchfile)
212 let g:patches['reason'] = "File " . patchfile . " is not readable"
213 return
214 endif
215 unlet! filterdiffcmd
216 let filterdiffcmd = '' . g:patchreview_filterdiff . ' --list -s ' . patchfile
217 let fileslist = split(system(filterdiffcmd), '[\r\n]')
218 for filewithchangetype in fileslist
219 if filewithchangetype !~ '^[!+-] '
220 Pecho '*** Skipping review generation due to unknown change for [' . filewithchangetype . ']'
221 continue
222 endif
223
224 unlet! this_patch
225 let this_patch = {}
226
227 unlet! relpath
228 let relpath = substitute(filewithchangetype, '^. ', '', '')
229
230 let this_patch['filename'] = relpath
231
232 if filewithchangetype =~ '^! '
233 let this_patch['type'] = '!'
234 elseif filewithchangetype =~ '^+ '
235 let this_patch['type'] = '+'
236 elseif filewithchangetype =~ '^- '
237 let this_patch['type'] = '-'
238 endif
239
240 unlet! filterdiffcmd
241 let filterdiffcmd = '' . g:patchreview_filterdiff . ' -i ' . relpath . ' ' . patchfile
242 let this_patch['content'] = split(system(filterdiffcmd), '[\n\r]')
243 let g:patches['patch'] += [this_patch]
244 Debug "Patch collected for " . relpath
245 endfor
246 endfunction
247 "}}}
248
249 function! <SID>ExtractDiffsPureVim(...) "{{{
250 " Sets g:patches = {'reason':'', 'patch':[
251 " {
252 " 'filename': filepath
253 " 'type' : '+' | '-' | '!'
254 " 'content' : patch text for this file
255 " },
256 " ...
257 " ]}
258 let g:patches = {'reason' : '', 'patch' : []}
259 " TODO : User pointers into lines list rather then use collect
260 if a:0 == 0
261 let g:patches['reason'] = "ExtractDiffsPureVim expects at least a patchfile argument"
262 return
263 endif
264 let patchfile = expand(a:1, ':p')
265 if a:0 > 1
266 let patch = a:2
267 endif
268 if ! filereadable(patchfile)
269 let g:patches['reason'] = "File " . patchfile . " is not readable"
270 return
271 endif
272 call s:PR_wipeMsgBuf()
273 let collect = []
274 let linum = 0
275 let lines = readfile(patchfile)
276 let linescount = len(lines)
277 State 'START'
278 while linum < linescount
279 let line = lines[linum]
280 let linum += 1
281 if State() == 'START'
282 let mat = matchlist(line, '^--- \([^\t]\+\).*$')
283 if ! empty(mat) && mat[1] != ''
284 State 'MAYBE_UNIFIED_DIFF'
285 let p_first_file = mat[1]
286 let collect = [line]
287 Debug line . State()
288 continue
289 endif
290 let mat = matchlist(line, '^\*\*\* \([^\t]\+\).*$')
291 if ! empty(mat) && mat[1] != ''
292 State 'MAYBE_CONTEXT_DIFF'
293 let p_first_file = mat[1]
294 let collect = [line]
295 Debug line . State()
296 continue
297 endif
298 continue
299 elseif State() == 'MAYBE_CONTEXT_DIFF'
300 let mat = matchlist(line, '^--- \([^\t]\+\).*$')
301 if empty(mat) || mat[1] == ''
302 State 'START'
303 let linum -= 1
304 continue
305 Debug 'Back to square one ' . line()
306 endif
307 let p_second_file = mat[1]
308 if p_first_file == '/dev/null'
309 if p_second_file == '/dev/null'
310 let g:patches['reason'] = "Malformed diff found at line " . linum
311 return
312 endif
313 let p_type = '+'
314 let filepath = p_second_file
315 else
316 if p_second_file == '/dev/null'
317 let p_type = '-'
318 let filepath = p_first_file
319 else
320 let p_type = '!'
321 let filepath = p_first_file
322 endif
323 endif
324 State 'EXPECT_15_STARS'
325 let collect += [line]
326 Debug line . State()
327 elseif State() == 'EXPECT_15_STARS'
328 if line !~ '^*\{15}$'
329 State 'START'
330 let linum -= 1
331 Debug line . State()
332 continue
333 endif
334 State 'EXPECT_CONTEXT_CHUNK_HEADER_1'
335 let collect += [line]
336 Debug line . State()
337 elseif State() == 'EXPECT_CONTEXT_CHUNK_HEADER_1'
338 let mat = matchlist(line, '^\*\*\* \(\d\+,\)\?\(\d\+\) \*\*\*\*$')
339 if empty(mat) || mat[1] == ''
340 State 'START'
341 let linum -= 1
342 Debug line . State()
343 continue
344 endif
345 let collect += [line]
346 State 'SKIP_CONTEXT_STUFF_1'
347 Debug line . State()
348 continue
349 elseif State() == 'SKIP_CONTEXT_STUFF_1'
350 if line !~ '^[ !+].*$'
351 let mat = matchlist(line, '^--- \(\d\+\),\(\d\+\) ----$')
352 if ! empty(mat) && mat[1] != '' && mat[2] != ''
353 let goal_count = mat[2] - mat[1] + 1
354 let c_count = 0
355 State 'READ_CONTEXT_CHUNK'
356 let collect += [line]
357 Debug line . State() . " Goal count set to " . goal_count
358 continue
359 endif
360 State 'START'
361 let linum -= 1
362 Debug line . State()
363 continue
364 endif
365 let collect += [line]
366 continue
367 elseif State() == 'READ_CONTEXT_CHUNK'
368 let c_count += 1
369 if c_count == goal_count
370 let collect += [line]
371 State 'BACKSLASH_OR_CRANGE_EOF'
372 continue
373 else " goal not met yet
374 let mat = matchlist(line, '^\([\\!+ ]\).*$')
375 if empty(mat) || mat[1] == ''
376 let linum -= 1
377 State 'START'
378 Debug line . State()
379 continue
380 endif
381 let collect += [line]
382 continue
383 endif
384 elseif State() == 'BACKSLASH_OR_CRANGE_EOF'
385 if line =~ '^\\ No newline.*$' " XXX: Can we go to another chunk from here??
386 let collect += [line]
387 let this_patch = {}
388 let this_patch['filename'] = filepath
389 let this_patch['type'] = p_type
390 let this_patch['content'] = collect
391 let g:patches['patch'] += [this_patch]
392 Debug "Patch collected for " . filepath
393 State 'START'
394 continue
395 endif
396 if line =~ '^\*\{15}$'
397 let collect += [line]
398 State 'EXPECT_CONTEXT_CHUNK_HEADER_1'
399 Debug line . State()
400 continue
401 endif
402 let this_patch = {}
403 let this_patch['filename'] = filepath
404 let this_patch['type'] = p_type
405 let this_patch['content'] = collect
406 let g:patches['patch'] += [this_patch]
407 let linum -= 1
408 State 'START'
409 Debug "Patch collected for " . filepath
410 Debug line . State()
411 continue
412 elseif State() == 'MAYBE_UNIFIED_DIFF'
413 let mat = matchlist(line, '^+++ \([^\t]\+\).*$')
414 if empty(mat) || mat[1] == ''
415 State 'START'
416 let linum -= 1
417 Debug line . State()
418 continue
419 endif
420 let p_second_file = mat[1]
421 if p_first_file == '/dev/null'
422 if p_second_file == '/dev/null'
423 let g:patches['reason'] = "Malformed diff found at line " . linum
424 return
425 endif
426 let p_type = '+'
427 let filepath = p_second_file
428 else
429 if p_second_file == '/dev/null'
430 let p_type = '-'
431 let filepath = p_first_file
432 else
433 let p_type = '!'
434 let filepath = p_first_file
435 endif
436 endif
437 State 'EXPECT_UNIFIED_RANGE_CHUNK'
438 let collect += [line]
439 Debug line . State()
440 continue
441 elseif State() == 'EXPECT_UNIFIED_RANGE_CHUNK'
442 let mat = matchlist(line, '^@@ -\(\d\+,\)\?\(\d\+\) +\(\d\+,\)\?\(\d\+\) @@$')
443 if ! empty(mat)
444 let old_goal_count = mat[2]
445 let new_goal_count = mat[4]
446 let o_count = 0
447 let n_count = 0
448 Debug "Goal count set to " . old_goal_count . ', ' . new_goal_count
449 State 'READ_UNIFIED_CHUNK'
450 let collect += [line]
451 Debug line . State()
452 continue
453 endif
454 State 'START'
455 Debug line . State()
456 continue
457 elseif State() == 'READ_UNIFIED_CHUNK'
458 if o_count == old_goal_count && n_count == new_goal_count
459 if line =~ '^\\.*$' " XXX: Can we go to another chunk from here??
460 let collect += [line]
461 let this_patch = {}
462 let this_patch['filename'] = filepath
463 let this_patch['type'] = p_type
464 let this_patch['content'] = collect
465 let g:patches['patch'] += [this_patch]
466 Debug "Patch collected for " . filepath
467 State 'START'
468 continue
469 endif
470 let mat = matchlist(line, '^@@ -\(\d\+,\)\?\(\d\+\) +\(\d\+,\)\?\(\d\+\) @@$')
471 if ! empty(mat)
472 let old_goal_count = mat[2]
473 let new_goal_count = mat[4]
474 let o_count = 0
475 let n_count = 0
476 Debug "Goal count set to " . old_goal_count . ', ' . new_goal_count
477 let collect += [line]
478 Debug line . State()
479 continue
480 endif
481 let this_patch = {}
482 let this_patch['filename'] = filepath
483 let this_patch['type'] = p_type
484 let this_patch['content'] = collect
485 let g:patches['patch'] += [this_patch]
486 Debug "Patch collected for " . filepath
487 let linum -= 1
488 State 'START'
489 Debug line . State()
490 continue
491 else " goal not met yet
492 let mat = matchlist(line, '^\([\\+ -]\).*$')
493 if empty(mat) || mat[1] == ''
494 let linum -= 1
495 State 'START'
496 continue
497 endif
498 let chr = mat[1]
499 if chr == '+'
500 let n_count += 1
501 endif
502 if chr == ' '
503 let o_count += 1
504 let n_count += 1
505 endif
506 if chr == '-'
507 let o_count += 1
508 endif
509 let collect += [line]
510 Debug line . State()
511 continue
512 endif
513 else
514 let g:patches['reason'] = "Internal error: Do not use the plugin anymore and if possible please send the diff or patch file you tried it with to Manpreet Singh <[email protected]>"
515 return
516 endif
517 endwhile
518 "Pecho State()
519 if (State() == 'READ_CONTEXT_CHUNK' && c_count == goal_count) || (State() == 'READ_UNIFIED_CHUNK' && n_count == new_goal_count && o_count == old_goal_count)
520 let this_patch = {}
521 let this_patch['filename'] = filepath
522 let this_patch['type'] = p_type
523 let this_patch['content'] = collect
524 let g:patches['patch'] += [this_patch]
525 Debug "Patch collected for " . filepath
526 endif
527 return
528 endfunction
529 "}}}
530
531 function! State(...) " For easy manipulation of diff extraction state "{{{
532 if a:0 != 0
533 let s:STATE = a:1
534 else
535 if ! exists('s:STATE')
536 let s:STATE = 'START'
537 endif
538 return s:STATE
539 endif
540 endfunction
541 com! -nargs=+ -complete=expression State call State(<args>)
542 "}}}
543
544 function! <SID>PatchReview(...) "{{{
545 let s:save_shortmess = &shortmess
546 let s:save_aw = &autowrite
547 let s:save_awa = &autowriteall
548 set shortmess=aW
549 call s:PR_wipeMsgBuf()
550 let s:reviewmode = 'patch'
551 call s:_GenericReview(a:000)
552 let &autowriteall = s:save_awa
553 let &autowrite = s:save_aw
554 let &shortmess = s:save_shortmess
555 endfunction
556 "}}}
557
558 function! <SID>_GenericReview(argslist) "{{{
559 " diff mode:
560 " arg1 = patchfile
561 " arg2 = strip count
562 " patch mode:
563 " arg1 = patchfile
564 " arg2 = strip count
565 " arg3 = directory
566
567 " VIM 7+ required
568 if version < 700
569 Pecho 'This plugin needs VIM 7 or higher'
570 return
571 endif
572
573 " +diff required
574 if ! has('diff')
575 Pecho 'This plugin needs VIM built with +diff feature.'
576 return
577 endif
578
579
580 if s:reviewmode == 'diff'
581 let patch_R_option = ' -t -R '
582 elseif s:reviewmode == 'patch'
583 let patch_R_option = ''
584 else
585 Pecho 'Fatal internal error in patchreview.vim plugin'
586 return
587 endif
588
589 " Check passed arguments
590 if len(a:argslist) == 0
591 Pecho 'PatchReview command needs at least one argument specifying a patchfile path.'
592 return
593 endif
594 let StripCount = 0
595 if len(a:argslist) >= 1 && ((s:reviewmode == 'patch' && len(a:argslist) <= 3) || (s:reviewmode == 'diff' && len(a:argslist) == 2))
596 let PatchFilePath = expand(a:argslist[0], ':p')
597 if ! filereadable(PatchFilePath)
598 Pecho 'File [' . PatchFilePath . '] is not accessible.'
599 return
600 endif
601 if len(a:argslist) >= 2 && s:reviewmode == 'patch'
602 let s:SrcDirectory = expand(a:argslist[1], ':p')
603 if ! isdirectory(s:SrcDirectory)
604 Pecho '[' . s:SrcDirectory . '] is not a directory'
605 return
606 endif
607 try
608 " Command line has already escaped the path
609 exe 'cd ' . s:SrcDirectory
610 catch /^.*E344.*/
611 Pecho 'Could not change to directory [' . s:SrcDirectory . ']'
612 return
613 endtry
614 endif
615 if s:reviewmode == 'diff'
616 " passed in by default
617 let StripCount = eval(a:argslist[1])
618 elseif s:reviewmode == 'patch'
619 let StripCount = 1
620 " optional strip count
621 if len(a:argslist) == 3
622 let StripCount = eval(a:argslist[2])
623 endif
624 endif
625 else
626 if s:reviewmode == 'patch'
627 Pecho 'PatchReview command needs at most three arguments: patchfile path, optional source directory path and optional strip count.'
628 elseif s:reviewmode == 'diff'
629 Pecho 'DiffReview command accepts no arguments.'
630 endif
631 return
632 endif
633
634 " Verify that patch command and temporary directory are available or specified
635 if ! s:PR_checkBinary('patch')
636 return
637 endif
638
639 " Requirements met, now execute
640 let PatchFilePath = fnamemodify(PatchFilePath, ':p')
641 if s:reviewmode == 'patch'
642 Pecho 'Patch file : ' . PatchFilePath
643 endif
644 Pecho 'Source directory: ' . getcwd()
645 Pecho '------------------'
646 if s:PR_checkBinary('filterdiff')
647 Debug "Using filterdiff"
648 call s:ExtractDiffsNative(PatchFilePath)
649 else
650 Debug "Using own diff extraction (slower)"
651 call s:ExtractDiffsPureVim(PatchFilePath)
652 endif
653 for patch in g:patches['patch']
654 if patch.type !~ '^[!+-]$'
655 Pecho '*** Skipping review generation due to unknown change [' . patch.type . ']', 1
656 continue
657 endif
658 unlet! relpath
659 let relpath = patch.filename
660 " XXX: svn diff and hg diff produce different kind of outputs, one requires
661 " XXX: stripping but the other doesn't. We need to take care of that
662 let stripmore = StripCount
663 let StrippedRelativeFilePath = relpath
664 while stripmore > 0
665 " strip one
666 let StrippedRelativeFilePath = substitute(StrippedRelativeFilePath, '^[^\\\/]\+[^\\\/]*[\\\/]' , '' , '')
667 let stripmore -= 1
668 endwhile
669 if patch.type == '!'
670 if s:reviewmode == 'patch'
671 let msgtype = 'Patch modifies file: '
672 elseif s:reviewmode == 'diff'
673 let msgtype = 'File has changes: '
674 endif
675 elseif patch.type == '+'
676 if s:reviewmode == 'patch'
677 let msgtype = 'Patch adds file : '
678 elseif s:reviewmode == 'diff'
679 let msgtype = 'New file : '
680 endif
681 elseif patch.type == '-'
682 if s:reviewmode == 'patch'
683 let msgtype = 'Patch removes file : '
684 elseif s:reviewmode == 'diff'
685 let msgtype = 'Removed file : '
686 endif
687 endif
688 let bufnum = bufnr(relpath)
689 if buflisted(bufnum) && getbufvar(bufnum, '&mod')
690 Pecho 'Old buffer for file [' . relpath . '] exists in modified state. Skipping review.', 1
691 continue
692 endif
693 let tmpname = tempname()
694
695 " write patch for patch.filename into tmpname
696 call writefile(patch.content, tmpname)
697 if patch.type == '+' && s:reviewmode == 'patch'
698 let inputfile = ''
699 let patchcmd = '!' . g:patchreview_patch . patch_R_option . ' -o "' . tmpname . '.file" "' . inputfile . '" < "' . tmpname . '"'
700 elseif patch.type == '+' && s:reviewmode == 'diff'
701 let inputfile = ''
702 unlet! patchcmd
703 else
704 let inputfile = expand(StrippedRelativeFilePath, ':p')
705 let patchcmd = '!' . g:patchreview_patch . patch_R_option . ' -o "' . tmpname . '.file" "' . inputfile . '" < "' . tmpname . '"'
706 endif
707 if exists('patchcmd')
708 let v:errmsg = ''
709 Debug patchcmd
710 silent exe patchcmd
711 if v:errmsg != '' || v:shell_error
712 Pecho 'ERROR: Could not execute patch command.'
713 Pecho 'ERROR: ' . patchcmd
714 Pecho 'ERROR: ' . v:errmsg
715 Pecho 'ERROR: Diff skipped.'
716 continue
717 endif
718 endif
719 call delete(tmpname)
720 let s:origtabpagenr = tabpagenr()
721 silent! exe 'tabedit ' . StrippedRelativeFilePath
722 if exists('patchcmd')
723 " modelines in loaded files mess with diff comparison
724 let s:keep_modeline=&modeline
725 let &modeline=0
726 silent! exe 'vert diffsplit ' . tmpname . '.file'
727 setlocal buftype=nofile
728 setlocal noswapfile
729 setlocal syntax=none
730 setlocal bufhidden=delete
731 setlocal nobuflisted
732 setlocal modifiable
733 setlocal nowrap
734 " Remove buffer name
735 silent! 0f
736 " Switch to original to get a nice tab title
737 silent! wincmd p
738 let &modeline=s:keep_modeline
739 else
740 silent! exe 'vnew'
741 endif
742 if filereadable(tmpname . '.file.rej')
743 silent! exe 'topleft 5split ' . tmpname . '.file.rej'
744 Pecho msgtype . '*** REJECTED *** ' . relpath, 1
745 else
746 Pecho msgtype . ' ' . relpath, 1
747 endif
748 silent! exe 'tabn ' . s:origtabpagenr
749 endfor
750 Pecho '-----'
751 Pecho 'Done.'
752
753 endfunction
754 "}}}
755
756 function! <SID>DiffReview(...) "{{{
757 let s:save_shortmess = &shortmess
758 set shortmess=aW
759 call s:PR_wipeMsgBuf()
760
761 let vcsdict = {
762 \'Mercurial' : {'dir' : '.hg', 'binary' : 'hg', 'diffargs' : 'diff' , 'strip' : 1},
763 \'Bazaar-NG' : {'dir' : '.bzr', 'binary' : 'bzr', 'diffargs' : 'diff' , 'strip' : 0},
764 \'monotone' : {'dir' : '_MTN', 'binary' : 'mtn', 'diffargs' : 'diff --unified', 'strip' : 0},
765 \'Subversion' : {'dir' : '.svn', 'binary' : 'svn', 'diffargs' : 'diff' , 'strip' : 0},
766 \'cvs' : {'dir' : 'CVS', 'binary' : 'cvs', 'diffargs' : '-q diff -u' , 'strip' : 0},
767 \}
768
769 unlet! s:theDiffCmd
770 unlet! l:vcs
771 if ! exists('g:patchreview_diffcmd')
772 for key in keys(vcsdict)
773 if isdirectory(vcsdict[key]['dir'])
774 if ! s:PR_checkBinary(vcsdict[key]['binary'])
775 Pecho 'Current directory looks like a ' . vcsdict[key] . ' repository but ' . vcsdist[key]['binary'] . ' command was not found on path.'
776 let &shortmess = s:save_shortmess
777 return
778 else
779 let s:theDiffCmd = vcsdict[key]['binary'] . ' ' . vcsdict[key]['diffargs']
780 let strip = vcsdict[key]['strip']
781
782 Pecho 'Using [' . s:theDiffCmd . '] to generate diffs for this ' . key . ' review.'
783 let &shortmess = s:save_shortmess
784 let l:vcs = vcsdict[key]['binary']
785 break
786 endif
787 else
788 continue
789 endif
790 endfor
791 else
792 let s:theDiffCmd = g:patchreview_diffcmd
793 let strip = 0
794 endif
795 if ! exists('s:theDiffCmd')
796 Pecho 'Please define g:patchreview_diffcmd and make sure you are in a VCS controlled top directory.'
797 let &shortmess = s:save_shortmess
798 return
799 endif
800
801 let outfile = tempname()
802 let cmd = s:theDiffCmd . ' > "' . outfile . '"'
803 let v:errmsg = ''
804 let cout = system(cmd)
805 if v:errmsg == '' && exists('l:vcs') && l:vcs == 'cvs' && v:shell_error == 1
806 " Ignoring CVS non-error
807 elseif v:errmsg != '' || v:shell_error
808 Pecho v:errmsg
809 Pecho 'Could not execute [' . s:theDiffCmd . ']'
810 Pecho 'Error code: ' . v:shell_error
811 Pecho cout
812 Pecho 'Diff review aborted.'
813 let &shortmess = s:save_shortmess
814 return
815 endif
816 let s:reviewmode = 'diff'
817 call s:_GenericReview([outfile, strip])
818 let &shortmess = s:save_shortmess
819 endfunction
820 "}}}
821
822 " End user commands "{{{
823 "============================================================================
824 " :PatchReview
825 command! -nargs=* -complete=file PatchReview call s:PatchReview (<f-args>)
826
827 " :DiffReview
828 command! -nargs=0 DiffReview call s:DiffReview()
829 "}}}
830
831 " Development "{{{
832 if exists('g:patchreview_debug')
833 " Tests
834 function! <SID>PRExtractTestNative(...)
835 "let patchfiles = glob(expand(a:1) . '/?*')
836 "for fname in split(patchfiles)
837 call s:PR_wipeMsgBuf()
838 let fname = a:1
839 call s:ExtractDiffsNative(fname)
840 for patch in g:patches['patch']
841 for line in patch.content
842 Pecho line
843 endfor
844 endfor
845 "endfor
846 endfunction
847
848 function! <SID>PRExtractTestVim(...)
849 "let patchfiles = glob(expand(a:1) . '/?*')
850 "for fname in split(patchfiles)
851 call s:PR_wipeMsgBuf()
852 let fname = a:1
853 call s:ExtractDiffsPureVim(fname)
854 for patch in g:patches['patch']
855 for line in patch.content
856 Pecho line
857 endfor
858 endfor
859 "endfor
860 endfunction
861
862 command! -nargs=+ -complete=file PRTestVim call s:PRExtractTestVim(<f-args>)
863 command! -nargs=+ -complete=file PRTestNative call s:PRExtractTestNative(<f-args>)
864 endif
865 "}}}
866
867 " modeline
868 " vim: set et fdl=0 fdm=marker fenc=latin ff=unix ft=vim sw=2 sts=0 ts=2 textwidth=78 nowrap :