/
comparison.vim
467 lines (425 loc) · 16.8 KB
/
comparison.vim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
" vvcs#comparison: display file comparison on a new tab
" Maintainer: Marcelo Montu
" Repository: https://github.com/mMontu/vim-vvcs
let save_cpo = &cpo " allow line continuation
set cpo&vim
" constants {{{1
let s:VVCS_CODE_REVIEW_DIFF_LABEL = ['OLD', 'NEW']
" g:vvcs#comparison#get dictionary {{{1
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" common (local) tasks for each version control system
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
let g:vvcs#comparison#get = {}
let g:vvcs#comparison#get.ClearCase = {} " {{{2
function! g:vvcs#comparison#get['ClearCase'].pathAndVersion(index, list) dict
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Return a pair of path@version for the line on a list of files.
"
" Each line on the list may contain:
"
" * two filenames separeted by a semi-colon (TODO: or spaces)
" * a single filename without version specified -- on this case it will be
" compared against the previous version based on the current branch/config
" spec
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
let item = a:list[a:index]
if isdirectory(vvcs#remote#toLocalPath(item))
return [] " no diff for directories
endif
let ret = []
if item =~ ';'
" two files specified
let splitItem = split(item, '\s*;\s*')
if len(splitItem) != 2
return [g:vvcs_PLUGIN_TAG.'invalid number of files: '.len(splitItem)]
else
let ret = splitItem
endif
else " item !~ ';'
if item =~ "@@"
let [path, rev] = split(item, '@@')
let rev = '@@'.rev
else
let [path, rev] = [item, '']
endif
" remote command info expects remote path and possible version identifier
if filereadable(path)
let path = vvcs#remote#toRemotePath(path)
" call vvcs#log#msg("converted path: ".path)
endif
call add(ret, path."@@".vvcs#remote#execute('info', path.rev, 1))
if ret[len(ret)-1] !~# '\V'.g:vvcs_PLUGIN_TAG
call add(ret, path."@@".vvcs#remote#execute('info', path.rev, 0))
endif
endif
return ret
endfunction
let g:vvcs#comparison#get.svn = {} " {{{2
function! g:vvcs#comparison#get['svn'].pathAndVersion(index, list) dict
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Return a pair of path@version for the line on a list of files.
"
" The list may contains:
"
" * no branch/revision specified: shows diff between the current and previous
" version on the working copy
" * file name(s) followed by branch/revision identification on separated
" lines
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
let item = a:list[a:index]
if isdirectory(vvcs#remote#toLocalPath(item))
return [] " no diff for directories
endif
let ret = []
let COMMMITED_REV_PAT = '\vCommitted revision\s*(\d+)\..*'
let revIdx = vvcs#utils#indexRegex(a:list, COMMMITED_REV_PAT, a:index, 0, 1)
let rev = -1
if revIdx != -1
let rev = substitute(a:list[revIdx], COMMMITED_REV_PAT, '\1', '')
endif
" call vvcs#log#msg("revision: ".rev)
if rev != -1
" search branch
let BRANCH_PAT = '\vCommitted revision\s*\d+\.\s*(\S+).*'
let branchIdx = vvcs#utils#indexRegex(a:list, BRANCH_PAT, a:index, 0, 1)
if branchIdx == -1
let ret = [g:vvcs_PLUGIN_TAG.'unable to determine the branch']
else
let item = substitute(item, '\v^\s*Sending\s+', '', '')
let branch = substitute(a:list[branchIdx], BRANCH_PAT, '\1', '')
" call vvcs#log#msg("branch: ".branch)
" let item = g:vvcs_remote_repo.'/'.branch.'/'.item.'@'.rev
" call add(ret, vvcs#remote#execute('info', item, 1))
" call add(ret, vvcs#remote#execute('info', item, 0))
let item = g:vvcs_remote_repo.'/'.branch.'/'.item
call add(ret, item.'@'.(rev-1))
call add(ret, item.'@'.rev)
endif
else " rev == -1
if filereadable(item)
" commands below expect remote path
let item = vvcs#remote#toRemotePath(item)
" call vvcs#log#msg("converted path: ".item)
endif
let isCheckedout = vvcs#remote#execute('isCheckedout', item)
if isCheckedout =~# g:vvcs_PLUGIN_TAG
return [isCheckedout]
endif
if isCheckedout
call add(ret, vvcs#remote#execute('info', item, 0))
if ret[len(ret)-1] !~# '\V'.g:vvcs_PLUGIN_TAG
call add(ret, item."@checkedout")
endif
else
call add(ret, vvcs#remote#execute('info', item, 1))
if ret[len(ret)-1] !~# '\V'.g:vvcs_PLUGIN_TAG
call add(ret, vvcs#remote#execute('info', item, 0))
" NOTE: maybe change the line above to @checkedout to avoid one
" 'info' if it becomes too slow;
endif
endif
endif
return ret
endfunction
function! vvcs#comparison#createSingle(files) " {{{1
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Create a new tab with two windows and display the diff of file specified in
" the string a:file.
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
call s:createDiffWindows()
let t:compareFile[2].bufNr = -1
call s:compareItem(0, [a:files])
endfunction
function! s:createDiffWindows() " {{{1
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Create a new tab and two windows to display the diff.
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
tabe
vnew
" t:compareFile holds the bufNumber for the 3 comparison windows
let t:compareFile = [
\ {'name' : 'first diff' , 'bufNr' : -1 , 'winNr' : -1},
\ {'name' : 'second diff' , 'bufNr' : -1 , 'winNr' : -1},
\ {'name' : 'file list' , 'bufNr' : -1 , 'winNr' : -1},
\ ]
for i in range(2)
exe i+1.'wincmd w'
let t:compareFile[i].bufNr = bufnr('%')
let t:compareFile[i].winNr = winnr()
call s:setTempBuffer()
call s:commonMappings()
endfor
endfunction
function! vvcs#comparison#create(file) " {{{1
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Create a new tab and display the diff of the first pair of selected files.
"
" \par file file containing the list of files to be compared.
"
" Each entry on a:list is can be one of the following:
" * two filenames separeted by a semi-colon
" * a single filename without version specified -- on this case it will be
" compared against the previous version based on the current branch/config
" spec
"
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
call s:createDiffWindows()
"""""""""""""""""""""""""""""
" open review list window "
"""""""""""""""""""""""""""""
new
exe 'silent edit '.a:file
1 " move to the first line
let t:compareFile[2].bufNr = bufnr('%')
""""""""""""""""""""""""""
" list window settings "
""""""""""""""""""""""""""
call s:setTempBuffer()
wincmd J
setlocal cursorline
resize 8
setlocal winfixheight
" highlight filenames
" TODO: move to a syntax file when it is created (and documented on the
" help)
match SpecialKey /^.\{-}[/\\]\zs[^/\\]\+\ze@@/
setlocal nowrap
" 'expandtab' is not useful on this window, and can cause problems.
" E.g.: when a variable contains \t it will expand to spaces when its
" contents are inserted, thus this variable won't match the inserted text
" on a substitute() used to remove it.
setlocal noexpandtab
"""""""""""""""""""""""""""
" list window mappings "
"""""""""""""""""""""""""""
call s:commonMappings()
nnoremap <buffer> <silent> <CR> :call <SID>compareFiles(0)<CR>
" map D to the same function on fugitive plugin
nnoremap <buffer> <silent> D :call <SID>compareFiles(0)<CR>
nnoremap <buffer> <silent> J :call <SID>compareFiles(1)<CR>
nnoremap <buffer> <silent> K :call <SID>compareFiles(-1)<CR>
" trigger the diff on the first line of the list
call s:compareFiles(0)
endfunction
function! s:compareFiles(offset) " {{{1
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Handle selection of a line in the file list.
" The offset parameter indicates the line relative to current cursor position.
" Empty lines and lines starting with comments are skipped.
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
""""""""""""""""""""""""""""""""""""""""
" check if windows are still present "
""""""""""""""""""""""""""""""""""""""""
if !s:updateWindowNumbers()
return
endif
let initialWindow = winnr()
exe t:compareFile[2].winNr.'wincmd w'
call cursor(line('.') + a:offset, 0)
"""""""""""""""""""""""""""""""""""""""""""""""""
" skip empty, commented and unimportant lines "
"""""""""""""""""""""""""""""""""""""""""""""""""
let IGNORE_PAT = ['Committed revision', 'Transmitting file data']
let ignore = '('.join(IGNORE_PAT, '|').')'
let comment = '('.join(g:vvcs_review_comment, '|').')'
while getline('.') =~ '\v^\s*%($|('.ignore.'|'.comment.'))'
if (a:offset >= 0 && line('.') == line('$')) ||
\ (a:offset < 0 && line('.') == 1)
return
endif
call cursor(line('.') + (a:offset >= 0 ? 1 : -1), 0)
endw
redraw " avoid that cursor move outside window
let files = getline(0, '$')
if g:vvcs_remote_vcs =~# 'ClearCase'
let files = map(files,
\ 'substitute(v:val, "\\v\\s*".comment.".*", "", "")')
endif
call s:compareItem(line('.')-1, files)
exe initialWindow.'wincmd w'
endfunction
function! s:compareItem(index, list) " {{{1
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Handle a line in the file list or a single diff
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" call vvcs#log#msg("index: ".a:index.", list: ".string(a:list))
let pathAndVer = g:vvcs#comparison#get[g:vvcs_remote_vcs].pathAndVersion(
\ a:index, a:list)
" call vvcs#log#msg("vers: ".string(pathAndVer))
" return
if empty(pathAndVer)
call vvcs#log#msg("no diff to display")
return
else
for elem in pathAndVer
if elem =~# '\V'.g:vvcs_PLUGIN_TAG
call vvcs#log#error(substitute(elem, g:vvcs_PLUGIN_TAG, '', ''))
return
endif
endfor
endif
let file = [
\ {'name' : '', 'cmd' : ''},
\ {'name' : '', 'cmd' : ''},
\ ]
for i in range(len(pathAndVer))
let splitPath = split(pathAndVer[i], '@\+')
if pathAndVer[i] =~ 'checkedout'
let file[i].cmd = 'silent edit '.vvcs#remote#toLocalPath(splitPath[0])
else
" remove the repo identification or use the tail of the file name
if splitPath[0] =~ g:vvcs_remote_repo
let path = substitute(splitPath[0], g:vvcs_remote_repo, '', '')
else
let path = fnamemodify(splitPath[0], ":t")
endif
let versTail = substitute(splitPath[1], '\v.*/([^/]+/[^/]+$)', '\1', '')
let file[i].name = "[".versTail."] ".path
let file[i].cmd = "call s:setLines(".
\ "vvcs#remote#execute('catVersion', '".pathAndVer[i]."'))"
endif
endfor
diffoff!
" display the files
for i in range(2)
exe t:compareFile[i].winNr.'wincmd w'
enew
call s:setTempBuffer()
setlocal modifiable
call s:commonMappings()
exe 'silent file '.
\ s:getTempFileName(s:VVCS_CODE_REVIEW_DIFF_LABEL[i], file[i].name)
exe file[i].cmd
" if &buftype != 'nofile'
let t:compareFile[i].bufNr = bufnr('%') " bufNr changes if cmd is edit
" endif
" trigger autocmd to detect filetype and execute any filetype plugins
silent doautocmd BufNewFile
if !exists("noDiff") && (line('$') > 1 || getline(1) != '')
" check before 'diffthis' to avoid redraw problem when file is empty
diffthis
endif
if &buftype == 'nofile'
setlocal nomodifiable
endif
" avoid hidding the log file if file[i].cmd fails
if !s:updateWindowNumbers()
return
endif
endfor
"""""""""""""""""""""""""""""
" jump to the first error "
"""""""""""""""""""""""""""""
exe t:compareFile[0].winNr.'wincmd w'
if line('$') > 1 || getline(1) != ''
normal! gg
redraw!
" normal! ]c
" redraw!
" normal! zb
" redraw!
" try to avoid some redraw problems
exe t:compareFile[1].winNr.'wincmd w'
normal! gg
redraw!
wincmd p
" redraw
" normal! gg]c
" redraw
else
diffo!
exe t:compareFile[1].winNr.'wincmd w'
redraw! " avoid hit-enter message
endif
endfunction
function! vvcs#comparison#switchToListWindow() " {{{1
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Return true if successfully switch to the window use to list compared files
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
let t:compareFile[2].winNr = bufwinnr(t:compareFile[2].bufNr)
if t:compareFile[2].winNr == -1
return 0
endif
exe t:compareFile[2].winNr.'wincmd w'
return 1
endfunction
function! s:commonMappings() " {{{1
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" mappings applicable to all compareFile windows
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
nnoremap <buffer> <c-down> ]c
nnoremap <buffer> <c-up> [c
nnoremap <buffer> J ]c
nnoremap <buffer> K [c
" 'q' closes without confirmation when there is no review list
nnoremap <buffer> <silent> q
\ :if t:compareFile[2].bufNr == -1 <bar><bar>
\ VvcsConfirm("Quit comparison?", "&Yes\n&No") == 1 <bar>
\ diffo! <bar> tabc <bar> endif<CR>
nnoremap <buffer> <silent> <leader>j :call <SID>compareFiles(1)<CR>
nnoremap <buffer> <silent> <leader>k :call <SID>compareFiles(-1)<CR>
endfunction
function! s:setTempBuffer() " {{{1
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Select settings of a temporary/scratch buffer
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
setlocal buftype=nofile
setlocal bufhidden=delete
setlocal noswapfile
setlocal nomodifiable
setlocal textwidth=0 " avoid automatic line break
endfunction
function! s:updateWindowNumbers() " {{{1
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Update the window number for the comparison buffers.
" Returns false if some window is not found.
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
""""""""""""""""""""""""""""""""""""""""
" check if windows are still present "
""""""""""""""""""""""""""""""""""""""""
for i in range(len(t:compareFile))
let t:compareFile[i].winNr = bufwinnr(t:compareFile[i].bufNr)
if t:compareFile[i].bufNr != -1 && t:compareFile[i].winNr == -1
call vvcs#log#error("missing comparison window: ".
\ t:compareFile[i].name .' ('.t:compareFile[i].bufNr.')')
return 0
endif
endfor
" echo t:compareFile
return 1
endfunction
function! s:setLines(content) " {{{1
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Replace the current line with the specified string (which may spam multiple
" lines).
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Using :put avoids some problems from call setline(getline('.'),
" split('...', '\n')). E.g.: if the string starts with a newline them
" split() will remove it. If the keepempty is set, split will include the
" first empty line, but it will also include an extra one at the end if the
" last line ends in '\n'.
"
" Note that both ':put =var' and 'setline()' don't work for a:content that
" contains text in dos format -- '^M' is displayed at every line. It seems
" that the file format option is ignored for these commands.
silent put =a:content
" delete original line
silent '[-1delete _
endfunction
function! s:getTempFileName(prefix, currentName) " {{{1
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
" Return the name for a temporary file, suitable for :file command.
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
let name = a:prefix.' '.a:currentName
" check if the name is already in used
let i = 1
while buflisted(name)
let name = a:prefix.' ('.i.') '.a:currentName
let i += 1
endw
" escape spaces for :file command
let name = substitute(name, ' ', '\\&', 'g')
return name
endfunction
let &cpo = save_cpo
" vim: ts=3 sts=0 sw=3 expandtab ff=unix foldmethod=marker :