Replace system() with wrapper to protect quotes in Windows.
[vcscommand.git] / plugin / vcssvk.vim
blob38111ff24e84a7ee50f3754acce28d135cc5d625
1 " vim600: set foldmethod=marker:
3 " SVK 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 " Options documentation: {{{2
32 " VCSCommandSVKExec
33 "   This variable specifies the SVK executable.  If not set, it defaults to
34 "   'svk' executed from the user's executable path.
36 " Section: Plugin header {{{1
38 if exists('VCSCommandDisableAll')
39         finish
40 endif
42 if v:version < 700
43         echohl WarningMsg|echomsg 'VCSCommand requires at least VIM 7.0'|echohl None
44         finish
45 endif
47 runtime plugin/vcscommand.vim
49 if !executable(VCSCommandGetOption('VCSCommandSVKExec', 'svk'))
50         " SVK is not installed
51         finish
52 endif
54 let s:save_cpo=&cpo
55 set cpo&vim
57 " Section: Variable initialization {{{1
59 let s:svkFunctions = {}
61 " Section: Utility functions {{{1
63 " Function: s:Executable() {{{2
64 " Returns the executable used to invoke SVK suitable for use in a shell
65 " command.
66 function! s:Executable()
67         return shellescape(VCSCommandGetOption('VCSCommandSVKExec', 'svk'))
68 endfunction
70 " Function: s:DoCommand(cmd, cmdName, statusText, options) {{{2
71 " Wrapper to VCSCommandDoCommand to add the name of the SVK executable to the
72 " command argument.
73 function! s:DoCommand(cmd, cmdName, statusText, options)
74         if VCSCommandGetVCSType(expand('%')) == 'SVK'
75                 let fullCmd = s:Executable() . ' ' . a:cmd
76                 return VCSCommandDoCommand(fullCmd, a:cmdName, a:statusText, a:options)
77         else
78                 throw 'SVK VCSCommand plugin called on non-SVK item.'
79         endif
80 endfunction
82 " Section: VCS function implementations {{{1
84 " Function: s:svkFunctions.Identify(buffer) {{{2
85 function! s:svkFunctions.Identify(buffer)
86         let fileName = resolve(bufname(a:buffer))
87         if isdirectory(fileName)
88                 let directoryName = fileName
89         else
90                 let directoryName = fnamemodify(fileName, ':p:h')
91         endif
92         let statusText = s:VCSCommandUtility.system(s:Executable() . ' info -- "' . directoryName . '"')
93         if(v:shell_error)
94                 return 0
95         else
96                 return 1
97         endif
98 endfunction
100 " Function: s:svkFunctions.Add() {{{2
101 function! s:svkFunctions.Add(argList)
102         return s:DoCommand(join(['add'] + a:argList, ' '), 'add', join(a:argList, ' '), {})
103 endfunction
105 " Function: s:svkFunctions.Annotate(argList) {{{2
106 function! s:svkFunctions.Annotate(argList)
107         if len(a:argList) == 0
108                 if &filetype == 'SVKAnnotate'
109                         " Perform annotation of the version indicated by the current line.
110                         let caption = matchstr(getline('.'),'\v^\s+\zs\d+')
111                         let options = ' -r' . caption
112                 else
113                         let caption = ''
114                         let options = ''
115                 endif
116         elseif len(a:argList) == 1 && a:argList[0] !~ '^-'
117                 let caption = a:argList[0]
118                 let options = ' -r' . caption
119         else
120                 let caption = join(a:argList, ' ')
121                 let options = ' ' . caption
122         endif
124         let resultBuffer = s:DoCommand('blame' . options, 'annotate', caption, {})
125         if resultBuffer > 0
126                 normal 1G2dd
127                 set filetype=SVKAnnotate
128         endif
129         return resultBuffer
130 endfunction
132 " Function: s:svkFunctions.Commit(argList) {{{2
133 function! s:svkFunctions.Commit(argList)
134         let resultBuffer = s:DoCommand('commit -F "' . a:argList[0] . '"', 'commit', '', {})
135         if resultBuffer == 0
136                 echomsg 'No commit needed.'
137         endif
138 endfunction
140 " Function: s:svkFunctions.Delete() {{{2
141 function! s:svkFunctions.Delete(argList)
142         return s:DoCommand(join(['delete'] + a:argList, ' '), 'delete', join(a:argList, ' '), {})
143 endfunction
145 " Function: s:svkFunctions.Diff(argList) {{{2
146 function! s:svkFunctions.Diff(argList)
147         if len(a:argList) == 0
148                 let revOptions = []
149                 let caption = ''
150         elseif len(a:argList) <= 2 && match(a:argList, '^-') == -1
151                 let revOptions = ['-r' . join(a:argList, ':')]
152                 let caption = '(' . a:argList[0] . ' : ' . get(a:argList, 1, 'current') . ')'
153         else
154                 " Pass-through
155                 let caption = join(a:argList, ' ')
156                 let revOptions = a:argList
157         endif
159         let resultBuffer = s:DoCommand(join(['diff'] + revOptions), 'diff', caption, {})
160         if resultBuffer > 0
161                 set filetype=diff
162         else
163                 echomsg 'No differences found'
164         endif
165         return resultBuffer
166 endfunction
168 " Function: s:svkFunctions.GetBufferInfo() {{{2
169 " Provides version control details for the current file.  Current version
170 " number and current repository version number are required to be returned by
171 " the vcscommand plugin.
172 " Returns: List of results:  [revision, repository]
174 function! s:svkFunctions.GetBufferInfo()
175         let originalBuffer = VCSCommandGetOriginalBuffer(bufnr('%'))
176         let fileName = resolve(bufname(originalBuffer))
177         let statusText = s:VCSCommandUtility.system(s:Executable() . ' status -v -- "' . fileName . '"')
178         if(v:shell_error)
179                 return []
180         endif
182         " File not under SVK control.
183         if statusText =~ '^?'
184                 return ['Unknown']
185         endif
187         let [flags, revision, repository] = matchlist(statusText, '^\(.\{3}\)\s\+\(\S\+\)\s\+\(\S\+\)\s\+\(\S\+\)\s')[1:3]
188         if revision == ''
189                 " Error
190                 return ['Unknown']
191         elseif flags =~ '^A'
192                 return ['New', 'New']
193         else
194                 return [revision, repository]
195         endif
196 endfunction
198 " Function: s:svkFunctions.Info(argList) {{{2
199 function! s:svkFunctions.Info(argList)
200         return s:DoCommand(join(['info'] + a:argList, ' '), 'info', join(a:argList, ' '), {})
201 endfunction
203 " Function: s:svkFunctions.Lock(argList) {{{2
204 function! s:svkFunctions.Lock(argList)
205         return s:DoCommand(join(['lock'] + a:argList, ' '), 'lock', join(a:argList, ' '), {})
206 endfunction
208 " Function: s:svkFunctions.Log() {{{2
209 function! s:svkFunctions.Log(argList)
210         if len(a:argList) == 0
211                 let options = []
212                 let caption = ''
213         elseif len(a:argList) <= 2 && match(a:argList, '^-') == -1
214                 let options = ['-r' . join(a:argList, ':')]
215                 let caption = options[0]
216         else
217                 " Pass-through
218                 let options = a:argList
219                 let caption = join(a:argList, ' ')
220         endif
222         let resultBuffer = s:DoCommand(join(['log', '-v'] + options), 'log', caption, {})
223         return resultBuffer
224 endfunction
226 " Function: s:svkFunctions.Revert(argList) {{{2
227 function! s:svkFunctions.Revert(argList)
228         return s:DoCommand('revert', 'revert', '', {})
229 endfunction
231 " Function: s:svkFunctions.Review(argList) {{{2
232 function! s:svkFunctions.Review(argList)
233         if len(a:argList) == 0
234                 let versiontag = '(current)'
235                 let versionOption = ''
236         else
237                 let versiontag = a:argList[0]
238                 let versionOption = ' -r ' . versiontag . ' '
239         endif
241         let resultBuffer = s:DoCommand('cat' . versionOption, 'review', versiontag, {})
242         if resultBuffer > 0
243                 let &filetype=getbufvar(b:VCSCommandOriginalBuffer, '&filetype')
244         endif
245         return resultBuffer
246 endfunction
248 " Function: s:svkFunctions.Status(argList) {{{2
249 function! s:svkFunctions.Status(argList)
250         let options = ['-v']
251         if len(a:argList) == 0
252                 let options = a:argList
253         endif
254         return s:DoCommand(join(['status'] + options, ' '), 'status', join(options, ' '), {})
255 endfunction
257 " Function: s:svkFunctions.Unlock(argList) {{{2
258 function! s:svkFunctions.Unlock(argList)
259         return s:DoCommand(join(['unlock'] + a:argList, ' '), 'unlock', join(a:argList, ' '), {})
260 endfunction
261 " Function: s:svkFunctions.Update(argList) {{{2
262 function! s:svkFunctions.Update(argList)
263         return s:DoCommand('update', 'update', '', {})
264 endfunction
266 " Section: Plugin Registration {{{1
267 let s:VCSCommandUtility = VCSCommandRegisterModule('SVK', expand('<sfile>'), s:svkFunctions, [])
269 let &cpo = s:save_cpo