Replace system() with wrapper to protect quotes in Windows.
[vcscommand.git] / plugin / vcssvn.vim
blobda738ee6ba47fe3c2485cb8cd04a272a937801ac
1 " vim600: set foldmethod=marker:
3 " SVN 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 " VCSCommandSVNExec
33 "   This variable specifies the SVN executable.  If not set, it defaults to
34 "   'svn' executed from the user's executable path.
36 " VCSCommandSVNDiffExt
37 "   This variable, if set, sets the external diff program used by Subversion.
39 " VCSCommandSVNDiffOpt
40 "   This variable, if set, determines the options passed to the svn diff
41 "   command (such as 'u', 'w', or 'b').
43 " Section: Plugin header {{{1
45 if exists('VCSCommandDisableAll')
46         finish
47 endif
49 if v:version < 700
50         echohl WarningMsg|echomsg 'VCSCommand requires at least VIM 7.0'|echohl None
51         finish
52 endif
54 runtime plugin/vcscommand.vim
56 if !executable(VCSCommandGetOption('VCSCommandSVNExec', 'svn'))
57         " SVN is not installed
58         finish
59 endif
61 let s:save_cpo=&cpo
62 set cpo&vim
64 " Section: Variable initialization {{{1
66 let s:svnFunctions = {}
68 " Section: Utility functions {{{1
70 " Function: s:Executable() {{{2
71 " Returns the executable used to invoke git suitable for use in a shell
72 " command.
73 function! s:Executable()
74         return shellescape(VCSCommandGetOption('VCSCommandSVNExec', 'svn'))
75 endfunction
77 " Function: s:DoCommand(cmd, cmdName, statusText, options) {{{2
78 " Wrapper to VCSCommandDoCommand to add the name of the SVN executable to the
79 " command argument.
80 function! s:DoCommand(cmd, cmdName, statusText, options)
81         if VCSCommandGetVCSType(expand('%')) == 'SVN'
82                 let fullCmd = s:Executable() . ' ' . a:cmd
83                 return VCSCommandDoCommand(fullCmd, a:cmdName, a:statusText, a:options)
84         else
85                 throw 'SVN VCSCommand plugin called on non-SVN item.'
86         endif
87 endfunction
89 " Section: VCS function implementations {{{1
91 " Function: s:svnFunctions.Identify(buffer) {{{2
92 function! s:svnFunctions.Identify(buffer)
93         let fileName = resolve(bufname(a:buffer))
94         if isdirectory(fileName)
95                 let directoryName = fileName
96         else
97                 let directoryName = fnamemodify(fileName, ':h')
98         endif
99         if strlen(directoryName) > 0
100                 let svnDir = directoryName . '/.svn'
101         else
102                 let svnDir = '.svn'
103         endif
104         if isdirectory(svnDir)
105                 return 1
106         else
107                 return 0
108         endif
109 endfunction
111 " Function: s:svnFunctions.Add() {{{2
112 function! s:svnFunctions.Add(argList)
113         return s:DoCommand(join(['add'] + a:argList, ' '), 'add', join(a:argList, ' '), {})
114 endfunction
116 " Function: s:svnFunctions.Annotate(argList) {{{2
117 function! s:svnFunctions.Annotate(argList)
118         if len(a:argList) == 0
119                 if &filetype == 'SVNAnnotate'
120                         " Perform annotation of the version indicated by the current line.
121                         let caption = matchstr(getline('.'),'\v^\s+\zs\d+')
122                         let options = ' -r' . caption
123                 else
124                         let caption = ''
125                         let options = ''
126                 endif
127         elseif len(a:argList) == 1 && a:argList[0] !~ '^-'
128                 let caption = a:argList[0]
129                 let options = ' -r' . caption
130         else
131                 let caption = join(a:argList, ' ')
132                 let options = ' ' . caption
133         endif
135         let resultBuffer = s:DoCommand('blame --non-interactive' . options, 'annotate', caption, {})
136         if resultBuffer > 0
137                 set filetype=SVNAnnotate
138         endif
139         return resultBuffer
140 endfunction
142 " Function: s:svnFunctions.Commit(argList) {{{2
143 function! s:svnFunctions.Commit(argList)
144         let resultBuffer = s:DoCommand('commit --non-interactive -F "' . a:argList[0] . '"', 'commit', '', {})
145         if resultBuffer == 0
146                 echomsg 'No commit needed.'
147         endif
148 endfunction
150 " Function: s:svnFunctions.Delete() {{{2
151 function! s:svnFunctions.Delete(argList)
152         return s:DoCommand(join(['delete --non-interactive'] + a:argList, ' '), 'delete', join(a:argList, ' '), {})
153 endfunction
155 " Function: s:svnFunctions.Diff(argList) {{{2
156 function! s:svnFunctions.Diff(argList)
157         if len(a:argList) == 0
158                 let revOptions = []
159                 let caption = ''
160         elseif len(a:argList) <= 2 && match(a:argList, '^-') == -1
161                 let revOptions = ['-r' . join(a:argList, ':')]
162                 let caption = '(' . a:argList[0] . ' : ' . get(a:argList, 1, 'current') . ')'
163         else
164                 " Pass-through
165                 let caption = join(a:argList, ' ')
166                 let revOptions = a:argList
167         endif
169         let svnDiffExt = VCSCommandGetOption('VCSCommandSVNDiffExt', '')
170         if svnDiffExt == ''
171                 let diffExt = []
172         else
173                 let diffExt = ['--diff-cmd ' . svnDiffExt]
174         endif
176         let svnDiffOpt = VCSCommandGetOption('VCSCommandSVNDiffOpt', '')
177         if svnDiffOpt == ''
178                 let diffOptions = []
179         else
180                 let diffOptions = ['-x -' . svnDiffOpt]
181         endif
183         let resultBuffer = s:DoCommand(join(['diff --non-interactive'] + diffExt + diffOptions + revOptions), 'diff', caption, {})
184         if resultBuffer > 0
185                 set filetype=diff
186         else
187                 if svnDiffExt == ''
188                         echomsg 'No differences found'
189                 endif
190         endif
191         return resultBuffer
192 endfunction
194 " Function: s:svnFunctions.GetBufferInfo() {{{2
195 " Provides version control details for the current file.  Current version
196 " number and current repository version number are required to be returned by
197 " the vcscommand plugin.
198 " Returns: List of results:  [revision, repository, branch]
200 function! s:svnFunctions.GetBufferInfo()
201         let originalBuffer = VCSCommandGetOriginalBuffer(bufnr('%'))
202         let fileName = bufname(originalBuffer)
203         let statusText = s:VCSCommandUtility.system(s:Executable() . ' status --non-interactive -vu -- "' . fileName . '"')
204         if(v:shell_error)
205                 return []
206         endif
208         " File not under SVN control.
209         if statusText =~ '^?'
210                 return ['Unknown']
211         endif
213         let [flags, revision, repository] = matchlist(statusText, '^\(.\{8}\)\s\+\(\S\+\)\s\+\(\S\+\)\s\+\(\S\+\)\s')[1:3]
214         if revision == ''
215                 " Error
216                 return ['Unknown']
217         elseif flags =~ '^A'
218                 return ['New', 'New']
219         else
220                 return [revision, repository]
221         endif
222 endfunction
224 " Function: s:svnFunctions.Info(argList) {{{2
225 function! s:svnFunctions.Info(argList)
226         return s:DoCommand(join(['info --non-interactive'] + a:argList, ' '), 'info', join(a:argList, ' '), {})
227 endfunction
229 " Function: s:svnFunctions.Lock(argList) {{{2
230 function! s:svnFunctions.Lock(argList)
231         return s:DoCommand(join(['lock --non-interactive'] + a:argList, ' '), 'lock', join(a:argList, ' '), {})
232 endfunction
234 " Function: s:svnFunctions.Log(argList) {{{2
235 function! s:svnFunctions.Log(argList)
236         if len(a:argList) == 0
237                 let options = []
238                 let caption = ''
239         elseif len(a:argList) <= 2 && match(a:argList, '^-') == -1
240                 let options = ['-r' . join(a:argList, ':')]
241                 let caption = options[0]
242         else
243                 " Pass-through
244                 let options = a:argList
245                 let caption = join(a:argList, ' ')
246         endif
248         let resultBuffer = s:DoCommand(join(['log --non-interactive', '-v'] + options), 'log', caption, {})
249         return resultBuffer
250 endfunction
252 " Function: s:svnFunctions.Revert(argList) {{{2
253 function! s:svnFunctions.Revert(argList)
254         return s:DoCommand('revert', 'revert', '', {})
255 endfunction
257 " Function: s:svnFunctions.Review(argList) {{{2
258 function! s:svnFunctions.Review(argList)
259         if len(a:argList) == 0
260                 let versiontag = '(current)'
261                 let versionOption = ''
262         else
263                 let versiontag = a:argList[0]
264                 let versionOption = ' -r ' . versiontag . ' '
265         endif
267         let resultBuffer = s:DoCommand('cat --non-interactive' . versionOption, 'review', versiontag, {})
268         if resultBuffer > 0
269                 let &filetype = getbufvar(b:VCSCommandOriginalBuffer, '&filetype')
270         endif
271         return resultBuffer
272 endfunction
274 " Function: s:svnFunctions.Status(argList) {{{2
275 function! s:svnFunctions.Status(argList)
276         let options = ['-u', '-v']
277         if len(a:argList) == 0
278                 let options = a:argList
279         endif
280         return s:DoCommand(join(['status --non-interactive'] + options, ' '), 'status', join(options, ' '), {})
281 endfunction
283 " Function: s:svnFunctions.Unlock(argList) {{{2
284 function! s:svnFunctions.Unlock(argList)
285         return s:DoCommand(join(['unlock --non-interactive'] + a:argList, ' '), 'unlock', join(a:argList, ' '), {})
286 endfunction
288 " Function: s:svnFunctions.Update(argList) {{{2
289 function! s:svnFunctions.Update(argList)
290         return s:DoCommand('update --non-interactive', 'update', '', {})
291 endfunction
293 " Annotate setting {{{2
294 let s:svnFunctions.AnnotateSplitRegex = '\s\+\S\+\s\+\S\+ '
296 " Section: Plugin Registration {{{1
297 let s:VCSCommandUtility = VCSCommandRegisterModule('SVN', expand('<sfile>'), s:svnFunctions, [])
299 let &cpo = s:save_cpo