Replace system() with wrapper to protect quotes in Windows.
[vcscommand.git] / plugin / vcscvs.vim
blob2e9c1025e8eb31bb256c85c9571b75d3e702d546
1 " vim600: set foldmethod=marker:
3 " CVS extension for VCSCommand.
5 " Version:       VCS development
6 " Maintainer:    Bob Hiestand <bob.hiestand@gmail.com>
7 " License:
8 " Copyright (c) 2007 Bob Hiestand
10 " Permission is hereby granted, free of charge, to any person obtaining a copy
11 " of this software and associated documentation files (the "Software"), to
12 " deal in the Software without restriction, including without limitation the
13 " rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
14 " sell copies of the Software, and to permit persons to whom the Software is
15 " furnished to do so, subject to the following conditions:
17 " The above copyright notice and this permission notice shall be included in
18 " all copies or substantial portions of the Software.
20 " THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 " IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 " FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 " AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 " LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25 " FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
26 " IN THE SOFTWARE.
28 " Section: Documentation {{{1
30 " Command documentation {{{2
32 " The following commands only apply to files under CVS source control.
34 " CVSEdit          Performs "cvs edit" on the current file.
36 " CVSEditors       Performs "cvs editors" on the current file.
38 " CVSUnedit        Performs "cvs unedit" on the current file.
40 " CVSWatch         Takes an argument which must be one of [on|off|add|remove].
41 "                  Performs "cvs watch" with the given argument on the current
42 "                  file.
44 " CVSWatchers      Performs "cvs watchers" on the current file.
46 " CVSWatchAdd      Alias for "CVSWatch add"
48 " CVSWatchOn       Alias for "CVSWatch on"
50 " CVSWatchOff      Alias for "CVSWatch off"
52 " CVSWatchRemove   Alias for "CVSWatch remove"
54 " Mapping documentation: {{{2
56 " By default, a mapping is defined for each command.  User-provided mappings
57 " can be used instead by mapping to <Plug>CommandName, for instance:
59 " nnoremap ,ce <Plug>CVSEdit
61 " The default mappings are as follow:
63 "   <Leader>ce CVSEdit
64 "   <Leader>cE CVSEditors
65 "   <Leader>ct CVSUnedit
66 "   <Leader>cwv CVSWatchers
67 "   <Leader>cwa CVSWatchAdd
68 "   <Leader>cwn CVSWatchOn
69 "   <Leader>cwf CVSWatchOff
70 "   <Leader>cwr CVSWatchRemove
72 " Options documentation: {{{2
74 " VCSCommandCVSExec
75 "   This variable specifies the CVS executable.  If not set, it defaults to
76 "   'cvs' executed from the user's executable path.
78 " VCSCommandCVSDiffOpt
79 "   This variable, if set, determines the options passed to the cvs diff
80 "   command.  If not set, it defaults to 'u'.
82 " Section: Plugin header {{{1
84 if exists('VCSCommandDisableAll')
85         finish
86 endif
88 if v:version < 700
89         echohl WarningMsg|echomsg 'VCSCommand requires at least VIM 7.0'|echohl None
90         finish
91 endif
93 runtime plugin/vcscommand.vim
95 if !executable(VCSCommandGetOption('VCSCommandCVSExec', 'cvs'))
96         " CVS is not installed
97         finish
98 endif
100 let s:save_cpo=&cpo
101 set cpo&vim
103 " Section: Variable initialization {{{1
105 let s:cvsFunctions = {}
107 " Section: Utility functions {{{1
109 " Function: s:Executable() {{{2
110 " Returns the executable used to invoke cvs suitable for use in a shell
111 " command.
112 function! s:Executable()
113         return shellescape(VCSCommandGetOption('VCSCommandCVSExec', 'cvs'))
114 endfunction
116 " Function: s:DoCommand(cmd, cmdName, statusText, options) {{{2
117 " Wrapper to VCSCommandDoCommand to add the name of the CVS executable to the
118 " command argument.
119 function! s:DoCommand(cmd, cmdName, statusText, options)
120         if VCSCommandGetVCSType(expand('%')) == 'CVS'
121                 let fullCmd = s:Executable() . ' ' . a:cmd
122                 let ret = VCSCommandDoCommand(fullCmd, a:cmdName, a:statusText, a:options)
124                 if ret > 0
125                         if getline(line('$')) =~ '^cvs \w\+: closing down connection'
126                                 $d
127                                 1
128                         endif
130                 endif
132                 return ret
133         else
134                 throw 'CVS VCSCommand plugin called on non-CVS item.'
135         endif
136 endfunction
138 " Function: s:GetRevision() {{{2
139 " Function for retrieving the current buffer's revision number.
140 " Returns: Revision number or an empty string if an error occurs.
142 function! s:GetRevision()
143         if !exists('b:VCSCommandBufferInfo')
144                 let b:VCSCommandBufferInfo =  s:cvsFunctions.GetBufferInfo()
145         endif
147         if len(b:VCSCommandBufferInfo) > 0
148                 return b:VCSCommandBufferInfo[0]
149         else
150                 return ''
151         endif
152 endfunction
154 " Section: VCS function implementations {{{1
156 " Function: s:cvsFunctions.Identify(buffer) {{{2
157 function! s:cvsFunctions.Identify(buffer)
158         let fileName = resolve(bufname(a:buffer))
159         if isdirectory(fileName)
160                 let directoryName = fileName
161         else
162                 let directoryName = fnamemodify(fileName, ':h')
163         endif
164         if strlen(directoryName) > 0
165                 let CVSRoot = directoryName . '/CVS/Root'
166         else
167                 let CVSRoot = 'CVS/Root'
168         endif
169         if filereadable(CVSRoot)
170                 return 1
171         else
172                 return 0
173         endif
174 endfunction
176 " Function: s:cvsFunctions.Add(argList) {{{2
177 function! s:cvsFunctions.Add(argList)
178         return s:DoCommand(join(['add'] + a:argList, ' '), 'add', join(a:argList, ' '), {})
179 endfunction
181 " Function: s:cvsFunctions.Annotate(argList) {{{2
182 function! s:cvsFunctions.Annotate(argList)
183         if len(a:argList) == 0
184                 if &filetype == 'CVSAnnotate'
185                         " This is a CVSAnnotate buffer.  Perform annotation of the version
186                         " indicated by the current line.
187                         let caption = matchstr(getline('.'),'\v^[0-9.]+')
189                         if VCSCommandGetOption('VCSCommandCVSAnnotateParent', 0) != 0
190                                 if caption != '1.1'
191                                         let revmaj = matchstr(caption,'\v[0-9.]+\ze\.[0-9]+')
192                                         let revmin = matchstr(caption,'\v[0-9.]+\.\zs[0-9]+') - 1
193                                         if revmin == 0
194                                                 " Jump to ancestor branch
195                                                 let caption = matchstr(revmaj,'\v[0-9.]+\ze\.[0-9]+')
196                                         else
197                                                 let caption = revmaj . "." .  revmin
198                                         endif
199                                 endif
200                         endif
202                         let options = ['-r' . caption]
203                 else
204                         " CVS defaults to pulling HEAD, regardless of current branch.
205                         " Therefore, always pass desired revision.
206                         let caption = ''
207                         let options = ['-r' .  s:GetRevision()]
208                 endif
209         elseif len(a:argList) == 1 && a:argList[0] !~ '^-'
210                 let caption = a:argList[0]
211                 let options = ['-r' . caption]
212         else
213                 let caption = join(a:argList)
214                 let options = a:argList
215         endif
217         let resultBuffer = s:DoCommand(join(['-q', 'annotate'] + options), 'annotate', caption, {})
218         if resultBuffer > 0
219                 set filetype=CVSAnnotate
220                 " Remove header lines from standard error
221                 silent v/^\d\+\%(\.\d\+\)\+/d
222         endif
223         return resultBuffer
224 endfunction
226 " Function: s:cvsFunctions.Commit(argList) {{{2
227 function! s:cvsFunctions.Commit(argList)
228         let resultBuffer = s:DoCommand('commit -F "' . a:argList[0] . '"', 'commit', '', {})
229         if resultBuffer == 0
230                 echomsg 'No commit needed.'
231         endif
232         return resultBuffer
233 endfunction
235 " Function: s:cvsFunctions.Delete() {{{2
236 " By default, use the -f option to remove the file first.  If options are
237 " passed in, use those instead.
238 function! s:cvsFunctions.Delete(argList)
239         let options = ['-f']
240         let caption = ''
241         if len(a:argList) > 0
242                 let options = a:argList
243                 let caption = join(a:argList, ' ')
244         endif
245         return s:DoCommand(join(['remove'] + options, ' '), 'delete', caption, {})
246 endfunction
248 " Function: s:cvsFunctions.Diff(argList) {{{2
249 function! s:cvsFunctions.Diff(argList)
250         if len(a:argList) == 0
251                 let revOptions = []
252                 let caption = ''
253         elseif len(a:argList) <= 2 && match(a:argList, '^-') == -1
254                 let revOptions = ['-r' . join(a:argList, ' -r')]
255                 let caption = '(' . a:argList[0] . ' : ' . get(a:argList, 1, 'current') . ')'
256         else
257                 " Pass-through
258                 let caption = join(a:argList, ' ')
259                 let revOptions = a:argList
260         endif
262         let cvsDiffOpt = VCSCommandGetOption('VCSCommandCVSDiffOpt', 'u')
263         if cvsDiffOpt == ''
264                 let diffOptions = []
265         else
266                 let diffOptions = ['-' . cvsDiffOpt]
267         endif
269         let resultBuffer = s:DoCommand(join(['diff'] + diffOptions + revOptions), 'diff', caption, {'allowNonZeroExit': 1})
270         if resultBuffer > 0
271                 set filetype=diff
272         else
273                 echomsg 'No differences found'
274         endif
275         return resultBuffer
276 endfunction
278 " Function: s:cvsFunctions.GetBufferInfo() {{{2
279 " Provides version control details for the current file.  Current version
280 " number and current repository version number are required to be returned by
281 " the vcscommand plugin.  This CVS extension adds branch name to the return
282 " list as well.
283 " Returns: List of results:  [revision, repository, branch]
285 function! s:cvsFunctions.GetBufferInfo()
286         let originalBuffer = VCSCommandGetOriginalBuffer(bufnr('%'))
287         let fileName = bufname(originalBuffer)
288         if isdirectory(fileName)
289                 let tag = ''
290                 if filereadable(fileName . '/CVS/Tag')
291                         let tagFile = readfile(fileName . '/CVS/Tag')
292                         if len(tagFile) == 1
293                                 let tag = substitute(tagFile[0], '^T', '', '')
294                         endif
295                 endif
296                 return [tag]
297         endif
298         let realFileName = fnamemodify(resolve(fileName), ':t')
299         if !filereadable(fileName)
300                 return ['Unknown']
301         endif
302         let oldCwd = VCSCommandChangeToCurrentFileDir(fileName)
303         try
304                 let statusText=s:VCSCommandUtility.system(s:Executable() . ' status -- "' . realFileName . '"')
305                 if(v:shell_error)
306                         return []
307                 endif
308                 let revision=substitute(statusText, '^\_.*Working revision:\s*\(\d\+\%(\.\d\+\)\+\|New file!\)\_.*$', '\1', '')
310                 " We can still be in a CVS-controlled directory without this being a CVS
311                 " file
312                 if match(revision, '^New file!$') >= 0
313                         let revision='New'
314                 elseif match(revision, '^\d\+\.\d\+\%(\.\d\+\.\d\+\)*$') <0
315                         return ['Unknown']
316                 endif
318                 let branch=substitute(statusText, '^\_.*Sticky Tag:\s\+\(\d\+\%(\.\d\+\)\+\|\a[A-Za-z0-9-_]*\|(none)\).*$', '\1', '')
319                 let repository=substitute(statusText, '^\_.*Repository revision:\s*\(\d\+\%(\.\d\+\)\+\|New file!\|No revision control file\)\_.*$', '\1', '')
320                 let repository=substitute(repository, '^New file!\|No revision control file$', 'New', '')
321                 return [revision, repository, branch]
322         finally
323                 call VCSCommandChdir(oldCwd)
324         endtry
325 endfunction
327 " Function: s:cvsFunctions.Log() {{{2
328 function! s:cvsFunctions.Log(argList)
329         if len(a:argList) == 0
330                 let options = []
331                 let caption = ''
332         elseif len(a:argList) <= 2 && match(a:argList, '^-') == -1
333                 let options = ['-r' . join(a:argList, ':')]
334                 let caption = options[0]
335         else
336                 " Pass-through
337                 let options = a:argList
338                 let caption = join(a:argList, ' ')
339         endif
341         let resultBuffer=s:DoCommand(join(['log'] + options), 'log', caption, {})
342         if resultBuffer > 0
343                 set filetype=rcslog
344         endif
345         return resultBuffer
346 endfunction
348 " Function: s:cvsFunctions.Revert(argList) {{{2
349 function! s:cvsFunctions.Revert(argList)
350         return s:DoCommand('update -C', 'revert', '', {})
351 endfunction
353 " Function: s:cvsFunctions.Review(argList) {{{2
354 function! s:cvsFunctions.Review(argList)
355         if len(a:argList) == 0
356                 let versiontag = '(current)'
357                 let versionOption = ''
358         else
359                 let versiontag = a:argList[0]
360                 let versionOption = ' -r ' . versiontag . ' '
361         endif
363         let resultBuffer = s:DoCommand('-q update -p' . versionOption, 'review', versiontag, {})
364         if resultBuffer > 0
365                 let &filetype=getbufvar(b:VCSCommandOriginalBuffer, '&filetype')
366         endif
367         return resultBuffer
368 endfunction
370 " Function: s:cvsFunctions.Status(argList) {{{2
371 function! s:cvsFunctions.Status(argList)
372         return s:DoCommand(join(['status'] + a:argList, ' '), 'status', join(a:argList, ' '), {})
373 endfunction
375 " Function: s:cvsFunctions.Update(argList) {{{2
376 function! s:cvsFunctions.Update(argList)
377         return s:DoCommand('update', 'update', '', {})
378 endfunction
380 " Section: CVS-specific functions {{{1
382 " Function: s:CVSEdit() {{{2
383 function! s:CVSEdit()
384         return s:DoCommand('edit', 'cvsedit', '', {})
385 endfunction
387 " Function: s:CVSEditors() {{{2
388 function! s:CVSEditors()
389         return s:DoCommand('editors', 'cvseditors', '', {})
390 endfunction
392 " Function: s:CVSUnedit() {{{2
393 function! s:CVSUnedit()
394         return s:DoCommand('unedit', 'cvsunedit', '', {})
395 endfunction
397 " Function: s:CVSWatch(onoff) {{{2
398 function! s:CVSWatch(onoff)
399         if a:onoff !~ '^\c\%(on\|off\|add\|remove\)$'
400                 echoerr 'Argument to CVSWatch must be one of [on|off|add|remove]'
401                 return -1
402         end
403         return s:DoCommand('watch ' . tolower(a:onoff), 'cvswatch', '', {})
404 endfunction
406 " Function: s:CVSWatchers() {{{2
407 function! s:CVSWatchers()
408         return s:DoCommand('watchers', 'cvswatchers', '', {})
409 endfunction
411 " Annotate setting {{{2
412 let s:cvsFunctions.AnnotateSplitRegex = '): '
414 " Section: Command definitions {{{1
415 " Section: Primary commands {{{2
416 com! CVSEdit call s:CVSEdit()
417 com! CVSEditors call s:CVSEditors()
418 com! CVSUnedit call s:CVSUnedit()
419 com! -nargs=1 CVSWatch call s:CVSWatch(<f-args>)
420 com! CVSWatchAdd call s:CVSWatch('add')
421 com! CVSWatchOn call s:CVSWatch('on')
422 com! CVSWatchOff call s:CVSWatch('off')
423 com! CVSWatchRemove call s:CVSWatch('remove')
424 com! CVSWatchers call s:CVSWatchers()
426 " Section: Plugin command mappings {{{1
428 let s:cvsExtensionMappings = {}
429 let mappingInfo = [
430                         \['CVSEdit', 'CVSEdit', 'e'],
431                         \['CVSEditors', 'CVSEditors', 'E'],
432                         \['CVSUnedit', 'CVSUnedit', 't'],
433                         \['CVSWatchers', 'CVSWatchers', 'wv'],
434                         \['CVSWatchAdd', 'CVSWatch add', 'wa'],
435                         \['CVSWatchOff', 'CVSWatch off', 'wf'],
436                         \['CVSWatchOn', 'CVSWatch on', 'wn'],
437                         \['CVSWatchRemove', 'CVSWatch remove', 'wr']
438                         \]
440 for [pluginName, commandText, shortCut] in mappingInfo
441         execute 'nnoremap <silent> <Plug>' . pluginName . ' :' . commandText . '<CR>'
442         if !hasmapto('<Plug>' . pluginName)
443                 let s:cvsExtensionMappings[shortCut] = commandText
444         endif
445 endfor
447 " Section: Menu items {{{1
448 amenu <silent> &Plugin.VCS.CVS.&Edit       <Plug>CVSEdit
449 amenu <silent> &Plugin.VCS.CVS.Ed&itors    <Plug>CVSEditors
450 amenu <silent> &Plugin.VCS.CVS.Unedi&t     <Plug>CVSUnedit
451 amenu <silent> &Plugin.VCS.CVS.&Watchers   <Plug>CVSWatchers
452 amenu <silent> &Plugin.VCS.CVS.WatchAdd    <Plug>CVSWatchAdd
453 amenu <silent> &Plugin.VCS.CVS.WatchOn     <Plug>CVSWatchOn
454 amenu <silent> &Plugin.VCS.CVS.WatchOff    <Plug>CVSWatchOff
455 amenu <silent> &Plugin.VCS.CVS.WatchRemove <Plug>CVSWatchRemove
457 " Section: Plugin Registration {{{1
458 let s:VCSCommandUtility = VCSCommandRegisterModule('CVS', expand('<sfile>'), s:cvsFunctions, s:cvsExtensionMappings)
460 let &cpo = s:save_cpo